python数据运算 matrix、mean、average、max、min、ptp
2018-01-17 10:48
696 查看
import numpy as np ''' numpy模块中的矩阵对象为numpy.matrix,包括矩阵数据的处理,矩阵的计算, 以及基本的统计功能,转置,可逆性等等,包括对复数的处理,均在matrix对象中。 class numpy.matrix(data,dtype,copy):返回一个矩阵, 其中data为ndarray对象或者字符形式;dtype:为data的type;copy:为bool类型。 '''
#寻找最大值和最小值 h,l=np.loadtxt('data.csv', delimiter=',', usecols=(4,5), unpack=True) print ("np.max(h) =", np.max(h)) print ("np.min(h) =", np.min(h)) print ("np.max(l) =", np.max(l)) print ("np.min(l) =", np.min(l)) print ((np.max(h) + np.min(l)) /2) print ("Spread high price", np.ptp(h)) print ("Spread low price", np.ptp(l)) ''' np.ptp 沿轴的 (最大值 - 最小值) 范围 ,也就是做差。 ''' x = np.arange(4).reshape((2,2)) print(x) print( np.ptp(x, axis=0) ) print( np.ptp(x, axis=1) )
# 创建二维矩阵 x = np.matrix([[1,2,3], [4,5,6]]) # 设置权重 w1 = [0.3, 0.7] # 纵向计算加权平均 print( 'np.average(x, axis=0, weights=w1)', np.average(x, axis=0, weights=w1) ) x2 = np.matrix([[ 3.1, 4.1, 5.1]]) w2 = [0.3, 0.3, 0.4] # 横向计算加权平均 print( 'np.average(x2, axis=1, weights=w2)', np.average(x2, axis=1, weights=w2) ) print( 'np.average(x2, axis=1, weights=w2, returned=True)' , np.average(x2, axis=1, weights=w2, returned=True) ) ###利用NumPy进行历史股价分析 #读入文件 # delimiter 分割字符 c,v=np.loadtxt('data.csv', delimiter=',', usecols=(6,7), unpack=True) print('c ',c) # 数据索引 第 6 列 从0 开始计算索引 print('v ',v) # 数据索引 第 7 列 从0 开始计算索引 #计算成交量加权平均价格 vwap = np.average(c, weights=v) print (("VWAP =", vwap)) #算术平均值函数 总和除以总个数 print ("mean =", np.mean(c)) #时间加权 平均价格 t = np.arange(len(c)) # 等差数列 print ("t =", t) print ("twap =", np.average(c, weights=t)) ''' python平均值和加权平均值 import numpy as np a=(70,80,60) np.mean(a) # 算数平均值 = (70 + 80 + 60)/3 = 70 70.0 np.average(a,weights=[3,3,4]) #加权平均值 = 70*3 + 80*3 + 60*4 = 69.0 -> 内积 ''' ''' data.csv AAPL,28-01-2017, ,344.17,344.4,333.53,336.1,21144800 AAPL,31-01-2017, ,335.8,340.04,334.3,339.32,13473000 AAPL,01-02-2017, ,341.3,345.65,340.98,345.03,15236800 AAPL,02-02-2017, ,344.45,345.25,343.55,344.32,9242600 AAPL,03-02-2017, ,343.8,344.24,338.55,343.44,14064100 AAPL,04-02-2017, ,343.61,346.7,343.51,346.5,11494200 AAPL,07-02-2017, ,347.89,353.25,347.64,351.88,17322100 AAPL,08-02-2017, ,353.68,355.52,352.15,355.2,13608500 AAPL,09-02-2017, ,355.19,359,354.87,358.16,17240800 AAPL,10-02-2017, ,357.39,360,348,354.54,33162400 AAPL,11-02-2017, ,354.75,357.8,353.54,356.85,13127500 AAPL,14-02-2017, ,356.79,359.48,356.71,359.18,11086200 AAPL,15-02-2017, ,359.19,359.97,357.55,359.9,10149000 AAPL,16-02-2017, ,360.8,364.9,360.5,363.13,17184100 AAPL,17-02-2017, ,357.1,360.27,356.52,358.3,18949000 AAPL,18-02-2017, ,358.21,359.5,349.52,350.56,29144500 AAPL,22-02-2017, ,342.05,345.4,337.72,338.61,31162200 AAPL,23-02-2017, ,338.77,344.64,338.61,342.62,23994700 AAPL,24-02-2017, ,344.02,345.15,338.37,342.88,17853500 AAPL,25-02-2017, ,345.29,348.43,344.8,348.16,13572000 AAPL,28-02-2017, ,351.21,355.05,351.12,353.21,14395400 AAPL,01-03-2017, ,355.47,355.72,347.68,349.31,16290300 AAPL,02-03-2017, ,349.96,354.35,348.4,352.12,21521000 AAPL,03-03-2017, ,357.2,359.79,355.92,359.56,17885200 AAPL,04-03-2017, ,360.07,360.29,357.75,360,16188000 AAPL,07-03-2017, ,361.11,361.67,351.31,355.36,19504300 AAPL,08-03-2017, ,354.91,357.4,352.25,355.76,12718000 AAPL,09-03-2017, ,354.69,354.76,350.6,352.47,16192700 AAPL,10-03-2017, ,349.69,349.77,344.9,346.67,18138800 AAPL,11-03-2017, ,345.4,352.32,345,351.99,16824200 '''
相关文章推荐
- C# 中奇妙的函数–五个序列聚合运算(Sum, Average, Min, Max,Aggregate)
- C#中五个序列聚合运算(Sum, Average, Min, Max,Aggregate)
- C# 中奇妙的函数–6. 五个序列聚合运算(Sum, Average, Min, Max,Aggregate)
- C#奇妙的函数(1)-五个序列聚合运算(Sum, Average, Min, Max,Aggregate)
- python中矩阵(matrix或array)运算比for循环速度更快
- Python学习笔记-数据类型,运算,变量
- Python-数据运算
- Python 核心编程笔记_Chapter_5_Note_1 数据类型及相关运算
- Integer.MAX_VALUE和Integer.MIN_VALUE的运算及原理
- LINQ 标准的查询操作符 合计操作符 Count()、Sum()、Min()、Max()、Average()和Aggregate()
- RX操作符之算术和聚合操作(averageInteger、min、max、count、sum、contact、reduce、collect)
- python/pandas数据挖掘(十四)-groupby,聚合,分组级运算
- 组函数(avg(),sum(),max(),min(),count())、多行函数,分组数据(group by,求各部门的平均工资),分组过滤(having和where),sql优化 .
- LINQ 标准的查询操作符 合计操作符 Count()、Sum()、Min()、Max()、Average()和Aggregate()
- python函数max与min的高阶使用,list强转的本质,字典列表比值办法max的隐藏功能
- 位运算实现 max,min
- Sklearn数据预处理:scale, StandardScaler, MinMaxScaler, Normalizer
- [导入]Linq学习笔记(1.8)——Count、Sum、Min、Max、Average
- python四个带 key 参数的函数(max、min、map、filter)
- python之数据运算