【问题标题】:Scikit use list in train set for MLPMLP 训练集中的 Scikit 使用列表
【发布时间】:2017-12-31 15:58:00
【问题描述】:

我想用以下数据训练一个神经网络(多感知器):

1              2              3             Other Field   Label
[1, 2, 3, 4]   [5, 6, 7, 8]   [9, 10, 11]   1234          5678
etc...

这里的123 是包含列表的列。其他两列只有数值。

只有我不断得到这个:

ValueError: setting an array element with a sequence.

这可能吗?

编辑:
我训练神经网络的代码如下:

from sklearn.neural_network import MLPClassifier
mlp = MLPClassifier(alpha=1e-5, hidden_layer_sizes=(10, 10), random_state=1)
mlp.fit(X_train, y_train)

这是我的火车数据的截图:

而我的标签只是一列数字。

【问题讨论】:

  • 看不到任何出现故障的 scikit 特定代码。也许添加你的代码?
  • 完成@PatrickArtner
  • 很好,数据可能是文本,但您提供了一些“示例数据” - 现在等待并观看 SO 魔术发生 :) +1

标签: python dataframe scikit-learn neural-network training-data


【解决方案1】:

如果您的列表始终具有相同的长度,则只需将每个列表列拆分为四个单独的列,就像描述的那样。 here:

# create a dataset
raw_data = {'score': [1,2,3], 
        'tags': [['apple','pear','guava'],['truck','car','plane'],['cat','dog','mouse']]}
df = pd.DataFrame(raw_data, columns = ['score', 'tags'])
# expand df.tags into its own dataframe
tags = df['tags'].apply(pd.Series)
# rename each variable
tags = tags.rename(columns = lambda x : 'tag_' + str(x))
# join the tags dataframe back to the original dataframe
df = pd.concat([df[:], tags[:]], axis=1)
df.drop('tags', inplace=True, axis=1)

如果不是,最好的答案可能是针对特定问题的。一种方法是通过填充填充值将每个列表扩展到最长列表的长度,然后执行相同操作。

【讨论】:

    猜你喜欢
    • 2016-12-07
    • 2020-08-08
    • 2016-03-04
    • 2021-11-17
    • 2016-03-26
    • 2018-08-18
    • 2015-05-04
    • 2016-10-01
    • 2021-02-06
    相关资源
    最近更新 更多