原理
通过高维向低维投影实现降维,通过评估信息损失实现维度的选择。
sk-learn实现
# Author: Daniel Geng
import csv
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
def ImportData(filePath):
X = [] # 2D arrays [[x11, x12, ...], [], ...[]], each item is a [] which contains columns of each row
Y = [] # 1D vector Stands for class
f = open(filePath)
r = csv.reader(f, delimiter=',')
r.__next__() # Skip header row
for row in r:
rowX = []
for i in row[1:5]:
rowX.append(float(i))
X.append(rowX)
Y.append(row[5])
return (X, Y)
# Format to Numpy
def FormatNumpy(X):
Xformat = np.array(X[:]) # change to numpy format
return Xformat
# Step 1, import raw data
Xvalue, Yvalue = ImportData(r"C:\Users\64134\PycharmProjects\pythonProject\PCA\Data\iris.csv")
# Step 1.1, format to Numpy
X = FormatNumpy(Xvalue)
Y = FormatNumpy(Yvalue)
# Step 1.2, normalization with z-score method
Xvalue_normal = StandardScaler().fit_transform(Xvalue)
print(Xvalue_normal)
# Step 2, PCA
pca = PCA(n_components = 0.9, svd_solver='full')
principalComponents = pca.fit_transform(Xvalue_normal)
print(principalComponents)
# Step 3, plot
df_feature = pd.DataFrame(data = principalComponents, columns = ['Principal component 1', 'Principal component 2'])
df_target = pd.DataFrame(data = Y, columns = ['target'])
df_final = pd.concat([df_feature, df_target [['target']]], axis = 1)
fig = plt.figure(figsize = (8, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
targets = ['setosa', 'versicolor', 'virginica']
colors = ['r', 'g', 'b']
for target, color in zip(targets, colors):
indicesToKeep

本文介绍了PCA主成分分析的原理,通过将高维数据投影到低维空间来减少维度。在sk-learn库中实现PCA,并结合多项式逻辑回归进行实验。结果显示,仅保留2维特征时,模型正确率和覆盖率约在75%。为提升分类效果,使用花瓣长度和宽度的平方项构造高维特征,再进行PCA降维,即使降至1维,分类效果仍保持良好,证明高维特征对分类至关重要。

711

被折叠的 条评论
为什么被折叠?



