已知若干家跨国公司的市值(美元),将其换算成人名币
import numpy as np
import random
a = [random.uniform(100.0, 200.0) for i in range(3)]
print(a)
b = np.array(a)
c = b * 6.8
print(c, c[1])
结果:
[129.70799881689956, 115.85222551317838, 161.37833819727018]
[ 882.01439195 787.79513349 1097.37269974] 787.795133489613
已知购物车中每件商品的价格与商品件数,求总金额
import numpy as np
import random
a = [random.uniform(10.0, 20.0) for i in range(3)] # 金额
a = np.array(a)
print(a)
b = [random.randint(1, 10) for i in range(3)] # 件数
b = np.array(b)
print(b)
print(a * b, (a*b).sum())
结果:
[18.21776421 13.63878433 13.37232689]
[9 3 7]
[163.95987791 40.916353 93.60628826] 298.48251916984424
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a)
b = np.array(range(10))
print(b)
print(b.size, b.ndim, b.shape, b.dtype)
print('------------------\n')
c = np.array([[1, 2, 3], [4, 5, 6]])
print(c)
print(c.T, c.size, c.ndim, c.shape, c.dtype)
结果:
[1 2 3 4 5]
[0 1 2 3 4 5 6 7 8 9]
10 1 (10,) int64
------------------
[[1 2 3]
[4 5 6]]
[[1 4]
[2 5]
[3 6]] 6 2 (2, 3) int64
import numpy as np
print(np.array([0] * 10))
print(np.zeros(10), np.zeros(10, dtype='int'), np.ones(10)) # 默认float64
print("empty: ", np.empty(3))
print("arange: ", np.arange(1, 5), np.arange(1, 5, 0.3))
print("linspace: ", np.linspace(0, 3, 4))
print("eye:\n", np.eye(5))
结果:
[0 0 0 0 0 0 0 0 0 0]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0 0 0 0 0 0 0 0 0 0] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
empty: [6.94190175e-310 6.94190175e-310 1.28822032e-057]
arange: [1 2 3 4] [1. 1.3 1.6 1.9 2.2 2.5 2.8 3.1 3.4 3.7 4. 4.3 4.6 4.9]
linspace: [0. 1. 2. 3.]
eye:
[[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.]]
import numpy as np
a = np.arange(1, 5)
print(a)
print(a + 1, a * 3, 1 // a, a ** 2, a > 2)
结果:
[1 2 3 4]
[2 3 4 5] [ 3 6 9 12] [1 0 0 0] [ 1 4 9 16] [False False True True]
import numpy as np
a = np.arange(1, 5)
b = np.arange(4, 8)
print(a, b)
print(a + b, b / a, b ** a, b % a, a == b)
结果:
[1 2 3 4] [4 5 6 7]
[ 5 7 9 11] [4. 2.5 2. 1.75] [ 4 25 216 2401] [0 1 0 3] [False False False False]
import numpy as np
a = np.arange(1, 5)
print(a, a[0])
b = np.arange(1, 7).reshape((2, 3))
print(b, b[0][0], b[0, 0])
结果:
[1 2 3 4] 1
[[1 2 3]
[4 5 6]] 1 1
注意
:array切片没有复制对象
import numpy as np
a = np.arange(1, 5)
print(a, a[0:2], a[:2], a[2:])
print('--------------------')
b = [1, 2, 3, 4]
c = a[:2] # 如果不想修改a[:2].copy()
d = b[:2]
c[0] = 10
d[0] = 10
print(a, b)
print('--------------------')
e = np.arange(1, 7).reshape((2, 3))
print(e)
print(e[0:2, 0:2])
结果:
[1 2 3 4] [1 2] [1 2] [3 4]
--------------------
[10 2 3 4] [1, 2, 3, 4]
--------------------
[[1 2 3]
[4 5 6]]
[[1 2]
[4 5]]
import numpy as np
a = np.arange(1, 5)
print(a, a[a > 2], a > 2)
# 取出偶数
print(a[a % 2 == 0], a[(a > 1) & (a % 2 == 0)])
print(a[(a > 3) | (a % 2 == 0)]) # 或
结果:
[1 2 3 4] [3 4] [False False True True]
[2 4] [2 4]
[2 4]
import numpy as np
a = np.arange(20, 40)
print(a)
print(a[[1, 4, 5, 6]])
print("----------------")
b = np.arange(20, 40).reshape(4, 5)
print(b)
print(b[0, 2:4], b[0, b[0] > 22])
# 特殊取法
print("----------------")
print(b[[1, 3], :][:, [1, 3]])
结果:
[20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]
[21 24 25 26]
----------------
[[20 21 22 23 24]
[25 26 27 28 29]
[30 31 32 33 34]
[35 36 37 38 39]]
[22 23] [23 24]
----------------
[[26 28]
[36 38]] [[26 28]
[36 38]]
通用函数:能同时对数组中所有元素进行运算的函数
常见通用函数:
补充:
import numpy as np
a = np.arange(-2.5, 2.5)
b = np.array([4, 16])
print(a, abs(a), np.abs(a), np.sqrt(b))
# floor: 往小取整 ceil: 往大取整 round,rint: 四舍五入 trunc: 去掉小数位
print(np.floor(a), np.ceil(a), np.round(a), np.trunc(a))
# modf: 整数和小数分开
print(np.modf(a))
结果:
[-2.5 -1.5 -0.5 0.5 1.5] [2.5 1.5 0.5 0.5 1.5] [2.5 1.5 0.5 0.5 1.5] [2. 4.]
[-3. -2. -1. 0. 1.] [-2. -1. -0. 1. 2.] [-2. -2. -0. 0. 2.] [-2. -1. -0. 0. 1.]
(array([-0.5, -0.5, -0.5, 0.5, 0.5]), array([-2., -1., -0., 0., 1.]))
sum 求和 min 求最小值
mean 求平均数 max 求最大值
std 求标准差 argmin 求最小值索引
var 求平方差 argmax 求最大值索引
import numpy as np
a = np.array([4, 3, 5, 6])
print(a.sum(), a.mean(), a.std(), a.var())
print(a.min(), a.max(), a.argmin(), a.argmax())
结果:
18 4.5 1.118033988749895 1.25
3 6 1 3
rand 给定形状产生随机数组(0到1之间的数)
randint 给定形状产生随机整数
choice 给定形状产生随机选择
shuffle 与random.shutffle相同,打乱
uniform 给定形状产生随机数组
import numpy as np
a = np.array([4, 3, 5, 6])
print(np.random.rand(2))
print(np.random.randint(2, 5))
print(np.random.choice([1, 2, 3], 3))
li = [4, 2, 5, 1]
np.random.shuffle(li)
print(li)
print(np.random.uniform(2.0, 4.0, 3))
结果:
[0.72368355 0.55070405]
2
[2 3 1]
[2, 4, 1, 5]
[3.08668595 2.13194105 2.28684747]