【问题标题】:MLP ClassificationMLP分类
【发布时间】:2017-09-03 19:40:07
【问题描述】:

我是机器学习的新手,我正在开发一个 python 应用程序,它使用我将发布 sn-ps 的数据集对扑克手进行分类。它似乎不能很好地工作。我收到以下错误:

File "C:Testing.py", line 32, in <module>
    print(classification_report(training_data, predictions))
  File "C:Anaconda3\lib\site-packages\sklearn\metrics\classification.py", line 1391, in classification_report
    labels = unique_labels(y_true, y_pred)
  File "C:\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py", line 84, in unique_labels
    raise ValueError("Mix type of y not allowed, got types %s" % ys_types)
ValueError: Mix type of y not allowed, got types {'multiclass-multioutput', 'multiclass'}

这是我设法创建的代码:

import pandas as pnd
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report,confusion_matrix

training_data = pnd.read_csv("train.csv")
print(training_data)
training_data['id'] = range(1, len(training_data) + 1)  # For 1-base index
print(training_data)

test_data = pnd.read_csv("test.csv")
result = pnd.DataFrame(test_data['id'])
print(result)
test_data = test_data.drop(['id'], axis=1)

training_datafile = training_data
labels = training_datafile['hand']
features = training_datafile.drop(['id', 'hand'], axis=1)
scaler = StandardScaler()
# Fit only to the training data
scaler.fit(training_datafile)
X_train = scaler.transform(training_datafile)
X_test = scaler.transform(training_datafile)
mlp = MLPClassifier(hidden_layer_sizes=(100, 100, 100))
mlp.fit(features, labels)
predictions = mlp.predict(test_data)
len(mlp.coefs_)
len(mlp.coefs_[0])
len(mlp.intercepts_[0])
result.insert(1, 'hand', predictions)
result.to_csv("./ANNTEST.csv", index=False)
print(classification_report(training_data, predictions))

以下是我分别使用训练和测试数据的数据集的sn-ps: 训练数据 测试数据

该程序基本上可以正常工作,我正在设法预测扑克手牌,如下所示:

我想知道的是显示某种准确度百分比或某种功能,如分类报告。引导我朝着正确的方向前进会很有帮助!

【问题讨论】:

  • 如果您在加载数据时明确将参数标头设置为 0 是否有效? test_data = pnd.read_csv("test.csv", header=0)
  • 不,我仍然遇到同样的错误..

标签: python pandas machine-learning scikit-learn neural-network


【解决方案1】:

我认为您在这里遇到错误是因为您错误地使用了classification_report。我们来看看documentation

classification_report(y_true, y_pred, ...)

y_true : 1d array-like, or label indicator array / sparse matrix    
         Ground truth (correct) target values.

y_pred : 1d array-like, or label indicator array / sparse matrix
         Estimated targets as returned by a classifier.

您将training_data 作为第一个参数(不是一维数组)传递。相反,您需要通过测试数据的 true 手,将其与训练有素的分类器的预测手进行比较。因此,这可能会起作用:

print(classification_report(test_data["hand"], predictions))

【讨论】:

  • return self._engine.get_loc(self._maybe_cast_indexer(key)) 文件“pandas\index.pyx”,第 132 行,在 pandas.index.IndexEngine.get_loc (pandas\index.c:4433 ) 文件“pandas\index.pyx”,第 154 行,在 pandas.index.IndexEngine.get_loc (pandas\index.c:4279) 文件“pandas\src\hashtable_class_helper.pxi”,第 732 行,在 pandas.hashtable.PyObjectHashTable .get_item (pandas\hashtable.c:13742) 文件“pandas\src\hashtable_class_helper.pxi”,第 740 行,在 pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696) KeyError: 'hand'
  • 判断错误信息,KeyError: 'hand'hand不包含在test_data中。我不知道hand 的实际值保存在您的数据中的什么位置。在任何情况下,您都必须传递测试数据的 true 值。
猜你喜欢
  • 2017-09-07
  • 2018-10-20
  • 2019-10-24
  • 1970-01-01
  • 2018-11-28
  • 2019-06-04
  • 2017-03-12
  • 2019-02-01
  • 2019-02-01
相关资源
最近更新 更多