AttributeError: 'DecisionTreeClassifier' object has no attribute 'n_features_'

Viewed 42

I was trying to deploy my NLP model to Heroku, and I got the following error in the logs upon trying to predict the result of the inputs-

2022-09-07T15:36:35.497488+00:00 app[web.1]: if self.n_features_ != n_features:      
2022-09-07T15:36:35.497488+00:00 app[web.1]: AttributeError: 'DecisionTreeClassifier' object has no attribute 'n_features_'
2022-09-07T15:36:35.498198+00:00 app[web.1]: 10.1.22.85 - - [07/Sep/2022:15:36:35 +0000] "POST /predict HTTP/1.1" 500 290 "https://stocksentimentanalysisapp.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"

This specific line is strange considering I never used a Decision Tree Classifier, only Random Forest-

AttributeError: 'DecisionTreeClassifier' object has no attribute 'n_features_'

The model runs perfectly well in Jupyter Notebook. This issue began only when I tried to deploy it. Here is my model-

import pandas as pd
import pickle


df = pd.read_csv('D:\Sa\Projects\Stock Sentiment Analysis\data\data.csv', encoding='ISO-8859-1')
train = df[df['Date']<'20150101']
test = df[df['Date']>'20141231']

#Removing non-alphabetic characters
data = train.iloc[:,2:27]
data.replace('[^a-zA-Z]', ' ', regex=True, inplace=True)

#Renaming columns to numerical index
idx_list = [i for i in range(25)]
new_index = [str(i) for i in idx_list]
data.columns = new_index

for index in new_index:
    data[index] = data[index].str.lower()

combined_headlines = []
for row in range(0, len(data.index)):
    combined_headlines.append(' '.join(str(x) for x in data.iloc[row, 0:25]))

from sklearn.ensemble import RandomForestClassifier
#Bag of words
from sklearn.feature_extraction.text import CountVectorizer
count_vectorizer = CountVectorizer(ngram_range=(2,2))
train_data = count_vectorizer.fit_transform(combined_headlines)
pickle.dump(count_vectorizer, open('countVectorizer.pkl', 'wb'))

rfc = RandomForestClassifier(n_estimators=200, criterion='entropy')
rfc.fit(train_data, train['Label'])

test_transform = []
for row in range(0, len(data.index)):
    test_transform.append(' '.join(str(x) for x in data.iloc[row, 2:27]))
test_data = count_vectorizer.transform(test_transform)
predictions = rfc.predict(test_data)

# Saving model to disk
pickle.dump(rfc, open('randomForestClassifier.pkl', 'wb'))

Please help me understand what is going wrong.

0 Answers
Related