Citation
Standard Python uses list to save values, which can be used when using arrays. But because the elements of the list are arbitrary objects, the list holds pointers to the objects. For numeric operations, this structure obviously wastes memory and CPU compute time.
In addition, Python provides an array module, but because it does not support multidimensional arrays, it is also not suitable for numerical calculations.
So,numpy just made up for these shortcomings, NumPy provided two basic objects: Ndarray and Ufunc. Ndarray is a multidimensional array that stores a single data type, whereas Ufunc is a function that can be processed on an array.
Import method
import numpy as np
list to array: using an array conversion method with NumPy
>>>list=[1,2]>>>_array = np.array(lists)
Array to list: nunpy array to list is also very convenient, directly using the ToList () method can be
>>>_list = _array.tolist()
Use the Random.shuffle method of NumPy to convert a given set of ordered sequences into a random sequence
>>>arr = np.arange(1000#生成一个序列,from 0 to 999>>>np.random.shuffle(arr)
Save NumPy array Format data in text format
>>>np.savetxt("a.txt",a,fmt="%d",delimiter=‘\t‘)
To convert the Python list to numpy format:
>>>pyList = [5, 11, 122]>>>mat(pyList)
An example:
>>> fromNumPyImport*>>>A = Arange ( the). Reshape (3,5)>>>Aarray ([[0,1,2,3,4], [5,6,7,8,9], [Ten, One, A, -, -]])>>>A.shape (3,5)>>>A.ndim2>>>A.dtype.name' Int32 '>>>A.itemsize4>>>A.size the>>>Type (a) Numpy.ndarray>>>b = Array ([6,7,8])>>>Barray ([6,7,8])>>>Type (b) numpy.ndarray
Array creation
>>> fromimport *>>> a = array( [2,3,4] )>>> aarray([234])>>> a.dtypedtype(‘int32‘)>>> b = array([1.23.55.1])>>> b.dtypedtype(‘float64‘)
Compare two ways of creating, the first of which is the wrong
array(1,2,3,4) # WRONGarray([1,2,3,4]) # RIGHT
Two-dimensional array creation
>>> b = array( [ (1.5,2,3), (4,5,6) ] )>>> barray([[ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]])
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python Learning-numpy Data processing