我正在尝试对 3、2 和 1 维的 iris 数据集执行 PCA。我不想使用 sklearn,并且我已经从头开始计算 PCA,但是我在将数据加载到我的图中时遇到了一些麻烦。有什么明显的我忽略/犯了错误吗?
import numpy as np
from numpy.linalg import eig
import pandas as pd
import matplotlib.pyplot as plt
#Read iris data file
data_iris = pd.read_csv(r"Iris.csv")
df = pd.DataFrame(data_iris)
#Separating the series column from the dataset
df["Species"] = pd.Categorical(df['Species'])
color_code = df['Species'].cat.codes
X = df.drop("Species",1)
#Finding the mean
mean = np.mean(X.T, axis = 1)
#Centering the columns
center = X - mean
#Calculating the covariance matric
covariance = np.cov(center.T)
#Finding the eigenvalues and vectors
eigen_values, eigen_vectors = eig(covariance)
#projecting the data
projection = eigen_vectors.T.dot(center.T)
fig = plt.figure()
ax = fig.add_subplot(projection = '3d')
plt.scatter(projection[:,1],projection[:,2],projection[:,3])
plt.show()