NumPy ndarray 是可变的,意味着 ndarray 中的元素在 ndarray 创建之后可以更改。NumPy ndarray 还可以切片,因此可以通过多种方式拆分 ndarray。例如,我们可以从 ndarray 中获取想要的任何子集。通常,在机器学习中,你需要使用切片拆分数据,例如将数据集拆分为训练集、交叉验证集和测试集。

我们首先将了解如何通过索引访问或修改 ndarray 中的元素。可以在方括号 [ ] 中添加索引来访问元素。在 NumPy 中,你可以使用正索引和负索引访问 ndarray 中的元素。正索引表示从数组的开头访问元素,负索引表示从数组的末尾访问元素。我们来看看如何访问秩为 1 的 ndarray 中的元素:

# We create a rank 1 ndarray that contains integers from 1 to 5

x = np.array([1, 2, 3, 4, 5])

# We print x

print()

print('x = ', x)

print()

# Let's access some elements with positive indices

print('This is First Element in x:', x[0])

print('This is Second Element in x:', x[1])

print('This is Fifth (Last) Element in x:', x[4])

print()

# Let's access the same elements with negative indices

print('This is First Element in x:', x[-5])

print('This is Second Element in x:', x[-4])

print('This is Fifth (Last) Element in x:', x[-1])

x = [1 2 3 4 5]

This is First Element in x: 1

This is Second Element in x: 2

This is Fifth (Last) Element in x: 5

This is First Element in x: 1

This is Second Element in x: 2

This is Fifth (Last) Element in x: 5

注意,要访问 ndarray 中的第一个元素,我们需要使用索引 0,而不是 1。此外注意,可以同时使用正索引和负索引访问同一个元素。正如之前提到的,正索引用于从数组的开头访问元素,负索引用于从数组的末尾访问元素。

现在我们看看如何更改秩为 1 的 ndarray 中的元素。方法是访问要更改的元素,然后使用 = 符号分配新的值:

# We create a rank 1 ndarray that contains integers from 1 to 5

x = np.array([1, 2, 3, 4, 5])

# We print the original x

print()

print('Original:\n x = ', x)

print()

# We change the fourth element in x from 4 to 20

x[3] = 20

# We print x after it was modified

print('Modified:\n x = ', x)

Original: x = [1 2 3 4 5]

Modified: x = [ 1 2 3 20 5]

同样,我们可以访问和修改秩为 2 的 ndarray 中的特定元素。要访问秩为 2 的 ndarray 中的元素,我们需要提供两个索引,格式为 [row, column]。我们来看一些示例:

# We create a 3 x 3 rank 2 ndarray that contains integers from 1 to 9

X = np.array([[1,2,3],[4,5,6],[7,8,9]])

# We print X

print()

print('X = \n', X)

print()

# Let's access some elements in X

print('This is (0,0) Element in X:', X[0,0])

print('This is (0,1) Element in X:', X[0,1])

print('This is (2,2) Element in X:', X[2,2])

X =

[[1 2 3]

[4 5 6]

[7 8 9]]

This is (0,0) Element in X: 1

This is (0,1) Element in X: 2

This is (2,2) Element in X: 9

注意,索引 [0, 0] 是指第一行第一列的元素。

可以像针对秩为 1 的 ndarray 一样修改秩为 2 的 ndarray 中的元素。我们来看一个示例:

# We create a 3 x 3 rank 2 ndarray that contains integers from 1 to 9

X = np.array([[1,2,3],[4,5,6],[7,8,9]])

# We print the original x

print()

print('Original:\n X = \n', X)

print()

# We change the (0,0) element in X from 1 to 20

X[0,0] = 20

# We print X after it was modified

print('Modified:\n X = \n', X)

Original:

X =

[[1 2 3]

[4 5 6]

[7 8 9]]

Modified:

X =

[[20 2 3]

[ 4 5 6]

[ 7 8 9]]

现在看看如何向 ndarray 中添加元素及删除其中的元素。我们可以使用 np.delete(ndarray, elements, axis) 函数删除元素。此函数会沿着指定的轴从给定 ndarray 中删除给定的元素列表。对于秩为 1 的 ndarray,不需要使用关键字 axis。对于秩为 2 的 ndarray,axis = 0 表示选择行,axis = 1 表示选择列。我们来看一些示例:

# We create a rank 1 ndarray

x = np.array([1, 2, 3, 4, 5])

# We create a rank 2 ndarray

Y = np.array([[1,2,3],[4,5,6],[7,8,9]])

# We print x

print()

print('Original x = ', x)

