# 用Python分析苹果公司股价数据

1.csv数据的读取

2.利用常用函数获取均值、中位数、方差、标准差等统计量

3.利用常用函数分析价格的加权均值、收益率、年化波动率等常用指标 4.处理数据中的日期

import numpy as np
c, v = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1, 2), unpack=True)
print(c)
print(v)

[ 178.02  178.65  178.44  179.97  181.72  179.98  176.94  175.03  176.67   176.82  176.21  175.    178.12  178.39  178.97  175.5   172.5   171.07   171.85  172.43  172.99  167.37  164.34  162.71  156.41  155.15  159.54   163.03  156.49  160.5   167.78  167.43  166.97  167.96  171.51  171.11   174.22  177.04  177.    178.46  179.26  179.1   176.19  177.09  175.28   174.29  174.33  174.35  175.    173.03  172.23  172.26  169.23  171.08   170.6   170.57  175.01  175.01  174.35  174.54  176.42]
[ 38313330.  22676520.  29334630.  31464170.  32191070.  32130360.   24518850.  31686450.  23273160.  27825140.  38426060.  48706170.   37568080.  38885510.  37353670.  33772050.  30953760.  37378070.   33690660.  40113790.  50908540.  40382890.  32483310.  60774900.   70583530.  54145930.  51467440.  68171940.  72215320.  85957050.   44453230.  32234520.  45635470.  50565420.  39075250.  41438280.   51368540.  32395870.  27052000.  31306390.  31087330.  34260230.   29512410.  25302200.  18653380.  23751690.  21532200.  20523870.   23589930.  22342650.  29461040.  25400540.  25938760.  16412270.   21477380.  33113340.  16339690.  20848660.  23451420.  27393660.   29385650.]

import numpy as np
c, v = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1, 2), unpack=True)
mean_c = np.mean(c) print(mean_c)

172.614918033

import numpy as np
c, v = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1, 2), unpack=True)
vwap = np.average(c, weights=v)
print(vwap)

170.950010035

import numpy as np
c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
print(np.max(c))
print(np.min(c))
print(np.ptp(c))

181.72
155.15
26.57

import numpy as np
c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
print(np.max(c))
print(np.min(c))
print(np.median(c))

181.72
155.15
174.35

import numpy as np
c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
print(np.var(c))

37.5985528621

import numpy as np
c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
print(np.mean((c - c.mean())**2))

37.5985528621

，即用今天的收盘价减去昨天的收盘价，再除以昨天的收盘价格。同时我们发挥NumPy的优势，利用向量运算，可以一次性算出所有交易日的收益率

diff函数时用数组的第N项减第N-1项，得到一个n-1项的一维数组。本例中我们注意到数组中日期越近的收盘价，数组索引越小，因此得取一个相反数，综上代码：

import numpy as np
c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
returns = -np.diff(c)/c[1:]
print(returns)

[-0.00352645  0.00117687 -0.00850142 -0.0096302   0.00966774  0.01718097   0.01091242 -0.00928284 -0.00084832  0.00346178  0.00691429 -0.01751628  -0.00151354 -0.00324077  0.01977208  0.0173913   0.00835915 -0.00453884  -0.00336368 -0.00323718  0.0335783   0.01843739  0.01001782  0.04027875   0.00812117 -0.02751661 -0.0214071   0.04179181 -0.02498442 -0.04339015   0.00209043  0.00275499 -0.00589426 -0.0206985   0.00233768 -0.01785099  -0.0159286   0.00022599 -0.00818111 -0.00446279  0.00089336  0.01651626  -0.00508216  0.01032634  0.00568019 -0.00022945 -0.00011471 -0.00371429   0.01138531  0.00464495 -0.00017416  0.01790463 -0.01081365  0.0028136   0.00017588 -0.02536998 -0.          0.00378549 -0.00108858 -0.01065639]

import numpy as np
c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
returns = -np.diff(c)/c[1:]
print(np.std(returns))

0.0150780328454

import numpy as np
c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
returns = -np.diff(c)/c[1:]
print(np.where(returns>0))

(array([ 1,  4,  5,  6,  9, 10, 14, 15, 16, 20, 21, 22, 23, 24, 27, 30, 31,  34, 37, 40, 41, 43, 44, 48, 49, 51, 53, 54, 57], dtype=int64),)

，依照对数的性质，他等于

，在计算年化波动率时，要用样本中所有的对数收益率的标准差除以其均值，再除以交易日倒数的平方根，一年交易日取252天。

