3

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))
1
  • .values is generally used to extract the numpy array from the pandas object. But in your code here, you are not even using pandas anywhere. Commented Oct 23, 2018 at 14:01

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

2

I had the same error but this worked

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.