4
$\begingroup$

I have trained and saved a model :

import numpy as np # load the dataset dataset = np.loadtxt("modiftrain.csv", delimiter=";") # split into input (X) and output (Y) variables X_train = dataset[:,0:5] Y_train = dataset[:,5] from sklearn.naive_bayes import GaussianNB # create Gaussian Naive Bayes model object and train it with the data nb_model = GaussianNB() nb_model.fit(X_train, Y_train.ravel()) # predict values using the training data nb_predict_train = nb_model.predict(X_train) # import the performance metrics library from sklearn import metrics # Accuracy print("Accuracy: {0:.4f}".format(metrics.accuracy_score(Y_train, nb_predict_train))) print() # import the lib to load / Save the model from sklearn.externals import joblib # Save the model joblib.dump(nb_predict_train, "trained-model.pkl") 

Then, i'm loading the model and try to make predictions on a new dataset :

# import the lib to load / Save the model from sklearn.externals import joblib import numpy as np # Load the model nb_predict_train = joblib.load("trained-model.pkl") # load the test dataset df_predict = np.loadtxt("modiftest.csv", delimiter=";") X_train = df_predict nb_predict_train.predict(X_train) print(X_train) 

Here comes the error :

 File "predict01.py", line 14, in <module> nb_predict_train.predict(X_train) AttributeError: 'numpy.ndarray' object has no attribute 'predict' 
$\endgroup$

    1 Answer 1

    6
    $\begingroup$

    You don't want to pickle the predictions but rather the fit.

    Change joblib.dump(nb_predict_train, "trained-model.pkl") to joblib.dump(nb_model, "trained-model.pkl")

    $\endgroup$
    6
    • $\begingroup$Error corrected ! I have one more question, if I want to print my predicted values, what shall I write ? print(X_train) does not seems to give me the rightout put$\endgroup$
      – Jed
      CommentedDec 14, 2017 at 15:50
    • 1
      $\begingroup$X_train is still your loaded dataset so I imagine you received a matrix. If you want to print your predictions try pred = nb_predict_train.predict(X_train) then print(pred)$\endgroup$
      – Tophat
      CommentedDec 14, 2017 at 15:54
    • $\begingroup$Oh ! Yes ! Looks like the right output now. Thx$\endgroup$
      – Jed
      CommentedDec 14, 2017 at 16:11
    • $\begingroup$Shall I push my luck ? And ask for a bit more help ? Now that I have this beautiful array, how can I write it in a csv file, in the column number 6 ?$\endgroup$
      – Jed
      CommentedDec 14, 2017 at 16:20
    • $\begingroup$I added that piece of code : import csv csvfile = "toto.csv" with open(csvfile, "w") as output: writer = csv.writer(output, lineterminator='\n') for val in pred: writer.writerow([val]) and it wrote the csv file. But I don't know how to write it in the column number 6 instead of in the first one....$\endgroup$
      – Jed
      CommentedDec 14, 2017 at 16:50

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.