利用Python进行数据分析(二):numpy
1、基本数组统计方法
import numpy as np
arr = np.random.randn(5,4)
arr
Out[3]:
array([[ 0.42741289, -0.14289007, 0.02772825, -2.06617452],
[-1.09157199, 0.51249564, 0.20785299, -0.41684576],
[ 1.74779292, -1.49081423, -0.16935059, 1.53038018],
[-0.60548802, -0.33727061, 0.52543707, -0.21070918],
[ 1.77986103, -0.5814942 , 1.3287214 , -0.33070122]])
arr.mean()
Out[4]: 0.03221859825961611
np.mean(arr)
Out[5]: 0.03221859825961611
arr.sum()
Out[6]: 0.6443719651923222
arr.mean(axis=1)
Out[7]: array([-0.43848086, -0.19701728, 0.40450207, -0.15700768, 0.54909675])
arr.mean(axis=0)
Out[8]: array([ 0.45160137, -0.4079947 , 0.38407782, -0.2988101 ])
mean和sum这类的函数可以接受一个axis选项参数,axis = 1对行进行计算,axis = 0对列进行计算。
arr2
Out[13]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
arr2.cumsum(axis=1)
Out[14]:
array([[ 1, 3, 6],
[ 4, 9, 15],
[ 7, 15, 24]], dtype=int32)
arr2.cumsum(axis=0)
Out[15]:
array([[ 1, 2, 3],
[ 5, 7, 9],
[12, 15, 18]], dtype=int32)
在多维数组中,累加函数(如cumsum)返回的是同样大小的数组,但是会根据每个低维的切片沿着标记轴计算部分聚类。
NumPy数组也可以通过sort方法就地排序
arry.sort()
多维数组可以在任何一个轴向上进行排序,只需将轴编号传给sort
arry
Out[26]:
array([[ 1, 3, 2],
[ 5, 3, 7],
[10, 5, 18]])
arry.sort(axis=0)
arry
Out[28]:
array([[ 1, 3, 2],
[ 5, 3, 7],
[10, 5, 18]])
2、唯一化以及其它的集合逻辑
unique函数:np.unique了,它用于找出数组中的唯一值并返回已排序的结果
arry
Out[36]: array([ 1, 5, 3, 2, 1, 5, 32, 2])
np.unique(arry)
Out[38]: array([ 1, 2, 3, 5, 32])
另一个函数np.in1d用于测试一个数组中的值在另一个数组中的成员资格,返回一个布尔型数组。
values = np.array([0,1,2,3,4,5,6])
np.in1d(values,[2,4,6])
Out[41]: array([False, False, True, False, True, False, True])
数组的集合运算
; 3、用于数组的文件输入输出
np.save和np.load是读写磁盘数组数据的两个主要函数。默认情况下,数组是以未压缩的原始二进制格式保存在扩展名为.npy的文件中的。
arr = np.arange(10)
np.save("same_arr",arr)
np.load("same_arr.npy")
Out[44]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
如果文件路径末尾没有扩展名.npy,则该扩展名会被自动加上。
通过np.savez可以将多个数组保存到一个未压缩文件中,将数组以关键字参数的形式传入即可。
np.savez("sz.npz",a=arr,b=arr)
np.load("sz.npz")
Out[48]: <numpy.lib.npyio.NpzFile at 0x23b4e05f160>
加载.npz文件时,你会得到一个类似字典的对象,该对象会对各个数组进行延迟加载。
如果要将数据压缩,可以使用numpy.savez_compressed
4、线性代数
NumPy提供了一个用于矩阵乘法的dot函数(既是一个数组方法也是numpy命名空间中的一个函数)。
x=np.array([[1,2,3],[4,5,6]])
y=np.array([[1,2],[3,4],[5,6]])
x.dot(y)
Out[53]:
array([[22, 28],
[49, 64]])
x.dot(y)等价于np.dot(x, y)
一个二维数组跟一个大小合适的一维数组的矩阵点积运算之后将会得到一个一维数组。
@符(类似Python 3.5)也可以用作中缀运算符,进行矩阵乘法。
x@y
Out[59]:
array([[22, 28],
[49, 64]])
numpy.linalg中有一组标准的矩阵分解运算以及诸如求逆和行列式之类的东西。
from numpy.linalg import inv,qr
x=np.random.randn(5,5)
mat=x.T.dot(x)
mat
Out[64]:
array([[10.37712152, -5.10419681, -0.80498547, 0.04520722, -0.84198893],
[-5.10419681, 5.94101822, 1.56400295, -1.34399934, 1.84710689],
[-0.80498547, 1.56400295, 3.35766495, 0.33595866, 0.03256461],
[ 0.04520722, -1.34399934, 0.33595866, 4.08700355, 0.293452 ],
[-0.84198893, 1.84710689, 0.03256461, 0.293452 , 2.05864858]])
inv(mat)
Out[65]:
array([[ 0.22342933, 0.30839169, -0.1003022 , 0.1216244 , -0.20106978],
[ 0.30839169, 0.78138849, -0.31645865, 0.32379726, -0.61611257],
[-0.1003022 , -0.31645865, 0.43441041, -0.15722382, 0.25845637],
[ 0.1216244 , 0.32379726, -0.15722382, 0.38377425, -0.2929986 ],
[-0.20106978, -0.61611257, 0.25845637, -0.2929986 , 0.9939976 ]])
mat.dot(inv(mat))
Out[66]:
array([[ 1.00000000e+00, -2.07992207e-17, 5.43011873e-17,
5.95568423e-17, 2.97700294e-17],
[ 1.13837630e-16, 1.00000000e+00, -1.02394604e-16,
1.48722899e-16, 4.70987699e-16],
[ 4.16393039e-17, 1.95644710e-16, 1.00000000e+00,
2.99156534e-17, 1.03333728e-17],
[-3.86509832e-17, -2.37798205e-16, 1.67187680e-16,
1.00000000e+00, -1.55389362e-16],
[ 4.58549145e-17, 1.10664271e-16, -4.56052878e-17,
6.59657520e-17, 1.00000000e+00]])
q,r=qr(mat)
r
Out[69]:
array([[-11.62310057, 7.41334018, 1.6391077 , -0.60193877,
1.71311653],
[ 0. , -3.7490203 , -1.62958074, 2.18151574,
-1.6085624 ],
[ 0. , 0. , -3.02320061, -1.62537854,
0.5251479 ],
[ 0. , 0. , 0. , -3.30874019,
-1.41675657],
[ 0. , 0. , 0. , 0. ,
0.80046046]])
表达式X.T.dot(X)计算X和它的转置X.T的点积。
常用的numpy.linalg函数
5、伪随机数生成
numpy.random模块
用normal来得到一个标准正态分布的4×4样本数组
a = np.random.normal(size=(4,4))
a
Out[71]:
array([[ 0.568, -0.683, 0.279, -1.866],
[-0.596, 0.387, -0.007, -0.379],
[-0.252, 0.679, 1.717, 0.288],
[-0.124, 0.299, 0.896, 2.186]])
这些都是伪随机数,是因为它们都是通过算法基于随机数生成器种子,在确定性的条件下生成的。可以用NumPy的np.random.seed更改随机数生成种子。
部分np.random函数
6、示例:随机漫步
通过模拟随机漫步来说明如何运用数组运算。先来看一个简单的随机漫步的例子:从0开始,步长1和-1出现的概率相等。
import random
position=0
walk=[position]
steps=1000
for i in range(steps):
step = 1 if random.randint(0,1) else -1
position += step
walk.append(position)
import matplotlib.pyplot as plt
plt.plot(walk[:100])
Out[88]: [<matplotlib.lines.Line2D at 0x23b4ec2a2e0>]
一次模拟多个随机漫步
walks=np.random.randint(0,2,size=(5000,10000))
steps=np.where(walks>0,1,-1)
s=steps.cumsum(1)
s
Out[100]:
array([[ 1, 0, 1, ..., -26, -27, -28],
[ -1, -2, -1, ..., -94, -93, -94],
[ 1, 0, 1, ..., -44, -43, -42],
...,
[ -1, -2, -3, ..., -182, -183, -184],
[ -1, -2, -3, ..., -72, -71, -70],
[ 1, 2, 3, ..., 32, 31, 32]], dtype=int32)
s.max()
Out[102]: 410
s.min()
Out[103]: -437
Original: https://blog.csdn.net/catchmeifyoucOol/article/details/121377912
Author: catchcatpath
Title: 利用Python进行数据分析(二):numpy
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/780943/
转载文章受原作者版权保护。转载请注明原作者出处!