2017-08-08 198 views
4

可以访问数据集在此链接https://drive.google.com/file/d/0B9Hd-26lI95ZeVU5cDY0ZU5MTWs/view?usp=sharing时间序列分类

我的任务是预测一个行业的基金的价格变动。它的上升或下降并不重要,我只想知道它是上涨还是下跌。所以我把它定义为分类问题。

由于这个数据集是一个时间序列数据,我遇到了很多问题。我已经阅读过有关这些问题的文章,比如我不能使用k-fold交叉验证,因为这是时间序列数据。你不能忽略数据的顺序。

我的代码如下:

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
import datetime 
from sklearn.linear_model import LinearRegression 
from math import sqrt 
from sklearn.svm import LinearSVC 
from sklearn.svm import SVCenter code here 

lag1 = pd.read_csv(#local file path, parse_dates=['Date']) 

#Trend : if price going up: ture, otherwise false 
lag1['Trend'] = lag1.XLF > lag1.XLF.shift() 
train_size = round(len(lag1)*0.50) 
train = lag1[0:train_size] 
test = lag1[train_size:] 

variable_to_use= ['rGDP','interest_rate','private_auto_insurance','M2_money_supply','VXX'] 
y_train = train['Trend'] 
X_train = train[variable_to_use] 
y_test = test['Trend'] 
X_test = test[variable_to_use] 

#SVM Lag1 

this_C = 1.0 
clf = SVC(kernel = 'linear', C=this_C).fit(X_train, y_train) 
print('XLF Lag1 dataset') 
print('Accuracy of Linear SVC classifier on training set: {:.2f}' 
.format(clf.score(X_train, y_train))) 
print('Accuracy of Linear SVC classifier on test set: {:.2f}' 
.format(clf.score(X_test, y_test))) 

#Check prediction results 
clf.predict(X_test) 

首先,是我的方法就在这里:第一生成的真假列?如果我只是简单地将这一栏提供给它,我恐怕机器不能理解这一栏。我是否应该首先执行回归,然后比较数字结果以生成上升或下降列表?

训练集的准确性非常低:0.58我得到一个数组,其中包含clf.predict(X_test)的所有特征,我不知道为什么我会得到所有特征。

我不知道得出的准确度是以哪种方式计算的:例如,我认为我目前的准确度只计算真假的数量,但忽略它们的顺序?由于这是时间序列数据,忽略订单是不正确的,并且没有提供关于预测价格变动的信息。假设我在测试集中有40个示例,并且我有20个Tures,我会得到50%的准确度。但是我认为这些真理并不处于正确的位置,因为它出现在基本真理集中。 (告诉我,如果我错了)

我也在考虑使用梯度增强树来做分类,会更好吗?

回答

0

对这些数据进行一些预处理可能会有帮助。第一步可能会去是这样的:

df = pd.read_csv('YOURLOCALFILEPATH',header=0) 
#more code than your method but labels rows as 0 or 1 and easy to output to new file for later reference 
df['Date'] = pd.to_datetime(df['date'], unit='d') 
df = df.set_index('Date') 
df['compare'] = df['XLF'].shift(-1) 
df['Label'] np.where(df['XLF']>df['compare'), 1, 0) 
df.drop('compare', axis=1, inplace=True) 

第二步可以通过喂养它到你的模型之前,你的缩放功能输入使用sklearn的built in scalers, such as the MinMax scaler之一,对数据进行预处理。

+0

你可以添加示例代码吗? –