Python: 为机器学习格式化时间序列数据

我正在处理NFL比赛位置跟踪数据,其中每个比赛有多个行数据。我希望按以下方式组织我的数据:

x_train = [[a1,b1,c1,…],[a2,b2,c2,…],…,[an,bn,cn,…]]y_train = [y1,y2,…,yn]

其中x_train包含比赛的跟踪数据,y_train包含比赛的结果。

我看到使用IMDb数据进行情感分析的示例,使用Keras的LSTM模型,我想用我的跟踪数据尝试同样的方法。但是,我在格式化x_train时遇到了问题。

for  rows in plays.itertuples():    #print(getattr(rows, 'gameId'), gameMax)    play = isolatePlay(week, getattr(rows, 'gameId'), getattr(rows, 'playId'))    train_x[count] = play    count+=1    print("train_x", train_x)def isolatePlay(data, gameNum, playNum):mod = data[data['gameId'] == gameNum]return mod[mod['playId'] == playNum]

上面的代码遍历我的比赛数据,获取一个比赛,然后找到该比赛的跟踪数据(isolatePlay)。我希望它能将比赛数据添加到我的train_x数组中。我之前的所有尝试都没有成功,结果得到一个数组[a1,b1,c1,…,a2,b2,c2,…],只是将比赛数据简单地添加到一个数组中。

我尝试了多种方法,例如使用append方法train_x = np.append(train_x, [play], axis=0),但都没有成功。

如何才能正确地实现我为机器学习格式化这些数据的目标呢?

编辑:

我尝试了这个:

 first = Truefor  rows in plays.itertuples():    #print(getattr(rows, 'gameId'), gameMax)    play = isolatePlay(week, getattr(rows, 'gameId'), getattr(rows, 'playId'))    if (first):        train_x = play        first = False    else:       train_x = train_x.append([play])

但仍然得到一个没有为不同比赛分隔的数组。

我也尝试了这个:

 first = Truefor  rows in plays.itertuples():    #print(getattr(rows, 'gameId'), gameMax)    play = isolatePlay(week, getattr(rows, 'gameId'), getattr(rows, 'playId'))    if (first):        train_x = [play]        first = False    else:       train_x = train_x.append([play])    print("train_x", train_x)

这在train_x.append([play])上抛出了错误

AttributeError: ‘NoneType’ object has no attribute ‘append’

编辑2(解决方案):

按照答案的建议将train_x变成一个列表,使用train_x.append(play)有效

 first = Truefor  rows in plays.itertuples():    #print(getattr(rows, 'gameId'), gameMax)    play = isolatePlay(week, getattr(rows, 'gameId'), getattr(rows, 'playId'))    if (first):        train_x = [play]        first = False    else:        train_x.append(play)        #train_x[len(train_x):] = [play]也有效    print("train_x", train_x)

输出结果是一个包含不同长度数据框的列表


回答:

我之前使用过Keras的LSTM层,这似乎是一个非常有趣的应用。我很乐意帮助,但格式化数据以用于LSTM层有很多需要考虑的地方,在正确运行之前,我想澄清一下这个应用的目标。

位置比赛数据,是指球员在场上的位置吗?

比赛结果数据,这是比赛的结果吗,例如获得/失去的码数,传球/跑动比赛等?

你希望从中得到什么样的值?(分类或数值)

编辑/回答:

在列表上使用.append()方法来添加元素。

train_x = []for  rows in plays.itertuples():     play = isolatePlay(week, getattr(rows, 'gameId'), getattr(rows, 'playId'))     train_x.append([play])     count=len(train_x)

我在评论中提到,你可能需要研究其他模型/层来应用于这个项目,但我希望这能帮助解决你目前的问题。如果没有

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注