gpt4 book ai didi

python-2.7 - Scikit-Learn One-hot-encode 在训练/测试拆分之前或之后

转载 作者:行者123 更新时间:2023-12-04 01:56:21 28 4
gpt4 key购买 nike

我正在研究使用 scikit-learn 构建模型的两个场景,我无法弄清楚为什么其中一个返回的结果与另一个完全不同。两种情况(我所知道的)之间唯一不同的是,在一种情况下,我一次性(在整个数据上)对分类变量进行单热编码,然后在训练和测试之间进行拆分。在第二种情况下,我在训练和测试之间进行拆分,然后根据训练数据对两组进行单热编码。

后一种情况在技术上更适合判断过程的泛化误差,但这种情况返回的归一化基尼系数与第一种情况相比有很大不同(并且很糟糕 - 基本上没有模型)。我知道第一种情况的基尼系数(~0.33)符合基于此数据构建的模型。

为什么第二种情况会返回如此不同的基尼系数?仅供引用 数据集包含数字和分类变量的混合。

方法一(one-hot 编码整个数据然后拆分)这将返回:Validation Sample Score: 0.3454355044 (normalized gini).

from sklearn.cross_validation import StratifiedKFold, KFold, ShuffleSplit,train_test_split, PredefinedSplit
from sklearn.ensemble import RandomForestRegressor , ExtraTreesRegressor, GradientBoostingRegressor
from sklearn.linear_model import LogisticRegression
import numpy as np
import pandas as pd
from sklearn.feature_extraction import DictVectorizer as DV
from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from sklearn.grid_search import GridSearchCV,RandomizedSearchCV
from sklearn.ensemble import RandomForestRegressor, ExtraTreesRegressor
from scipy.stats import randint, uniform
from sklearn.metrics import mean_squared_error
from sklearn.datasets import load_boston

def gini(solution, submission):
df = zip(solution, submission, range(len(solution)))
df = sorted(df, key=lambda x: (x[1],-x[2]), reverse=True)
rand = [float(i+1)/float(len(df)) for i in range(len(df))]
totalPos = float(sum([x[0] for x in df]))
cumPosFound = [df[0][0]]
for i in range(1,len(df)):
cumPosFound.append(cumPosFound[len(cumPosFound)-1] + df[i][0])
Lorentz = [float(x)/totalPos for x in cumPosFound]
Gini = [Lorentz[i]-rand[i] for i in range(len(df))]
return sum(Gini)

def normalized_gini(solution, submission):
normalized_gini = gini(solution, submission)/gini(solution, solution)
return normalized_gini

# Normalized Gini Scorer
gini_scorer = metrics.make_scorer(normalized_gini, greater_is_better = True)



if __name__ == '__main__':

dat=pd.read_table('/home/jma/Desktop/Data/Kaggle/liberty/train.csv',sep=",")
y=dat[['Hazard']].values.ravel()
dat=dat.drop(['Hazard','Id'],axis=1)


folds=train_test_split(range(len(y)),test_size=0.30, random_state=15) #30% test

#First one hot and make a pandas df
dat_dict=dat.T.to_dict().values()
vectorizer = DV( sparse = False )
vectorizer.fit( dat_dict )
dat= vectorizer.transform( dat_dict )
dat=pd.DataFrame(dat)


train_X=dat.iloc[folds[0],:]
train_y=y[folds[0]]
test_X=dat.iloc[folds[1],:]
test_y=y[folds[1]]


rf=RandomForestRegressor(n_estimators=1000, n_jobs=1, random_state=15)
rf.fit(train_X,train_y)
y_submission=rf.predict(test_X)
print("Validation Sample Score: {:.10f} (normalized gini).".format(normalized_gini(test_y,y_submission)))

