0%

python数据分析与机器学习实战-03.Numpy矩阵基础

1
2
3
4
import numpy as np
print(np.arange(15))
a = np.arange(15).reshape(3,5)
print(a)
1
2
3
4
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
1
#结构a.shape
1
(3, 5)
1
2
#维度
a.ndim
1
2
1
2
3
#数据类型
a.dtype
dtype('int64')
1
2
3
#元素数量
a.size
15
1
2
3
4
5
#初始化元素为0的矩阵
np.zeros((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
1
2
3
4
5
6
7
8
9
#初始化元素为1的矩阵,数据类型为int32,结构为(2,3,4)
np.ones((2,3,4),dtype=np.int32)
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],

[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]], dtype=int32)
1
2
3
#生成序列,从10到30,间隔为5
np.arange(10,30,5)
array([10, 15, 20, 25])
1
2
3
#生成序列,从0到2,间隔为0.3
np.arange(0,2,0.3)
array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
1
2
3
4
5
#生成序列,并对序列进行结构操作,当arange()没有制定起始值时,默认从0开始
np.arange(12).reshape(3,4)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
1
2
3
4
5
#生成随机数
np.random.random((3,4))
array([[0.29086774, 0.66853514, 0.80893891, 0.4672383 ],
[0.65005622, 0.433645 , 0.6389985 , 0.00900272],
[0.49442838, 0.66853125, 0.57854652, 0.85672554]])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from numpy import pi
#linspace(),从0到2*pi均匀取100个数
np.linspace(0,2*pi,100)
array([ 0. , 1.01010101, 2.02020202, 3.03030303,
4.04040404, 5.05050505, 6.06060606, 7.07070707,
8.08080808, 9.09090909, 10.1010101 , 11.11111111,
12.12121212, 13.13131313, 14.14141414, 15.15151515,
16.16161616, 17.17171717, 18.18181818, 19.19191919,
20.2020202 , 21.21212121, 22.22222222, 23.23232323,
24.24242424, 25.25252525, 26.26262626, 27.27272727,
28.28282828, 29.29292929, 30.3030303 , 31.31313131,
32.32323232, 33.33333333, 34.34343434, 35.35353535,
36.36363636, 37.37373737, 38.38383838, 39.39393939,
40.4040404 , 41.41414141, 42.42424242, 43.43434343,
44.44444444, 45.45454545, 46.46464646, 47.47474747,
48.48484848, 49.49494949, 50.50505051, 51.51515152,
52.52525253, 53.53535354, 54.54545455, 55.55555556,
56.56565657, 57.57575758, 58.58585859, 59.5959596 ,
60.60606061, 61.61616162, 62.62626263, 63.63636364,
64.64646465, 65.65656566, 66.66666667, 67.67676768,
68.68686869, 69.6969697 , 70.70707071, 71.71717172,
72.72727273, 73.73737374, 74.74747475, 75.75757576,
76.76767677, 77.77777778, 78.78787879, 79.7979798 ,
80.80808081, 81.81818182, 82.82828283, 83.83838384,
84.84848485, 85.85858586, 86.86868687, 87.87878788,
88.88888889, 89.8989899 , 90.90909091, 91.91919192,
92.92929293, 93.93939394, 94.94949495, 95.95959596,
96.96969697, 97.97979798, 98.98989899, 100. ])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#矩阵数学运算
a = np.array([20,30,40,50])
b = np.arange(4)
print(a)
print(b)

[20 30 40 50]
[0 1 2 3]

c = a+b
print(c)
[20 31 42 53]
c = c-1
print(c)
[19 30 41 52]
print(b**2)
[0 1 4 9]
print(a<35)
[ True True False False]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#矩阵乘法
a = np.array((
[1,5],
[0,1]))
b = np.array((
[2,0],
[3,4]))
print(a)
print(b)
print("-------")
#对应位置相乘
print(a*b)
[[1 5]
[0 1]]
[[2 0]
[3 4]]
-------
[[2 0]
[0 4]]
1
2
3
4
5
6
print(a.dot(b))
print(np.dot(a,b))
[[17 20]
[ 3 4]]
[[17 20]
[ 3 4]]