I have trained a classifier model and its working nicely in terms of giving prediction(boolian) in my local machine.
% movie data
% [Rating, Production_Budget_million]
movie_data=[1,20]
likelihood_pos=multivariate_normal_pos.pdf(movie_data)
likelihood_neg=multivariate_normal_neg.pdf(movie_data)
post_pos=likelihood_pos*prior_pos/(likelihood_pos*prior_pos+likelihood_neg*prior_neg)
post_neg=likelihood_neg*prior_neg/(likelihood_neg*prior_neg+likelihood_pos*prior_pos)
pred= post_pos>post_neg
pred
The problem I am facing ,after dumping the model as pickle when I am trying to calling the model and test with user data its giving an error"AttributeError: 'numpy.bool_' object has no attribute 'predict'"
I know the result will be boolian as"TRUE/FALSE".
#dumping/saving the model
import pickle
pickle.dump(pred, open('pred.pkl','wb'))
#calling the model
predx = pickle.load(open('pred.pkl','rb'))
#testing with user data
print(predx.predict([[1,20]]))
How can I solve the issue and fined the boolian output?
Related
I've been following Apple's coremltools docs for converting PyTorch segmentation models to CoreML.
While it works fine when we're loading a remote PyTorch model, I'm yet to figure out a working Python script to perform conversions with local/already-downloaded PyTorch models.
The following piece of code throws a TypeError: 'dict' object is not callable
#This works fine: model = torch.hub.load('pytorch/vision:v0.6.0', 'deeplabv3_resnet101',pretrained=True).eval()
model = torch.load('local_model_file.pth')
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0)
with torch.no_grad():
output = model(input_batch)['out'][0] #error here
torch_predictions = output.argmax(0)
There is a SO answer that offers a solution by initialising the model class and loading the state_dict, but I wonder what's the concrete solution when we don't have access to the PyTorch model?
In your code, model is a state dict, which is a dictionary from parameter names to the parameter tensor values. As the linked answer stated, the right way to load a state dict is by (a) creating the model object that the state dict belongs to and then (b) use nn.Module.load_state_dict to load the state dict. To do (a), you need access to the model's class definition. If you don't have that access, then unfortunately I don't see any reliable way to load the state dict.
You might be able to guess what the class' __init__ look like by looking at the parameter names in the state dict (e.g., 'module.stage1.rebnconvin.conv_s1.weight' looks like a convolution). However, even if the guess is correct and the state dict can be loaded, you still need to define the forward method because the state dict only stores the parameters.
I used PyCaret 3.0-rc. I developed model and saved them. after almost 3 weeks, now I loaded model. But when I want to use them :
train_predictions = predict_model(model, data=train)
I got the following error:
AttributeError: 'TransformerWrapper' object has no attribute '_memory_transform'
Can any one help me solve it?
I have a some models and I am trying to fit them all.
At the moment I have tried with a dictionary and fit them:
dictionary_of_models = {'catboost':CatBoostClassifier(random_state=0,), 'logistic_regression':LogisticRegression(random_state=0)}
for model in dictionary_of_models.keys():
print(model)
dictionary_of_models[model]=model.fit(X_train, y_train)
But, even the model is printed out, I receive this error:
model.fit(X_train, y_train)
AttributeError: 'str' object has no attribute 'fit'
What's wrong with the code?
I think that a string is going passed to the fit function instead of a model object, but I don't know I can create a model from a dictionary, except for way I tried.
The problem is that you tried to apply fit to the name you gave the model. YOu have to fit the model not its name.
dictionary_of_models = {'catboost':CatBoostClassifier(random_state=0,),
'logistic_regression':LogisticRegression(random_state=0)}
for name, model in dictionary_of_models.items():
print(name)
dictionary_of_models[model]=model.fit(X_train, y_train)
Complementing Prune's answer, I believe you can avoid items. dictionary_of_model[model] would generate a KeyError given you are not passing the Key of the dictionary, but the value itself.
Please try:
for model in dictionary_of_models:
print(model)
dictionary_of_models[model] = dictionary_of_models[model].fit(X_train, y_train)
i am trying to find a predicted Y value (output is numerical) with x inputs using strings (eg. Business type, Department and Region). After using this :
print(model.predict([['Finance and Control'], ['EMEA'], ['Professional Services']]))
it returned this error : AttributeError: 'numpy.ndarray' object has no attribute 'predict'
import pickle
model = pickle.load(open('model3.pkl','rb'))
print(model.predict([['Finance and Control'], ['EMEA'], ['Professional Services']]))
Sample array after OHE
I ran into a similar issue as this, though without additional context, I'm not sure our errors derive from the same problem. However, the resolution I arrived at may help someone in the future as they go down the SO rabbit hole.
Using sklearn-0.20,
import joblib
model = joblib.load('model.pkl')
model.predict(previously_loaded_data)
Resulted in
AttributeError: 'numpy.ndarray' object has no attribute 'predict'
However, the following allowed me to load the actual model and use its predict method:
from sklearn.externals import joblib
model = joblib.load('model.pkl')
model.predict(previously_loaded_data)
sklearn.externals.joblib is deprecated since sklearn-0.23+, but my use case required sklearn-0.20.
I use the method save_model and load_mode but it don't work.
I have an error : AttributeError: 'GridSearchCV' object has no attribute 'get_config'
I don't know if I use correctly this method. I show my code for take an example:
gridSearch = GridSearchCV(estimator = classifier,
param_grid = parameters,
scoring = "accuracy",
cv = 10)
gridSearch.fit(X_train, y_train)
save_model(gridSearch, filepath = 'monModele.h5')
The result is the error attribute Error. Can you help me to find a solution for this problem or to find an other method to save and load a keras model.
That is because GridSearchCV is not a Keras model, but a module from sklearn that also has a fit function with a similar API.
In order to use save_model and load_model you need the actual Keras model, my guess is it is your classifier. Specifically, an instance of the Model class from Keras.