python - MLP分类

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

我是机器学习的新手,正在开发一个 python 应用程序,该应用程序使用数据集对扑克牌进行分类,我将发布该数据集的片段。似乎效果不太好。我收到以下错误:

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))

以下是我分别使用的训练和测试数据的数据集片段: 列车数据 enter image description here 测试数据 enter image description here

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

我想知道的是显示某种准确率或某种函数,例如分类报告。引导我走向正确的方向将会有很大的帮助!

最佳答案

我认为您在这里收到错误是因为您错误地使用了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作为第一个参数(不是一维数组)。相反,您需要传递测试数据的真实手牌,并将其与经过训练的分类器的预测手牌进行比较。因此,这可能有效:

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

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

相关文章:

python-3.x - python : How to set n previous rows as True if row x is True in a DataFrame

python - 加入数据框——一个有多索引列,另一个没有

matlab - 带 softmax 激活的神经网络

python - 识别网站上的链接,仅在使用屏幕视觉效果时单击它

python - 如何使用层次聚类将聚类分配给新的观察结果(测试数据)?

Python JSON 纽约时报 API

python - 名称错误 : name 'history' is not defined

python - 使用棉花糖而不重复我自己

python - 如何在 Python3 中找到按数字顺序排列的列表中的数字?

python - 从多个列表读取到 Pandas Dataframe 中