使用来自 PCA 的组件进行分类
Classify using components from PCA
我像这样对我的数据集使用 PCA 分析:
from sklearn.decomposition import PCA
pca = PCA(n_components=3)
principalComponents = pca.fit_transform(scale_x)
principalDf = pd.DataFrame(data=principalComponents, columns = ['PC1', 'PC2', 'PC3'])
然后使用 MatPlotLib 可视化结果 - 我可以看到我的两个 类 之间的划分,如下所示:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(principalDf['PC1'].values, principalDf['PC2'].values, principalDf['PC3'].values, c=['red' if m==0 else 'green' for m in y], marker='o')
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_zlabel('PC3')
plt.show()
但是当我使用 SVM 或 Logistic 回归等分类模型时,它无法学习这种关系:
from sklearn.linear_model import LogisticRegression
lg = LogisticRegression(solver = 'lbfgs')
lg.fit(principalDf.values, y)
lg_p = lg.predict(principalDf.values)
print(classification_report(y, lg_p, target_names=['Failure', 'Success']))
precision recall f1-score support
Failure 1.00 0.03 0.06 67
Success 0.77 1.00 0.87 219
accuracy 0.77 286
macro avg 0.89 0.51 0.46 286
weighted avg 0.82 0.77 0.68 286
这可能是什么原因?
首先,使用三个特征PC1、PC2、PC3。附加特征(PC4 ~ PC6),未在图中表示,可能会影响分类结果。
其次,分类器有时并没有像你想象的那样训练好。我建议使用决策树而不是您使用的分类器,因为树是 (horizon) 线性分类器,它会产生您认为的结果。
不管你的结果是否有意义,你在这里做的事情根本上是错误的,那就是在整个数据集上训练分类器并在已见数据[=21=上测试结果].我已经使用 iris 数据集重现了你的问题,并拟合了一个逻辑回归器,对我来说产生了很好的结果:
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
data = load_iris()
X = data.data
y = data.target
pca = PCA(n_components=3)
principalComponents = pca.fit_transform(X)
principalDf = pd.DataFrame(data=principalComponents, columns = ['PC1', 'PC2', 'PC3'])
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(principalDf['PC1'].values,
principalDf['PC2'].values,
principalDf['PC3'].values,
c=[['red', 'green', 'blue'][m] for m in y], marker='o')
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_zlabel('PC3')
plt.show()
现在,如果我们尝试在 X_test
上进行预测,我们会发现混淆矩阵在这种情况下看起来相当不错,这意味着整体思路应该运作良好:
X_train, X_test, y_train, y_test = train_test_split(principalDf, y)
lg = LogisticRegression(solver = 'lbfgs')
lg.fit(X_train, y_train)
y_pred = lg.predict(X_test)
confusion_matrix(y_true=y_test, y_pred=y_pred)
array([[ 9, 0, 0],
[ 0, 12, 1],
[ 0, 0, 16]], dtype=int64)
我像这样对我的数据集使用 PCA 分析:
from sklearn.decomposition import PCA
pca = PCA(n_components=3)
principalComponents = pca.fit_transform(scale_x)
principalDf = pd.DataFrame(data=principalComponents, columns = ['PC1', 'PC2', 'PC3'])
然后使用 MatPlotLib 可视化结果 - 我可以看到我的两个 类 之间的划分,如下所示:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(principalDf['PC1'].values, principalDf['PC2'].values, principalDf['PC3'].values, c=['red' if m==0 else 'green' for m in y], marker='o')
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_zlabel('PC3')
plt.show()
但是当我使用 SVM 或 Logistic 回归等分类模型时,它无法学习这种关系:
from sklearn.linear_model import LogisticRegression
lg = LogisticRegression(solver = 'lbfgs')
lg.fit(principalDf.values, y)
lg_p = lg.predict(principalDf.values)
print(classification_report(y, lg_p, target_names=['Failure', 'Success']))
precision recall f1-score support Failure 1.00 0.03 0.06 67 Success 0.77 1.00 0.87 219 accuracy 0.77 286 macro avg 0.89 0.51 0.46 286 weighted avg 0.82 0.77 0.68 286
这可能是什么原因?
首先,使用三个特征PC1、PC2、PC3。附加特征(PC4 ~ PC6),未在图中表示,可能会影响分类结果。
其次,分类器有时并没有像你想象的那样训练好。我建议使用决策树而不是您使用的分类器,因为树是 (horizon) 线性分类器,它会产生您认为的结果。
不管你的结果是否有意义,你在这里做的事情根本上是错误的,那就是在整个数据集上训练分类器并在已见数据[=21=上测试结果].我已经使用 iris 数据集重现了你的问题,并拟合了一个逻辑回归器,对我来说产生了很好的结果:
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
data = load_iris()
X = data.data
y = data.target
pca = PCA(n_components=3)
principalComponents = pca.fit_transform(X)
principalDf = pd.DataFrame(data=principalComponents, columns = ['PC1', 'PC2', 'PC3'])
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(principalDf['PC1'].values,
principalDf['PC2'].values,
principalDf['PC3'].values,
c=[['red', 'green', 'blue'][m] for m in y], marker='o')
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_zlabel('PC3')
plt.show()
现在,如果我们尝试在 X_test
上进行预测,我们会发现混淆矩阵在这种情况下看起来相当不错,这意味着整体思路应该运作良好:
X_train, X_test, y_train, y_test = train_test_split(principalDf, y)
lg = LogisticRegression(solver = 'lbfgs')
lg.fit(X_train, y_train)
y_pred = lg.predict(X_test)
confusion_matrix(y_true=y_test, y_pred=y_pred)
array([[ 9, 0, 0],
[ 0, 12, 1],
[ 0, 0, 16]], dtype=int64)