This article mainly introduces the addition, deletion, modification and query of Numpy array data. It has certain reference value. Now I share it with you. Friends in need can refer to it
Preparation work:
There are many ways to add, delete, modify, and check. Here are only some commonly used ones.
>>> import numpy as np >>> a = np.array([[1,2],[3,4],[5,6]])#创建3行2列二维数组。 >>> a array([[1, 2], [3, 4], [5, 6]]) >>> a = np.zeros(6)#创建长度为6的,元素都是0一维数组 >>> a = np.zeros((2,3))#创建3行2列,元素都是0的二维数组 >>> a = np.ones((2,3))#创建3行2列,元素都是1的二维数组 >>> a = np.empty((2,3)) #创建3行2列,未初始化的二维数组 >>> a = np.arange(6)#创建长度为6的,元素都是0一维数组array([0, 1, 2, 3, 4, 5]) >>> a = np.arange(1,7,1)#结果与np.arange(6)一样。第一,二个参数意思是数值从1〜6,不包括7.第三个参数表步长为1. a = np.linspace(0,10,7) # 生成首位是0,末位是10,含7个数的等差数列[ 0. 1.66666667 3.33333333 5. 6.66666667 8.33333333 10. ] a = np.logspace(0,4,5)#用于生成首位是10**0,末位是10**4,含5个数的等比数列。[ 1.00000000e+00 1.00000000e+01 1.00000000e+02 1.00000000e+03 1.00000000e+04]
increased
>>> a = np.array([[1,2],[3,4],[5,6]]) >>> b = np.array([[10,20],[30,40],[50,60]]) >>> np.vstack((a,b)) array([[ 1, 2], [ 3, 4], [ 5, 6], [10, 20], [30, 40], [50, 60]]) >>> np.hstack((a,b)) array([[ 1, 2, 10, 20], [ 3, 4, 30, 40], [ 5, 6, 50, 60]])
Direct addition of arrays of different dimensions is obviously not allowed. But you can use an n column vector and an m column row vector to construct an n×m matrix
>>> a = np.array([[1],[2]]) >>> a array([[1], [2]]) >>> b=([[10,20,30]])#生成一个list,注意,不是np.array。 >>> b [[10, 20, 30]] >>> a+b array([[11, 21, 31], [12, 22, 32]]) >>> c = np.array([10,20,30]) >>> c array([10, 20, 30]) >>> c.shape (3,) >>> a+c array([[11, 21, 31], [12, 22, 32]])
check
>>> a array([[1, 2], [3, 4], [5, 6]]) >>> a[0] # array([1, 2]) >>> a[0][1]#2 >>> a[0,1]#2 >>> b = np.arange(6)#array([0, 1, 2, 3, 4, 5]) >>> b[1:3]#右边开区间array([1, 2]) >>> b[:3]#左边默认为 0array([0, 1, 2]) >>> b[3:]#右边默认为元素个数array([3, 4, 5]) >>> b[0:4:2]#下标递增2array([0, 2])
NumPy’s where function uses
np.where(condition, x, y), No. One parameter is a boolean array, and the second and third parameters can be scalars or arrays.
cond = numpy.array([True,False,True,False]) a = numpy.where(cond,-2,2)# [-2 2 -2 2] cond = numpy.array([1,2,3,4]) a = numpy.where(cond>2,-2,2)# [ 2 2 -2 -2] b1 = numpy.array([-1,-2,-3,-4]) b2 = numpy.array([1,2,3,4]) a = numpy.where(cond>2,b1,b2) # 长度须匹配# [1,2,-3,-4]
Change
>>> a = np.array([[1,2],[3,4],[5,6]]) >>> a[0] = [11,22]#修改第一行数组[1,2]为[11,22]。 >>> a[0][0] = 111#修改第一个元素为111,修改后,第一个元素“1”改为“111”。 >>> a = np.array([[1,2],[3,4],[5,6]]) >>> b = np.array([[10,20],[30,40],[50,60]]) >>> a+b #加法必须在两个相同大小的数组键间运算。 array([[11, 22], [33, 44], [55, 66]])
Direct addition of arrays of different dimensions is obviously not allowed. But you can use an n column vector and an m column row vector to construct an n×m matrix
>>> a = np.array([[1],[2]]) >>> a array([[1], [2]]) >>> b=([[10,20,30]])#生成一个list,注意,不是np.array。 >>> b [[10, 20, 30]] >>> a+b array([[11, 21, 31], [12, 22, 32]]) >>> c = np.array([10,20,30]) >>> c array([10, 20, 30]) >>> c.shape (3,) >>> a+c array([[11, 21, 31], [12, 22, 32]])
array and the operations of addition, subtraction, multiplication and division of a number, It is equivalent to a broadcast, broadcasting this operation to each element.
>>> a = np.array([[1,2],[3,4],[5,6]]) >>> a*2#相当于a中各个元素都乘以2.类似于广播。 array([[ 2, 4], [ 6, 8], [10, 12]]) >>> a**2 array([[ 1, 4], [ 9, 16], [25, 36]]) >>> a>3 array([[False, False], [False, True], [ True, True]]) >>> a+3 array([[4, 5], [6, 7], [8, 9]]) >>> a/2 array([[0.5, 1. ], [1.5, 2. ], [2.5, 3. ]])
Delete
Method 1:
Use the method in the search, such as a=a[0]. After the operation, there is only one row left for a.
>>> a = np.array([[1,2],[3,4],[5,6]]) >>> a[0] array([1, 2])
Method 2:
>>> a = np.array([[1,2],[3,4],[5,6]]) >>> np.delete(a,1,axis = 0)#删除a的第二行。 array([[1, 2], [5, 6]]) >>> np.delete(a,(1,2),0)#删除a的第二,三行。 array([[1, 2]]) >>> np.delete(a,1,axis = 1)#删除a的第二列。 array([[1], [3], [5]])
Method 3:
First split, and then assign value according to slice a=a[0].
>>> a = np.array([[1,2],[3,4],[5,6]]) >>> np.hsplit(a,2)#水平分割(搞不懂,明明是垂直分割嘛?) [array([[1], [3], [5]]), array([[2], [4], [6]])] >>> np.split(a,2,axis = 1)#与np.hsplit(a,2)效果一样。 >>> np.vsplit(a,3) [array([[1, 2]]), array([[3, 4]]), array([[5, 6]])] >>> np.split(a,3,axis = 0)#与np.vsplit(a,3)效果一样。
Related recommendations:
Methods for storing and reading data in text format in numpy
The above is the detailed content of Add, delete, modify, and query Numpy array data. For more information, please follow other related articles on the PHP Chinese website!