'''----------------------保存数据savetxt-----------------------'''
import numpy as np
example = np.arange(24).reshape(3,8)
'''
将numpy的ndarray数组保存到文本文件中:
savetxt(fileName,data)
fileName:保存文件路径和名称
data:需要保存的数据
'''
np.savetxt("./data/example.txt",example)
print("保存完成~")
'''---------------------加载数据loadtxt-----------------------'''
import numpy as np
'''
numpy加载csv文件数据
np.loadtxt(filepath,delimiter,usecols,converters,unpack)
filepath:加载文件的路径
delimiter:数据文件的分隔符
usecols:加载数据文件中数据列索引
unpack:当加载多列数据时是否需要将数据列进行解耦赋值给不同的变量
converters:{},第一个参数为要加载的数据是第几列,第二个参数是对数据操作的函数或表达式
'''
result = np.loadtxt('data/data.csv',delimiter=",",usecols=(6,7))
print(result)
#从result中切片分别取出收盘价和成交量
close = result[:,0]
print("收盘价:\n",close)
amount = result[:,1]
print("成交量:\n",amount)
#当加载csv文件的多列数据时可以使用unpack将加载的数据列进行解耦到不同的数组中
arr1,arr2 = np.loadtxt('data/data.csv',delimiter=",",usecols=(6,7),unpack=True)
print("收盘价:\n",arr1)
print("成交量:\n",arr2)
#加载收盘价并将数据转化为整数
close = np.loadtxt("data/data.csv",delimiter=",",usecols=(6),
converters={6:lambda x: int(float(x))})
print(close)
'''---------------------数组的属性-----------------------'''
import numpy as np
example = np.arange(24).reshape(3,4,2)
#01->ndim数组的维度
print("01维度:",example.ndim) #3
#02->size数组元素的个数
print("02元素个数:",example.size) #24
#03->len
print("03元素个数:",len(example)) #3
#04->itemsize数组中的元素在内存中所占的字节数
print("每个元素在内存中所占的字节数:",example.itemsize) #int32/8=4
#05->nbytes整个数组所占的存储空间/字节数
print("整个数组所占的存储空间/字节数:",example.nbytes) #24*4=96
print("整个数组所占的存储空间/字节数:",example.size*example.itemsize) #24*4=96
#06->T数组的转置结果
print("原数组:\n",example) #3x4x2
print("转置结果:\n",example.T) #2x4x3
#07->real复数数组的实部
comp = np.array([2+5j,6+3j,4+2j])
real = comp.real
print("复数数组的实部是:\n",real) #[2. 6. 4.]
#08->imag复数数组的虚部
imag = comp.imag
print("复数数组的虚部是:",imag) # [5. 3. 2.]
#09求comp复数数组的每个元素的模
print("复数数组的每个元素的模是:",np.sqrt(real**2+imag**2)) #[5.38516481 6.70820393 4.47213595]
iterator = np.arange(9).reshape(3,3)
#10将数组展平成扁平迭代器
result = iterator.flat
for item in result:
print(item) #0 1 2 3 4 5 6 7 8
'''---------------数组的分割split/hsplit/vsplit/dsplit------------'''
import numpy as np
#数组的分割
full = np.arange(9).reshape(3,3)
print(full)
#np.split 当axis=0则是按行进行分割
axis0 = np.split(full,3,axis=0)
print(axis0[0]) #[[0 1 2]]
print(axis0[0].shape) #(1,3)
print(axis0[1]) #[[3 4 5]]
print(axis0[2]) #[[6 7 8]]
#np.split 当axis=1则是按列进行分割
axis1 = np.split(full,3,axis=1)
print(axis1)
#hsplit(arg,num),与np.split当axis=1一样水平分割与按列分割一样
axis2 = np.hsplit(full,3)
print(axis2)
#vsplit(arg,num),与np.split当axis=0一样垂直分割与按行分割一样
axis3 = np.vsplit(full,3)
print(axis3)
#2x2x2
d3 = np.arange(8).reshape(2,2,2)
print("d3:\n",d3)
#dsplit深度分割,dsplit only works on arrays of 3 or more dimensions
dsplit = np.dsplit(d3,2)
print(dsplit)
print(dsplit[0]) #2x2x1
print(dsplit[1]) #2x2x1
#2x2x6
d4 = np.arange(24).reshape(2,2,6)
print("d4:\n",d4)
#dsplit深度分割,dsplit only works on arrays of 3 or more dimensions
dsplit = np.dsplit(d4,2)
print(dsplit)
print(dsplit[0]) #2x2x1
print(dsplit[1]) #2x2x1
'''---------------股票波动率------------'''
import numpy as np
close = np.loadtxt("data/data.csv",delimiter=",",usecols=(6))
#计算简单的收益率:相邻两天的差除以前一天的价格
returns = np.diff(close)/close[:-1]
print(returns)
#对数收益率
#对所有收盘价取对数
logClose = np.log(close)
print("收盘价的对数:",logClose)
diffLogClose = np.diff(logClose)
print("收盘价的对数收益率:",diffLogClose)
#对数收益率的标准差
std = np.std(diffLogClose)
print("对数收益率的标准差",std)
#计算年波动率
annual_volatility = std/np.mean(diffLogClose)
year_volatility = annual_volatility/np.sqrt(1/252)
print("年波动率是:",year_volatility)
#计算月波动率
month_volatility = annual_volatility/np.sqrt(1/12)
print("月波动率是:",month_volatility)
'''---------------统计方法average/mean/max/min------------'''
import numpy as np
#加载收盘价和成交量
close,amount = np.loadtxt("data/data.csv",delimiter=",",
usecols=(6,7),unpack=True)
print("收盘价:\n",close)
print("成交量:\n",amount)
#01->成交量加权平均价:np.average(ndarray,weights)
aver = np.average(close,weights=amount)
print("成交量加权平均价:",aver)
#02->收盘价算数平均值:np.mean(ndarray) np.average(ndarray)
mean = np.mean(close)
average = np.average(close) #350.5895493532009
print("收盘价算数平均值:",mean) #351.0376666666667
print("收盘价算数平均值:",average) #351.0376666666667
#03->求最大值最小值 np.max(ndarray) np.min(ndarray)
#求成交量最大值和最小值
maxAmount = np.max(amount)
print("最大成交量:",maxAmount)
minAmount = np.min(amount)
print("最小成交量:",minAmount)
example = np.arange(24).reshape(2,3,4)
print(example)
print(np.max(example))
print(np.min(example))
#自定义一个函数获取数组的最大值最小值
def getMaxandMin(ndarray):
max,min = ndarray[0],ndarray[0]
result = ndarray.flat #先展平
for item in result:
if item>max:
max = item
if item<min:
min = item
return max,min
max,min = getMaxandMin(example)
print(max,min)
'''---------------统计方法ptp/median/var/repeat---------------'''
import numpy as np
#04->计算数组的极差(最大值和最小值的差值 ) np.ptp
high = np.loadtxt("data/data.csv",delimiter=",",usecols=(4))
print(high)
#计算最高价的极差
ptp = np.ptp(high)
print("最高价的极差",ptp)
#利用最大值最小值得到极差
max = np.max(high)
min = np.min(high)
ptp2 = max - min
print("ptp2:",ptp2)
#05->计算收盘价的中位数 np.median()
close = np.loadtxt("data/data.csv",delimiter=",",usecols=(6))
print(close)
median = np.median(close)
print("收盘价的中位数:",median)
list(close)
# print(close)
close.sort()
print(close)
if close.size%2 !=0:
median1 = close[(close.size-1)/2]
else:
median1 = (close[int(close.size/2)]+close[int(close.size/2)-1])/2
print(median1)
#06->计算数据样本的方差 np.var()
print("收盘价的方差:",np.var(close))
open = np.loadtxt("data/data.csv",delimiter=",",usecols=(3))
print("开盘价的方差:",np.var(open))
#07->np.repeat()
a = np.array([1,3,5])
times = np.array([4,6,2])
b = np.repeat(a,times)
print(b) #[1,1,1,1,3,3,3,3,3,3,5,5]
'''---------------统计方法prod/cumprod/compress---------------'''
import numpy as np
#1.求阶乘 np.prod
arr = np.array([5,2,7,4])
prod = np.prod(arr)
print("阶乘:",prod) # 5*2*7*4=280
#2.累积乘积 np.cumprod()
cumArr = np.array([2,3,4,5])
cumprod = np.cumprod(cumArr)
print(cumprod) #[ 2 6 24 120]
#3.将数组中所有比num1小的元素设为num1,
# 所有比num2大的元素设为num2,
# 介于num1和num2之间的数值保持不变 np.clip(num1,num2)
test = np.array([3,56,7,44,22,18])
print("clip",test.clip(48,20)) #clip [48 20 48 48 48 48]与num1比小,与num2比大
#4.np.compress()返回根据一定条件筛选后的数组
close = np.loadtxt("data/data.csv",delimiter=",",usecols=(6))
#计算对数收益率
logReturns = np.diff(np.log(close))
print("对数收益率:",logReturns)
#筛选出对数收益率中大于0 的元素
mask = logReturns>0
print("mask索引:",mask)
result= logReturns.compress(mask)
print("对数收益率中大于0 的元素:",result)
'''---------------数组数据的筛选---------------'''
import numpy as np
close = np.loadtxt("data/data.csv",delimiter=",",usecols=(6))
#对数收益率
logReturns = np.diff(np.log(close))
print(logReturns)
#筛选出对数收益率大于0的所有元素的索引
indices= np.where(logReturns>0)
print("对数收益率大于0的索引为:",indices)
#根据索引值,获取相应的数组元素
result = np.take(logReturns,indices)
print(result)
'''---------------日期格式转化---------------'''
from datetime import datetime
dateStr = "2018-5-28"
newdate = datetime.strptime(dateStr,"%Y-%m-%d")
print(newdate) #2018-05-28 00:00:00
#获取dateStr日期对应的星期
weekday = newdate.date().weekday()
print(type(weekday))
print("{}是星期{}".format(dateStr,weekday+1)) #2018-5-28是星期1
'''---------------案例---------------'''
import numpy as np
from datetime import datetime
#将日期格式字符串转化得到星期数
def datestr2num(s):
print(type(s))
# 加载过来的第一列日期数据是bytes类型,需转化为str
return datetime.strptime(s.decode("utf8"),"%d-%m-%Y").date().weekday()
#converters参数:“1”表示要转化的是第一列数据,datestr2num是指使用哪个函数做转化
dates,close = np.loadtxt("data/data.csv",delimiter=",",usecols=(1,6),
converters={1:datestr2num},unpack=True)
print("日期:",dates)
print("收盘价:",close)
aver = []
for i in range(5):
#根据星期数条件得到一个mask数组
mask = dates==i
#根据mask数组筛选出星期数为i的所有的收盘价
weekday = close.compress(mask)
#计算出星期数为i的所有收盘价的均值
dayAver = np.mean((weekday))
#将星期数为i的收盘价的均值添加到aver数组中
aver.append(dayAver) #aver里存的是周一到周五各个的平均值
print(aver)
#获取数组中最大值的索引
maxIndex = np.argmax(aver)
minIndex = np.argmin(aver)
print("星期{}的平均收盘价最高".format(maxIndex+1))
print("星期{}的平均收盘价最低".format(minIndex+1))