【问题标题】:Time Series Classification时间序列分类
【发布时间】:2018-01-16 09:15:23
【问题描述】:

您可以通过此链接https://drive.google.com/file/d/0B9Hd-26lI95ZeVU5cDY0ZU5MTWs/view?usp=sharing访问数据集

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

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

我的代码如下:

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% 的准确率。但我猜真实情况并不正确,因为它出现在基本事实集中。 (如果我错了请告诉我)

我也在考虑用 Gradient Boosted Tree 来做分类,会不会更好?

【问题讨论】:

    标签: python machine-learning time-series classification


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-12-05
      • 2018-03-29
      • 2018-05-25
      • 2018-09-10
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多