我想根据pycharm中的以下代码绘制pca组件图。
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model, decomposition, datasets
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
logistic = linear_model.LogisticRegression()
pca = decomposition.PCA()
pipe = Pipeline(steps = [('pca',pca), ('logistic', logistic)])
digits = datasets.load_digits()
x_digits = digits.data
y_digits = digits.target
# plot pca spectrum
pca.fit(x_digits)
plt.figure(1, figsize=(4,3))
# clear the current figure
plt.clf()
# add axes
plt.axes([.2,.2,.7,.7])
plt.plot(pca.explained_variance_, linewidth = 2)
plt.xlabel('n_components')
plt.ylabel('explained_variance_')
# prediction
n_comp = [20, 40, 64]
# logspace default is base 10, this is 10^-4 to 10^4
cs = np.logspace(-4, 4, 3)
# parameters of pipelines can be set using '__' separated parameter names:
estimator = GridSearchCV(pipe,
dict(pca__n_components = n_comp,
logistic__C = cs))
estimator.fit(x_digits, y_digits)
plt.axvline(estimator.best_estimator_.named_steps['pca'].n_components,
linestyle = ':',label = 'n_compoenents chosen')
plt.legend(prop = dict(size = 12))
plt.axis('tight')
plt.show()
我在spyder中尝试了相同的代码,但效果却令人吃惊。
pycharm plot设置有什么问题?spyder和pycharm都使用python 3.5。
慕森卡
相关分类