Array is the main object of NumPy operation, and also the main object of Python data analysis, this series is my note in learning NumPy.
The following numpy are imported in the article, based on the following methods:
Import NumPy as NP from Import *
1. Create an Ordinary array--np.arange (), Np.array (),
(1) Arange () established is an array of sequences, function prototypes: Arange ([Start,]stop[,step],dtype=none)
Where the start parameter is omitted, the default dtype is float32, starting with 0.
# Create a one-dimensional array from 0-19 ar_1ar_1=np.arange#Output:ar_1=array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ten, one, all,, (+), 16,17, +)# Create 11-20, One-dimensional array of step=2 ar_2ar_2=np.arange (11,21,2)#Output:ar_2=array ([11, 13, 15, 17, 19])
(2) array () is used primarily to create multidimensional arrays, with the prototype: Array (object, Dtype=none, Copy=true, Order=none, Subok=false, ndmin=0)
- Object: is a array_like image, including list,tuple, etc. __array__ custom classes can return objects of an array of classes.
- Dtype: Data type, which defaults to the smallest type of data that can be saved, can be developed
- Other parameters can be viewed through help, which only describes the most common
#creates an array of 2*3 with a data type of Int32Ary_1=np.array ([[2,2,3],[5,7,3]],dtype=int32)#output:ary_1= ([[2, 2, 3],#[5, 7, 3]], Dtype=int32)#create an array of 2*3*3, with data type default:Ary_2=np.array ([[[3,4,5],[3.3,4.2,4.2],[1.3,2.2,5.8]],[[2.3,1.9,5.7],[4.5,6.7,9.7],[2.2,1.2,7.99]]])#Output:#array ([[[3]. , 4. , 5. ],#[3.3, 4.2, 4.2],#[1.3, 2.2, 5.8]],##[2.3, 1.9, 5.7],#[4.5, 6.7, 9.7],#[2.2, 1.2, 7.99]])Ary_2.dtype#Output:dtype (' float64 '),
#虽然由int和float, the array is saved with the smallest data type that can hold the data, so it is float32
#当然也可以通过reshape等方式改变数组的维度 so that you get the array you want
Ary_3=np.arange (20,30). Reshape (2,5)
#output: Ary_3
#array ([[20, 21, 22, 23, 24],
# [25, 26, 27, 28, 29]])
2. Creation of special arrays:
(1) Null array: Empty (), Empty_like ()
- Empty (shape[,dtype=none,order=]), creating an empty array of shapes that form shape, dtype as data types, order as ordered: C (C language)-row-major ; F (Fortran) column-major.
- Empty_like (array) that returns a new, empty array based on the shape and type of the given array (a).
#create an empty array of 3*3:E_1=np.empty ([3,3])#In [the]: print E_1#[[1.72723371e-077 2.68678134e+154 4.44659081e-323] #[0.00000000e+000 0.00000000e+000 0.00000000e+000] #[0.00000000e+000 0.00000000e+000 0.00000000e+000]]#The number of fills is random.#create an empty array of shapes like e_1 with Empty_likeE_2=np.empty_like (e_1)#In [the]: print e_2#[[1.72723371e-077 1.72723371e-077 2.00299617e-313] #[1.72723371e-077 5.92878775e-323 3.18299369e-313] #[0.00000000e+000 9.73471935e-309 0.00000000e+000]]
(2) Other special array creation: eye, ones, zeros also have a similar structure, there is also an identity function to create a square
- Eye[n,[, M, K, Dtype]), N is the number of rows, and M is the number of columns (if you do not set the default to N), the diagonal sequence number: 0 corresponds to the main diagonal, the integer corresponds to the upper diagonal, the negative numbers correspond to lower diagonal;
- Eye_like (array), creating an array of shape diagonal 1 with shape as array
# Create a square matrix with a 3*3 main diagonal of 1: Ey_1=np.eye (3,3 , K=0) print Ey_1 # [[1.0. 0.] # [0. 1.0.] # [0.0. 1.]] # ey_2 the diagonal uper a position (row) ey_2 =np.eye (3,3,k=1) pringt ey_2 # [[0. 1.0.] # [0.0. 1.] # [0. 0.0.]
- Ones (Shape[,dtpe=,order]): Returns an array of shape-shaped elements of 1 according to the given shape
- Ones_like (a): Returns an array of 1 elements with the same shape as a
One_1=np.ones ([5,9])print one_1#in[©]: Print one_1#[[ 1. 1. 1. 1. 1. 1. 1. 1. 1.]#[1. 1. 1. 1. 1. 1. 1. 1. 1.] # [1. 1. 1. 1. 1. 1. 1. 1. 1.]#[1. 1. 1. 1. 1. 1. 1. 1. 1.] # [1. 1. 1. 1. 1. 1. 1. 1. 1.]
- Zeros (Shape[,dtype,order]): Returns an array of shape-shaped elements of 0 according to the given shape
- Zeros_like (a): Returns an array of 0 elements with the same shape as a
Zero_1=np.zeros ([2,3])print zero_1#[[0]. 0. 0.] # [0. 0. 0.]
- Identity (N[,dtype=]) returns an n-dimensional square
In [103]: idenout[1031., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0.], [0., 0., 0., 0. , 0., 1., 0.], [0., 0., 0., 0., 0., 0., 1.]]
3. Structure array
In general, an array holds elements that are homologous, that is, all elements within an array need to be of the same type. In the actual data analysis process, especially the two-dimensional spreadsheet format data, the same row of data is composed of different types. This requires defining a personality dtype for this kind of data. Dtype is actually a class that can be defined by assigning a parameter to a special struct array type. (Personally feel a bit like the Informat in SAS)
#defines a dtype called person, by Dtype#a peson consisting of name,age and weightPerson=np.dtype ([('name', str,20), (' Age', Int32), ('Weight', float32)])Print Person#[(' Name ', ' S20 '), (' Age ', ' <i4 '), (' Weight ', ' <f4 ')]#You can create an array of Dtype as a person.Student=array ([('Cnblog', 10,12.2), ('MyBlog', 40,30)],dtype=Person )#because the type specifies the number of arguments, a tuple is required to create the array's number of rows (because it is immutable), otherwise there may be a readable exceptionPrintStudent#[(' Cnblog ', Ten, 12.199999809265137) (' MyBlog ', Max, 30.0)]
4. Create from file (followed by a special introduction)
Numpy Learning Path (1)--creation of arrays