ValueError:模型的特征数必须与输入匹配模型 n_features 为 3001,输入 n_features 为 5

问题描述

我使用随机森林回归器来训练我的模型。输入测试数据是一个主题行(使用 NLP 预处理)和一个帐户 ID。相关特征“y_or_R”是主题的开放率。我现在想通过提供一个主题行和一个帐户 id 条目来测试模型,但我收到一个错误,即模型的特征数量与输入不匹配。 “df_de”是包含“subject”、“account_id”和“unique_opening_rate”列的数据框。代码如下:

X = df_de[['subject']]
y_or_R = df_de.unique_opening_rate

from sklearn.feature_extraction.text import TfidfVectorizer
vec = TfidfVectorizer(max_features=3000) # change the number of features here for better accuracy
X_trans = vec.fit_transform(X.subject)
X_df = pd.DataFrame(X_trans.toarray(),columns = vec.get_feature_names())

X_or = pd.concat([X_df,df_de['account_id']],axis = 1)

# Let's divide the data to train and test split
from sklearn.model_selection import train_test_split
X_train_R,X_test_R,y_train_R,y_test_R = train_test_split(X_or,y_or_R)

# importing the model
from sklearn.ensemble import RandomForestRegressor
model_or_R = RandomForestRegressor()
model_or_R.fit(X_train_R,y_train_R)

from sklearn.metrics import r2_score
y_pred_or_R = model_or_R.predict(X_test_R)
score_or_R = r2_score(y_test_R,y_pred_or_R)
print(score_or_R)

#single data input of subject line
text = ['ein rabatt für sie']

#transforming the text 
text_trans = vec.fit_transform(text)
text_df = pd.DataFrame(text_trans.toarray(),columns = vec.get_feature_names())

#id input
num = [2.290813]
text_df['id'] = num

model_or_R.predict(text_df)

这个问题的可能解决方案是什么?我实际上想在网络上部署模型来预测电子邮件主题行的打开率。所以,请帮我解决这个问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)