Python library numpy basic content study notes

NumPy is the basic package of scientific computing in Python. It is a Python library that provides multi-dimensional array objects, various derived objects (such as masked arrays and matrices), and various APIs for fast array operations, including math, logic, shape operations, sorting, selection, input and output , Discrete Fourier Transform, basic linear algebra, basic statistical operations and random simulation, etc.
For detailed study, go to numpy Chinese website : https://www.numpy.org.cn/

Create an array:

You can use the array function to create an array from a regular Python list or tuple. The type of the resulting array is deduced from the type of the elements in the Python list. That is, the array element type is the same as the element type in the Python list or tuple .
(The results of the code will be written in the comments)

import numpy as np

#将列表转换为数组
array = np.array([[1,2,3],
                 [4,5,6]])
print(array)

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

If the array element is unknown, but the size of the array is known, NumPy provides several functions to create an array with initial placeholder content.

  1. zeros(): You can create an array of all zeros with a specified length or shape
  2. ones(): You can create an array of all 1s with a specified length or shape
  3. empty(): Create an array whose initial content is random , depending on the state of the memory
import numpy as np

zeroarray = np.zeros((2,3))
print(zeroarray)
#[[0. 0. 0.]
# [0. 0. 0.]]

onearray = np.ones((3,4))
print(onearray)
#[[1. 1. 1. 1.]
# [1. 1. 1. 1.]
# [1. 1. 1. 1.]]

emptyarray = np.empty((3,4))
print(emptyarray)
# [[1. 1. 1. 1.]
#  [1. 1. 1. 1.]
#  [1. 1. 1. 1.]]

In order to create an array of numbers, NumPy provides a function similar to range, which is similar to the range function.

import numpy as np

array = np.arange( 10, 31,5 )
print(array)
#[10 15 20 25 30]

Output array information:

import numpy as np

array = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(array)
# [[ 1  2  3]
#  [ 4  5  6]
#  [ 7  8  9]
#  [10 11 12]]

#数组维度
print(array.ndim)
#2

#数组形状
print(array.shape)
#(4, 3)

#数组元素个数
print(array.size)
#12

#数组元素类型
print(array.dtype)
#int32

Calculation of array:

Basic operations:

import numpy as np

arr1 = np.array([[1,2,3],[4,5,6]])
arr2 = np.ones([2,3])

print(arr1 + arr2)
# [[2. 3. 4.]
#  [5. 6. 7.]]

print(arr1 - arr2)
# [[0. 1. 2.]
#  [3. 4. 5.]]

print(arr1 * arr2)
# [[1. 2. 3.]
#  [4. 5. 6.]]

print(arr2 / arr1)
# [[1.         0.5        0.33333333]
#  [0.25       0.2        0.16666667]]

print(arr1 ** 2)
# [[ 1  4  9]
#  [16 25 36]]

It is worth noting that the operation here is the operation of the corresponding elements of the matrix, including multiplication and power operations. Therefore, if the two matrices are of different sizes, an error will occur.

Matrix multiplication

import numpy as np

arr3 = np.array([[1,2,3],[4,5,6]])
arr4 = np.ones([3,2],dtype=np.int64)
print(np.dot(arr3,arr4))
# [[ 6  6]
#  [15 15]]

Matrix transposition and flattening (this Chinese is not easy to describe...flattening??)

import numpy as np

arr3 = np.array([[1,2,3],[4,5,6]])
arr3_tran = arr3.transpose()
print(arr3)
# [[1 2 3]
#  [4 5 6]]

print(arr3_tran)
# [[1 4]
#  [2 5]
#  [3 6]]

print(arr3.flatten())
# [1 2 3 4 5 6]

Indexing, slicing, and iteration of arrays

Same as other sequence types in python

import numpy as np

arr5 = np.arange(0,6).reshape([2,3])
print(arr5)
# [[0 1 2]
#  [3 4 5]]

print(arr5[1])
#[3 4 5]

print(arr5[1][2])
#5

print(arr5[1,2])
#5

print(arr5[1,:])
#[3 4 5]

print(arr5[:,1])
#[1 4]

print(arr5[1,0:2])
# [3 4]

for i in arr5:
    print(i**2)
#[0 1 4]
#[ 9 16 25]

Basic operations are about these, more content is still to go to the Numpy Chinese website : https://www.numpy.org.cn/

If there are errors in this article, or there are problems with the content, please feel free to communicate in the comment area~

Guess you like

Origin blog.csdn.net/splnn/article/details/111596363