Index
The array index form in NumPy is consistent with Python. Such as:
Np.arange (10)
Print x[2] #单个元素, forward index from the go. Note that the subscript is starting from 0.
Print X[-2] #从后往前索引. The subscript for the last element is-1
Print X[2:5] #多个元素, left closed right open, default step value is 1
Print x[:-7] #多个元素, from the back forward, developed the end position, using the default step value
Print X[1:7:2] #指定步长值
X.shape= (2,5) #x的shape属性被重新赋值, the requirement is that the number of elements is constant. 2*5=10
Print x[1,3] #二维数组索引单个元素, the element in the 4th column of row 2nd
Print X[0] #第一行所有的元素
Y=np.arange (+). Reshape (5,7) #reshape () function to change the dimensions of an array
Print Y[1:5:2,::2] #选择二维数组中的某些符合条件的元素
#python学习之数组 2018.4.17#-*-coding:utf-8-*-from numpy import *import matha =arange (All). Reshape (3,5) # The reshape parameter can be preceded by a number that represents the number of reshape generated after the
Print (A.sum (axis=0)) #A列合计
Print (A.cumsum (Axis=1)) #A中每行的累计和print (a.shape) #A的形状 several rows of print (A.ndim) #A的秩 that is, the number of array axes print (a.dtype.name) # Look in the array for data type print (a.itemsize) #数组中每个元素的字节大小int32/8=4print (a.size) #数据元素个数print (Type (A)) #A的类型为numpy. Ndarray
# C=array ([(1.5,2,3), (4,5,6)]) #可以使用但是1.5,2,3 and other numbers cannot be changed C, so do not apply # C=array ([1.5,2,3],[4,5,6]) #错误C =array ([[1.5,2,3],[ 4,5,6]],dtype=complex) #正确 and specify the array type print (C)
# Print (zeros (3,4)) #一个三行四列都是0的数组 # Print (Ones ((2,3,4), dtype=int16)) #两个三行四列都是1的数组print (Empty ((3,4))) # An array of random content-dependent memory states, which are float64 by default
# set_printoptions (threshold= ' nan ') #表示强制打印整个数组, the middle part does not omit D=array ([1,2,3,4],dtype=int) print (d**2) print (D<2)
E=array ([[[1,1],[0,1]]) F=array ([[[2,0],[3,4]]) print (e*f) #矩阵对应位置相乘print (dot (e,f)) #矩阵乘法print (Linspace (0,pi,3), endpoint=false,retstep=true)) #从0到pi之间均分, produces 3 numbers, excluding the end, the number of returns, and the size of the interval
#一维数组可以被索引, Slice Iteration g=arange (**3print) (G[2:5]) #输出2的3次方, 3 of 3, 4 of three g[:6:2]=-5# equivalent to G[0:6:2], refers to from the No. 0 to 6th number, to 5, the step is 2, that is, the first =-5, the third equals -5print (G[::-1]) #倒序输出
def f (x, y): return 10*x+yh=fromfunction (F, (5,4)) #多维数组每个轴可以有索引
Print (h[0:4,1]) #第一行到第四行的第二列的数值输出print (h[:,1]) #第二列全部的数值输出print (H[1:3,]) #第二行到第三行的数值输出print (h[-1]) #相当于H [-1,:] #点 (... ) represents many of the necessary semicolons that produce a complete set of indexes. If x is an array of rank 5 (that is, it has 5 axes), then: # x[1,2,...] equals x[1,2,:,:,:], #x [..., 3] equals x[:,:,:,:,3] #x [4,..., 5,:] equals x[4,:,:,5,:].# for element in H. Flat: #flag是数组的一个属性 that can be used to operate an array of elements in # print (Element)
#向下取整floor () print (H.ravel ()) #将数组全展平
#复制与视图 # # # no copies of simple assignments do not copy array objects or their data. A=arange (B=aprint) (b is a)
B.shape = 3,4#2. View and shallow copy different array objects sharing the shape of the same data C will not change but the value will be C=a.view () print (c is a)
C.shape = 2,6print (a.shape) C[0,4]=12345print (a)
#3. Deep copy the fully copied array and its data is two arrays completely independent open d=a.copy () print (d is a) print (D.base is a) d[0,0]=-1print (a)
#-----------------------------------------------------------------#numpy函数方法归类. Create array arange array copy empty Empty_like Eye FromFile fromfunction identity, Linspace, Logspace, Mgrid, Ogrid, ones, Ones_like, R, Zeros, zeros_like#2. Conversion Astype, atleast 1d, atleast 2d, atleast 3d, mat#3. Operation Array split, column stack, concatenate, diagonal, Dsplit, Dstack, HS Plit, Hstack, item, Newaxis, Ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, Vsplit, vstack#4. Ask all, a NY, nonzero, where#5. Sort Argmax, Argmin, Argsort, Max, Min, ptp, searchsorted, sort#6. Op choose, Compress, Cumprod, cumsum, I Nner, fill, imag, prod, put, putmask, Real, sum#7. Basic statistics CoV, mean, STD, var#8. Basic linear algebra Cross, Dot, outer, SVD, vdot#----------- ----------------------------------------------------#numpyj进阶 # Fancy Index NumPy provides more indexing capabilities than regular Python sequences. An array can be indexed by an integer array and a Boolean array. K=arange (**2i=array) ([1,1,3,8,5]) print (K[i])
#线性代数 #a=array ([[[2,3],[3,4]]) #inv (a) inverse trace (a) Find Trace A.transpose () Transpose # Solve (A, b) solve ax=b# eigvals solve eigenvalue # EIG (C) The C1 returned is a characteristic value C2 is a feature vector # U,SIGMA,V=NP.LINALG.SVD (d,full_matric=false) SVD singular value decomposition # pinv generalized inverse # det determinant # Matrix Class #amatrix (' 1.0 2.0;3.0 4.0 ') #A. TL = Arange (L.shape=3,4print) (l[:,[1,3])
Print (l[:,l[0,:]>1]) #保留第一行大于1的列
Print (l[l[:,0]>2,l[0,:]>1]) #在矩阵两个方向有条件的切片
#技巧L. shape=2,-1,3 #-1 means that omitting a dimension will automatically deduce 12 numbers without knowing how many rows to output, but to output 3 columns while outputting two print (L)
Set up an array
Tip: Can take the form from NumPy import *+array~
Import NumPy as Np+np.array
#常见错误 error Point: array should provide a list of values as arguments instead of calling with multiple numeric parameters
# B=[[6,7],[8]] #这是list, and we need to use array
# B=array (6,7,8) #错 # b=array[6,7,8] # wrong # b=array[[6,7,8]] #错
B=array ([6,7,8]) #对
Python Small white array index