威望0
积分7946
贡献0
在线时间763 小时
UID1
注册时间2021-4-14
最后登录2024-11-21
管理员
- UID
- 1
- 威望
- 0
- 积分
- 7946
- 贡献
- 0
- 注册时间
- 2021-4-14
- 最后登录
- 2024-11-21
- 在线时间
- 763 小时
|
[mw_shl_code=applescript,true]# -*- coding: utf-8 -*-
"""
Created on Tue Oct 4 20:43:48 2022
@author: BIT
"""
from sklearn.decomposition import PCA
import os
import numpy as np
path = eval(input('请输入详细输入文件的路径:')) # 去掉输入路径的''
Path = os.path.dirname(path) #返回上级目录
data_select = np.loadtxt(path, dtype=np.float64, delimiter=",", skiprows=1)
X = data_select[:,1:]
y = data_select[:,0]
for i in range(1, X.shape[1]):
Pca = PCA(n_components=i)
Pca.fit(X,y)
n = sum(Pca.explained_variance_ratio_)
if n > 0.99:
break
elif n < 0.99:
i = i+1
pca = PCA(n_components=i)
pca.fit(X,y)
X_Feature = pca.transform(X)
full_path = Path + '/PCA_select.csv' #该目录下创建新文件
PCA_data = np.column_stack((y,X_Feature))
np.savetxt(full_path, PCA_data, delimiter = ',')[/mw_shl_code] |
|