我当前的数据框看起来像这样:
salary job title Raiting Company_Name Location Seniority Excel_needed0 100 SE 5 apple sf vp 01 120 DS 4 Samsung la Jr 12 230 QA 5 google sd Sr 1
现在,在对多个类别应用了sklearn的OneHotEncoding后,我得到了一个满意的模型分数,并且我想基于它们的字符串值进行预测,例如:model.predict('SE','5','apple','ca','vp','1')
,而不是尝试输入成千上万个基于one-hot编码数据框的0和1。该如何做呢?
回答:
你需要保存所有处理过程,并编写一个函数来使用它。
这里是一个基本的示例:
title_encoder = LabelEncoder()title_encoder.fit(train['job title'])def predict(model, data, job_title_column, encoder): data[job_title_column] = encoder.transform(data[job_title_column]) prediction = model.predict(data) return predictionpredictions = predict(model, data, 'job title', title_encoder)
你也可以尝试使用Pipeline: https://scikit-learn.org/stable/modules/compose.html