Python与数据科学实战课程——第二章numpy:数组与矩阵运算

快速创建数组

import numpy as np
#使用random生成随机数组(随机的意思是同一行代码每次运行结果都会不同)
np.random.randn(10)     #生成的是长度为10的一维数组,数组的元素符合标准正态分布

array([-2.01407771, -0.77011376, 0.48240321, 0.52711222, 1.87221082,
0.28280583, 0.89647812, -0.07944895, -0.10669309, -0.02220787])

np.random.randint(10)      #生成一个随机的int,大小在0-10之间 ,每次运行得到不同的int值

8

np.random.randint(10,size=(2,3))   #其中10定义元素范围,size定义维数

array([[1, 3, 7],
[6, 4, 9]])

np.random.randint(10,size=20)

array([1, 5, 6, 3, 3, 7, 7, 1, 7, 6, 3, 6, 8, 1, 0, 1, 8, 2, 0, 1])

np.random.randint(10,size=20).reshape(4,5)   #使用reshape进行重塑

array([[9, 0, 3, 9, 6],
[7, 0, 2, 6, 5],
[8, 7, 2, 6, 6],
[3, 0, 9, 0, 3]])

数组运算

a = np.random.randint(10,size=20).reshape(4,5) 
b = np.random.randint(10,size=20).reshape(4,5) 
a

array([[6, 9, 2, 9, 0],
[3, 5, 1, 3, 9],
[9, 4, 4, 1, 9],
[3, 1, 1, 2, 9]])

b

array([[5, 9, 6, 3, 9],
[1, 9, 0, 5, 7],
[4, 5, 1, 5, 2],
[1, 7, 3, 3, 1]])

a + b

array([[11, 18, 8, 12, 9],
[ 4, 14, 1, 8, 16],
[13, 9, 5, 6, 11],
[ 4, 8, 4, 5, 10]])

a - b

array([[ 1, 0, -4, 6, -9],
[ 2, -4, 1, -2, 2],
[ 5, -1, 3, -4, 7],
[ 2, -6, -2, -1, 8]])

a * b

array([[30, 81, 12, 27, 0],
[ 3, 45, 0, 15, 63],
[36, 20, 4, 5, 18],
[ 3, 7, 3, 6, 9]])

a / b      #报错是因为b中有元素为0,结果中会给出一个 inf 表示无穷大

D:\Anaconda\envs\myPython36\lib\site-packages\ipykernel_launcher.py:1:
RuntimeWarning: divide by zero encountered in true_divide
“”"Entry point for launching an IPython kernel.

array([[ 1.2 , 1. , 0.33333333, 3. , 0.
],
[ 3. , 0.55555556, inf, 0.6 , 1.28571429],
[ 2.25 , 0.8 , 4. , 0.2 , 4.5 ],
[ 3. , 0.14285714, 0.33333333, 0.66666667, 9. ]])

矩阵的创建

np.mat([[1,2,3],[4,5,6]])

matrix([[1, 2, 3],
[4, 5, 6]])

a

array([[6, 9, 2, 9, 0],
[3, 5, 1, 3, 9],
[9, 4, 4, 1, 9],
[3, 1, 1, 2, 9]])

np.mat(a)    #array可以转换为matrix ,所以之前的创建数组的方法,都可以用来快速创建矩阵,
#只需要创建完以后加一个np.mat()即可

matrix([[6, 9, 2, 9, 0],
[3, 5, 1, 3, 9],
[9, 4, 4, 1, 9],
[3, 1, 1, 2, 9]])

矩阵的运算

A = np.mat(a)
B = np.mat(b)
A

matrix([[6, 9, 2, 9, 0],
[3, 5, 1, 3, 9],
[9, 4, 4, 1, 9],
[3, 1, 1, 2, 9]])

B

matrix([[5, 9, 6, 3, 9],
[1, 9, 0, 5, 7],
[4, 5, 1, 5, 2],
[1, 7, 3, 3, 1]])

A + B  #矩阵加法和数组加法相同

matrix([[11, 18, 8, 12, 9],
[ 4, 14, 1, 8, 16],
[13, 9, 5, 6, 11],
[ 4, 8, 4, 5, 10]])

A - B #矩阵减法和数组减法相同

matrix([[ 1, 0, -4, 6, -9],
[ 2, -4, 1, -2, 2],
[ 5, -1, 3, -4, 7],
[ 2, -6, -2, -1, 8]])

A * B   #报错因为前面的矩阵列数和后面矩阵的行数不相等导致的

ValueError Traceback (most recent call last)

in 1 A * B

D:\Anaconda\envs\myPython36
\lib\site-packages\numpy\matrixlib\defmatrix.p

in mul(self, other)
307 if isinstance(other, (N.ndarray, list, tuple)) :
308 # This promotes 1-D vectors to row vectors
–> 309 return N.dot(self, asmatrix(other))
310 if isscalar(other) or not hasattr(other, ‘rmul’) :
311 return N.dot(self, other)

ValueError: shapes (4,5) and (4,5) not aligned: 5 (dim 1) != 4 (dim
0)

a = np.mat(np.random.randint(10,size=20).reshape(4,5))
b = np.mat(np.random.randint(10,size=20).reshape(5,4))
a

matrix([[6, 7, 0, 0, 9],
[8, 0, 4, 7, 8],
[3, 4, 1, 2, 0],
[6, 9, 4, 6, 0]])

b

matrix([[0, 9, 1, 7],
[4, 8, 4, 9],
[6, 2, 2, 8],
[9, 9, 0, 2],
[0, 9, 9, 9]])

a * b

matrix([[ 28, 191, 115, 186],
[ 87, 215, 88, 174],
[ 40, 79, 21, 69],
[114, 188, 50, 167]])

#本课程使用array会多于matrix

array常用函数

a = np.random.randint(10,size=20).reshape(4,5)
np.unique(a) #返回唯一的值。类似于从文本中生成词汇表

array([0, 1, 2, 3, 4, 5, 6, 7, 9])

a

array([[1, 4, 5, 7, 4],
[0, 2, 2, 9, 0],
[7, 4, 5, 1, 6],
[3, 7, 2, 9, 6]])

sum(a)   #求和函数返回所有列的和

array([11, 17, 14, 26, 16])

sum(a[0])  #求第一行的和

21

sum(a[:,0])  #求第一列的和 注意细节 行部分的处理

11

a.max() #看array的中最大元素

9

max(a[0])  #看第0行的最大值

7

max(a[:,2])   #看第2列的最大值

5

你可能感兴趣的:(实战网课,python)