首页 文章

时间序列分类

提问于
浏览
5

您可以访问此链接中的数据集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)的所有trues的数组,我不知道为什么我会得到所有的真实 .

而且我不知道最终的准确度是否以哪种方式计算:例如,我认为我当前的准确度只计算真假的数量而忽略了它们的顺序?由于这是时间序列数据,忽略订单是不正确的,并没有给我任何关于预测价格变动的信息 . 假设我在测试集中有40个例子,我得到20个Tures我会得到50%的准确率 . 但是,我猜这些真理不在正确的位置,因为它出现在地面真相中 . (告诉我,如果我错了)

我也在考虑使用Gradient Boosted Tree进行分类,它会更好吗?

1 回答

  • 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之一,通过缩放功能输入来预处理数据,然后再将其输入模型 .

相关问题