在PyStruct中拟合SSVM模型时出现IndexError

我在使用pystruct Python模块进行讨论线程中帖子分类的结构化学习问题时,遇到了一个问题。当尝试训练OneSlackSSVM以与LinearChainCRF一起使用时出现了问题。我按照文档中的OCR示例进行操作,但似乎无法在SSVM上调用.fit()方法。以下是我遇到的错误:

Traceback (most recent call last):File "<ipython-input-47-da804d135818>", line 1, in <module>ssvm.fit(X_train, y_train)File "/Users/kylefth/anaconda/lib/python2.7/site-  packages/pystruct/learners/one_slack_ssvm.py", line 429, in fitjoint_feature_gt = self.model.batch_joint_feature(X, Y)File "/Users/kylefth/anaconda/lib/python2.7/site-       packages/pystruct/models/base.py", line 40, in batch_joint_feature      joint_feature_ += self.joint_feature(x, y)File "/Users/kylefth/anaconda/lib/python2.7/site-    packages/pystruct/models/graph_crf.py", line 197, in joint_featureunary_marginals[gx, y] = 1IndexError: index 7 is out of bounds for axis 1 with size 7

以下是我编写的代码。我尝试按照文档示例中的数据结构进行数据组织,其中整体数据结构是一个dict,包含datalabelsfolds的键。

from pystruct.models import LinearChainCRFfrom pystruct.learners import OneSlackSSVM# 打印整体数据结构的键print threads.keys()>>> ['folds', 'labels', 'data']# 创建模型实例crf = LinearChainCRF()ssvm = OneSlackSSVM(model=crf)# 按照示例将数据分成训练和测试集X, y, folds = threads['data'], threads['labels'], threads['folds']X_train, X_test = X[folds == 1], X[folds != 1]y_train, y_test = y[folds == 1], y[folds != 1]# 打印数据和标签中第一个元素的维度print X[0].shape, y[0].shape>>> (8, 211), (8,)# 拟合ssvm模型ssvm.fit(X_train, y_train)>>> 见上面的错误

在尝试拟合模型后直接出现了上述错误。X_trainX_testy_trainy_test的所有实例都有211列,并且所有标签维度似乎与其对应的训练和测试数据相匹配。任何帮助将不胜感激。


回答:

我认为你做的一切都是正确的,这是https://github.com/pystruct/pystruct/issues/114。你的标签y需要从0到n_labels开始。我认为你的标签是从1开始的。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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