1 回答
TA贡献1946条经验 获得超3个赞
我们可以分配get_params()给一个应该返回类型对象的变量sklearn.decomposition.pca.PCA。有了这个,我们就可以访问分解的所有方法和属性。
from sklearn.datasets import load_breast_cancer
import numpy as np
import pandas as pd
from sklearn.decomposition import FastICA, PCA
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
#Convert the dataset to data frame
cancer = load_breast_cancer()
data = np.c_[cancer.data, cancer.target]
columns = np.append(cancer.feature_names, ["target"])
df = pd.DataFrame(data, columns=columns)
#Split data into train and test
X = df.iloc[:, 0:30].values
Y = df.iloc[:, 30].values
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.25, random_state = 0)
#Create a pipeline
n_comp = 12
clf = Pipeline([('pca', PCA(n_comp)), ('RandomForest', RandomForestClassifier(n_estimators=100))])
clf.fit(X_train, Y_train)
### --- ###
pca = clf.get_params()['pca']
type(pca)
#sklearn.decomposition.pca.PCA
pca.explained_variance_ratio_
#array([9.81327198e-01, 1.67333696e-02, 1.73934848e-03, 1.05758996e-04,
# 8.29268494e-05, 6.34081771e-06, 3.75309113e-06, 7.08990845e-07,
# 3.16742542e-07, 1.75055859e-07, 7.11274270e-08, 1.43003803e-08])
pca.components_.shape
#(12, 30)
希望这可以帮助。
添加回答
举报