Numpy
- 一、ndarray
- 二、基本操作
- 三、生成数组
- 1、生成0和1的数组
- 2、从现有数组生成
- 3、生成固定范围的数组
- 4、生成随机数组
- 5、 数组的索引、切片、形状修改、类型修改、去重
- 四、运算
- 逻辑运算
- 运算符
- 通用判断函数
- 三元运算符
- 统计运算
- 数组间运算
- 数组与数的运算
- 数组与数组的运算
- 矩阵运算
- 五、合并与分割
- 1、合并
- 2、分割
numpy 使用ndarray对象处理多维数组,该对象是一个快速而灵活的大数据容器
一、ndarray
相同类型的“items”的集合
import numpy as np
score=np.array([1,2,3],dtype='float') # 转换为array格式,创建数组时指定类型
print("数据维度的元组",score.shape)
print('数据维数',score.ndim)
print('数据中元素个数',score.size)
print('一个数据元素的长度',score.itemsize)
print('数据元素类型',score.dtype)
---输出---
数据维度的元组 (3,)
数据维数 1
数据中元素个数 3
一个数据元素的长度 4
数据元素类型 int32
属性名字 | 属性解释 |
---|---|
ndarray.shape | 数组维度的元组 |
ndarray.ndim | 数组维数 |
ndarray.size | 数组中的元素数量 |
ndarray.itemsize | 一个数组元素的长度(字节) |
ndarray.dtype | 数组元素的类型 |
ndarray运行时间比python list 快很多
ndarray比python list 强的优势:
- 存储风格
- ndarray - 相同类型 - 通用性不强
- list - 不同类型 - 通用性很强
- 并行化运算
- ndarray支持向量化运算
- 底层语言
- Numpy底层使用C语言编写,内部解除了GIL(全局解释器锁),其对数组的操作速度不受Python解释器的限制,效率远高于纯Python代码。
import random
import time
import numpy as np
a=[]
for i in range(10000000):
a.append(random.random())
t1=time.time()
sum1=sum(a)
t2=time.time()
print('list运行时间',t2-t1)
b=np.array(a)
t3=time.time()
sum2=np.sum(b)
t4=time.time()
print('ndarray运行时间',t4-t3)
---结果--
list运行时间 0.05385541915893555
ndarray运行时间 0.019013643264770508
二、基本操作
ndarray.方法()
np.函数名()
三、生成数组
1、生成0和1的数组
l=np.zeros(shape=(3,4),dtype="int32")
m=np.ones(shape=(4,5),dtype='float64')
2、从现有数组生成
# 深拷贝
data1=np.array(score)
data2=np.copy(score)
# 浅拷贝
data3=np.asarray(score)
# 区别 浅拷贝随着score的改变而改变,深拷贝则一直是原始score
3、生成固定范围的数组
# 生成[0,10]之间等距离的100个数
np.linspace(0,10,100)
# 生成[0,11)之间,5为步长的数组
np.arange(0,11,5)
4、生成随机数组
均匀分布:生成每一组的概率相等
# 生成均匀分布的一组数[low,high)
data1=np.random.uniform(low=-1,high=1,size=1000000)
import matplotlib.pyplot as plt
plt.figure(dpi=80)
plt.hist(data1,1000)
plt.show()
正态分布:
标准差越大,数据越扁(分散),均值代表中心x轴(对称轴)
# 生成正态分布的一组数,loc:均值;scale:标准差
data2=np.random.normal(loc=0,scale=0.1,size=10000000)
import matplotlib.pyplot as plt
plt.figure(dpi=80)
plt.hist(data2,1000)
plt.show()
5、 数组的索引、切片、形状修改、类型修改、去重
数组索引、切片
# ———案例随机生成8只股票2周的交易日涨幅数据———
# 数据生成
stock_change=np.random.normal(loc=0,scale=1,size=(8,10))
# 获取第一个股票前三个交易日的涨跌幅数据
print(stock_change[0,0:3])
# 切片
stock_change[0,3]=10
形状修改
# 行列反转--转置
stock_change.reshape((10,8)) # 返回新的ndarray,原始数据没有改变,只改变形状,不会行列互换
stock_change.resize((10,8)) # 无返回值,对原始的ndarray进行了修改
stock_change.T # 转置
类型修改
stock_change.astype('int32')
stock_change.tostring() # ndarray序列化到本地
数组去重
temp=np.array([[1,2,3,4],[3,4,5,6]])
np.unique(temp)
# ————结果————
# [1 2 3 4 5 6]
# 使用set,set的使用条件是一维数组,故需要展平
a=temp.flatten()
set(a) # 类型为set
# ————结果————
# {1, 2, 3, 4, 5, 6}
四、运算
逻辑运算
-
< , >, >=,stock_change>0.5
-
np.all(布尔值) np.any(布尔值)
-
np.where(布尔值,True的位置的值,False的位置的值)
-
np.logical_and(temp>0.5,temp<1) np.logical_or(temp>0.5,temp<-0.5)
运算符
# 逻辑判断,若涨跌幅大于0.5就标记为True,否则为False
stock_change>0.5
stock_change[stock_change>0.5]=1.1
通用判断函数
- np.all(布尔值)
只要一个是false,就是false
- np.any(布尔值)
只要一个是true,就是true
# 判断stock_change[0:2,0:5]是否全是上涨
np.all(stock_change[0:2,0:5]>0)
# 判断前5只股票这段期间是否有上涨的
np.any(stock_change[0:5,:]>0)
三元运算符
# np.where(布尔值,True的位置的值,false的位置的值)
np.where(temp>0,1,0)
# 大于0.5,且小于1
np.where(np.logical_and(temp>0.5,temp<1),1,0)
# 大于0.5,或小于-0.5
np.where(np.logiacl_or(temp>0.5,temp<-0.5),1,0)
统计运算
-
统计指标函数
-
min,max,mean,median,var,std,argmin,argmax
-
-
使用方法
-
temp.max(axis=0) # 按列求最大值 np.max(temp,axis=1) temp.max() # 整个数组的最大值 # 求最大值所在位置 temp.argmax(axis=0) # 按列求最大值位置 temp.argmin(axis=1)
-
数组间运算
- 数组与数的运算
- 数组与数组的运算
数组与数的运算
arr=np.array([[1,2,3,2,1,4],[5,6,1,2,3,1]])
# 数组整体加1
arr1=arr+1
# 数组整体除10
arr2=arr/10
# 数组整体乘5
arr3=arr*5
数组与数组的运算
-
两个数组能够进行数组与数组的运算需要满足
维度相等(对应维度上的数)或者shape(其中相对应的一个地方为1)
例如(正例)
A (3d) 15x3x5
B (2d) 1x5
result(3d) 15x3x5
A=np.array([[1,2,3,4,5],[1,23,5,6,7],[5,4,3,2,1]])
B=np.array([1,2,3,4,5])
C=A*B
print(C)
————结果————
[[ 1 4 9 16 25]
[ 1 46 15 24 35]
[ 5 8 9 8 5]]
矩阵运算
-
矩阵(matrix)必须是二维的,array(数组)可以是多维的
-
两种方法存储矩阵
-
ndarray,二维数组
# ndarray存储矩阵 a=np.array([[1,2],[3,4]]) print(a) ——结果—— array([[1, 2], [3, 4]])
-
matrix, 矩阵
# matrix存储矩阵 b=np.mat([[1,2],[3,4]]) print(b) ——结果—— matrix([[1, 2], [3, 4]])
-
-
矩阵乘法
形状:(m,n)*(n,l)=(m,l)
如果是ndarray
-
np.dot(data,data1) np.matmul(data,data1) data=np.array([[1,2,3],[2,4,6]]) data1=np.array([[1,2],[3,4],[5,6]]) a=np.dot(data,data1) print(a) --结果-- array([[22, 28], [44, 56]])
如果是martix
-
data*data1 data@data1 data=np.mat([[1,2,3],[2,4,6]]) data1=np.mat([[1,2],[3,4],[5,6]]) b=data@data1 print(b) --结果-- matrix([[22, 28], [44, 56]])
-
五、合并与分割
1、合并
np.hstack((a1,a2)) 水平拼接
np.vstack((a1,a2)) 竖直拼接
np.concatenate((a1,a2),axis=0) 水平|竖直拼接
# 合并
import numpy as np
a1=np.array([[1,2,3],[12,3,5],[1,5,7]]) #(3,3)
a2=np.array([[1],[2],[3]]) # (3,1)
a=np.hstack((a1,a2)) # (3,4)
print(a)
---结果---
array([[ 1, 2, 3, 1],
[12, 3, 5, 2],
[ 1, 5, 7, 3]])
import numpy as np
a1=np.array([[1,2,3],[12,3,5],[1,5,7]]) #(3,3)
a2=np.array([[1],[2],[3]]) # (3,1)
b=np.concatenate((a1,a2),axis=1)
print(b)
--结果--
array([[ 1, 2, 3, 1],
[12, 3, 5, 2],
[ 1, 5, 7, 3]])
2、分割
np.split(x,3) # 分成3份
np.split(x,[1,4,5,6]) # 按索引分割,(1,2,3)都是索引
# 一般处理一维数组,处理多维数组只能分割第一维
b=np.array([[ 1, 2, 3, 1], #(3,4)
[12, 3, 5, 2],
[ 1, 5, 7, 3]])
c1,c2,c3=np.split(b,3)
print(c1) #(1,4)
print(c2)
print(c3)
----result---
[[1 2 3 1]]
[[12 3 5 2]]
[[1 5 7 3]]
a=np.array([[[1],[2],[4]],[[2],[4],[6]]]) #(2,3,1)
print(a.shape)
a1,a2=np.split(a,2)
print('---------')
print(a1.shape) #(1,3,1)
-- result --
[[[1]
[2]
[4]]
[[2]
[4]
[6]]]
---------
[[[1]
[2]
[4]]]
c=np.array([1,3,4,6,8,0,2,4,6])
c1,c2,c3,c4=np.split(c,[2,4,6])
print(c1) # (1,3)
print(c2) # (4,6)
print(c3) # (8,0)
print(c4) # (2,4,6)
——————
[1 3]
[4 6]
[8 0]
[2 4 6]