简体   繁体   中英

AttributeError: 'numpy.ndarray' object has no attribute 'values'

I am trying to buid a simple Neural Network and you can find my code below. When I run it I get an error message saying:

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'

Could you tell me what I am doing wrong and what I need to do to fix it? Thanks in advance.

Full code:

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

On the line

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

y_train is of type numpy.ndarray and, as staded on the error message

has no attribute 'values'

If you have properly encoded this array, you should be able to use it simply as

mlp.fit(X_train, y_train)

我有同样的错误,但这可行

random_forest.fit(X_train,y_train.ravel())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM