Import NumPy as np#create an array of 1*10^7 Elementsarr = Np.arange (1e7) #Converting ndarray to Listlarr = Arr.tolist () #Cr Eate a 2D numpy Arrayarr = Np.zeros ((3,3)) #Converting a array to Matrixmat = Np.matrix (arr) np.matrix (' 1,2,3;4,5,6;7,8,9 '); #Array Creation#first We create a list and then#wrap it with the Np.array () functionalist = [1,2,3]arr = Np.array (alist) #C Reating an array of zeros with 5 Elementsarr = Np.zeros (5) #Creating a array going from 0 to 100#not include 100arr = Np.a Range #from (not include) arr = Np.arange (+) #100 steps Form 1 to 100# (start, end, step) arr = Np.lin Space (0, 1, over) #Creating an 5x5 array of zerosimage = Np.zeros ((5,5)) #Creating a 5X5X5 cube of 1 ' s#the astype () method SE TS the array with integer elementscube = Np.zeros (5,5,5). Astype (int.) + 1#or even simpler with 16-bit floating-point precis Ioncube = Np.ones ((5,5,5)). Astype (np.float16) #Change Data type#use dtype:int numpy.float16, Numpy.float32, Numpy.float64arr = Np.zeros (2, Dtype=iNT) arr = Np.zeros (2, Dtype=np.float32) "The restructured arrays is just different viewsof the same data in memory. If Chang One of them, you'll change all. If you don ' t want this to happen and then use the Numpy.copy functionto separete the arrays mamory-wise. " #Created arrays and reshape them in many others ways#creating an array with elements from 0 to 999arr1d = np.arange (1000) # Reshaping the array to a 10x10x10 3D Arrayarr3d = Arr1d.reshape ((10,10,10)) Arr3d = Np.reshape (arr1d, (10,10,10)) #Invesely, We can flatten arraysarr4d = Np.zeros ((10,10,10,10)) arr1d = Arr4d.ravel () Print Arr1d.shaperecarr = Np.zeros ((2,), Dtype (' I4, F4, A10 ')) #the type for the first to third columns#i4: = 32-bit integer#f4: = 32-bit float#a10: = a string of characte RS Long#we can assign names to each columnrecarr.dtype.names = (' integers ', ' floats ', ' Strings ') #Indexing and Slicingalist = [[1,2],[3,4]]arr = Np.array (alist) arr[0,1] #It ' s the same as arr[0][1]arr[:,1] #return the last columnarr[1,:] #return the BottoM row
[Python] Scipy and Numpy (1)