时间: 2020-09-6|tag: 42次围观|0 条评论

用Python 分析股票入门步骤插图
image

初入数据分析大门,感觉需要补的知识太多太多。不太建议系统补齐各种知识,因为大概率会倒在半路上。

以项目为导向,梳理大概流程,对流程中所需的知识点进行大致学习,以后遇到知识盲点再回来补。

接下来我以股票分析为例来了解数据分析流程。

个人认为股票分析无非就分为获取数据、数据处理、数据可视化三个部分,依赖 Python 强大的第三方开源库,上手难度变得非常低。
去官网了解更多

Python 基本语法

小编整理了全套Python零基础资料视频,点击获取。
点击获取

安装 anaconda

Anaconda是一个包含180+的科学包及其依赖项的开源Python发行版本。

安装 anaconda,直接去anaconda 官网下载安装即可。

anaconda 安装成功后,会自带安装 Jupyter,jupyter 主要用于我们代码的编写和运行。

创建一个新文件夹 stock-market-analysis ,进入当前目录,启动 jupyter 。

# 启动 jupyterconda notebook 

启动成功,在浏览器中打开 http://localhost:8888/tree) ,单击 new,创建一个新的 notebook 就可以开始愉快的玩耍了!

numpy

numpy 是一个用于科学计算的 Python 库。

基本用法
# 引入 numpyimport numpy as np
# 创建一个长度为15,3乘5的二维数组a = np.arange(15).reshape(3, 5)# 打印aa
array([[ 0,  1,  2,  3,  4],       [ 5,  6,  7,  8,  9],       [10, 11, 12, 13, 14]])
# 创建一个长度为15,间隔10,3乘5的二维数组b = np.arange( 1, 150, 10 ).reshape(3, 5)# 打印bb
array([[  1,  11,  21,  31,  41],       [ 51,  61,  71,  81,  91],       [101, 111, 121, 131, 141]])
# 两个二维数组相加 a + b
array([[  1,  12,  23,  34,  45],       [ 56,  67,  78,  89, 100],       [111, 122, 133, 144, 155]])

pandas

pandas 是一个基于 numpy 强大的 Python 数据分析包,它提供了很高级的数据结构和大量处理数据的方法。最终目的是为了我们更好的理解和处理数据。

基本用法

pandas 提供了两种数据结构,Series 和 DataFrame。

import numpy as npimport pandas as pdfrom pandas import Series, DataFrame
Series

Series 类似于字典,可以根据索引查找对应值

# 创建一个长度为4,1到10的随机整数Series(np.random.randint(1,10,4))
0    41    22    33    1dtype: int64
# 指定indexs = Series(np.random.randint(1,10,4), index=['a','b','c','d'])s
a    4b    5c    5d    8dtype: int64
# 根据 index 查找s['a']
4
DataFrame

DataFrame 是二维的数据结构,可以用行列的方式表示,可以把它想象成一个 Excel 表。

# 生成值为时间的数组dates = pd.date_range('20130101', periods=6)dates
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',               '2013-01-05', '2013-01-06'],              dtype='datetime64[ns]', freq='D')
# 生成 index 为日期,列名为 ABCD 的 DataFramedf = DataFrame(np.random.randint(1,10,24).reshape(6,4), index=dates, columns=list('ABCD'))df

去官网了解更多

matplotlib seaborn

matplotlib 是 Python 非常重要的数据可视化库,而 seaborn 是基于 matplotlib 开发的可视化库,更为强大易用。

基本用法
import numpy as npimport pandas as pdfrom pandas import Series, DataFrameimport matplotlib.pyplot as pltimport seaborn as sns
# 创建一个 Series,1000个从1到100间隔均匀的数组s = Series(np.linspace(1, 100, 1000))
s.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x1a226a23c8>
用Python 分析股票入门步骤插图1
image
# 创建一个 DataFrame,1到10的随机整数,10乘4的二维数组,列名为 a,b,c,ddf = DataFrame(np.random.randint(1,10,40).reshape(10,4),columns=list("abcd"))
df
用Python 分析股票入门步骤插图2
DU)VLH~H}SUPSKFG%$_RXLR.png
# matpoltlib 画图df.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x11fe2ab38>
用Python 分析股票入门步骤插图3
image
# 对每列进行求和df_sum = df.sum()# 指定柱状图df_sum.plot(kind='bar')
<matplotlib.axes._subplots.AxesSubplot at 0x1a22821668>
用Python 分析股票入门步骤插图4
image
# seaborn 画图index = df_sum.indexvalues = df_sum.valuessns.barplot(index, values)
<matplotlib.axes._subplots.AxesSubplot at 0x1a229041d0>

去matplotlib官网了解更多
去seaborn官网了解更多

股票分析

股票分析的步骤:

获取数据
处理数据
数据可视化
分析数据

# basicimport numpy as npimport pandas as pdfrom pandas import Series, DataFrame# get dataimport pandas_datareader as pdr# visualimport matplotlib.pyplot as pltimport seaborn as sns# timefrom datetime import datetime
# pandas_datareader 这个库提供 API 来获取股票数据# get_data_yahoo 代表数据源来自 yahoo,'BABA' 是阿里巴巴的股票代码df = pdr.get_data_yahoo('BABA')# 由于数据较多,我们只取头部的五条数据来看# high 表示最高价,low 表示最低价,open 表示开盘价,close 表示收盘价,volume 表示交易量df.head()
# 直接用 matplotlib 画它们的股价走势top_df['Close'].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x1a1741f748>
用Python 分析股票入门步骤插图5
image
# 根据他们的股价走势,画出股价波动top_df_dr = top_df['Close'].pct_change()top_df_dr.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x1a173004a8>
用Python 分析股票入门步骤插图6
image
# 上面的估计波动图太密集,不容易开出问题,我们选亚马逊和谷歌的进行对比# 利用 seaborn 画出亚马逊和谷歌的散点图# 每个点对应的横坐标和纵坐标,分别对应谷歌和亚马逊当日的涨跌情况,如果都为负数说明当日股价均为下跌sns.jointplot('AMZN', 'GOOG', top_df_dr)
<seaborn.axisgrid.JointGrid at 0x1a172d8048>
用Python 分析股票入门步骤插图7
image
# 我们还可以利用强大的 seaborn 对五家公司进行相互对比sns.pairplot(top_df_dr.dropna())
<seaborn.axisgrid.PairGrid at 0x1a1786ada0>
用Python 分析股票入门步骤插图8
image

来源:本文为第三方转载,如有侵权请联系小编删除。

文章转载于:https://www.jianshu.com/p/09e9cc29f1bf

原著是一个有趣的人,若有侵权,请通知删除

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《用Python 分析股票入门步骤
   

还没有人抢沙发呢~