【问题标题】:How to take a user input and pass that to a predictive model如何获取用户输入并将其传递给预测模型
【发布时间】:2019-06-29 02:04:24
【问题描述】:

我有一个构建预测模型的数据框。数据分为训练和测试,我用的是随机森林分类器。

现在,用户传入一个新数据,需要通过这个模型并给出结果。

这是一个文本数据,下面是数据框:

Description          Category
Rejoin this domain   Network
Laptop crashed       Hardware
Installation Error   Software

代码:

############### Feature extraction ##############
countvec = CountVectorizer()
counts = countvec.fit_transform(read_data['Description'])
df = pd.DataFrame(counts.toarray())
df.columns = countvec.get_feature_names()
print(df)

########## Join with original data ##############
df = read_data.join(df)
a = list(df.columns.values)

########## Creating the dependent variable class for "Category" variable ###########
factor = pd.factorize(df['Category'])
df.Category = factor[0]
definitions = factor[1]
print(df.Category.head())
print(definitions)

########## Creating the dependent variable class for "Description" variable ###########
factor = pd.factorize(df['Description'])
df.Description = factor[0]
definitions_1 = factor[1]
print(df.Description.head())
print(definitions_1)

######### Split into Train and Test data #######################
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.80, random_state = 21)

############# Random forest classification model #########################
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 42)
classifier.fit(X_train, y_train)

######### Predicting the Test set results ##############
y_pred = classifier.predict(X_test)

#####Reverse factorize (converting y_pred from 0s,1s and 2s to original class for "Category" ###############
reversefactor = dict(zip(range(3),definitions))
y_test = np.vectorize(reversefactor.get)(y_test)
y_pred = np.vectorize(reversefactor.get)(y_pred)

#####Reverse factorize (converting y_pred from 0s,1s and 2s to original class for "Description" ###############
reversefactor = dict(zip(range(53),definitions_1))
X_test = np.vectorize(reversefactor.get)(X_test)

【问题讨论】:

  • 用户提供什么数据?您想将该数据附加到您的训练数据中,然后想在该新数据上训练模型吗?您的原始数据以哪种形式存在……我的意思是某种csv 文件或其他格式?

标签: python-3.x pandas dataframe machine-learning user-input


【解决方案1】:

如果您只想对用户数据进行预测,那么我只需加载包含用户数据的新 csv(或其他格式)(确保列与原始训练数据集中的列相同,减去依赖项)显然是可变的),您可以为您的任务提取预测:

user_df = pd.read_csv("user_data.csv")

#insert a preprocessing step if needed to make sure user_df is identical to the original dataset

new_predictions = classifier.predict(user_df)

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 2022-11-25
    • 2015-03-31
    • 1970-01-01
    • 2015-08-23
    相关资源
    最近更新 更多