振幅和成交量的关系
用广晟有色的历史数据,用sklearn进行回归,数据如下:
假设每日振幅和成交量以及价格是有关系的,于是构造:
# coding=utf-8 from pandas import Series,DataFrame import pandas as pd import numpy as np from datetime import datetime import tushare as ts from sklearn import datasets, linear_model df=pd.read_csv('data.csv') X=df[['open','volume']] y=df['high']-df['low'] from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1) from sklearn.linear_model import LinearRegression linreg = LinearRegression() linreg.fit(X_train,y_train) print linreg.intercept_ print linreg.coef_ print "振幅=%f+%f价格+%f成交量"%(linreg.intercept_,linreg.coef_[0],linreg.coef_[1]) #模型拟合测试集 y_pred = linreg.predict(X_test) from sklearn import metrics # 用scikit-learn计算MSE print "MSE:",metrics.mean_squared_error(y_test, y_pred) # 用scikit-learn计算RMSE print "RMSE:",np.sqrt(metrics.mean_squared_error(y_test, y_pred))
输出:
-0.542840729241 [ 2.91283661e-02 1.89720767e-05] 振幅=-0.542841+0.029128价格+0.000019成交量 MSE: 1.16361481737 RMSE: 1.07870979293
但是,按说应该跟成交量关系更大一些,等我把数据处理了继续研究。
程序基本照抄http://www.cnblogs.com/pinard/p/6016029.html
原来打算只研究成交量和价格的关系,但报错,原因好像是0.17版后,变量不能是一个维度啥的,所以把价格也加进来了。