1、random 隨機模塊
import random
code = random.choice(stock_list)
# 從一個列表中隨機選取元素下面是我目前經(jīng)常用到的模塊,為了方便使用,不是有特殊需求的話,個人覺得一開始沒比亞每個模塊都很深入學(xué)習(xí),只要知道自己常用的一些方法就行。隨時更新,請搜索使用。
random 隨機選取模塊:
1
2
3
4
|
import random a = [ 1 , 2 , 3 , 4 , 5 ] print (random.choice(a)) # 隨機從列表中抽取一個元素 code = random.choice(stock_list) # 從一個列表中隨機選取元素 |
os 文件夾模塊:
1
2
3
4
5
6
7
|
import os # 設(shè)置默認文件路徑 os.chdir() os.chdir(u 'C:/Users/Ocean/OneDrive/class5/data/input_data/stock_data' ) df = pd.read_csv( 'sz300001.csv' ) print df |
程序根目錄地址,os.pardir:父目錄 parent directory
1
2
|
root_path = os.path.abspath(os.path.join(current_file, os.pardir, os.pardir)) # 兩級父目錄 print root_path |
輸入數(shù)據(jù)根目錄地址
1
|
input_data_path = os.path.abspath(os.path.join(root_path, 'data' , 'input_data' )) |
time 時間模塊:
1
|
import time |
獲取當前日期
1
|
date_now = time.strftime( '%Y-%m-%d' , time.localtime(time.time())) |
計時器
1
2
3
4
|
start = time.time() end = time.time() used_time = str (end - start) print "used_time: " + used_time |
2、matplotlab.pyplot 作圖模塊
1
|
import matplotlib.pyplot as plt |
添加空白畫布
1
|
fig = plt.figure(figsize = ( 12 , 5 )) |
在空白畫布上設(shè)置一塊區(qū)域
1
|
ax = fig.add_subplot( 1 , 1 , 1 ) |
設(shè)置畫塊的標題
1
2
3
|
ax.set_title( str (code)) ax.set_xlabel( 'Time' ) # 設(shè)置橫坐標x軸的名字 ax.set_ylabel( 'Return' ) # 設(shè)置Y軸 |
畫一根2D線圖,并設(shè)置名稱為 'stock_return'
1
|
plt.plot(df[equity], label = 'stock_return' ) |
繪制散點圖
1
|
plt.scatter(df[ 'ma_long' ], df[ 'final_ratio' ], label = 'ma_long' ) |
還有更多的圖形可以繪制,如果真的有需要,可以網(wǎng)上再搜索
1
2
|
plt.legend(loc = 'best' ) # 顯示圖線的名字 plt.show() # 繪出圖像結(jié)果 |
3、mpl_toolkits.mplot3d 繪制3D圖模塊
1
2
3
4
5
6
7
8
9
|
from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) ax.scatter(df[ 'ma_long' ],df[ 'ma_short' ],df[ 'final_ratio' ], c = 'b' ) #繪制數(shù)據(jù)點 # 設(shè)置坐標軸名字 ax.set_zlabel( 'final_ratio' ) #坐標軸 ax.set_ylabel( 'ma_short' ) ax.set_xlabel( 'ma_long' ) plt.show() |
原文鏈接:https://zhuanlan.zhihu.com/p/33375411