# We delete the first and last element of x

x = np.delete(x, [0,4])

# We print x with the first and last element deleted

print()

print('Modified x = ', x)

# We print Y

print()

print('Original Y = \n', Y)

# We delete the first row of y

w = np.delete(Y, 0, axis=0)

# We delete the first and last column of y

v = np.delete(Y, [0,2], axis=1)

# We print w

print()

print('w = \n', w)

# We print v

print()

print('v = \n', v)

Original x = [1 2 3 4 5]

Modified x = [2 3 4]

Original Y =

[[1 2 3]

[4 5 6]

[7 8 9]]

w =

[[4 5 6]

[7 8 9]]

v =

[[2]

[5]

[8]]

现在我们来看看如何向 ndarray 中附加值。我们可以使用 np.append(ndarray, elements, axis) 函数向 ndarray 中附加值。该函数会将给定的元素列表沿着指定的轴附加到 ndarray 中。我们来看一些示例:

# We create a rank 1 ndarray

x = np.array([1, 2, 3, 4, 5])

# We create a rank 2 ndarray

Y = np.array([[1,2,3],[4,5,6]])

# We print x

print()

print('Original x = ', x)

# We append the integer 6 to x

x = np.append(x, 6)

# We print x

print()

print('x = ', x)

# We append the integer 7 and 8 to x

x = np.append(x, [7,8])

# We print x

print()

print('x = ', x)

# We print Y

print()

print('Original Y = \n', Y)

# We append a new row containing 7,8,9 to y

v = np.append(Y, [[7,8,9]], axis=0)

# We append a new column containing 9 and 10 to y

q = np.append(Y,[[9],[10]], axis=1)

# We print v

print()

print('v = \n', v)

# We print q

print()

print('q = \n', q)

Original x = [1 2 3 4 5]

x = [1 2 3 4 5 6]

x = [1 2 3 4 5 6 7 8]

Original Y =

[[1 2 3]

[4 5 6]]

v =

[[1 2 3]

[4 5 6]

[7 8 9]]

q =

[[ 1 2 3 9]

[ 4 5 6 10]]

注意,当我们将行或列附加到秩为 2 的 ndarray 中时,行或列的形状必须正确,以与秩为 2 的 ndarray 的形状相符。

现在我们来看看如何向 ndarray 中插入值。我们可以使用 np.insert(ndarray, index, elements, axis) 函数向 ndarray 中插入值。此函数会将给定的元素列表沿着指定的轴插入到 ndarray 中,并放在给定的索引前面。我们来看一些示例:

# We create a rank 1 ndarray

x = np.array([1, 2, 5, 6, 7])

# We create a rank 2 ndarray

Y = np.array([[1,2,3],[7,8,9]])

# We print x

print()

print('Original x = ', x)

# We insert the integer 3 and 4 between 2 and 5 in x.

x = np.insert(x,2,[3,4])

# We print x with the inserted elements

print()

print('x = ', x)

# We print Y

print()

print('Original Y = \n', Y)

# We insert a row between the first and last row of y

w = np.insert(Y,1,[4,5,6],axis=0)

# We insert a column full of 5s between the first and second column of y

v = np.insert(Y,1,5, axis=1)

# We print w

print()

print('w = \n', w)

# We print v

print()

print('v = \n', v)

Original x = [1 2 5 6 7]

x = [1 2 3 4 5 6 7]

Original Y =

[[1 2 3]

[7 8 9]]

w =

[[1 2 3]

[4 5 6]

[7 8 9]]

v =

[[1 5 2 3]

[7 5 8 9]]

NumPy 还允许我们将 ndarray 上下堆叠起来,或者左右堆叠。可以使用 np.vstack() 函数进行垂直堆叠,或使用 np.hstack() 函数进行水平堆叠。请务必注意,为了堆叠 ndarray,ndarray 的形状必须相符。我们来看一些示例:

# We create a rank 1 ndarray

x = np.array([1,2])

# We create a rank 2 ndarray

Y = np.array([[3,4],[5,6]])

# We print x

print()

print('x = ', x)

# We print Y

print()

print('Y = \n', Y)

# We stack x on top of Y

z = np.vstack((x,Y))

# We stack x on the right of Y. We need to reshape x in order to stack it on the right of Y.

w = np.hstack((Y,x.reshape(2,1)))

# We print z

print()

print('z = \n', z)

# We print w

print()

print('w = \n', w)

x = [1 2]

Y =

[[3 4]

[5 6]]

z =

[[1 2]

[3 4]

[5 6]]

