import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.decomposition import PCA
data = pd.read_csv("abalone.data")
data.columns = ['Sex', 'Length', 'Diameter', 'Height',
'Whole weight', 'Shucked weight', 'Viscera weight',
'Shell weight', 'Rings']
y = pd.DataFrame(data,columns=['Sex'])
X=data[['Length', 'Diameter', 'Height',
'Whole weight', 'Shucked weight', 'Viscera weight',
'Shell weight', 'Rings']]
X=np.array(X) #将数据储存于矩阵中
from sklearn.decomposition import PCA
pca = PCA(n_components=8)
pca.fit(X)
print("\n特征方差百分比:",pca.explained_variance_ratio_)
结果:
特征方差百分比: [9.77011099e-01 2.22693010e-02 2.81354455e-04 2.42233878e-04
9.78633807e-05 4.55098674e-05 3.88842581e-05 1.37538404e-05]
不降维时候的平行坐标图:
#变量关系可视化平行坐标图
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv("abalone.data")
array = data.iloc[:,1:9].values
array1=data.iloc[:,1:8].values
plt.figure(figsize=(10,6))
for i in range(array.shape[0]):
point = array[i,:]
plt.plot(point)
plt.xticks(np.arange(8),('l','2','3','4','5','6','7','8'),fontsize = 14)
plt.show()
for i in range(array1.shape[0]):
point = array1[i,:]
plt.plot(point)
plt.xticks(np.arange(7),('l','2','3','4','5','6','7'),fontsize = 13)
plt.show()#画出的第八个与前面相差较大则删去第八
<