您的位置:首页 > 编程语言 > Python开发

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
'''
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