我使用随机森林回归器来训练我的模型。输入的测试数据是一个主题行(使用 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)
这个问题的可能解决方案是什么?我实际上想在网络上部署模型来预测电子邮件主题行的打开率。所以,请帮我解决这个问题。