# Numpy most!!! Recommended collection!!!

## Numpy Introduction!!! Suggested Collection!!!! (1)

###### Write before:

The Numpy package provides a large number of library functions and operations, and its built-in library functions are much faster than python's built-in functions, which is why numpy is used extensively in data analysis and machine learning. Also, be good at using the help() function to learn how to use a function in the learning process.

#### Installation:

Install in cmd via pip install numpy.

#### Array:

Arrays in numpy are used much like lists in python, but they are different

1. #### Array in numpy

```import numpy as np

# Elements in lists in python can be of different data types
a = [1, 2.3, '4']
print(a)

# Arrays in numpy must all have the same data type
b = np.array([4, 5, 6.0])
print(b)
#
# [1, 2.3, '4']
# [4. 5. 6.]
```
2. #### Create Array

```import numpy as np

# 1.Use np.array to create arrays
a = np.array([1, 2, 3, 4])
print(a)
# [1 2 3 4]

# 2.Creating arrays using np.arange
b = np.arange(0, 10, 2)
print(b)
# Create a one-dimensional array with a step of 2 in the range 0,10 (excluding 10,)
# [0 2 4 6 8]
#
# View np.arange() using the help function
# help(np.arange)

# 3. Use np.random.random to randomly create an array of m rows and n columns with values ranging from 0 to 1
c = np.random.random((2, 3))
print(c)
# [[0.37130822 0.3524612  0.97197089]
#  [0.43064737 0.53399519 0.26157308]]

# 4.np.random.randint to randomly create an integer array of m rows and n columns with values ranging from 0 to 10 (excluding 10,)
d = np.random.randint(0, 10, size=(2, 3))
print(d)
# [[5 1 6]
#  [5 2 1]]

# 5. Special Functions
# 1. Generate a two-dimensional array of 3 rows and 3 columns with all elements 0
a1 = np.zeros((3, 3))
print(a1)
# 2. Generate a matrix of 3 rows and 3 columns with values of 1
a2 = np.ones((3, 3))
print(a2)
# 3. Generate a matrix of 2 rows and 3 columns with values of all 9
a3 = np.full((2, 3), 9)
print(a3)
# 4. Generate a matrix whose diagonals are all 1 and the rest are all 0
a4 = np.eye(4)
print(a4)
# [[1. 0. 0. 0.]
#  [0. 1. 0. 0.]
#  [0. 0. 1. 0.]
#  [0. 0. 0. 1.]]
```
3. #### ndarray common properties

1. array.dtype

Arrays in numpy can only store one data type, so you can get the data type of the elements in the array through array.dtype. Numpy has many data types, which allow it to process a large amount of data with as little space as possible. The common data types in array.dtype are listed below.

data typedescribeUnique identifier
boolBoolean type (True or False) stored in one byte'b'
int8One byte size, -128 to 127'i1'
int16Integer, 16-bit integer (-32768 ~ 32767)'i2'
int32Integer, 32-bit integer (-2147483648 to 2147483647)'i4'
int64Integer, 64-bit integer (-9223372036854775808 ~ 9223372036854775807)'i8'
uint8Unsigned integer, 0 to 255'u1'
uint16Unsigned integer, 0 to 65535'u2'
uint32Unsigned integer, 0 to 2 ** 32 - 1'u4'
uint64Unsigned integer, 0 to 2 ** 64 - 1'u8'
float16Semi-precision floating point number: 16 bits, plus or minus sign 1 bit, exponential 5 bits, precision 10 bits'f2'
float32Single precision floating point number: 32 bits, plus or minus sign 1 bit, exponential 8 bits, precision 23 bits'f4'
float64Double precision floating point number: 64 bits, plus or minus sign 1 bit, exponential 11 bits, precision 52 bits'f8'
complex64Complex numbers, where real and imaginary parts are represented by two 32-bit floating-point numbers'c8'
complex128Complex numbers, where real and imaginary parts are represented by two 64-bit floating-point numbers'c16'
object_python object'O'
string_Character string'S'
unicode_unicode type'U'
```import numpy as np

# 1. Default data type
a1 = np.arange(10)
print(a1)
print(a1.dtype)
# [0 1 2 3 4 5 6 7 8 9]
# int32
# windows system defaults to int32, mac or linux based on actual system

# 2. Specify dtype
b = np.array([1, 2, 3, 4, 5], dtype='i1')
# b=np.array([1,2,3,4,5],dtype=np.int8)
print(b)
print(b.dtype)
# [1 2 3 4 5]
# int8

# 3. Modify dtype
a2 = a1.astype(np.int64)
print(a2)
print(a2.dtype)
# [0 1 2 3 4 5 6 7 8 9]
# int64
# astype does not modify the array itself but returns the modified result

```
2. ##### ndarray.size

Gets the total number of elements in the array.

```import numpy as np

#Gets the number of elements in a two-dimensional array
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.size)
```
3. ##### ndarray.ndim

