# numpy

  • N-dimensional array (matrix), fast and efficient, vector mathematical operations

  • Efficient Index without Loop

  • Open source, free cross platform, with running efficiency comparable to C/Matlab

*WebSite https://numpy.org/doc/stable/user/index.html

# Creating and accessing arrays

# create Array from Python list

import numpy as np
list_1 = [1, 2, 3, 4]
array_1 = np.array(list_1)   # one-dimensional array

list_2 = [5, 6, 7, 8]
array_2 = np.array([list_1, list_2])  # two-dimensional array

array_3=np.arange(1,10) # Create arrays from 1 to 10
array_4=np.arange(1,10,2) # Create an array from 1 to 10 with step 2
array_5=np.zeros(5)             # Create 5 arrays of 0
array_6=np.zeros([2,3])       # Create 2 * 3 arrays with 0
array_7=np.eye(5)                # Create a 5 * 5 identity matrix

array_2.shape  # Dimensions of arrays
array_2.size     # The number of array elements
array_2.dtype  # The number of array elements

array_3[1]  # Accessing the first element
array_3[2:4]  # Accessing 2,3,4 element

array_2[1,2]  # Access the first line, second element
array_2[1][2]  # Access the first line, second element

array_7=np.array([[1,2,3],[4,5,6],[7,8,9]])
#section
array_8[:2,1:]     # From line 0 to line 2, from column 1 to the last column

Array And Matrix
import numpy as np
np.random.randn(10)  # Create a 1-dimensional array of 10 elements

np.random.randint(10) # Create a data integer up to 10

np.random.randint(10,size=10) # Create a one-dimensional integer array of up to 10 elements

np.random.randint(10,size=(2,3)) # Create a 2D integer array of 2 * 3 within 10

np.random.randint(10,size=20).reshape(4,5)  # Create a one-dimensional integer array of 20 elements within 10, and convert it to a 2D array of 4 * 5


# Operation of arrays

a=np.random.randint(10,size=20).reshape(4,5)
b=np.random.randint(10,size=20).reshape(4,5)
a+b
a-b
a*b
a/b

# Array and matrix

np.mat([[1,2,3],[4,5,6]])
np.mat(a)                      # Create through a two-dimensional array

# matrix operations

A = np.mat(a)
B = np.mat(b)
A+B
A-B

A1=np.mat(np.random.randint(10,size=20).reshape(5,4))
B1=np.mat(np.random.randint(10,size=20).reshape(4,5))
A1*B1

# Array's function

a=np.random.randint(10,size=20).reshape(4,5)
a
np.unique(a)  # Calculate the unique element in a
sum(a)  # Calculate the sum of elements for each column
sum(a[0]) # Calculate the sum of elements in the first row
sum(a[:,0]) # Calculate the sum of elements in the first column from top to bottom
a.max() # Calculate the maximum value in a
max(a[0]) # Calculate the maximum value of the first row in a
max(a[:,0]) # Calculate the maximum value of the element in the first column from top to bottom

# Array input And output

# Serializing numpy arrays using pickle
import pickle
import numpy as np
x = np.arange(10)

f = open('x.pkl','wb')
pickle.dump(x,f)
!ls

f=open('x.pkl','rb')
b=pickle.load(f)

np.save('one_array',x)
!ls
t=np.load('one_array.npy')

y=np.arange(20)
np.savez('two_array.npz',a=x,b=y)
!ls
c=np.load('two_array.npz')
c['a']
c['b']

# Refer

# Creating and accessing arrays (refer)

