python - 属性错误 : 'numpy.ndarray' object has no attribute 'values'

标签 python neural-network

我正在尝试构建一个简单的神经网络,您可以在下面找到我的代码。当我运行它时,我收到一条错误消息:

Traceback (most recent call last):
  File "algosofleetNNkuantic2.py", line 41, in <module>
    mlp.fit(X_train, y_train.values.ravel())
AttributeError: 'numpy.ndarray' object has no attribute 'values'

你能告诉我我做错了什么以及我需要做些什么来解决它吗?提前致谢。

完整代码:

import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report, confusion_matrix 
import csv
from sklearn.preprocessing import LabelEncoder


path = "C:\\Users\\YannickLECROART\\Desktop\\modeleimportMD.csv"
file = open(path, newline='')
reader = csv.reader(file)
#commandes pour ouvrir et lire un fichier CSV en tant que modele pour le classifieur

header = next(reader) #la 1ere ligne correspond au titre
modele_X = []
modele_Y =  []
#on crée 2 variables que l'on va remplir des données des colonnes du fichier CSV importé pour le modèle
for row in reader: #on va associer les données des colonnes à des variables

    param1  = float(row[0])
    param2 =  float(row[1])
    param3 = float(row[2])
    param4 = float(row[3])
    param5 = float(row[4])
    param6 = float(row[5])
    resultat = str(row[6])
    modele_X.append([param1,param2,param3,param4,param5,param6]) #on associe ensuite toutes les données collectées à la variable créée plus haut sans tenir compte de la dernière colonne qui correspond au résultat 
    modele_Y.append(resultat) #on associe les données venant de la dernière colonne résultat à la variable créée plus haut et non utilisée pour les prédictions mais utiles pour l'export CSV


le = preprocessing.LabelEncoder()

enc = LabelEncoder().fit(modele_Y)
Y_encode = enc.transform(modele_Y)
 #print(Y_encode)

X_train, X_test, y_train, y_test = train_test_split(modele_X, Y_encode, test_size = 0.20)

mlp = MLPClassifier(hidden_layer_sizes=(10, 10, 10), max_iter=1000)  
mlp.fit(X_train, y_train.values.ravel())

predictions = mlp.predict(X_test)
print(predictions)

print(confusion_matrix(y_test,predictions))  
print(classification_report(y_test,predictions))

最佳答案

在线

mlp.fit(X_train, y_train.values.ravel())

y_trainnumpy.ndarray 类型,并且如错误消息所述

has no attribute 'values'

如果你已经正确编码这个数组,你应该能够简单地使用它

mlp.fit(X_train, y_train)

关于python - 属性错误 : 'numpy.ndarray' object has no attribute 'values' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52873680/

相关文章:

Python:如何为单个迹线添加辅助 x 轴?

python - django.db.utils.OperationalError : server closed the connection unexpectedly

neural-network - 神经网络的显着图(使用 Keras)

python - 尝试将 DataFrame 写入 Feather 时出错。 feather 是否支持列表列?

python - 在 python 中旋转 3d 对象

machine-learning - 如果损失函数乘以一个常数会发生什么?

machine-learning - 回到 XOR 问题的基础 - 从根本上感到困惑

python - 将具有许多二进制数据特征的大量观察结果输入 TensorFlow

python - SSH连接成功后执行命令

machine-learning - 为什么在 Caffe 的示例中要交换 RGB channel ?