机器学习 Day?(python环境下)

1.处理沪深300股票获得收益率

(数据内容来自dsai-book/tools at master · dsai-python/dsai-book · GitHub

#1 load the api

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os


#2 read the data

index_path = r'D:\python_pycharm\dsai-book-master\data\SZ399300.TXT'
stock_path = r'D:/python_pycharm/dsai-book-master/data/hs300'
index0 = pd.read_table(index_path, encoding='cp936', header=None)
index0.columns = ['date', 'o', 'h', 'l', 'c', 'v', 'to']    # set the title of the columns
index1 = index0[:-1]    # delete some chinese words in the last line 
index1.index = index1['date']    # set the index of the data

names = os.listdir(stock_path)    # read the 300 file names

close = []    # carry on the closing price column


#3 process the data

for name in names:
    spach = stock_path + '/' + name     # get every single data of the stocks
    tmp = pd.read_table(spath, enconding='cp936', header=None)
    df = tmp[:-1]    # delete some chinese words in the last line 
    df.columns = ['date', 'o', 'h', 'l', 'c', 'v', 'to']
    df.index = df['date']
    df1 = df.reindex(index1.index, method='ffill')
    df2 = df.fillna(method='bfill')
    close.append(df2['c'].values)

close = np.asarry(close).T    # from columns to arrays
retx = (close[1:,:] - clsoe[0:-1,:]) / close[:-1,:]    # calculate the profit rate

2. 面向对象的编程思维:

# 1. import api 
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os

# 2 import data
def read_data(path, name):
    f = open(path + name, 'r')
    close_price = []
    for line in f:
        s = line.split('\t')
        if(len(s)>4):    # ignore the last line
            close_price.append(float(s.[4]))
    f.close()
    return close_price

# 3 deal with the data
def  process_data(data):
    p = np.asarray(data)
    rate = (p[1:] - p[:-1])/p[:-1]
    return rate

# 4 draw plot
def draw_plot(rate,k=30):
    plt.figure(figsize=(20,8), dpi=100)
    plt.plot(rate)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值