1.计算均值
dataArr = [[1, 2, 3], [4, 5, 6]]
dataMat = np.mat(dataArr)
print("dataMat=\n", dataMat)
# 计算均值
meanVal = np.mean(dataMat, axis = 0)
print("meanVal=\n", meanVal)
#矩阵减去向量
print("dataMat - meanVal=\n", dataMat - meanVal)
2.提取矩阵的某一行或某一列赋给另一个矩阵的某一行或某一列
import numpy as np
a = np.arange(9).reshape(3, 3)
b = np.eye(3, 3)
print(a)
print(b)
b[:, 0] = a[:, 1]
print(b)
for i in range(3):
b[:, i] = a[:, i]
print(b)
3.计算协方差矩阵 numpy.cov
numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None)
Paramets:
m : array_like
A 1-D or 2-D array containing multiple variables and observations. Each row of m represents a variable, and each column a single observation of all those variables. Also see rowvar below.
y : array_like, optional
An additional set of variables and observations. y has the same form as that of m.
rowvar : bool, optional
If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.
Examples:
Consider two variables, x_0 and x_1, which correlate perfectly, but in opposite directions,
Note how x_0 increases while x_1 decreases. The covariance matrix shows this clearly:
x = np.array([[0, 2], [1, 1], [2, 0]])
print("x =\n", x)
print("np.cov(x1) =\n", np.cov(x, rowvar = 0)) # 2 features
# three variables, x_0, x_1 and x_2:
print("np.cov(x2) =\n", np.cov(x, rowvar = 1)) # 3 features
reference:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html
4.array数组(n,1)与(n,)的转换
使用reshape:
pre_new1 = pre_new.reshape(-1,1) #-1默认是任何数量的数集,转化为(n,1)
pre_new1 = pre_new.reshape(-1,) #转换为(n,)