I have created a model using scikit learn algorithm.
rf = RandomForestClassifier(n_estimators = 10,random_state=seed)
rf.fit(X_train,Y_train)
shift_id=2099.0
user_id=1402.0
status=['S']
shift_organisation_id=15.0
shift_department_id=20.0
open_positions=71.0
city=['taunton']
role_id=3.0
specialty_id=16.0
years_of_experience=10.0
nurse_zip=2780.0
shifts_zip=2021.0
status = status_encoder.transform(status)
city = city_encoder.transform(city)
X = np.array([shift_id, user_id, status, shift_organisation_id, shift_department_id, open_positions, city, role_id, specialty_id, years_of_experience, nurse_zip, shifts_zip])
location_id = rf.predict(X.reshape(1,-1))
print(location_id)
which gives result like this
[25]
What I understand is 25
is the best prediction value for this model. But I want to get top best 3 values as a result. How can I get it?
In that case prediction result would be like
[23,45,25]
You can you predict_proba
method to return class probabilities and get top 3 values from it ref
rf = RandomForestClassifier(n_estimators = 10,random_state=seed)
rf.fit(X_train,Y_train)
shift_id=2099.0
user_id=1402.0
status=['S']
shift_organisation_id=15.0
shift_department_id=20.0
open_positions=71.0
city=['taunton']
role_id=3.0
specialty_id=16.0
years_of_experience=10.0
nurse_zip=2780.0
shifts_zip=2021.0
status = status_encoder.transform(status)
city = city_encoder.transform(city)
X = np.array([shift_id, user_id, status, shift_organisation_id, shift_department_id, open_positions, city, role_id, specialty_id, years_of_experience, nurse_zip, shifts_zip])
location_id = rf.predict_proba(X.reshape(1,-1))
print(location_id)
You have the predict_proba
method for that, which returns the prediction of the class probabilities.
Lets check on an example using the iris dataset :
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.
y = iris.target
# train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y)
rf = RandomForestClassifier(n_estimators = 10, random_state=10)
rf.fit(x_train,y_train)
If you now call the predict
method, as expected you get the highest probability class:
rf.predict(X_test)
# array([1, 2, 1, 0, 2, 0, 2, 0, 0, 1, 2, ...
However calling predict_proba
you will get the corresponding probabilities:
rf.predict_proba(X_test)
array([[0. , 1. , 0. ],
[0.11 , 0.1 , 0.79 ],
[0. , 0.7 , 0.3 ],
[0.5 , 0.4 , 0.1 ],
[0. , 0.3 , 0.7 ],
[0.5 , 0.2 , 0.3 ],
[0.4 , 0. , 0.6 ],
...
In order to get the highest k
probabilities you could use argsort
and index the corresponding probabilities rf.classes_
:
k = 2
rf.classes_[rf.predict_proba(X_test).argsort()[:,-k:]]
array([[2, 1],
[0, 2],
[2, 1],
[1, 0],
[1, 2],
[2, 0],
[0, 2],
[1, 0],
[1, 0],
[2, 1],
...
In the above can be improved using argpartition
as wer'e only interested in the top k
probabilities:
rf.classes_[rf.predict_proba(X_test).argpartition(range(k))[:,-k:]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.