【问题标题】:PCA, when applied on new data, performance collapsedPCA,当应用于新数据时,性能崩溃
【发布时间】:2018-05-26 00:55:00
【问题描述】:

我正在使用 PCA 进行降维,我的训练数据有 335 个维度的 1200000 条记录。这是我训练模型的代码

X, y = load_data(f_file1)
valid_X, valid_y = load_data(f_file2)

pca = PCA(n_components=n_compo, whiten=True)
X = pca.fit_transform(X)
valid_input = pca.transform(valid_X)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
clf = DecisionTreeClassifier(criterion='entropy', max_depth=30, 
          min_samples_leaf=2, class_weight={0: 10, 1: 1})  # imbalanced class
clf.fit(X_train, y_train)

print(clf.score(X_train, y_train)*100, 
      clf.score(X_test, y_test)*100,
      recall_score(y_train, clf.predict(X_train))*100, 
      recall_score(y_test, clf.predict(X_test))*100,
      precision_score(y_train, clf.predict(X_train))*100, 
      precision_score(y_test, clf.predict(X_test))*100,
      auc(*roc_curve(y_train, clf.predict_proba(X_train)[:, 1], pos_label=1)[:-1])*100,
      auc(*roc_curve(y_test, clf.predict_proba(X_test)[:, 1], pos_label=1)[:-1])*100)

print(precision_score(valid_y, clf.predict(valid_input))*100, 
      recall_score(valid_y, clf.predict(valid_input))*100,
      accuracy_score(valid_y, clf.predict(valid_input))*100,
      auc(*roc_curve(valid_y, clf.predict_proba(valid_input)[:, 1], pos_label=1)[:-1])*100)

输出是

99.80, 99.32, 99.87, 99.88, 99.74, 98.78, 99.99, 99.46
0.00, 0.00, 97.13, 49.98, 700.69

所以召回率和准确率都是 0。为什么 PCA 似乎不适用于验证数据并且模型是否过度拟合?

【问题讨论】:

  • 您应该只在 X_train 上安装 pca,然后在 X_test 上执行 transform()。目前您正在对整个 X 进行 PCA,然后将其拆分为训练和测试,这将是过拟合的。

标签: python scikit-learn pca


【解决方案1】:

可能是过拟合了,因为

max_depth=30

太多了。

您是如何选择 PCA 维度的?您可以通过特征向量/特征值方法获得的最佳值:

data = data.values
mean = np.mean(data.T, axis=1)
demeaned = data - mean
evals, evecs = np.linalg.eig(np.cov(demeaned.T))
order = evals.argsort()[::-1]

evals = evals[order]

plt.plot(evals)
plt.grid(True)
plt.savefig('_!pca.png')

您通过 x 值选择的最佳值,其中行下降到非常零。

【讨论】:

    猜你喜欢
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    相关资源
    最近更新 更多