Cris 的 Python 数据分析笔记 03:NumPy 矩阵运算和常用函数(重点)

版权声明:转载请注明出处~ 摸摸博主狗头 https://blog.csdn.net/cris_zz/article/details/84200216

03. 矩阵运算和常用函数(重点)

1. numpy 矩阵判断和计算

1.1 与运算

import numpy as np

# numpy 中对 ndarray 数据类型的与运算,对矩阵中的每一个元素都执行两次判断
data = np.array([1,2,3,4,5])
result = (data == 1) & (data == 3)
print(result)
[False False False False False]

1.2 或运算

# 矩阵的或运算,对矩阵中的每一个元素都执行判断,只要满足其中一个条件就返回True
data = np.array([1,2,3,4,5])
result = (data == 1)| (data == 3)
print(result)
[ True False  True False False]

1.3 或运算作为矩阵索引赋值

'''
    可以将或运算判断后的结果作为索引重新为矩阵赋值,对应位置的判断结果为True,则赋值;否则保持原元素
'''
data = np.array([1,2,3,4,2])
index = (data == 1) | (data == 2)
data[index] = 10
print(data)
[10 10  3  4 10]

1.4 或运算为二维矩阵赋值

'''
    先判断所有行的第二列是否包含 2 ,返回 bool 数据类型的列表,然后将这个列表作为二维矩阵的行参数,为
    列表中所有为 True 的行的第三列赋值为 111
'''
matrix = np.array([[1,2,3],[1,4,2],[2,1,5]])
index = matrix[:,1] == 2
print(index)
matrix[index,2] = 111
print(matrix)
[ True False False]
[[  1   2 111]
 [  1   4   2]
 [  2   1   5]]

1.5 astype 改变元素数据类型

'''
    astype 可以对矩阵中的元素统一进行数据类型的转换
'''
data = np.array(['1','23','33'])
print (data.dtype)
data = data.astype(float)
print(data.dtype)
print(data)
<U2
float64
[ 1. 23. 33.]

1.6 极值函数

# 求最小值
data = np.array([1,23,44,-3])
print(data.min())
-3

1.7 axis 指定维度求和

'''
    axis 为 1 表示对每行求和,返回一个列表;axis 为 0 表示对每列求和,同样返回列表
'''
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix.sum(axis=1))
print(matrix.sum(axis=0))
[ 6 15 24]
[12 15 18]

1.8 符号计算

'''
    对于相同维度的矩阵,- 表示对应位置的元素相减
    矩阵直接 - 数字,表示对该矩阵的每一个元素都执行 - 数字的操作
    ** 表示乘方运算
    矩阵 < 数字 or 矩阵 > 数字 可以对矩阵中的每个元素进行判断,返回列表或者是二维列表
    矩阵 * 矩阵 表示对应位置的元素进行乘法计算
    矩阵a.dot(矩阵b) 表示 a 的每一行元素和 b 的每一列元素对应位置相乘求和然后放入新的结果矩阵中/可以使用 np.dot(a,b) 代替
    
'''
a = np.array([11,22,33,44])
b = np.arange(4)
c = a - b
print(c)
c = c -1
print(c)
print(b**2)
print(b)
print(b < 2)
a = np.array([[1,2],[3,4]])
b = np.array([[0,1],[1,2]])
print(a*b)
print(a.dot(b))
print(np.dot(a,b))
[11 21 31 41]
[10 20 30 40]
[0 1 4 9]
[0 1 2 3]
[ True  True False False]
[[0 2]
 [3 8]]
[[ 2  5]
 [ 4 11]]
[[ 2  5]
 [ 4 11]]

2. numpy 常用函数(重点)

2.1 reshape 函数快速重构二维矩阵

'''
    arange 函数快速生成一维的有序的矩阵,可通过 reshape 方法对一维矩阵快速变换为二维矩阵,第一个参数表示行
    第二个参数表示列,shape 属性可以查看当前二维矩阵的维度信息(行列信息),ndim 属性表示维度,二维矩阵就是2
    dtype 属性表示每个元素的数据类型,size 表示总共有多少个元素
    
'''
data = np.arange(15)
print(data)
data = data.reshape(3,5)
print(data)
# (3,5)
data.shape
# 2
data.ndim
# 'int32'
data.dtype.name
# dtype('int32')
data.dtype
data.size
[ 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]]





15

2.2 矩阵的初始化

# 根据元祖初始化所有元素都是 0. 的二维矩阵(默认是 float 类型)
data = np.zeros((2,3))
data
# 构造一个所有元素都是 1 的三维矩阵,并可以指定元素的类型为 int
# 第一个参数表示三维矩阵的元素个数,第二个参数表示二维矩阵的元素个数,第三个参数表示一维矩阵的元素个数
data = np.ones((2,3,4),dtype=np.int32)
data
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]]])

2.3 快速生成序列

# 从 10-40,每隔 5 取一个数生成序列,满足前包后不包
data = np.arange(10,40,5)
data
array([10, 15, 20, 25, 30, 35])

2.4 random 生成随机值

# 生成二维矩阵类型随机值的函数,需要一个元祖参数,第一个参数表示行,第二个参数表示列,随机值默认 [0.0,1.0)
'''
    array([[0.39091032, 0.94434359, 0.09856153],
       [0.32999742, 0.30763014, 0.23284478]])
'''
np.random.random((2,3))
help(np.random.random)
Help on built-in function random_sample:

random_sample(...) method of mtrand.RandomState instance
    random_sample(size=None)
    
    Return random floats in the half-open interval [0.0, 1.0).
    
    Results are from the "continuous uniform" distribution over the
    stated interval.  To sample :math:`Unif[a, b), b > a` multiply
    the output of `random_sample` by `(b-a)` and add `a`::
    
      (b - a) * random_sample() + a
    
    Parameters
    ----------
    size : int or tuple of ints, optional
        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
        ``m * n * k`` samples are drawn.  Default is None, in which case a
        single value is returned.
    
    Returns
    -------
    out : float or ndarray of floats
        Array of random floats of shape `size` (unless ``size=None``, in which
        case a single float is returned).
    
    Examples
    --------
    >>> np.random.random_sample()
    0.47108547995356098
    >>> type(np.random.random_sample())
    <type 'float'>
    >>> np.random.random_sample((5,))
    array([ 0.30220482,  0.86820401,  0.1654503 ,  0.11659149,  0.54323428])
    
    Three-by-two array of random numbers from [-5, 0):
    
    >>> 5 * np.random.random_sample((3, 2)) - 5
    array([[-3.99149989, -0.52338984],
           [-2.99091858, -0.79479508],
           [-1.23204345, -1.75224494]])

2.5 指定区间取平均间隔的值

# 从指定区间根据 区间长度/元素个数 得到的均值为取值间隔来取值, pi 表示 圆周率派
np.linspace(0,2*pi,5)
array([0.        , 1.57079633, 3.14159265, 4.71238898, 6.28318531])

2.6 求 e 的幂和数组所有元素的平方根

number = np.arange(5)
print(number)
print(np.exp(number))
print(np.sqrt(number))
[0 1 2 3 4]
[ 1.          2.71828183  7.3890561  20.08553692 54.59815003]
[0.         1.         1.41421356 1.73205081 2.        ]

2.7 矩阵变换

# 随机生成两行三列的 0-1 之间的随机数
nums = np.random.random((2,3))
print(nums)

# floor 函数用于向下取整
nums = np.floor(10*nums)
print(nums)

# ravel 函数用于 flatten the array
nums = np.ravel(nums)
print(nums)

# shape 函数用于重新变换矩阵结构,第一个参数都是行,第二个参数都是列
nums.shape = (3,2)
print(nums)

# 完成矩阵的行列转置(原矩阵每一行作为新矩阵的每一列)
print(nums.T)

# reshape 函数自动根据第一个行参数计算列参数,传入 -1 就可以自动计算
print(nums.reshape(6,-1))
print(nums)
[[0.34773665 0.22488604 0.95113666]
 [0.56553515 0.64450662 0.92665817]]
[[3. 2. 9.]
 [5. 6. 9.]]
[3. 2. 9. 5. 6. 9.]
[[3. 2.]
 [9. 5.]
 [6. 9.]]
[[3. 9. 6.]
 [2. 5. 9.]]
[[3.]
 [2.]
 [9.]
 [5.]
 [6.]
 [9.]]
[[3. 2.]
 [9. 5.]
 [6. 9.]]

2.8 矩阵之间的拼接

# hstack 函数用于矩阵之间根据每行来拼接,增加列(特征)
# vstack 函数用于矩阵之间根据每列来拼接,增加行
# 不管是横拼还是竖拼,注意拼接时的维度数量要一致
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print('---------------')
print(b)
print(np.hstack((a,b)))
print(np.vstack((a,b)))
[[6. 5.]
 [8. 6.]]
---------------
[[8. 2.]
 [9. 6.]]
[[6. 5. 8. 2.]
 [8. 6. 9. 6.]]
[[6. 5.]
 [8. 6.]
 [8. 2.]
 [9. 6.]]

2.9 矩阵切分

'''
    hsplit(矩阵,x):x 表示对矩阵的每行按列切割为几份;如果 x 为元祖,表示按照元祖指定的索引进行自定义切分
    vsplit(矩阵,y):y 表示对矩阵的每行按行切割为几份
'''
nums = np.floor(10*np.random.random((2,6)))
print(nums)
print(np.hsplit(nums,3))
nums = np.floor(10*np.random.random((2,6)))
print(nums)
print(np.hsplit(nums,(3,4)))
print('---------------')
nums = nums.T
print(nums)
print(np.vsplit(nums,2))
[[6. 4. 5. 7. 2. 2.]
 [8. 2. 8. 2. 1. 8.]]
[array([[6., 4.],
       [8., 2.]]), array([[5., 7.],
       [8., 2.]]), array([[2., 2.],
       [1., 8.]])]
[[5. 8. 5. 6. 3. 6.]
 [5. 2. 1. 0. 9. 1.]]
[array([[5., 8., 5.],
       [5., 2., 1.]]), array([[6.],
       [0.]]), array([[3., 6.],
       [9., 1.]])]
---------------
[[5. 5.]
 [8. 2.]
 [5. 1.]
 [6. 0.]
 [3. 9.]
 [6. 1.]]
[array([[5., 5.],
       [8., 2.],
       [5., 1.]]), array([[6., 0.],
       [3., 9.],
       [6., 1.]])]

猜你喜欢

转载自blog.csdn.net/cris_zz/article/details/84200216