How to get predicted classes from y_pred - python

I have trained my model and saved it as json file and then i have saved the wights and load it as h5
Then i have used it in another script in order to feed some records to test the accuracy of the model what i want to do is:
Making the model say what is the predicted label and then i must compare it with the true label from the csv file but I don’t know how to get the predicted values?
I tried this
predict_x= loaded_model(X_test) y_pred=np.argmax(predict_x,axis=1)
And then
for i in y_pred
to loop through y_pred, i thought i could see my y classes which are from 0-4 while actually i found numers like 17,22,13…
Can in one till me what I should do?
Thank you in advance
………………………………………………….

Related

fasttext train_supervised model: get top predicted labels

I have used fasttext train_supervised utility to train a classification model according to their webpage https://fasttext.cc/docs/en/supervised-tutorial.html .
model = fasttext.train_supervised(input='train.txt', autotuneValidationFile='validation.txt', autotuneDuration=600)
After I got the model how could I explore what kind of best parameters for the model like in sklearn after a set of best parameters trained, we could always check the values for these parameters but I could not find any document to explain this.
I also used this trained model to make prediction on my data
model.predict(test_df.iloc[2, 1])
It will return the label with a probability like this
(('__label__2',), array([0.92334366]))
I'm wondering if I have 5 labels, every time when make prediction,is it possible for each text to get all the probability for each label?
Like for the above test_df text,
I could get something like
model.predict(test_df.iloc[2, 1])
(('__label__2',), array([0.92334366])),(('__label__1',), array([0.82334366])),
(('__label__3',), array([0.52333333])),(('__label__0',), array([0.07000000])),
(('__label__4',), array([0.00002000]))
could find anything related to make change to get such prediction results.
Any suggestion?
Thanks.
As you can see here in the documentation, when using predict method, you should specify k parameter to get the top-k predicted classes.
model.predict("Why not put knives in the dishwasher?", k=5)
OUTPUT:
((u'__label__food-safety', u'__label__baking', u'__label__equipment',
u'__label__substitutions', u'__label__bread'), array([0.0857 , 0.0657,
0.0454, 0.0333, 0.0333]))

Using sklearn's roc_auc_score for OneVsOne Multi-Classification?

So I am working on a model that attempts to use RandomForest to classify samples into 1 of 7 classes. I'm able to build and train the model, but when it comes to evaluating it using roc_auc function, I'm able to perform 'ovr' (oneVsrest) but 'ovo' is giving me some trouble.
roc_auc_score(y_test, rf_probs, multi_class = 'ovr', average = 'weighted')
The above works wonderfully, I get my output, however, when I switch multi_class to 'ovo' which I understand might be better with class imbalances, I get the following error:
roc_auc_score(y_test, rf_probs, multi_class = 'ovo')
IndexError: too many indices for array
(I pasted the whole traceback below!)
Currently my data is set up as follow:
y_test (61,1)
y_probs (61, 7)
Do I need to reshape my data in a special way to use 'ovo'?
In the documentation, https://thomasjpfan.github.io/scikit-learn-website/modules/generated/sklearn.metrics.roc_auc_score.html, it says "binary y_true, y_score is supposed to be the score of the class with greater label. The multiclass case expects shape = [n_samples, n_classes] where the scores correspond to probability estimates."
Additionally, the whole traceback seems to hint as using maybe using a more binary array (hopefully that's the right term! I'm new to this!)
Very, very thankful for any ideas/thoughts!
#Tirth Patel provided the right answer, I needed to reshape my test set using one hot encoding. Thank you!

Prediction values in Python

Can someone help me with the code for getting the predicted probability values? My model is working fine and is giving me predictions as 1 and 0 however I need the probability values also. The code is in two python files. The first file uses the training data set to create the map file. The second python file (scoring file) uses the map file on the test data to predict. Can someone let me know the code I should insert to get the probability values.
The below code is from the scoring file and here I need the code the get the probability values
pred = model.predict(X.values)
data["Predicted"] = pred
# I NEED THE CODE HERE TO GET THE PROBABILITY VALUES.
data.to_excel(r'result.xlsx', index=False)
Thanks a lot
Check if your model has predict_proba method.
The usage is same as the same predict method.
prob = model.predict_proba(X.values)
Edit:
Some of the learning model implementations from sklearn provide the predict_proba method. It is not a metric but as I said, a method of the class of the learning model.
For example:
from sklearn.tree import DecisionTreeClassifier
# after split you have X_train,y_train,X_testy_test
model = DecisionTreeClassifier()
model.fit(X_train,y_train)
proba = model.predict_proba(X_test)
I am no longer able to edit my question so putting it here.
Thanks for all the help. I am using random forest model. This is my code and the line 4 below is giving an error. If I remove line 4, the code runs but in the final excel file I do not get the probabilities but only the predictions as 1 and 0. Can someone please let me know how to resolve this error. The last line of the error says
ValueError: Wrong number of items passed 2, placement implies 1
pred = model.predict(X.values)
data["Predicted"] = pred
prob = model.predict_proba(X.values)
data["Pred Value"]= prob - this line causes error
data.to_excel(r'result.xlsx', index=False)'
Thanks

convert predicted sequence back to text in keras?

I am using simple RNN model in keras to predict the categorize of simple text data .But I am unable to converted my predicted sequence in to categories
test_sequences = tok.texts_to_sequences(X)
test_sequences_matrix = sequence.pad_sequences(test_sequences,maxlen=max_len)
classes = model.predict(test_sequences_matrix)
I want to convert classes in to X format (Sequence to text). are there any function from which i can revert back.Thanks in advance
I have seen this but unable to solve to my problem

Tensorflow predictions change as more predictions are made

I'm using tensorflow 0.8.0 and skflow (or now known as learn). My model is very similar to this example but with a dnn as the last layer (similar tot he minst example). Nothing very fancy going on, the model works pretty well on its own. The text inputs are a max of 200 characters and 3 classes.
The problem I'm seeing is when I try to load the model and make many predictions (Usually around 200 predictions or more), I start to see results vary.
For example, my model is already trained and I load it and go through my data and make predictions.
char_processor = skflow.preprocessing.ByteProcessor(200)
classifier = skflow.TensorFlowEstimator.restore('/path/to/model')
for item in dataset:
# each item is an array of strings, ex: ['foo', 'bar', 'hello', 'world']
line_data = np.array(list(char_processor.transform(item)))
res = classifier.predict_proba(line_data)
If I load my classifier and only give it one item to predict upon then quit, it works perfectly. When I continue to make predictions, I start to see weirdness.
What could I be missing here? Shouldn't my model always return the same results for the same data?

Categories

Resources