你已经知道如何创建各种 ndarray,现在将学习 NumPy 使我们如何有效地操纵 ndarray 中的数据。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 5x = 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 indicesprint('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: 5This 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 5x = np.array([1, 2, 3, 4, 5])# We print the original xprint()print('Original:\n x = ', x)print()# We change the fourth element in x from 4 to 20x[3] = 20# We print x after it was modifiedprint('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 9X = 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 9X = np.array([[1,2,3],[4,5,6],[7,8,9]])

# We print the original xprint()print('Original:\n X = \n', X)print()

# We change the (0,0) element in X from 1 to 20X[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 xprint()print('Original x = ', x)

# We append the integer 6 to x

x = np.append(x, 6)

# We print xprint()print('x = ', x)

# We append the integer 7 and 8 to x

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

# We print xprint()print('x = ', x)

# We print Yprint()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 vprint()print('v = \n', v)

# We print qprint()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 ndarrayx = np.array([1,2])# We create a rank 2 ndarrayY = np.array([[3,4],[5,6]])# We print xprint()print('x = ', x)# We print Yprint()print('Y = \n', Y)# We stack x on top of Yz = 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 zprint()print('z = \n', z)# We print wprint()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如何取元素_访问和删除 ndarray 中的元素及向其中插入元素相关推荐

  1. java list去除最后一个元素_如何快速删除list中的最后一个元素?

    (前言: 在项目中,在统计在线用户量及其行为方式的时候,想在项目如"/bob/recode/online",结果发现:把写日志的东西放到了ebin文件下,即:/bob/ebin/r ...

  2. java删除集合元素吗_java如何删除集合中的元素

    java如何删除集合中的元素 如何使用java删除集合中的'元素呢?下面是小编给大家提供的删除集合中元素的常见方法,欢迎阅读,更多详情请关注应届毕业生考试网. Java代码如下: package co ...

  3. ndarray如何取元素_Numpy之访问和删除 ndarray 中的元素及向其中插入元素

    NumPy ndarray 是可变的,意味着 ndarray 中的元素在 ndarray 创建之后可以更改.NumPy ndarray 还可以切片,因此可以通过多种方式拆分 ndarray.例如,我们 ...

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

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

  5. pta数据结构实验在数组中查找指定元素_如何从一个数组中查找指定的元素,并返回这个元素在数组中的位置...

    展开全部 和Vector都是使用Objec的数组形式来存储的.当你向这两种类型中增加元素的时候,如果元素的数目超出e68a84e8a2ad62616964757a686964616f313332646 ...

  6. php中删除数组元素的函数,php删除数组中的元素函数用法汇总

    php中删除数组元素是非常的简单的,主要是unset,array_splice,但是关于两者有,长时间混用,有的时候竟然分不出区别. 很多地方都这样区别unset,array_splice array ...

  7. java栈顶元素_栈在Java类库中的实现

    栈是一种后进先出的数据结构.在它之上,主要有三种操作: (1)判断栈是否为空--empty(): (2)在栈顶添加一个元素--push(E): (3)删除并返回栈顶元素--pop(). 在Java类库 ...

  8. QMap删除其中的保存的元素本身,并删除容器中保存的元素

    前言 qmap删除元素可以采用erase(),但使用过程中却必须注意以下: qmap中的元素被删除后,其迭代器自动指向下一个元素: 示例 要求删除创建时保存在QMap中的指针,并将qmap中的元素删除 ...

  9. c语言删除字符常数组的某一个元素,C语言实现删除数组中某个元素

    C语言实现删除数组中某个元素 大家知道C语言实现删除数组中某个元素方法吗?下面将讲述在C语言中实现删除数组中某个元素的两种方法,大家一起看看吧. 方法1:/* name: c语言 删除数组的某个元素 ...

最新文章

  1. 青蛙跳台阶c语言递归函数,青蛙跳台阶问题的四种解法
  2. 让数百万台手机训练同一个模型?Google把这套框架开源了
  3. matlab光强值,光强分布MATLAB.doc
  4. 【推荐】微服务分布式企业框架Springmvc+mybatis+shiro+Dubbo+ZooKeeper+Redis
  5. tengine简单安装_实操丨如何在EAIDK上部署Tengine开发AI应用之物体检测应用入门(C++)...
  6. nacos mysql8.0修改
  7. oracle格式化列宽度,ORACLE日期时间的格式化参数大全
  8. 自己眼中的淡定生活!
  9. php完全匹配,如何在PHP中使用正则表达式找到完全匹配的内容?
  10. php有哪些debug方式,Console有哪些Debug方法
  11. dedecms怎么改php版本_php.ini配置中有3处设置不当会使网站存在安全问题
  12. 翻译:《CSS权威指南》(第3版)-概览和目录部分
  13. 局域网桌面共享软件(Java版)
  14. 使用JQuery TreeTable实现树形表格
  15. 16进制颜色透明度对照表
  16. 基于TI DLP技术的工业级DLP3010光机
  17. S3cmd命令行工具使用
  18. Android 9.0打开wifi时关闭热点流程
  19. 玩转 MATLAB 附加功能/硬件支持包安装
  20. STM32普通IO模拟SPI和W25Q32通信调试

热门文章

  1. 彻底实现Linux TCP的Pacing发送逻辑-普通timer版
  2. 苹果电脑python编程里面怎么切到中文_电脑语言怎么切换中文,AdobeInDesignCCforMac苹果笔记本上,怎样转换页面语言为中文英语?...
  3. HDU - 6386 - Age of Moyu (BFS分层+DFS 或者 最短路+set)
  4. tomcat做图片服务器
  5. QQ宠物等级 宠物总的成长值 省级所需成长值 增加值
  6. if-elif语句的使用方法【Python培训】
  7. 如何搭建Qt跨平台环境和部署工作
  8. Python教程:itertools模块
  9. 福建厦门 金砖五国(BRICS)峰会 9.3-9.5
  10. 华为云分布式全系列产品组合,帮助企业轻松上云