import numpy as np
list_1 = [1, 2, 3, 4]
array_1 = np.array(list_1)   # 一维数组
list_1
[1, 2, 3, 4]
array_1
array([1, 2, 3, 4])
list_2 = [5, 6, 7, 8]
array_2 = np.array([list_1, list_2])  # 二维数组
list_2
[5, 6, 7, 8]
array_2
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
array_3=np.arange(1,10) #创建1到10的数组
array_4=np.arange(1,10,2) #创建1到10的数组,间隔2
array_5=np.zeros(5)             #创建5个为0的数组
array_6=np.zeros([2,3])       #创建2*3个为0的数组
array_7=np.eye(5)                #创建5*5的单位矩阵
array_3
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
array_4
array([1, 3, 5, 7, 9])
array_5
array([0., 0., 0., 0., 0.])
array_6
array([[0., 0., 0.],
       [0., 0., 0.]])
array_7
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])
array_2.shape  # 数组的维度
(2, 4)
array_2.size     # 数组元素的个数
8
array_2.dtype  #数组元素的数据类型
dtype('int64')
array_3[1]#访问第1个元素
2
array_3[2:4]  #访问第2到3个元素
array([3, 4])
array_2[1,2]  #访问第1行,第2个元素
7
array_2[1][2]  #访问第1行,第2个元素
7
array_8=np.array([[1,2,3],[4,5,6],[7,8,9]])
#切片
array_8[:2,1:]     #从第0行到第2行,从第1列到最后一列
array([[2, 3],
       [5, 6]])
array_7=np.eye(5) 


# Array and Matrix(refer)

import numpy as np
np.random.randn(10)  #创建一个10个元素的1维数组
array([ 0.42493784, -1.00915342, -0.94640226,  0.60185647,  0.54586824,
        0.69668488, -0.8211607 ,  0.23638224, -0.62685731, -2.0602249 ])
np.random.randint(10) # 创建一个10以内的数据整数
7
np.random.randint(10,size=10) # 创建一个10以内的10个元素的一维的整数数组
array([4, 1, 8, 0, 9, 4, 7, 8, 0, 7])
np.random.randint(10,size=(2,3)) # 创建一个10以内的2*3的二维的整数数组
array([[1, 6, 0],
       [9, 9, 8]])
np.random.randint(10,size=20).reshape(4,5)  # 创建一个10以内的20个元素的一维的整数数组,转化为4*5的二维数组
array([[8, 8, 9, 7, 9],
       [2, 7, 0, 7, 2],
       [2, 8, 6, 5, 7],
       [4, 4, 8, 5, 5]])
# 数组的运算
a=np.random.randint(10,size=20).reshape(4,5)
b=np.random.randint(10,size=20).reshape(4,5)
a
array([[5, 1, 0, 3, 6],
       [0, 6, 4, 2, 8],
       [2, 5, 2, 8, 2],
       [0, 3, 1, 2, 7]])
b
array([[9, 8, 3, 6, 6],
       [6, 1, 8, 9, 0],
       [5, 6, 3, 8, 5],
       [3, 7, 7, 7, 7]])
a+b
array([[14,  9,  3,  9, 12],
       [ 6,  7, 12, 11,  8],
       [ 7, 11,  5, 16,  7],
       [ 3, 10,  8,  9, 14]])
a-b
array([[-4, -7, -3, -3,  0],
       [-6,  5, -4, -7,  8],
       [-3, -1, -1,  0, -3],
       [-3, -4, -6, -5,  0]])
a*b
array([[45,  8,  0, 18, 36],
       [ 0,  6, 32, 18,  0],
       [10, 30,  6, 64, 10],
       [ 0, 21,  7, 14, 49]])
a/b
/tmp/ipykernel_5901/1348051284.py:1: RuntimeWarning: divide by zero encountered in divide
  a/b





array([[0.55555556, 0.125     , 0.        , 0.5       , 1.        ],
       [0.        , 6.        , 0.5       , 0.22222222,        inf],
       [0.4       , 0.83333333, 0.66666667, 1.        , 0.4       ],
       [0.        , 0.42857143, 0.14285714, 0.28571429, 1.        ]])
# matrix
np.mat([[1,2,3],[4,5,6]])
matrix([[1, 2, 3],
        [4, 5, 6]])
