import numpy as np
ones = np.ones(shape=4)print("ones in 4 dimension",ones)
zeros = np.zeros(shape=4)print("zeros in 4 dimension",zeros)
ones_float = np.ones(shape=4,dtype=np.float)print("ones float in 4 dimension",ones_float)
matrix = np.ones((3,4))print("ones matrix in 4 dimension",matrix)
sum_ones_zeros=ones+ones
print("sum ones and zeros",sum_ones_zeros)#线形数据
line_data = np.linspace(1,20,8)print("line data",line_data)# reshape 数据后
line_data_reshape= np.reshape(line_data,(2,4))print("数据重塑后",line_data_reshape)
ones in 4 dimension [1. 1. 1. 1.]
zeros in 4 dimension [0. 0. 0. 0.]
ones float in 4 dimension [1. 1. 1. 1.]
ones matrix in 4 dimension [[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
sum ones and zeros [2. 2. 2. 2.]
line data [ 1. 3.71428571 6.42857143 9.14285714 11.85714286 14.57142857
17.28571429 20. ]
数据重塑后 [[ 1. 3.71428571 6.42857143 9.14285714]
[11.85714286 14.57142857 17.28571429 20. ]]
运算
# refer tutorial https://mofanpy.com/tutorials/data-manipulation/np-pd/np-math1/# plus
ones_plus_ones = ones+ones
print("plus",ones_plus_ones)# minus# ones_minus= # multiply
ones_multiply = ones*zeros
print("multiply",ones_multiply)# power
data_range = np.array([1,2,3,4])print("power",data_range**2)# sin
data_sin= np.sin(data_range)print("sin",data_sin)
zeros_5 = np.zeros(5)# need same dimension to plus or minus# ones_plus_zeros_5 = ones+zeros_5# print("plus",ones_plus_zeros_5)# 矩阵乘法分为两种, 其一是前文中的对应元素相乘,其二是标准的矩阵乘法运算,需要对齐(行 对 列 长度)
a = np.arange(1,10).reshape((3,3))
b = np.arange(0,6).reshape((3,2))
c_dot = np.dot(a,b)print("dot",c_dot)# aggregate function(聚合函数)
sum_a = np.sum(a)print("sum",sum_a)
max_a = np.max(a)print("max in a",max_a)
min_a= np.min(a)print("min in a",min_a)
plus [2. 2. 2. 2.]
multiply [0. 0. 0. 0.]
power [ 1 4 9 16]
sin [ 0.84147098 0.90929743 0.14112001 -0.7568025 ]
dot [[16 22]
[34 49]
[52 76]]
sum 45
max in a 9
min in a 1