美文网首页
Python#常用的模块和简单用法

Python#常用的模块和简单用法

作者: 刀客特鹿 | 来源:发表于2018-02-10 21:31 被阅读17次

random 随机选取模块:

import random

a = [1, 2, 3, 4, 5]

print(random.choice(a))  # 随机从列表中抽取一个元素

os 文件夹模块:

import os

# 设置默认文件路径

os.chdir()

os.chdir(u'C:/Users/Ocean/OneDrive/文档/量化交易/量化小讲堂/视频课程/第五课资料/class5/data/input_data/stock_data')

df = pd.read_csv('sz300001.csv')

print df

# 获取当前程序的地址

current_file = __file__

# 程序根目录地址,os.pardir:父目录 parent directory

root_path = os.path.abspath(os.path.join(current_file, os.pardir, os.pardir))  # 两级父目录

print root_path

# 输入数据根目录地址

input_data_path = os.path.abspath(os.path.join(root_path, 'data', 'input_data'))

time 时间模块:

import time

获取当前日期

date_now = time.strftime('%Y-%m-%d', time.localtime(time.time()))

计时器

start = time.time()

end = time.time()

used_time = str(end - start)

print "used_time: " + used_time

matplotlab.pyplot 作图模块

import matplotlib.pyplot as plt

# 添加空白画布

fig = plt.figure(figsize=(12,5))

# 在空白画布上设置一块区域

ax = fig.add_subplot(1,1,1)

# 设置画块的标题

ax.set_title(str(code))

ax.set_xlabel('Time')  # 设置横坐标x轴的名字

ax.set_ylabel('Return')  # 设置Y轴

# 画一根2D线图,并设置名称为'stock_return’

plt.plot(df[equity], label='stock_return') 

# 绘制散点图

plt.scatter(df['ma_long'], df['final_ratio'], label='ma_long')

plt.legend(loc='best')  # 显示图线的名字

plt.show()  # 绘出图像结果

mpl_toolkits.mplot3d  绘制3D图模块

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') #绘制数据点

# 设置坐标轴名字

ax.set_zlabel('final_ratio') #坐标轴

ax.set_ylabel('ma_short')

ax.set_xlabel('ma_long')

plt.show()

random 随机模块

import random

code = random.choice(stock_list)  # 从一个列表中随机选取元素

相关文章

网友评论

      本文标题:Python#常用的模块和简单用法

      本文链接:https://www.haomeiwen.com/subject/xxphtftx.html