np.mat(a)                      #通过二维数组创建
matrix([[5, 1, 0, 3, 6],
        [0, 6, 4, 2, 8],
        [2, 5, 2, 8, 2],
        [0, 3, 1, 2, 7]])
# 矩阵的运算
A = np.mat(a)
B = np.mat(b)
A
matrix([[5, 1, 0, 3, 6],
        [0, 6, 4, 2, 8],
        [2, 5, 2, 8, 2],
        [0, 3, 1, 2, 7]])
B
matrix([[9, 8, 3, 6, 6],
        [6, 1, 8, 9, 0],
        [5, 6, 3, 8, 5],
        [3, 7, 7, 7, 7]])
A+B
matrix([[14,  9,  3,  9, 12],
        [ 6,  7, 12, 11,  8],
        [ 7, 11,  5, 16,  7],
        [ 3, 10,  8,  9, 14]])
A-B
matrix([[-4, -7, -3, -3,  0],
        [-6,  5, -4, -7,  8],
        [-3, -1, -1,  0, -3],
        [-3, -4, -6, -5,  0]])
A1=np.mat(np.random.randint(10,size=20).reshape(5,4))
B1=np.mat(np.random.randint(10,size=20).reshape(4,5))
A1
matrix([[2, 8, 7, 5],
        [4, 1, 4, 4],
        [5, 1, 0, 3],
        [9, 6, 4, 2],
        [2, 6, 4, 5]])
B1
matrix([[1, 4, 7, 9, 2],
        [7, 4, 3, 3, 0],
        [6, 8, 9, 3, 7],
        [9, 7, 4, 6, 8]])
A1*B1

matrix([[145, 131, 121,  93,  93],
        [ 71,  80,  83,  75,  68],
        [ 39,  45,  50,  66,  34],
        [ 93, 106, 125, 123,  62],
        [113,  99,  88,  78,  72]])
a1=np.random.randint(10,size=20).reshape(4,5)
a1

array([[6, 1, 1, 7, 7],
       [7, 0, 7, 1, 3],
       [4, 7, 6, 9, 7],
       [6, 6, 6, 2, 0]])
np.unique(a1)  #计算a中唯一的元素
array([0, 1, 2, 3, 4, 6, 7, 9])
sum(a1)  #计算每一列的元素和
array([23, 14, 20, 19, 17])
a2=np.random.randint(10,size=8).reshape(4,2)
a2
array([[6, 8],
       [5, 1],
       [5, 5],
       [4, 3]])
np.unique(a2)
array([1, 3, 4, 5, 6, 8])
sum(a1[:,0]) #从上到下计算第一列的元素和
23
a1.max() #计算a中的最大值
9
a2.max()
8
max(a1[0]) #计算a中第一行的最大值
7
max(a2[0]) 
8
max(a1[:,0]) #从上到下计算第一列的元素最大值
7
max(a2[:,0]) #从上到下计算第一列的元素最大值
6

# Array's input & output(refer)

# 使用pickle序列化numpy array
import pickle
import numpy as np
x = np.arange(10)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
f = open('x.pkl','wb')
pickle.dump(x,f)
!ls
ArrayOpt .ipynb			 Matrix.ipynb  x.pkl
Array 的input和output操作.ipynb  nohup.out
f=open('x.pkl','rb')
b=pickle.load(f)
b
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.save('one_array',x)
!ls
ArrayOpt .ipynb			 Matrix.ipynb  one_array.npy
Array 的input和output操作.ipynb  nohup.out     x.pkl
t=np.load('one_array.npy')
t
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y=np.arange(20)
y
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
np.savez('two_array.npz',a=x,b=y)
!ls
ArrayOpt .ipynb			 Matrix.ipynb  one_array.npy  x.pkl
Array 的input和output操作.ipynb  nohup.out     two_array.npz
c=np.load('two_array.npz')
c
<numpy.lib.npyio.NpzFile at 0x7f1e8c392c90>
c['a']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
c['b']
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])

lastUpdated: 1/25/2024, 9:14:07 AM