Ad

How To Get Prediction From Trained Random Forest Model?

i have a dataset with two columns user posts (posts) and personality type (type) , i need personality type according to posts using this dataset so i used random forest regression for prediction here is my code:-

df = pd.read_csv('personality_types.csv')

count_vectorizer = CountVectorizer(decode_error='ignore')
X = count_vectorizer.fit_transform(df['posts'])
y = df['type'].values

Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, y, test_size=0.33)

random_forest = RandomForestClassifier(n_estimators=100)
random_forest.fit(Xtrain, Ytrain)
Y_prediction = random_forest.predict(Xtest)

accuracy:

random_forest.score(Xtrain, Ytrain)
acc_random_forest = round(random_forest.score(Xtrain, Ytrain) * 100, 2)
print(round(acc_random_forest,2,), "%")

100%

now i want to get prediction from a custom text how can i achive that ? how can i get personality type of a post separately using this model.

Ad

Answer

Make a new column in the same dataset which is df . Name it as custom_text or user_text or anything else.Take the input store it in that column so that all the rows of that column contains the same values

custom_text = input("Enter Text")
custom_text = count_vectorizer.transform(df['custom_text'])
value_predicted = random_forest.predict(custom_text)
print(value_predicted[0])

as all values of value_predicted contains the same value

Ad
source: stackoverflow.com
Ad