如何解决让我所有的预测都偏向于二进制分类
我正在训练一个包含8个特征的模型,这些特征使我们能够预测房间出售的可能性。
-
区域:房间所属的区域(整数,取值介于1到10之间)
-
日期:停留日期(1-365之间的整数,这里我们只考虑一日请求)
-
工作日:星期几(1到7之间的整数)
-
公寓:房间是整个公寓(1)还是仅一个房间(0)
-
#beds:房间中的床位数(1-4之间的整数)
-
评论:卖家的平均评论(1到5之间的连续变量)
-
图片质量:房间图片的质量(0到1之间的连续变量)
-
价格:房间的历史价格(连续变量)
-
接受:此帖子最后是否被接受(有人接受,为1)或不接受(0)*
“接受”列为“ y”。因此,这是一个二进制分类。
- 我已经完成了
OneHotEncoder
的分类数据。 - 我已对数据进行归一化。
- 我修改了以下
RandomRofrest
参数:
-
Max_depth
:在16点达到峰值 -
n_estimators
:峰值为300 -
min_samples_leaf
: 高峰在2 -
max_features
:对AUC没有影响。
AUC达到0.7889的峰值。我还能做些什么来增加它?
这是我的代码
import pandas as pd
import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import make_pipeline
from sklearn.compose import make_column_transformer
from sklearn.model_selection import train_test_split
df_train = pd.read_csv('case2_training.csv')
# Exclude ID since it is not a feature
X,y = df_train.iloc[:,1:-1],df_train.iloc[:,-1]
y = y.astype(np.float32)
# Split the data
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.05,shuffle=False)
ohe = OneHotEncoder(sparse = False)
column_trans = make_column_transformer(
(OneHotEncoder(),['Region','Weekday','Apartment']),remainder='passthrough')
X_train = column_trans.fit_transform(X_train)
X_test = column_trans.fit_transform(X_test)
# Normalization
from sklearn.preprocessing import MaxAbsScaler
mabsc = MaxAbsScaler()
X_train = mabsc.fit_transform(X_train)
X_test = mabsc.transform(X_test)
X_train = X_train.astype(np.float32)
X_test = X_test.astype(np.float32)
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
RF = RandomForestClassifier(min_samples_leaf=2,random_state=0,n_estimators=300,max_depth = 16,n_jobs=-1,oob_score=True,max_features=i)
cross_val_score(RF,X_train,cv=5,scoring = 'roc_auc').mean()
RF.fit(X_train,y_train)
yhat = RF.predict_proba(X_test)
print("AUC:",roc_auc_score(y_test,yhat[:,-1]))
# Run the prediction on the given test set.
testset = pd.read_csv('case2_testing.csv')
testset = testset.iloc[:,1:] # exclude the 'ID' column
testset = column_trans.fit_transform(testset)
testset = mabsc.transform(testset)
yhat_2 = RF.predict_proba(testset)
final_prediction = yhat[:,-1]
但是,所有来自“ final_prediction”的概率均低于0.45,基本上,该模型认为所有样本均为0。 有人可以帮忙吗?
解决方法
您正在测试集上使用column_trans.fit_transform
,这将完全覆盖训练期间安装的功能。基本上,数据现在采用的是您的训练模型无法理解的格式。
一旦在训练集上进行了训练,则之后只需使用column_trans.transform
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。