欢迎使用CSDN-markdown编辑器

来源:互联网 发布:数据科学家职业规划 编辑:程序博客网 时间:2024/06/05 07:22
# -*- coding: utf-8 -*-"""Created on Mon Dec 19 12:39:05 2016@author: 郑运龙"""#一、进行数据清洗import pandas as pd data=pd.read_excel(r'C:\Users\古丁\Desktop\原油期货数据\WTI现货与期货的数据.xlsx')print(data.head())import pandas as pdfrom sklearn import linear_modelimport matplotlib.font_manager as fmmyfont = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')import matplotlib.pylab as pltimport matplotlibf1=plt.figure(1,figsize=(9,6))frame=pd.read_excel(r'C:\Users\古丁\Desktop\原油期货数据\us+wti.xlsx')print(frame.head())ax2=plt.subplot(211)ax3=ax2.twinx()  #用来构造双y轴'ax2.plot(data['日期'],data['spot price'],color='red', linewidth=1.5,label='spot sprice')ax2.legend(loc='upper right')ax3.plot(data['日期'],data['期货结算价'],color='blue', linewidth=1,label='settle price')ax3.plot(data['日期'],data['下一交易日的期货结算价'],color='green', linewidth=1,label='settle price of next day')plt.xlabel('日期',fontproperties=myfont)ax2.set_ylabel('WTU现货价格',fontproperties=myfont)ax3.set_ylabel('WTI 期货收盘价',fontproperties=myfont)ax3.legend(loc='lower left')plt.title('(图十):WTI现货和期货收盘价的折线图',fontproperties=myfont)plt.subplot(223)'回归'regr = linear_model.LinearRegression()regr.fit(data['spot price'].reshape(-1,1),data['期货结算价'])y=regr.predict(data['spot price'].reshape(-1,1))a, b = regr.coef_, regr.intercept_print('1----',a,b)plt.scatter(data['spot price'],data['期货结算价'],label='scatter')plt.plot(data['spot price'],y,color='red', linewidth=1,label='regression')plt.xlabel('WTI 原油现货价格',fontproperties=myfont)plt.ylabel('WTI期货收盘价',fontproperties=myfont)plt.text(60,-15,'回归公式:y = b+kx+e\nk=0.6678 \nb=22.0987',fontproperties=myfont)plt.legend(loc='upper left')plt.title('(图十一):现货价格和下一天的\nWTI期货价格的散点图',fontproperties=myfont)plt.subplot(224)'回归'regr = linear_model.LinearRegression()regr.fit(data['spot price'].reshape(-1,1),data['下一交易日的期货结算价'])y=regr.predict(data['spot price'].reshape(-1,1))a, b = regr.coef_, regr.intercept_print('2---',a,b)plt.scatter(data['spot price'],data['下一交易日的期货结算价'],label='scatter')plt.plot(data['spot price'],y,color='red', linewidth=1,label='regression')plt.xlabel('WTI原油现货价格',fontproperties=myfont)plt.ylabel('下一交易日的WTI期货收盘价',fontproperties=myfont)plt.text(60,-15,'回归公式:y = b+kx+e\nk=0.6675 \nb= 22.089',fontproperties=myfont)plt.legend(loc='upper left')plt.title('(图十二):现货价格和下一交易日的\nWTI期货价格的散点图',fontproperties=myfont)'--------------------------------------------------------------------------------------------'plt.subplots_adjust(wspace = 0.3,hspace=0.5)plt.savefig('现货价格与WTI价格之间的关系.jpg',dpi=600)
0 0
原创粉丝点击