w =

[[3 4 1]

[5 6 2]]

ndarray如何取元素_Numpy之访问和删除 ndarray 中的元素及向其中插入元素相关推荐

  1. ndarray如何取元素_访问和删除 ndarray 中的元素及向其中插入元素

    你已经知道如何创建各种 ndarray,现在将学习 NumPy 使我们如何有效地操纵 ndarray 中的数据.NumPy ndarray 是可变的,意味着 ndarray 中的元素在 ndarray ...

  2. python删除元素del 可以删除部分元素吗_可以使用del删除集合中的部分元素。

    [判断题]Python集合可以包含相同的元素.(3.0分) [单选题]student = dict(姓名='张三', 年龄=20, 性别='男'),则student.get("name&qu ...

  3. c++把数组所有元素剔除_C语言数组——删除数组中的某个值

    前言 在家里闲着还是闲着,这几天见证了Python各种数据结构的强大.Python中的数据类型如:列表.元组.字典等都具有其的特点.列表无需要设定其的长度,我们可以随机插入元素,同时元素的类型也是随意 ...

  4. js remove 当前元素_详解js删除数组中的指定元素

    本篇文章将会给大家介绍两种删除数组中的指定元素的方式,分别为: 1.单独定义一个的函数,通过函数来删除指定数组元素. 2.为Array对象定义了一个removeByValue的方法,在调用方法来删除指 ...

  5. java向有序数组里插数_Java向有序数组中插入一个元素,,使其仍按有序排列,并求出这个插入元素的下标...

    /** * * @create time [2014-4-13] */ public class Test { public static void main(String args[]) { //原 ...

  6. Python算法教程第一章知识点:利用插入元素的例子详解list之本质

    声明:由于中译本翻译过于冗余,所以将有用处的知识点罗列出来. 微信公众号:geekkr 本文目录:一.利用插入元素的例子详解list之本质 </br> 一.利用插入元素的例子详解list之 ...

  7. python 删除列表中的指定元素

    python 删除列表中的指定元素 def delete_list(list1,ele):"""删除列表中的指定元素:param list1:原列表:param ele: ...

  8. Java 数组插入元素

    在我们已经创建好的Java数组里面插入元素. 我们自定义任意一个数组,使用sort()方法对数组进行排序,使用insertElement()方法向数组插入元素,我们还定义了一个printArray() ...

  9. C语言创建顺序表并插入元素 详细注释

    顺序表是用一组地址连续的存储单元依次存储数据元素的数据结构.顺序表是线性表的一种,线性表是最常用且最简单的一种数据结构,一个线性表是 n 个数据元素的有限序列.我们使用 c 语言来创建顺序表并插入元素 ...

最新文章

  1. 方法功能从无参方法、含参方法到重载方法
  2. AlertDialog.Builder setCancelable用法
  3. CCF - 201509-2 - 日期计算
  4. 防火墙启动被拒绝解决方案
  5. HDU 3001 Travelling
  6. 【CyberSecurityLearning 附】域的复习+小综合实验(重要!)
  7. boost::graph模块实现一个只读隐式加权图的简单示例的测试程序
  8. 《深入理解JVM.2nd》笔记(一):走进Java
  9. 多元正态分布、多元t分布中的行列式求解 Java
  10. linux里netstat与ps,linux命令——ps和netstat
  11. SAS详细的下载与安装流程
  12. Oracle中表pagesize,Oracle使用pagesize命令
  13. 煤矿矿长相当于什么级别?
  14. 【Unity】判断视频是否播放完毕
  15. windows更改文件权限,获取SYSTEM 或者 Administrator权限,解决删除文件需要来自SYSTEM的权限
  16. 如何使用OLED显示图片
  17. xxx牌JUC学习加油奥利给001初始篇章
  18. 2022年初级审计师考试复习题及答案
  19. 《当程序员的那些狗日日子》(五十七)迟来的爱恋
  20. 项目管理-今天学Wiki

热门文章

  1. 别只知道马斯克,太空互联网还有这些重磅选手
  2. 【CF套题】Educational Codeforces Round 57
  3. 1936年 柏林 第十一届奥运会
  4. 时间的涟漪(Ripples of time)
  5. 计算机二级开封大学通过率,考生注意:河南这所学校专科层次,本科名字,千万不要弄错...
  6. Android手机记账应用开发视频教程
  7. 9种炫酷loading加载cssjs特效
  8. URLpattern匹配规则
  9. matlab中randperm函数设置随机种子的方法
  10. Cinema 4D 第一节(CG及软件介绍)