python进阶—numpy案例(⼀)⾸先总结⼀下numpy如何读取txt,excel,csv等格式的⽂档。
import numpy as np
arr2 = np.arange(12).reshape(3,4)
# 保存数据到txt⽂档(fmt:数据格式,delimiter:分隔符)
np.savetxt(r"C:\Urs\67505\", arr2, fmt="%d", delimiter=" ", newline="\r\n")
# 从txt⽂档中读取数据(dtype:数据格式)
values1 = np.loadtxt(r"C:\Urs\67505\", delimiter=" ",
unpack=Fal,dtype=int) # unpack=True表⽰分别存储不同元素
# 从csv⽂档中读取数据(ucols表⽰我们需要获取的列)
values2 = np.loadtxt(r"C:\Urs\67505\Desktop\numpyData.csv",delimiter=",",ucols=(6,7),unpack=True)
案例:股票价格
股票价格数据:股票代码、时间、开盘价、最⾼价、最低价、收盘价、交易数
# 股票价格
纸铅笔
c, v = np.loadtxt(r"C:\Urs\67505\Desktop\表1.csv", delimiter=",", ucols=(6, 7), unpack=True)
# 计算成交量加权平均价格
vwap = np.average(c, weights=v)
# 时间加权平均价格
t = np.arange(len(c))
twap = np.average(c, weights=t)
# 计算平均价格
mean = np.mean(c)
五年级的作文# 计算收盘价的最⼤值和最⼩值
c_max = np.max(c)
c_min = np.min(c)
# 计算收盘价的极差
c_ptp = np.ptp(c)
# 计算收盘价的中位数
c_median = np.median(c)
# 计算⽅差
c_var = np.var(c)
# 计算股票收益率
returns = np.diff(c) / c[:-1]
# 计算对数收益率
logReturn = np.diff(np.log(c))
# 计算哪些交易⽇收益率为正数
posretindices = np.where(returns > 0)
# ⽇期转换
def date_str_to_week(s):
import datetime
如何美白全身
return datetime.datetime.strptime(s.decode("utf-8"), "%d-%m-%Y").date().weekday()
# ⽇期分析(converters:数据列和转换函数之间进⾏映射的字典)
dates, clo = np.loadtxt(r"C:\Urs\67505\Desktop\表1.csv", delimiter=",",
converters={1: date_str_to_week},ucols=(1, 6), unpack=True)
# 创建数组保存⼯作⽇的平均收盘价
weekday_average = np.zeros(5)
for i in range(5):
weekday_index = np.where(dates == i) # 获取每个指定⼯作⽇的索引
prices = np.take(clo, weekday_index)
mean_clo = np.mean(prices)
weekday_average[i] = mean_clo
# 查看哪个⼯作⽇的平均收盘价最⾼或者最低
top_weekday = np.argmax(weekday_average) # 获取最⾼平均收盘价对应的索引值
low_weekday = np.argmin(weekday_average) # 获取最低平均收盘价对应的索引值
按照周汇总计算每周开盘价(monday_open)、收盘价(friday_clo)、最⾼价(week_high)、最低价(week_low)
dates, start, high, low, clo = np.loadtxt(r"C:\Urs\67505\Desktop\表1.csv",
delimiter=",",converters={1: date_str_to_week},ucols=(1, 3, 4, 5, 6), unpack=True) # 周汇总数据
dates = dates[:16]
clo = clo[:16]
# 找到第⼀个星期⼀
first_monday = np.ravel(np.where(dates == 0))[0]
# 找到最后⼀个星期五
last_friday = np.ravel(np.where(dates == 4))[-1]
# 创建数组保存三周内每⼀天的索引值
weeks_indices = np.arange(first_monday, last_friday + 1)
# 数组切分,每个⼦数组5个元素
weeks_indices = np.split(weeks_indices, 3)
def summarize(a, s, h, l, c):
"""
获取⼀周之内的开盘价,最⾼价,最低价,收盘价
:param a: ⽇期索引
:param s: 开盘⼦数组
:param h: 最⾼价数组
:param l: 最低价数组
:param c: 收盘价数组
:return:
monday_start: ⼀周开盘价
week_high: ⼀周最⾼价
week_low: ⼀周最低价
week_clo: ⼀周收盘价
"""
monday_start = s[a[0]]
week_high = np.max(np.take(h, a))
week_low = np.min(np.take(l, a))
week_clo = c[a[-1]]
return monday_start, week_high, week_low, week_clo
week_summary = np.apply_along_axis(summarize, 1, weeks_indices, start, high, low, clo)
计算真实波动幅度均值(ATR)
# 设置交易时间段
N = 20
# 时间段最⾼价
period_high = high[-N:]
# 时间段最低价
period_low = low[-N:]
# 前⼀个交易⽇的收盘价
previous_clo = clo[-N - 1:-1]
# 计算当⽇股价范围(period_high-period_low),
# #当⽇最⾼价和前⼀个交易⽇收盘价之差(period_high-previous_clo)
# #前⼀个交易⽇收盘价和当⽇最低价之差(previous_clo - period_low)
true_range = np.maximum(period_high - period_low, period_high - previous_clo,
准爸爸
previous_clo - period_low)
# 设置ATR初始值
ATR = np.zeros(N)
# 设置ATR初始值
ATR[0] = np.mean(true_range)
# 计算剩下的ATR值
for i in range(1, N):
ATR[i] = (N * ATR[i - 1] + true_range[i]) / N
线性模型预测股价
假设:当前股价可以⽤之前股价的线性组合表⽰,也就是说当前股价等于之前的股价与各⾃的系数相乘再做加和的结果。
# 线性模型预测股价
# 获取⼀个包含N个股票价格的数组
stock_price_vec = clo[-5:]
# 反转数组
stock_price_vec = stock_price_vec[::-1]
# 初始化⼀个5*5的⼆维数组
A = np.zeros((5, 5))
待业证明怎么开
# ⽤股票价格数组填充⼆维数组
for i in range(5):
牛a
A[i,] = clo[-5 - i - 1:-1 - i]
x, residuals, rank, s = np.linalg.lstsq(A, stock_price_vec)
# 预测下⼀次股价
next_stock_price = np.dot(stock_price_vec, x)
案例:计算股票的相关系数(corr)和净额成交量(OBV)
bhp = np.loadtxt(r"C:\Urs\67505\Desktop\numpy教程数据集\ch4code\ch4code\BHP.csv", delimiter=",", ucols=(6,),
unpack=True)
vale = np.loadtxt(r"C:\Urs\67505\Desktop\numpy教程数据集\ch4code\ch4code\VALE.csv", delimiter=",", ucols=(6,),
unpack=True)
# 计算收益率(diff函数计算前者与后者的差值,收益率=(a1-a2)/a1)
bhp_returns = np.diff(bhp) / bhp[:-1]
vale_returns = np.diff(vale) / vale[:-1]
# 计算收益率协⽅差矩阵,cov(x,y)=E(x-E(x))(y-E(y))
covariance = np.cov(bhp_returns, vale_returns)
# 计算收益率协⽅差矩阵的迹(主对⾓元素之和)
return_trace = ace()
# 计算相关系数矩阵,corr(x,y)=cov(x,y)/(x.std()*y.std())
corr = np.corrcoef(bhp_returns, vale_returns)
# 绘图
t = np.arange(len(bhp_returns))
plot(t, bhp_returns, lw=2)
plot(t, vale_returns, lw=2)
show()
# 计算OBV(净额成交量),sign(cloPrice(t)-cloPrice(t-1))*volumn(t)+OBV(t-1)
宋辽战争
clo_price, volumn = np.loadtxt(r"C:\Urs\67505\Desktop\numpy教程数据集\ch4code\ch4code\BHP.csv", delimiter=",",
unpack=True, ucols=(6, 7))
change = np.diff(clo_price)
# 设置初始化OBV值
显山露水OBV = np.zeros(len(volumn) - 1)
for i in range(len(volumn) - 1): # change.shape=(28,),故减1
OBV[i] = np.sign(change[i]) * volumn[i] + OBV[i - 1]
print(OBV)