首页 > 编程语言 > 详细

01.Numpy数组的基本应用

时间:2020-11-24 20:27:44      阅读:65      评论:0      收藏:0      [点我收藏+]
  1. 数组的创建

  2. 数组的访问

  3. 数组的合并

  4. 数组的分割

 

数组创建

>>> import numpy as np

创建一维数组
>>> x = np.arange(10)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

创建二维数组
>>> X = np.arange(10).reshape(2, 5)
>>> X
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

查看数组为维度
>>> x.ndim
1
>>> X.ndim
2

查看数组的形状
>>> X.shape
(2, 5)

数组访问

>>> X
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

>>> X[0]
array([0, 1, 2, 3, 4])

>>> X[1,1]
6

>>> X[0:4]
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

>>> X[0:1]
array([[0, 1, 2, 3, 4]])

>>> X[0:2]
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

>>> X[:2, :2]
array([[0, 1],
       [5, 6]])

>>> X[:, 1]
array([1, 6])

>>> X[1, :]
array([5, 6, 7, 8, 9])

创建子数组
>>> subX = X[:2, :2]
>>> subX
array([[0, 1],
       [5, 6]])

子数组修改
>>> subX[0, 0] = 100
>>> subX
array([[100,   1],
       [  5,   6]])
>>> X
array([[100,   1,   2,   3,   4],
       [  5,   6,   7,   8,   9]])

如何使子数组的修改不影响原数组
>>> subX = X[:2, :2].copy()
>>> subX
array([[100,   1],
       [  5,   6]])
>>> subX[0, 1] = 200
>>> subX
array([[100, 200],
       [  5,   6]])
>>> X
array([[100,   1,   2,   3,   4],
       [  5,   6,   7,   8,   9]])

数组形状

>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x.reshape(2, 5)
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> x.reshape(5, 2)
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
>>> A = x.reshape(5, 2)
>>> A
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
>>> x.reshape(10, -1)
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])
>>> x.reshape(-1, 10)
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

数组合并

>>> a = np.array([1,2,3])
>>> b = np.array([4,5,6])
>>> a,b
(array([1, 2, 3]), array([4, 5, 6]))

>>> np.concatenate([a,b])
array([1, 2, 3, 4, 5, 6])

>>> c = np.array([7,8,9])
>>> np.concatenate([a,b,c])
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> A = np.array([[1,2,3],[4,5,6]])
>>> np.concatenate([A, A])
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [4, 5, 6]])
>>> np.concatenate([A, A], axis=0)
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [4, 5, 6]])
>>> np.concatenate([A, A], axis=1)
array([[1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6]])

不能合并两个维度不同的数组
>>> np.concatenate([A, a])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

如何忽略维度问题
>>> np.concatenate([A, a.reshape(1, -1)])
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3]])
>>> A,a
(array([[1, 2, 3],
       [4, 5, 6]]), array([1, 2, 3]))
>>> A.shape, a.shape
((2, 3), (3,))
>>> np.vstack([A, a])
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3]])
>>> a = np.array([[6],[6]])
>>> a
array([[6],
       [6]])
>>> np.hstack([A, a])
array([[1, 2, 3, 6],
       [4, 5, 6, 6]])

数组分割

>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x1,x2,x3 = np.split(x, [3,7])
>>> x1,x2,x3
(array([0, 1, 2]), array([3, 4, 5, 6]), array([7, 8, 9]))
>>> A = np.arange(16).reshape(4,4)
>>> A
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> A1,A2 = np.split(A, [2])
>>> A1,A2
(array([[0, 1, 2, 3],
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]]))
>>> A1,A2 = np.split(A,[2],axis=1)
>>> A1,A2
(array([[ 0,  1],
       [ 4,  5],
       [ 8,  9],
       [12, 13]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11],
       [14, 15]]))
>>> A1, A2 = np.vsplit(A, [2])
>>> A1,A2
(array([[0, 1, 2, 3],
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]]))
>>> A1,A2 = np.hsplit(A,[2])
>>> A1,A2
(array([[ 0,  1],
       [ 4,  5],
       [ 8,  9],
       [12, 13]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11],
       [14, 15]]))

 

01.Numpy数组的基本应用

原文:https://www.cnblogs.com/waterr/p/14031926.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!