numpy learning

# 创建数组
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值