Get the dimension of the array

```import numpy as np

a1=np.array([1,2,3])#One-dimensional
print(a1.ndim)
a2=np.array([[1,2,3],[4,5,6]])
print(a2.ndim)#Two-dimensional
a3=np.array([
[
[1,1,1],
[2,2,2]
],
[
[3,3,3],
[4,4,4]
]
])
print(a3.ndim)#three-dimensional
# 1
# 2
# 3
```
4. ##### ndarray.shape

Get tuples of array dimensions

```import numpy as np

a1 = np.array([1, 2, 3])
print(a1.shape)
a2 = np.array([[1, 2, 3], [4, 5, 6]])
print(a2.shape)
a3 = np.array([
[
[1, 1, 1],
[2, 2, 2]
],
[
[3, 3, 3],
[4, 4, 4]
]
])
print(a3.shape)
# (3,)
# (2, 3)
# (2, 2, 3)
# #

```

Re-modify the dimensions of an array using reshape

```import numpy as np

a1 = np.array([1, 2, 3])
a2 = np.array([[1, 2, 3], [4, 5, 6]])
a3 = np.array([
[
[1, 1, 1],
[2, 2, 2]
],
[
[3, 3, 3],
[4, 4, 4]
]
])
# Modify a3 to a two-dimensional array of 2 rows and 6 columns and copy to a4
a4 = a3.reshape((2, 6))
print(a4)
print(a4.shape)

# a5 = a3.reshape((1, 12))
# a5 = a3.reshape((12))
a5 = a3.flatten()  # Quickly convert multidimensional arrays to one-dimensional arrays in row order using the flatten function
print(a5)
print(a5.ndim)

# reshape returns the modified result without changing the original array. resize can modify the original array
```
5. ##### ndarray.itemsize

Gets the number of bytes per element in the array

```import numpy as np

a = np.array([1, 2, 3], dtype=np.int64)
print(a.itemsize)
# 8
```
4. #### Numpy Array Operation

• ###### Array Index and Slice
```import numpy as np

# 1. Index and slicing of one-dimensional arrays
a1 = np.arange(10)
print(a1[4])  # 4
# Get the element with subscript 4
print(a1[4:6])  # [4 5]
# Slice: From element with subscript 4 to element with subscript 6 (excluding 6)
print(a1[::2])  # [0 2 4 6 8]
# Take element values at step 2 in all ranges
print(a1[-1])  # 9
# Take the first to last number

print("+++++++++++++++++++++++++++++++++++++")
# Multidimensional Array
a2 = np.random.randint(0, 10, size=(4, 6))
print(a2)
# [[2 1 1 1 6 0]
#  [8 9 5 4 8 8]
#  [4 3 6 1 7 2]
#  [7 3 2 9 2 0]]
print("+++++++++++++++++++++++++++++++++++++")
print(a2[0])  # Remove lines marked 0
# [2 1 1 1 6 0]
print(a2[0:2])  # Remove lines with subscripts of 0,1
# [[2 1 1 1 6 0]
# [8 9 5 4 8 8]]
print(a2[[0, 2, 3]])  # Remove rows with subscripts of 0,2,3 to form a new matrix
# [[2 1 1 1 6 0]
# [4 3 6 1 7 2]
# [7 3 2 9 2 0]]
print(a2[[1, 2], [4, 5]])  # Remove element values labeled (1,4), (2,5)
# [8 2]
print(a2[1:3, 4:6])  # Take elements that overlap rows 2, 3 and 5, 6
# [[8 8]
# [7 2]]

```

Generally speaking, the range of elements before commas in an array index is the range of rows, and the range of elements after commas is the range of columns.

• ###### Boolean Index
```import numpy as np

a1=np.arange(24).reshape((4,6))
print(a1)
#Find elements less than 10 in a1 and place them side by side as True, otherwise as False
print(a1<10)
#Juxtapose elements less than 10 in a1 into a one-dimensional array
print(a1[a1<10])
# Take elements less than 5 or more than 10 in a1 and place them in a one-dimensional array
print(a1[(a1<5)|(a1>10)])
```
• ###### Value substitution
```import numpy as np

a1 = np.random.randint(0, 10, size=(3, 5))
print(a1)
# Replace lines with line subscript 1
a1[1] = np.array([1, 2, 3, 4, 5])
print(a1)
# Set the number of elements less than 3 in the array to 1
a1[a1 < 3] = 1
print(a1)
#Set elements greater than 3 in a3 to 0, otherwise set to 1
a2 = np.where(a1 < 3, 0, 1)
print(a2)

```

Example: There is an array of 8 rows and 9 columns. Please take out all columns greater than 3 in column 8 of 1-5 rows (including row 5).

```import numpy as np

b = np.random.randint(0, 10, size=(8, 9))
print(b)
# Gets an array of data conditions for the specified part first
conditions = b[1:6, 8] > 3
print(conditions)
# Retrieve the data for the specified part
datas = b[1:6, 8]
print(datas)
# Then extract qualified data from the array
ans = datas[conditions]
print(ans)

```
5. #### Broadcast mechanism for arrays

