3.10 统计分析
np.median(c) 中位数
np.msort(c) 升序排序
np.var(c) 方差
3.12 分析股票收益率
np.diff(c) 可以返回一个由相邻数组元素的差
值构成的数组
returns = np.diff( arr ) / arr[ : -1] #diff返回的数组比收盘价数组少一个元素
np.std(c) 标准差
对数收益率
logreturns = np.diff( np.log(c) ) #应检查输入数组以确保其不含有零和负数
where 可以根据指定的条件返回所有满足条件的数
组元素的索引值 。
posretindices = np.where(returns0)
np.sqrt(1./252.) 平方根 , 浮点数
3.14 分析日期数据
# AAPL,28-01-2011, ,344.17,344.4,333.53,336.1,21144800
dates, close=np.loadtxt('data.csv', delimiter=',', usecols=(1,6), converters={1:datestr2num}, unpack=True)
print "Dates =", dates
def datestr2num(s):
return datetime.datetime.strptime(s, "%d-%m-%Y").date().weekday()
# 星期一 0
# 星期二 1
# 星期三 2
# 星期四 3
# 星期五 4
# 星期六 5
# 星期日 6
#output
Dates = [ 4. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4. 1. 2. 4. 0. 1. 2. 3. 4. 0.
1. 2. 3. 4.]
averages = np.zeros(5)
for i in range(5):
indices = np.where(dates == i)
prices = np.take(close, indices) #按数组的元素运算,产生一个数组作为输出 。
a = [4, 3, 5, 7, 6, 8]
indices = [0, 1, 4]
np.take(a, indices)
array([4, 3, 6])
np.argmax(c) #返回的是数组中最大元素的索引值
np.argmin(c)
3.16 汇总数据
# AAPL,28-01-2011, ,344.17,344.4,333.53,336.1,21144800
#得到第一个星期一和最后一个星期五
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个元素,用split函数切分数组
weeks_indices = np.split(weeks_indices, 5)
#output
[array([1, 2, 3, 4, 5]), array([ 6, 7, 8, 9, 10]), array([11,12, 13, 14, 15])]
weeksummary = np.apply_along_axis(summarize, 1, weeks_indices,open, high, low, close)
def summarize(a, o, h, l, c): #open, high, low, close
monday_open = o[a[0]]
week_high = np.max( np.take(h, a) )
week_low = np.min( np.take(l, a) )
friday_close = c[a[-1]]
return("APPL", monday_open, week_high, week_low, friday_close)
np.savetxt("weeksummary.csv", weeksummary, delimiter=",", fmt="%s") #指定python未知函数梯度了文件名、需要保存的数组名、分隔符(在这个例子中为英文标点逗号)以及存储浮点数的格式 。
0818b9ca8b590ca3270a3433284dd417.png
格式字符串以一个百分号开始 。接下来是一个可选的标志字符python未知函数梯度:-表示结果左对齐,0表示左端补0,+表示输出符号(正号+或负号-) 。第三部分为可选的输出宽度参数,表示输出的最小位数 。第四部分是精度格式符,以”.”开头,后面跟一个表示精度的整数 。最后是一个类型指定字符,在例子中指定为字符串类型 。
numpy.apply_along_axis(func1d, axis, arr, *args, **kwargs)
def my_func(a):
... """Average first and last element of a 1-D array"""
... return (a[0] + a[-1]) * 0.5
b = np.array([[1,2,3], [4,5,6], [7,8,9]])
np.apply_along_axis(my_func, 0, b) #沿着X轴运动,取列切片
array([ 4., 5., 6.])
np.apply_along_axis(my_func, 1, b) #沿着y轴运动,取行切片
array([ 2., 5., 8.])
b = np.array([[8,1,7], [4,3,9], [5,2,6]])
np.apply_along_axis(sorted, 1, b)
array([[1, 7, 8],
[3, 4, 9],
[2, 5, 6]])
3.20 计算简单移动平均线
(1) 使用ones函数创建一个长度为N的元素均初始化为1的数组,然后对整个数组除以N,即可得到权重 。如下所示python未知函数梯度:
- mysqli获取所有数据 mysql的提取函数
- mysql 分组函数 mysql分组取数据
- mysql统计函数
- mysql有rank函数吗 mysql有rank
- mongdb python pythonmongodb操作
- redis-cli命令不能用 redis函数调用失败
- mysql的判断函数 mysql怎么判断奇偶数
- mysql数据库编码格式查询 mysql编码函数
- mysql日期yyyymmdd mysql中的日期型函数
- mysql序列函数 mysql如何查序列
