PCA中n_分量的取值方法

2024-09-30 12:34:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是如何在PCA中获取n_分量的值(n_分量=?)。该项目的背景是使用机器学习算法来预测疾病的阶段。我正在使用sklearn

我的项目中的示例:
主成分分析(n_分量=0.95),准确率为0.72。它生成了53个新组件。
主成分分析(n_分量=0.55),准确率为0.78。它生成了5个新组件

svm_clf04 = SVC(kernel="linear", random_state=42)
start = time.process_time()

# Feature scaling
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(rfecv_forest01_x_train01)

# Dimension reduction
pca = PCA(n_components=0.95, svd_solver='full')  # n_components
x_train_scaled_reduced = pca.fit_transform(x_train_scaled)

print (pca.explained_variance_ratio_)
print (pca.explained_variance_)
print ("Components:",pca.n_components_)

svm_clf04.fit(x_train_scaled_reduced, y_train01)


pred = cross_val_predict(svm_clf04, x_train_scaled_reduced, y_train01, cv=10)


print("Time: ", time.process_time() - start)
print(confusion_matrix(y_train01, pred))
print(classification_report(y_train01, pred))

对于解释方差,互联网上的一些人说0.95是最佳选择。但是如果我减少解释的方差,精确度就会提高。我该如何选择?0.95或更高精度的解释方差


Tags: timecomponentstrainfit分量print方差svm
1条回答
网友
1楼 · 发布于 2024-09-30 12:34:07

我不确定你是否正确使用PCA。如果查看文档,您会发现当解算器为full(我假设为scikit-learn)时,它可以正确解释介于0和1之间的浮点值:

If 0 < n_components < 1 and svd_solver == 'full', select the number of components such that the amount of variance that needs to be explained is greater than the percentage specified by n_components.

同时,默认解算器是auto。我建议重新运行PCA,同时显式指定PCA(n_components=0.95, svd_solver='full')

其次,0.95不是“最佳选择”,我不知道为什么有人会建议它。PCA数量的选择取决于手头的问题,即,如果您进行PCA以能够绘制多维数据,那么您只需要留下2或3个PCA;在大多数其他应用程序中,问题将定义为了简单起见您准备放弃多少数据差异

另一个选项是绘制1、2、3的组合解释方差。。。etc PCA和选择图形产生“扭结”的点,以便添加更多PCA几乎不会增加总体解释方差,即如下所示:

enter image description here

相关问题 更多 >

    热门问题