```import numpy as np

a1 = np.random.randint(0, 5, size=(3, 5))
print(a1)

# Put each element value in a1*2
print(a1 * 2)
a2 = np.random.randint(0, 5, size=(3, 5))

# Add the elements A1 and A2 together
# Note that only two matrices with the same number of rows and columns can be added or subtracted
print(a1 + a2)

# Add column vector a3 to the first column of a1
a3 = np.random.randint(0, 5, size=(3, 1))
print(a3 + a1)

# Add row vector a4 to the first row of a1
a4 = np.random.randint(0, 5, size=(1, 5))
print(a4 + a1)

# If the trailing dimension of two arrays has the same axis length or one of them has a length of 1, they are considered broadcasting compatible.
a5 = np.random.randint(0, 5, size=(3, 8, 2))
a6 = np.random.randint(0, 5, size=(8, 2))
print(a5)
print("++++++++++++++++++++++++++++++++++")
print(a6)
print("++++++++++++++++++++++++++++++++++")
print(a6 + a5)

```
6. #### Operation of Array Shape

flatten and ravel functions:

```import numpy as np

a1 = np.random.randint(0, 10, size=(3, 4)).reshape((2, 6))
print(a1)
a1.resize((4, 3))
print(a1)

# flatten and ravel functions
a2 = np.random.randint(0, 10, size=(3, 4))
print(a2)

#flatten converts an array to a one-dimensional array and then returns the copy back, so subsequent modifications to the return value will not affect the previous array.
a3=a2.flatten()
a3[0]=100
print(a2[0,0])
#8

# ravel converts an array to a one-dimensional array and returns this view (which can be interpreted as a reference), so subsequent modifications to this return value affect the previous array.
a4=a2.ravel()
a4[0]=100
print(a2[0,0])
#100
```

Combination of different arrays: overlay multiple arrays together via vstack and hstack and concatenate

```import numpy as np

#np.vstack overlays two arrays with the same number of columns vertically
vstack1=np.random.randint(0,10,size=(3,4))
vstack2 = np.random.randint(0,10,size=(2,4))
# vstack3=np.vstack([vstack1,vstack2])
vstack3=np.concatenate([vstack1,vstack2],axis=0)#axis here will talk later
print(vstack3)
print("="*30)

#np.hstack overlays two arrays with the same number of rows in a horizontal direction
h1=np.random.randint(0,10,size=(3,4))
h2=np.random.randint(0,10,size=(3,1))
# h3=np.hstack([h1,h2])
h3=np.concatenate([h1,h2],axis=1)
print(h3)
```

Array cutting: An array is cut by hsplit, vsplit, and split.

```import numpy as np

sp1 = np.random.randint(0, 10, size=(3, 4))
print(sp1)
# [[9 9 8 7]
#  [6 5 4 5]
#  [1 7 4 3]]

# Cut the sp1 array into vertical squares, cut it into two parts, and form two arrays horizontally
print(np.hsplit(sp1, 2))
# [array([[9, 9],
#        [6, 5],
#        [1, 7]]), array([[8, 7],
#        [4, 5],
#        [4, 3]])]

# Cut a knife in front of the columns labeled 1 and 2 to form three arrays
print(np.hsplit(sp1,(1,2)))
# [array([[9],
#        [6],
#        [1]]), array([[9],
#        [5],
#        [7]]), array([[8, 7],
#        [4, 5],
#        [4, 3]])]
print("=*"*30)
vs1 = np.random.randint(0,10,size=(4,5))
print(vs1)
# [[7 8 8 0 9]
#  [9 7 9 6 2]
#  [4 3 5 1 5]
#  [7 9 0 0 4]]

print(np.vsplit(vs1,4))
# [array([[7, 8, 8, 0, 9]]), array([[9, 7, 9, 6, 2]]), array([[4, 3, 5, 1, 5]]), array([[7, 9, 0, 0, 4]])]

print(np.vsplit(vs1,(1,3)))
# [array([[7, 8, 8, 0, 9]]), array([[9, 7, 9, 6, 2],
#        [4, 3, 5, 1, 5]]), array([[7, 9, 0, 0, 4]])]

#Cutting using the split function
print(np.split(sp1,4,axis=1))
#axis=1 means cut by column, axis=0 means cut by row
print(np.split(vs1,4,axis=0))
```
7. #### Matrix and axis transposition

```import numpy as np

t1=np.random.randint(0,10,size=(2,4))
print(t1)
#t1 transpose, does not affect the original array
print(t1.T)
#You can also use the transpose method to transpose, but transpose affects the original array
t2=t1.transpose()
t2[0]=100
print(t1)
#Find the inner product of a matrix
print(t1.dot(t1.T))
```

Write at the end...