Numpy--------回顾与重点复习总结
2019-07-04 19:29
811 查看
import numpy as np score = np.array([[90,89,65,78,88],[86,85,83,78,90],[88,65,68,78,95]]) score type(score)
array([[90, 89, 65, 78, 88],
[86, 85, 83, 78, 90],
[88, 65, 68, 78, 95]])
numpy.ndarray
ndarray与python列表效率对比
Python_list = [] for i in range(100000000): Python_list.append(i) Python_list
import time t1 = time.time() sum1 = sum(Python_list) t2 = time.time() d1 = t2-t1 ndarray_list = np.array(Python_list) t3 = time.time() sum2 = np.sum(ndarray_list) t4 = time.time() d2 = t4-t3
ndarray属性
score
array([[90, 89, 65, 78, 88],
[86, 85, 83, 78, 90],
[88, 65, 68, 78, 95]])
#数组维度的元组 score.shape #数组中元素数量 score.size #一个数组元素的长度 score.itemsize #数组元素类型 score.dtype #生成固定范围的数组,等距(左闭右闭) np.linspace(0,100,10) #左闭右开,步长 np.arange(1,10,2) #生成随机数,均匀分布与正态分布 np.random.rand(0,1,10) np.random.uniform(-1,1,10) np.random.uniform(-1,1,10) np.random.randint(1,8,100) data1 = np.random.uniform(low=-1,high=1,size=10000) #查看均匀分布状况 import matplotlib.pyplot as plt plt.figure(figsize=(18,10),dpi=90) plt.hist(data1,100) plt.show()
正态分布
#loc为均值,scale为标准差,size为个数 data2 = np.random.normal(loc=1.75,scale=0.2,size=10000) plt.figure(figsize=(18,10),dpi=90) plt.hist(data2,1000) plt.show()
array([ 1.58218279, 1.64174763, 1.83364957, …, 1.91884743,
1.919905 , 1.63108949])
案例:随机生成8只股2周交易日的涨幅数据
stock_change = np.random.normal(0,1,(8,10)) #获取第一个股票的前3个交易日的涨跌数据 stock_change[0,0:3] #三维数组操作 a1 = np.array([[[1,2,3],[4,5,6]],[[12,3,34],[5,6,7]]]) #切片赋值 a1[1,0,2]=10000
形状的修改
stock_change.reshape((10,8)) #返回新的ndarray,原始数据没有改变 stock_change.resize((10,8)) #没有返回值,对原始的ndarray进行了修改 stock_change.T #转置,行变列 列变行
类型修改
stock_change.astype('int32') stock_change.tostring() #序列化到本地
数组的去重
temp = np.array([[1,2,3,4],[3,4,5,6]]) np.unique(temp) #或者 a1 = temp.flatten() #把ndarraya拍平 set(a1) #集合去重法
ndarray运算
逻辑运算 统计运算 数组间运算
#逻辑运算 stock_change = np.random.normal(loc=0,scale=1,size=(8,10)) #逻辑判断 如果涨跌幅大于0.5就标记为True 否则为False stock_change>0.5 #布尔索引 stock_change[stock_change>0.5] #赋值 stock_change[stock_change>0.5] = 1.111
通用判断函数
np.all(布尔值) np.any()
#判断stock_change[0:2,0:5]是否全是上涨 stock_change[0:2,0:5]>0 np.all(stock_change[0:2,0:5]>0) #判断前5只股票是否有上涨的 stock_change[:5]>0 np.any(stock_change[:5]>0)
np.where(三元运算符)
# np.where(布尔值,True的位置的值,False的位置的值)---------------相当于excel里面的if判断 print(stock_change) temp = stock_change[:4,:4] print(temp) a1 = np.where(temp>0,1,0) a1
复合逻辑函数 :
np.logical_and()¶
np.logical_or()
#判断前4个股票前4天的涨跌幅 大于0.5 并且 小于1的,换为1,否则为0 #判断前4个股票前4天的涨跌幅 大于0.5 或者 小于-0.5的,换为1,否则为0 np.logical_and(temp>0.5,temp<1) np.where(np.logical_and(temp>0.5,temp<1),1,0) np.logical_or(temp>0.5,temp<-0.5) np.where(np.logical_or(temp>0.5,temp<-0.5),1,99)
统计运算
min,max,median,mean,std,var 有两种调用方法如下
#前4只股票前4天的最大涨幅 temp temp.max() np.max(temp) temp.max(axis=0) #按列计算 np.max(temp,axis=1) #按行计算 #波动幅度 np.std(temp,axis=0) #计算方差 标准差的验证 即std是方差的开平方 a1 = np.array([[1,3,5,9],[2,4,6,18]]) print(a1) print(np.std(a1,axis=0)) print(np.std(a1,axis=1)) #最大值与最小值所在位置 np.argmax(temp,axis=1) np.argmin(temp,axis=1)
数组间的运算
#数组与数的运算 arr = np.array([[1,2,4,5,7,2],[5,6,1,3,2,1]]) arr+1 arr*10 #python原生的就不可以 a = [1,2,4,6,5] a*3
广播机制 :1,维度相同,2,其中对应的一个地方为1
arr1 = np.array([[1,2,3,2,1,4],[5,6,1,2,3,1]]) arr2 = np.array([[1],[3]]) arr1.shape # (2,6) arr2 .shape# (2,1) arr1+arr2 arr1*arr2
什么是矩阵
矩阵,matrix,和array的区别矩阵必须是2维的,但是array可以是多维的
np,mat() 将数组转化为矩阵类型
矩阵乘法的两个关键:1,形状改变.2,运算规则. (m行,n列)*(n行,l列) = (m行,l列),即对应行乘以对应列的和
data = np.array([[80,86],[82,80],[85,78],[90,90],[86,82],[82,90],[78,80],[92,94]]) #平时成绩和期末考试成绩 weights = np.array([[0.3],[0.7]]) weights.shape
data * weights #不满足广播机制,不能进行运算,所以会报错
#但是如果把他们中一个转化为矩阵就可以相乘了 data*np.mat(weights)
#矩阵运算 乘法 np.matmul(data,weights) np.dot(data,weights)
合并与分割
#np.hstack a = np.array([1,2,3]) b = np.array([2,3,4]) np.hstack((a,b)) a = np.array([[1],[2],[3]]) b = np.array([[2],[3],[4]]) np.hstack((a,b)) #np.vstack a = np.array([1,2,3]) b = np.array([2,3,4]) np.vstack((a,b)) a = np.array([[1],[2],[3]]) b = np.array([[2],[3],[4]]) np.vstack((a,b)) #np.concatenate() a = np.array([[1,2],[3,4]]) b = np.array([[5,6]]) np.concatenate((a,b)) #相当于np.vstack np.concatenate((a,b.T),axis=1) #相当于水平拼接 hstack #分割 了解就行 ##np.split() x = np.arange(0,9) x np.split(x,3) np.split(x,[3,5,7]) #按索引进行分割
numpy读取文件 与缺失值插补
data = np.genfromtxt('test_numpy.csv',delimiter=',')
缺失值处理
1,直接删除
2,插补或者替换
用numpy处理很麻烦,pandas一行搞定
def fill_nan_by_column_mean(t): #遍历所有列 for i in range(t.shape[1]): #计算nan的个数 nan_num = np.count_nonzero(t[:,i][t[:,i]!=t[:,i]]) if nan_num>0: now_col = t[:,i] #求和 now_col_not_nan = now_col[np.isnan(now_col)==False].sum() # 和/个数 now_col_mean = now_col_not_nan/(t.shape[0]-nan_num) #赋值给now_col now_col[np.isnan(now_col)] = now_col_mean # 赋值给t t[:,i] = now_col return t fill_nan_by_column_mean(data)
相关文章推荐
- javase复习整理(一):基础要点、重点、易错点、多线程梳理总结
- 数据结构复习之查找算法的总结回顾
- DB2 sql 重点难点复习总结
- 【人工智能】AI与深度学习重点回顾,从研究到应用,这是一份2017年AI领域的最全面总结
- 总结前十一课中老师重点回顾的问题
- STL之vector,list复习总结回顾
- C语言理论复习,重点回顾
- 计算机组成原理期末复习重点总结(2011—2012学年)
- 软件质量保证划重点&期末复习总结
- JavaScript核心重点回顾复习
- 数据结构复习之排序算法的总结回顾
- 【Accelerated C++】重点回顾(续)
- 回顾大学--大一JAVA实验--总结
- 01--2017年同济大学公共课复习总结
- C++ 知识回顾总结 -- 指针
- Masonry 使用复习总结
- 大学毕业4年-回顾和总结(1)-钱,金钱观
- 国外艺术妹纸自学编程的第一年总结回顾
- 【C语言复习(二十四)】C语言程序内存结构总结
- SQl Server 数据库总结复习