在预处理和降维之前或之后将 X 拆分为 test/train?机器学习
Split X into test/train before pre-processing and dimension reduction or after? Machine Learning
我已经完成了 Microsoft 的课程 DAT210X - Programming with Python for Data Science。
在为机器学习创建 SVC
模型时,我们鼓励将数据集 X 拆分为 test
和 train
组,使用 sci-kit learn
中的 train_test_split
],在执行 preprocessing
之前,例如scaling
和 dimension reduction
例如PCA/Isomap
。我在下面包含了一个代码示例,它是我使用这种做事方式为给定问题编写的解决方案的一部分。
然而,在将 X 拆分为 test
和 train
之前,X 上的 preprocess
和 PCA/IsoMap
似乎要快得多,并且 accuracy
得分。
我的问题是:
1) 有什么理由不能切出标签(y),对所有的X进行预处理和降维,然后再拆分出来测试和训练?
2) 对所有 X(负 y)进行预处理和降维的得分高于拆分 X 然后进行预处理和降维的得分。为什么会这样?
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.30, random_state=7)
step_c = .05
endpt_c = 2 + step_c
startpt_c = .05
step_g = .001
endpt_g = .1 + step_g
startpt_g = .001
bestscore = 0.0
best_i = 0.0
best_j = 0.0
pre_proc = [
preprocessing.Normalizer(),
preprocessing.MaxAbsScaler(),
preprocessing.MinMaxScaler(),
preprocessing.KernelCenterer(),
preprocessing.StandardScaler()
]
best_proc = ''
best_score = 0
print('running......')
# pre-processing (scaling etc)
for T in pre_proc:
X_train_T = T.fit_transform(X_train)
X_test_T = T.transform(X_test) # only apply transform to X_test!
# dimensionality reduction
for k in range(2, 6):
for l in range(4, 7):
iso = Isomap(n_neighbors = k, n_components = l)
X_train_iso = iso.fit_transform(X_train_T)
X_test_iso = iso.transform(X_test_T)
# SVC parameter sweeping
for i in np.arange(startpt_c,endpt_c, step_c):
# print(i)
for j in np.arange(startpt_g,endpt_g, step_g):
clf = SVC(C=i, gamma=j , kernel='rbf'
# max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)
)
clf.fit(X_train_iso, y_train)
score = clf.score(X_test_iso, y_test)
if bestscore < score:
bestscore = score
best_c = i
best_g = j
best_proc = T
best_n_neighbors = k
best_n_components = l
# Print final variables that gave best score:
print('proc: ' + str(T), 'score:' + str(bestscore), 'C: ' + str(i), 'g: ' + str(j), 'n_neigh: ' + str(k), 'n_comp: ' + str(l))enter code here
关于
1) Is there a reason why we can't slice out the label (y) and perform
pre-processing and dimension reduction on all of X before splitting
out to test and train?
原因是您应该在训练数据上训练您的模型,而不使用任何关于测试数据的信息。如果您在训练模型之前对整个数据(包括测试数据)应用 PCA,那么您实际上使用了测试数据中的一些信息。因此,您无法使用测试数据真正判断模型的行为,因为它不再是看不见的数据。
关于:
2) There was a higher score with pre-processing and dimension
reduction on all of X (minus y) than for splitting X and then
performing pre-processing and dimension reduction. Why might this be?
这完全有道理。您使用了测试数据中的一些信息来训练模型,因此测试数据的分数会更高是有道理的。然而,这个分数并没有真正给出模型在看不见的数据上的行为的估计。
我已经完成了 Microsoft 的课程 DAT210X - Programming with Python for Data Science。
在为机器学习创建 SVC
模型时,我们鼓励将数据集 X 拆分为 test
和 train
组,使用 sci-kit learn
中的 train_test_split
],在执行 preprocessing
之前,例如scaling
和 dimension reduction
例如PCA/Isomap
。我在下面包含了一个代码示例,它是我使用这种做事方式为给定问题编写的解决方案的一部分。
然而,在将 X 拆分为 test
和 train
之前,X 上的 preprocess
和 PCA/IsoMap
似乎要快得多,并且 accuracy
得分。
我的问题是:
1) 有什么理由不能切出标签(y),对所有的X进行预处理和降维,然后再拆分出来测试和训练?
2) 对所有 X(负 y)进行预处理和降维的得分高于拆分 X 然后进行预处理和降维的得分。为什么会这样?
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.30, random_state=7)
step_c = .05
endpt_c = 2 + step_c
startpt_c = .05
step_g = .001
endpt_g = .1 + step_g
startpt_g = .001
bestscore = 0.0
best_i = 0.0
best_j = 0.0
pre_proc = [
preprocessing.Normalizer(),
preprocessing.MaxAbsScaler(),
preprocessing.MinMaxScaler(),
preprocessing.KernelCenterer(),
preprocessing.StandardScaler()
]
best_proc = ''
best_score = 0
print('running......')
# pre-processing (scaling etc)
for T in pre_proc:
X_train_T = T.fit_transform(X_train)
X_test_T = T.transform(X_test) # only apply transform to X_test!
# dimensionality reduction
for k in range(2, 6):
for l in range(4, 7):
iso = Isomap(n_neighbors = k, n_components = l)
X_train_iso = iso.fit_transform(X_train_T)
X_test_iso = iso.transform(X_test_T)
# SVC parameter sweeping
for i in np.arange(startpt_c,endpt_c, step_c):
# print(i)
for j in np.arange(startpt_g,endpt_g, step_g):
clf = SVC(C=i, gamma=j , kernel='rbf'
# max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)
)
clf.fit(X_train_iso, y_train)
score = clf.score(X_test_iso, y_test)
if bestscore < score:
bestscore = score
best_c = i
best_g = j
best_proc = T
best_n_neighbors = k
best_n_components = l
# Print final variables that gave best score:
print('proc: ' + str(T), 'score:' + str(bestscore), 'C: ' + str(i), 'g: ' + str(j), 'n_neigh: ' + str(k), 'n_comp: ' + str(l))enter code here
关于
1) Is there a reason why we can't slice out the label (y) and perform pre-processing and dimension reduction on all of X before splitting out to test and train?
原因是您应该在训练数据上训练您的模型,而不使用任何关于测试数据的信息。如果您在训练模型之前对整个数据(包括测试数据)应用 PCA,那么您实际上使用了测试数据中的一些信息。因此,您无法使用测试数据真正判断模型的行为,因为它不再是看不见的数据。
关于:
2) There was a higher score with pre-processing and dimension reduction on all of X (minus y) than for splitting X and then performing pre-processing and dimension reduction. Why might this be?
这完全有道理。您使用了测试数据中的一些信息来训练模型,因此测试数据的分数会更高是有道理的。然而,这个分数并没有真正给出模型在看不见的数据上的行为的估计。