In my book, the functions of numpy are used to create arrays and random numbers. In my book, numpy is used to create arrays and random numbers. Without further ado look at the code and then explain the creation of an array

Ar_1 = np. Array ([1, 2, 3, 4, 5]) ar_2 = np, array ([[1], [2], [3]])Copy the code

Basic usage of the array function

print(ar_2.ndim)# print the number of array ranks, resulting in 2
print(ar_2.shape)Ar_2 = (3,1)
print(ar_2.size)3*1=3
print(ar_2.dtype)Type () is a function, while dtype is a numpy method.

Copy the code

Irregular creation of three arrays

Ar3 = np. Array ([[1, 2, 3], ('a'.'b'.'c')])   # 2d arrays: nested sequences (lists, progenitors)Ar4 = np. Array ([[1, 2, 3], ('a'.'b'.'c'.'d')])   What if the number of nested sequences is different
Copy the code

Create an array using arange()

print(np.arange(10))    # return 0-9, integer
print(np. Arange (10.0))# return 0.0-9.0, floating point
print(np. Arange (5, 12))Return to 5 to 11 #
print(np. Arange (5.0, 12, 2))# return 5.0-12.0, step 2
Copy the code

Linspace function, zeros and ones, eyes

#np.linspace = 10 numbers between 2 and 3Ar0 = np. Linspace (2.0, 3.0, 10)print(ar0)
#np.zeros() generates all zerosAr1 = np.zeros(5) ar2 = np.zeros((2,2), dtype = np.int)print(ar1,ar1.dtype)
print(ar2,ar2.dtype)
print('-- -- -- -- -- -)
# zerOS_like () creates a 0 matrix based on the dimensions of other arraysAr3 = np.array([list(range(5)),list(range(5,10))]) ar4 = Np.zerOS_like (ar3)print(ar4)
#np.ones is similar to ZerosAr5 = np. 'ones ((3, 3))# Notice the two parentheses
ar7 = np.ones_like(ar3)
print(ar5)
print(ar7)
# create diagonal matrix
Copy the code

Shape change & transpose:.t /.resize() 0

0 0.t /.resize()Ar2 = np. Catalog. ((5, 2))print(ar2,'\n',ar2.T)
Shape = (3,4)/(2,3,4) → shape = (4,3)/(4,3,2) → shape = (3,4)/(2,3,4) → shape = (4,3,2) → shape = (3,4)/(4,3,2) →Ar4 = np. Zeros ((4, 6)). Reshape (3, 8)# Usage 2: Change the shape of the array directly after generating itAr5 = np. Reshape (np) arange (12), (3, 4))# Usage 3: Add array to parameter, target shape
0 0 # Change the shape of the array, but be aware that the number of elements using the 0 matrix is the same 0Ar6 = np. The resize (np) arange (5), (3, 4))print(ar6)
# numpy.resize(a, new_shape) : Returns a new array with the specified shape, refilling as many elements as necessary.
0.t /.resize() are new arrays
Copy the code

Copy of array (deep and shallow copy)

Deep copy of array

ar1 = np.arange(10)
ar2 = ar1
print(ar2 is ar1)
ar1[2] = 9
print(ar1,ar2)
Ar1 and ar2 refer to the same value, so ar1 changes, and ar2 changes together
# shallow copy of array
ar3 = ar1.copy()
print(ar3 is ar1)
ar1[0] = 9
print(ar1,ar3)
The copy method generates a full copy of the array and its data
Copy the code

Simple operations on arrays

#astype()
ar=np.arange(10)
ar.astype(np.float)
Convert the data type of the array

An array is either an integer or a floating-point type. When an array is evaluated, all numbers change
print(ar+2)[2,3,4,5,6,7,8,9,10,11]
print(ar-2)
print(ar*2)
print(ar/2)
print(ar**2)

print(ar.mean())  # average
print(ar.max())  # maximize
print(ar.min())  Take the minimum
print(ar.std())  Find standard deviation STD
print(ar.var())  Var
print(ar.sum(), np.sum(ar,axis = 0))  # sum, np.sum() → axis = 0, sum by column; Axis is 1, sum by row
print(np. Sort (np) array (,4,3,2,5,6 [1])))# sort
Copy the code

Slicing is similar to slicing strings and I’m not going to talk about it

Array stacking and splitting

The level of the stack

a = np.array([[1],[2],[3]])   # a is a two-dimensional array with three rows and one column
b = np.array([['a'], ['b'], ['c']])  # b is a two-dimensional array with three rows and one column
ar2 = np.hstack((a,b))  # note :(a,b)), here the shape must be the same
Note that the rank of both arrays must be the same when stacked horizontally. But arrays are the exception. You can do it differently
Copy the code

The vertical stack

A = np. Arange (2, 5) b = np. Arange ar1 (5, 8) = np. Vstack ((a, b))The rank of the two arrays must be the same, and the length can be different
Copy the code

Summary: When stacking, the dimensions of the two arrays must be the same. When stacking horizontally, the shapes of the arrays must be the same. When stacking vertically, the west navigation can be different

Break up

S = np. Arange (16). Reshape ar1 (4, 4) = np. Hspilt (s, 4)Numpy's function, shape, and other methods are not availableAr2 =np.vsplit(s,4)# split the same list
Copy the code

Random number generation

Random number generation is an important auxiliary tool for data analysis, which needs to be mastered emphatically

One: the random number that conforms to the positive distribution

Ran = np. Random. Normal ((4, 4))Generate a standard 4*4 sample value

# randnSample1 = np. Random. Rand (10) the sample2 = np. Random. Randn (10, 4)# generate a sample that conforms to the normal distribution. The numbers in the parameters are array shapes
Copy the code

Random numbers with uniform distribution

# the rand functionA = np. Random. Rand () b = np. Random. Rand (4) c = np. Random. Rand (2, 3)# generate a sample that conforms to the normal distribution. The numbers in the parameters are array shapes

# randint function

flag=np.random.randint(low=4,high=10,size=10)
# generate 10 random numbers between 4 and 9, consistent with uniform distribution, and random number can only be integer type
# Remember that low cannot be greater than high,
t=np.random.randint(3)
Generate a random number between 0 and 2

Copy the code

Numpy is the most popular function in the world

To be familiar with the use of their own hands must be done, may be written by others code is always someone else