python - 时间序列分类

标签 python machine-learning time-series classification

您可以通过此链接访问数据集 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) 得到一个包含所有真值的数组,我不知道为什么我会得到所有真值。

而且我也不知道结果准确率是通过什么方式计算出来的:比如我觉得我现在的准确率只统计了true和false的个数而忽略了它们的先后顺序?由于这是时间序列数据,忽略顺序是不对的,并且无法提供有关预测价格变动的信息。假设我在测试集中有 40 个示例,我得到 20 个 Tures 我将获得 50% 的准确率。但我猜 trues 不在正确的位置,因为它出现在 ground truth 集中。 (如果我错了告诉我)

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

最佳答案

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

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

关于python - 时间序列分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45574454/

相关文章:

python pandas merge_asof groupby

Python Docker API 需要知道 attach(**kwargs) 到容器的语法

python - 将 IP 列表转换为相应 IP 范围的列表 (python)

python - 随机森林 : Effect of Number of Samples in Each Class

R ggplot2 在一个图中绘制多个时间序列

python - django-import-export 外部管理员

python - 如何用卷积神经网络表示多标签分类中的空类?

c - 如何理解Locality Sensitive Hashing?

python - 从 pandas 中的不规则时间序列生成规则时间序列

python - 在 python 中聚合时间序列