This article mainly introduces the NumPy array data increase, delete, change, check, has a certain reference value, now share to everyone, have the need for friends can refer to
Preparatory work:
There are many ways to increase, delete, change, and check, and there are only a few common 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的, elements are 1-dimensional arrays >>> a = NP. Zeros ((2,3)) #创建3行2列, elements are two-dimensional arrays of 0 >>> a = np.ones (2,3) #创建3行2列, elements are two-dimensional arrays of 1 >>> a = Np.empty ((2,3)) # Create 3 rows and 2 columns, uninitialized two-dimensional arrays >>> a = Np.arange (6) #创建长度为6的, elements are 1-dimensional arrays Array ([0, 1, 2, 3, 4, 5]) >>> a = Np.arange (1,7,1 ) #结果与np. Arange (6). First, two parameters mean values from 1〜6, not including 7. The third parameter table step is 1. A = Np.linspace (0,10,7) # The first generation is 0, the bottom is 10, with 7 numbers of arithmetic progression [0. 1.66666667 3.33333333 5. 6.66666667 8.33333333. ] A = Np.logspace (0,4,5) #用于生成首位是10 **0, the bottom is 10**4, with 5 numbers of geometric series. [1.00000000e+00 1.00000000e+01 1.00000000e+02 1.00000000e+03 1.00000000e+04]
Increase
>>> a = Np.array ([[1,2],[3,4],[5,6]]) >>> B = Np.array ([[10,20],[30,40],[50,60]]) >>> Np.vstack ((a)) array ([[1, 2], [3, 4], [ 5, 6], [ten], [+], [ []] ) >>> Np.hstack ((a)) array ([[1, 2, ten,], [3, 4, +, +], [5, 6, 50, 60]])
The direct addition of arrays of different dimensions is obviously not allowed. However, a nxm matrix can be constructed with an n-row vector and an M-column lines vector.
>>> a = Np.array ([[[1],[2]]) >>> a array ([[[1], [2]]) >>> b= ([[10,20,30]]) #生成一个list, note that Not Np.array. >>> b [[[Ten],]] >>> a+b Array ([[One, one, ten]] ) >>> C = Np.array ([10,20,30 ]) >>> c Array ([12, 22, 32]) >>> C.shape (3,) >>> a+c Array ([[one-by-one], +--]
Check
>>> Aarray ([[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])
The WHERE function of the numpy uses the
Np.where (condition, x, y), the first parameter is a Boolean array, the second argument and the third parameter can be scalar or an array.
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 = Nump Y.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) # length must match # [1,2,-3,-4]
Change
>>> a = Np.array ([[1,2],[3,4],[5,6]]) >>> a[0] = [11,22] #修改第一行数组 [] [11,22]. >>> A[0][0] = 111# modifies the first element to 111, and after modification, the first element "1" is changed to "111". >>> A = Np.array ([[1,2],[3,4],[5,6]]) >>> B = Np.array ([[10,20],[30,40],[50,60]]) >>> a +b #加法必须在两个相同大小的数组键间运算. Array ([[One, one], [+], [ 55, 66]])
The direct addition of arrays of different dimensions is obviously not allowed. However, a nxm matrix can be constructed with an n-row vector and an M-column lines vector.
>>> a = Np.array ([[[1],[2]]) >>> Aarray ([[[1], [2]]) >>> b= ([[10,20,30]]) #生成一个list, note that Not Np.array. >>> b[[10, 30]]>>> a+barray ([[One, one, +]] ) >>> C = Np.array ([10,20,30]) & Gt;>> CArray ([12, 22, 32]) >>> C.shape (3,) >>> A+carray ([[One-by-one];
The subtraction of an array and a number is equivalent to a broadcast, which broadcasts the operation to each element.
>>> a = Np.array ([[1,2],[3,4],[5,6]]) >>> a*2# is equivalent to multiplying each element in a by 2. Similar to broadcasting. Array ([[2, 4], [6, 8], [ten]]) >>> a**2 Array ([[1, 4], [9, +], [+ ]]) >>> a& Gt;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.])
By deleting
Method One:
Using the method in the lookup, such as A=a[0], after the operation, the number of rows in a is only one line.
>>> a = Np.array ([[[1,2],[3,4],[5,6]]) >>> a[0] Array ([1, 2])
Method Two:
>>> a = Np.array ([[1,2],[3,4],[5,6]]) >>> np.delete (a,1,axis = 0) #删除a的第二行. Array ([[1, 2], [5, 6]]) >>> Np.delete (A, (), 0) #删除a的第二, three rows. Array ([[[1, 2]]) >>> np.delete (A,1,axis = 1) #删除a的第二列. Array ([[1], [3], [5]])
Method Three:
Split first, then by Slice a=a[0] assignment.
>>> a = Np.array ([[1,2],[3,4],[5,6]]) >>> np.hsplit (a,2) #水平分割 (do not understand, obviously is vertical division?) ) [Array ([[[1], [3], [5]]), Array ([[[2], [4], [6]])] >>> np.split (a,2,axis = 1) #与np. Hsplit (A, 2) the same effect. >>> Np.vsplit (a,3) [Array ([[[1, 2]]), Array ([[[3, 4]]), Array ([[[5, 6]])] >>> np.split (a,3,axis = 0) # Same as the Np.vsplit (a,3) effect.