Python数据分析笔记-02

1.为数组加上或者乘以一个标量

>>> import numpy as np

>>> a=np.array([1,2,3,4,5])

>>> a

array([1, 2, 3, 4, 5])

>>> a*1

array([1, 2, 3, 4, 5])

>>> a

array([1, 2, 3, 4, 5])

>>> a*2

array([ 2, 4, 6, 8, 10])

>>> a

array([1, 2, 3, 4, 5])

2.两个数组进行计算

1)元素数量相同

>>> a

array([1, 2, 3, 4, 5])

>>> b=np.array([2,3,4,5,6])

>>> b

array([2, 3, 4, 5, 6])

>>> a*b

array([ 2, 6, 12, 20, 30])

>>> a

array([1, 2, 3, 4, 5])

>>> b

array([2, 3, 4, 5, 6])

2)元素数量不同:会报错

>>> b

array([2, 3, 4, 5, 6])

>>> c=np.array([3,4,5,6,7,8,9])

>>> c

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

>>> b*c

Traceback (most recent call last):

 File "", line 1, in

ValueError: operands could not be broadcast together with shapes (5,) (7,)

3.可以对一个数组先进行函数运算,该函数运算返回值也是一个数组

>>> a

array([1, 2, 3, 4, 5])

>>> b=np.sin(a)

>>> b

array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427])

4.多维数组的运算也是元素级别的

>>> a=np.arange(9).reshape(3,3)

>>> a

array([[0, 1, 2],

      [3, 4, 5],

      [6, 7, 8]])

>>> b=np.zeros((3,3))

>>> b

array([[ 0., 0., 0.],

      [ 0., 0., 0.],

      [ 0., 0., 0.]])

>>> a*b

array([[ 0., 0., 0.],

      [ 0., 0., 0.],

      [ 0., 0., 0.]])

5.矩阵积

1)c=np.dot(a,b)求a和b的矩阵积--第一种写法

>>> a=np.arange(9).reshape(3,3)

>>> b=np.random.random(9).reshape(3,3)

>>> a

array([[0, 1, 2],

      [3, 4, 5],

      [6, 7, 8]])

>>> b

array([[ 0.1752667 , 0.61713814, 0.63455636],

      [ 0.42635687, 0.9609163 , 0.40790306],

      [ 0.01270341, 0.29411413, 0.52187812]])

>>> c=np.dot(a,b)

>>> c

array([[ 0.4517637 ,  1.54914457,  1.45165931],

      [ 2.29474465,  7.1656503 ,  6.14467194],

      [ 4.1377256 , 12.78215603, 10.83768457]])

2)c=a.dot(b)

>>> c=a.dot(b)

>>> c

array([[ 0.4517637 ,  1.54914457,  1.45165931],

      [ 2.29474465,  7.1656503 ,  6.14467194],

      [ 4.1377256 , 12.78215603, 10.83768457]])

>>> a

array([[0, 1, 2],

      [3, 4, 5],

      [6, 7, 8]])

>>> b

array([[ 0.1752667 , 0.61713814, 0.63455636],

      [ 0.42635687, 0.9609163 , 0.40790306],

      [ 0.01270341, 0.29411413, 0.52187812]])

6.数组的自运算:不会新生数组,改变原数组

>>> a

array([[0, 1, 2],

      [3, 4, 5],

      [6, 7, 8]])

>>> a+=1

>>> a

array([[1, 2, 3],

      [4, 5, 6],

      [7, 8, 9]])

7.通用函数:ufunx

对数组中的每个元素逐一进行操作,生成一个新数组,如平方根函数sqrt() 对数函数 log() 正弦函数sin()

>>> a

array([[1, 2, 3],

      [4, 5, 6],

      [7, 8, 9]])

>>> b=np.sin(a)

>>> b

array([[ 0.84147098, 0.90929743, 0.14112001],

      [-0.7568025 , -0.95892427, -0.2794155 ],

      [ 0.6569866 , 0.98935825, 0.41211849]])

>>> c=np.sqrt(a)

>>> c

array([[ 1.       , 1.41421356, 1.73205081],

      [ 2.       , 2.23606798, 2.44948974],

      [ 2.64575131, 2.82842712, 3.       ]])

>>> d=np.log(a)

>>> d

array([[ 0.       , 0.69314718, 1.09861229],

      [ 1.38629436, 1.60943791, 1.79175947],

      [ 1.94591015, 2.07944154, 2.19722458]])

8.聚合函数

对一数组进行聚合函数的套用,返回一个单一值作为结果

>>> a

array([[1, 2, 3],

      [4, 5, 6],

      [7, 8, 9]])

>>> a.sum()

45

>>> a.max()

9

>>> a.min()

1

>>> a.mean()

5.0

>>> a.std()

2.5819888974716112

你可能感兴趣的:(Python数据分析笔记-02)