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

# 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



Here comes the error :

  File "predict01.py", line 14, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'predict'

1 Answer 1


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

  • $\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
    Dec 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
    Dec 14, 2017 at 15:54
  • $\begingroup$ Oh ! Yes ! Looks like the right output now. Thx $\endgroup$
    – Jed
    Dec 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
    Dec 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
    Dec 14, 2017 at 16:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.