python学习笔记(4)--numpy

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,)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值