PCA主成分分析及其实现

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

原理

通过高维向低维投影实现降维,通过评估信息损失实现维度的选择。

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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值