Numpy学习笔记
1、数组的定义和应用
2、数组元素的索引选取
3、数组的计算
4、线性代数的运行计算
1 Arrays
array的维数就是array的秩(rank)
import numpy as np
a = np.array([1, 3, 4, 6])
# 此时是列向量
print(a)
[1 3 4 6]
print(a.size)
print(a.shape)
4
(4,)
三维数组
b = np.array([[[1,2,3],[1,2,3]]])
print(b)
print('---------')
print(b.shape)
[[[1 2 3]
[1 2 3]]]
---------
(1, 2, 3)
2、创建Array
c = np.zeros([2,3])
print(c)
[[0. 0. 0.]
[0. 0. 0.]]
zeros_like : 仿照一个数组格式创造一个全0数组
d = np.zeros_like(b)
print(d)
[[[0 0 0]
[0 0 0]]]
eye(2) : 创建 i
e = np.eye(3)
print(e)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
3、Array的常用属性和方法
random.rand(3,4):返回 3 行 4 列的 0-1 之间随机值
f = np.random.rand(3,4)
f
array([[0.29918133, 0.99695443, 0.70050163, 0.16706489],
[0.44481688, 0.55517105, 0.9650693 , 0.93577429],
[0.70169262, 0.95443642, 0.52026445, 0.0306768 ]])
SUM:求和的 3 种方法
np.sum()和print(np.sum())区别在输出是否有array()
np.sum(f,axis=0)
array([1.44569082, 2.5065619 , 2.18583537, 1.13351597])
axis=0 /1 分别代表对列和对行操作
print(np.sum(f))
print(np.sum(f,axis=0))
print(np.sum(f,axis=1))
7.271604071001589
[1.44569082 2.5065619 2.18583537 1.13351597]
[2.16370227 2.90083152 2.20707029]
sort
# 默认是按行穿透排序,即axis=1
np.sort(f)
array([[0.16706489, 0.29918133, 0.70050163, 0.99695443],
[0.44481688, 0.55517105, 0.93577429, 0.9650693 ],
[0.0306768 , 0.52026445, 0.70169262, 0.95443642]])
np.sort(f,axis=0)
array([[0.29918133, 0.55517105, 0.52026445, 0.0306768 ],
[0.44481688, 0.95443642, 0.70050163, 0.16706489],
[0.70169262, 0.99695443, 0.9650693 , 0.93577429]])
# 返回的是元素大小的索引
f.argsort()
array([[3, 0, 2, 1],
[0, 1, 3, 2],
[3, 2, 0, 1]])
# 返回对应行/列的最大值索引
f.argmax(axis=1)
array([1, 2, 1])
# 返回大于0.5的索引,数组1是行数,数组2是列数,对应着看
np.where(f>0.5)
(array([0, 0, 1, 1, 1, 2, 2, 2]), array([1, 2, 1, 2, 3, 0, 1, 2]))
4、随机数
rand(d0, d1, d2 …… dn) 输出0-1之间的随机数,每个参数是一维
randn(d0, d1, d2 …… dn) 输出正态分布的0-1随机数,每个参数是一维
randint(low, high, size,dtype) 输入下上界,维数,*
choice(a, size) 输入参数可以是上界/数组(限定从中筛选)
生成0-1之间的随机数
np.random.rand(10)
array([0.71028704, 0.43191226, 0.50255482, 0.21935992, 0.39390923,
0.57976832, 0.9898773 , 0.95256839, 0.33815693, 0.89487692])
np.random.rand(3,4)
array([[0.03521269, 0.68399925, 0.00300619, 0.13844359],
[0.6677425 , 0.54130992, 0.34670055, 0.6620398 ],
[0.87538985, 0.7452303 , 0.5317525 , 0.95381903]])
生成正态分布的随机数
np.random.randn(10)
array([ 1.31280276, -0.10793412, -0.73151144, -2.25348606, 1.04692662,
0.53299785, -0.28173337, 1.37021352, 1.33366902, 1.62139734])
生成范围内随机数组
np.random.randint(4,7,size=3)
array([5, 4, 5])
np.random.randint(9,15,size=(3,4))
array([[10, 11, 11, 9],
[ 9, 14, 14, 13],
[14, 13, 10, 9]])
random.random()和random.rand()没有明显区别
np.random.random(10)
array([0.69736939, 0.35938908, 0.05222814, 0.36664614, 0.45729116,
0.92235331, 0.00982576, 0.48825766, 0.52762227, 0.18377659])
random.choice()和random.randint()的区别
randint()的前两个参数是下上界,choice的第一个参数可以是数组和上界
np.random.choice([1,2,3],(3,4))
array([[2, 2, 2, 2],
[2, 2, 2, 1],
[1, 2, 1, 3]])
np.random.rand(2,4)
array([[0.15795165, 0.12727261, 0.5790727 , 0.14881722],
[0.95894915, 0.60085816, 0.28078072, 0.86660551]])
5、数组的索引
g = np.random.randint(1,10,(6,7))
g
array([[2, 8, 8, 2, 4, 3, 3],
[8, 9, 7, 9, 1, 5, 1],
[3, 4, 8, 6, 9, 5, 9],
[6, 2, 4, 7, 2, 1, 2],
[1, 4, 8, 1, 5, 4, 7],
[6, 1, 9, 6, 2, 5, 9]])
切片
g[0:2,0:3]
array([[2, 8, 8],
[8, 9, 7]])
布尔型索引
g>4
array([[False, True, True, False, False, False, False],
[ True, True, True, True, False, True, False],
[False, False, True, True, True, True, True],
[ True, False, False, True, False, False, False],
[False, False, True, False, True, False, True],
[ True, False, True, True, False, True, True]])
g[g>7]
array([8, 8, 8, 9, 9, 8, 9, 9, 8, 9, 9])
g[2::2,::2]
array([[3, 8, 9, 9],
[1, 8, 5, 7]])
6、数组的数学
h = np.random.random([3,4])
i = np.random.random([3,4])
h
array([[0.31145957, 0.6485324 , 0.62999148, 0.34106203],
[0.08457065, 0.20985707, 0.20120685, 0.17891792],
[0.50814369, 0.12085831, 0.87225719, 0.35348592]])
h+2
array([[2.31145957, 2.6485324 , 2.62999148, 2.34106203],
[2.08457065, 2.20985707, 2.20120685, 2.17891792],
[2.50814369, 2.12085831, 2.87225719, 2.35348592]])
h+i
array([[1.14708914, 0.89204308, 1.19424484, 0.6964331 ],
[0.74506304, 0.38368983, 0.63390267, 0.29182269],
[1.40465142, 1.10406526, 1.40642566, 1.02236906]])
# 矩阵的内积
np.dot(a,b.T) #或者a.dot(b.T)
array([[1.64305069, 1.03716829, 1.50919486],
[1.42773865, 1.13712743, 1.37372482],
[1.06103007, 0.6484528 , 0.75675077]])
网友评论