Starting today , Xiaobai, I will lead you to learn Python Introduction to zero foundation . This column will explain + Practice mode , Let's get familiar with it Python The grammar of , application , And the basic logic of the code .
Numpy yes Python A very important library , It provides us with a large number of data processing functions .
Installation command :
pip install numpy
pip3 install numpy
Anaconda It is a library of Computing Science , It can provide convenience for us Python Environmental Science .
install :
Anaconda Official website
Import Numpy package :
# Guide pack
import numpy as np
![ Insert picture description here ](https://img-blog.csdnimg.cn/002699042b894844a1349b86b29bdc6a.gif)
ndarray yes Numpy The most important feature . ndarray It's a N Dimensional array object .
np.array
It can help us create a ndarray.
Format :
numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
Parameters :
Example :
# Guide pack
import numpy as np
# establish ndarray
array1 = np.array([1, 2, 3]) # adopt lsit establish
array2 = np.array([1, 2, 3], dtype=float)
# Debug output
print(array1, type(array1))
print(array2, type(array2))
Output results :
# Guide pack
import numpy as np
# establish ndarray
array1 = np.array([1, 2, 3]) # adopt lsit establish
array2 = np.array([1, 2, 3], dtype=float)
# Debug output
print(array1, type(array1))
print(array2, type(array2))
np.zeros
It can help us create a full 0 Array .
Format :
numpy.zeros(shape, dtype=float, order='C', *, like=None)
Parameters :
Example :
import numpy as np
# Create whole 0 Of ndarray
array = np.zeros((3, 3), dtype=int)
print(array)
Output results :
[[0 0 0]
[0 0 0]
[0 0 0]]
np.zeros
It can help us create a full 1 Array .
Format :
numpy.ones(shape, dtype=float, order='C', *, like=None)
Parameters :
Example :
import numpy as np
# Create whole 1 Of ndarray
array = np.ones((3, 3), dtype=int)
print(array)
print(type(array))
Output results :
[[1 1 1]
[1 1 1]
[1 1 1]]
<class 'numpy.ndarray'>
adopt reshape()
We can change the shape of the array .
Format :
numpy.reshape(arr, newshape, order='C')
Parameters :
Example :
import numpy as np
# establish ndarray
array = np.zeros(9)
print(array)
# reshape
array = array.reshape((3,3))
print(array)
print(array.shape) # Debug output array shape
Output results :
[0. 0. 0. 0. 0. 0. 0. 0. 0.]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
(3, 3)
adopt flatten()
We can flatten the multidimensional array into 1 Dimension group .
Example :
import numpy as np
# Create multidimensional arrays
array = np.zeros((3, 3))
print(array)
# flatten Convert to one-dimensional array
array = array.flatten()
print(array)
Output results :
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[0. 0. 0. 0. 0. 0. 0. 0. 0.]