方法二(先拆分后one-hot encode)这将返回: Validation Sample Score: 0.0055124452 (normalized gini).
from sklearn.cross_validation import StratifiedKFold, KFold, ShuffleSplit,train_test_split, PredefinedSplit
from sklearn.ensemble import RandomForestRegressor , ExtraTreesRegressor, GradientBoostingRegressor
from sklearn.linear_model import LogisticRegression
import numpy as np
import pandas as pd
from sklearn.feature_extraction import DictVectorizer as DV
from sklearn import metrics
from sklearn.preprocessing import StandardScaler
from sklearn.grid_search import GridSearchCV,RandomizedSearchCV
from sklearn.ensemble import RandomForestRegressor, ExtraTreesRegressor
from scipy.stats import randint, uniform
from sklearn.metrics import mean_squared_error
from sklearn.datasets import load_boston

def gini(solution, submission):
df = zip(solution, submission, range(len(solution)))
df = sorted(df, key=lambda x: (x[1],-x[2]), reverse=True)
rand = [float(i+1)/float(len(df)) for i in range(len(df))]
totalPos = float(sum([x[0] for x in df]))
cumPosFound = [df[0][0]]
for i in range(1,len(df)):
cumPosFound.append(cumPosFound[len(cumPosFound)-1] + df[i][0])
Lorentz = [float(x)/totalPos for x in cumPosFound]
Gini = [Lorentz[i]-rand[i] for i in range(len(df))]
return sum(Gini)

def normalized_gini(solution, submission):
normalized_gini = gini(solution, submission)/gini(solution, solution)
return normalized_gini

# Normalized Gini Scorer
gini_scorer = metrics.make_scorer(normalized_gini, greater_is_better = True)



if __name__ == '__main__':

dat=pd.read_table('/home/jma/Desktop/Data/Kaggle/liberty/train.csv',sep=",")
y=dat[['Hazard']].values.ravel()
dat=dat.drop(['Hazard','Id'],axis=1)


folds=train_test_split(range(len(y)),test_size=0.3, random_state=15) #30% test

#first split
train_X=dat.iloc[folds[0],:]
train_y=y[folds[0]]
test_X=dat.iloc[folds[1],:]
test_y=y[folds[1]]

#One hot encode the training X and transform the test X
dat_dict=train_X.T.to_dict().values()
vectorizer = DV( sparse = False )
vectorizer.fit( dat_dict )
train_X= vectorizer.transform( dat_dict )
train_X=pd.DataFrame(train_X)

dat_dict=test_X.T.to_dict().values()
test_X= vectorizer.transform( dat_dict )
test_X=pd.DataFrame(test_X)


rf=RandomForestRegressor(n_estimators=1000, n_jobs=1, random_state=15)
rf.fit(train_X,train_y)
y_submission=rf.predict(test_X)
print("Validation Sample Score: {:.10f} (normalized gini).".format(normalized_gini(test_y,y_submission)))

最佳答案

虽然前面的评论正确地建议最好先映射整个特征空间,但在您的情况下,训练和测试都包含所有列中的所有特征值。

如果比较vectorizer.vocabulary_两个版本之间,完全一样,所以映射上没有区别。因此,它不会导致问题。

方法 2 失败的原因是因为您的 dat_dict获取 重新排序 执行此命令时按原始索引。

dat_dict=train_X.T.to_dict().values()

换句话说, train_X有一个混洗的索引进入这行代码。当你把它变成 dict , dict order 重新排序为原始索引的数字顺序。这会导致您的训练和测试数据与 y 完全不相关。 .

方法 1 不会遇到此问题,因为您在映射后对数据进行了混洗。

您可以通过添加 .reset_index() 来解决此问题。两次分配 dat_dict在方法 2 中,例如,
dat_dict=train_X.reset_index(drop=True).T.to_dict().values()

这确保在转换为 dict 时保留数据顺序。 .

当我添加那段代码时,我得到以下结果:
- 方法 1:验证样本得分:0.3454355044(标准化基尼系数)
- 方法 2:验证样本得分:0.3438430991(标准化基尼系数)

关于python-2.7 - Scikit-Learn One-hot-encode 在训练/测试拆分之前或之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31506987/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com