import numpy as np
c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
logreturns = -np.diff(np.log(c))
volatility = np.std(logreturns) / np.mean(logreturns)
annual_volatility = volatility / np.sqrt(1./252.)
print(volatility)
print(annual_volatility)

100.096757388
1588.98676256

import numpy as np
dates,c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(0,1), unpack=True)

Traceback (most recent call last):
File "E:/12homework/12homework.py", line 2, in <module>
dates,c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(0,1), unpack=True)
File "C:\Python34\lib\site-packages\numpy\lib\npyio.py", line 930, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Python34\lib\site-packages\numpy\lib\npyio.py", line 930, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Python34\lib\site-packages\numpy\lib\npyio.py", line 659, in floatconv
return float(x)
ValueError: could not convert string to float: b'2018/3/16'

import numpy as np import datetime
strdate = '2017/3/16'
d = datetime.datetime.strptime(strdate,'%Y/%m/%d')
print(type(d))
print(d)

<class 'datetime.datetime'>
2017-03-16 00:00:00

datetime对象有一个date方法，把datetime对象中的time部分去掉，变成一个纯的日期，再调用weekday可以转换为一周中的第几天，这里是从周日开始算起的。

import numpy as np import datetime
strdate = '2018/3/16'
d = datetime.datetime.strptime(strdate,'%Y/%m/%d')
print(d.date())
print(d.date().weekday())

2018-03-16 4

import numpy as np import datetime
def datestr2num(bytedate):
return datetime.datetime.strptime(
bytedate.decode('utf-8'),'%Y/%m/%d').date().weekday()

converters={0: datestr2num}, unpack=True)
averages = np.zeros(5)
for i in range(5):
index = np.where(dates == i)
prices = np.take(c, index)
avg = np.mean(prices)
averages[i] = avg  print("Day {} prices: {},avg={}".format(i,prices,avg))
top = np.max(averages)
top_index = np.argmax(averages)
bot = np.min(averages)
bot_index = np.argmin(averages)
print('highest:{}, top day is {}'.format(top,top_index))
print('lowest:{},bottom day is {}'.format(bot,bot_index))

Day 0 prices: [[ 181.72  176.82  178.97  162.71  156.49  167.96  177.    174.35  176.42]],avg=172.49333333333334
Day 1 prices: [[ 179.97  176.67  178.39  171.85  164.34  163.03  166.97  177.04  176.19    174.33  172.26  170.57  174.54]],avg=172.78076923076924
Day 2 prices: [[ 178.44  175.03  178.12  171.07  167.37  159.54  167.43  174.22  179.1    174.29  172.23  170.6   174.35]],avg=172.44538461538463
Day 3 prices: [[ 178.65  176.94  175.    172.5   172.99  155.15  167.78  171.11  179.26    175.28  173.03  171.08  175.01]],avg=172.59846153846152
Day 4 prices: [[ 178.02  179.98  176.21  175.5   172.43  156.41  160.5   171.51  178.46    177.09  175.    169.23  175.01]],avg=172.71923076923073
highest:172.78076923076924, top day is 1
lowest:172.44538461538463,bottom day is 2

import numpy as np
a = np.arange(5)
print(a.clip(1,3))

[1 1 2 3 3]

import numpy as np
a = np.arange(5)
print(a.compress(a > 2))

[3 4]

*声明：推送内容及图片来源于网络，部分内容会有所改动，版权归原作者所有，如来源信息有误或侵犯权益，请联系我们删除或授权事宜。

- END -

1684 篇文章144 人订阅

0 条评论

## 相关文章

22820

11700

### 用Python分析苹果公司股价数据

❈ 作者：酱油哥，清华程序猿、IT非主流 专栏地址：https://zhuanlan.zhihu.com/c_147297848 ❈ 要点抢先看 1.csv数...

36260

16420

### 6.3 数学操作符（Math Operators）

Cg语言对向量的数学操作提供了内置的支持，Cg中的数学操作符有：*乘法、/除法、-取反、+加法、—减法、%求余、++、——、*=、/=、+=、-=。后面四种运算...

9510

2.1K70

51290

### Sweet Snippet系列 之 随机选择

平日工作学习时总会遇到一些令人欣喜的代码段子（Snippet），虽然都很短小，但是其间所含的道理都颇有意味，遂而觉得应该不时的将她们记下，一来算作复习整理，...

12420

52720

10220