PCA降维与线性判别分析--鲍鱼

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()#画出的第八个与前面相差较大则删去第八

 <

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值