105 PCA和logistic regression的联合使用

本文用了pipeline将pca和logistic chain起来,并且用了GridSearchCV选择了最佳参数。

1.主要函数介绍

1.1 PCA

当矩阵为n*n时,其运算复杂度为n^3
http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html#sklearn.decomposition.PCA

其主要参数有sklearn.decomposition.PCA(n_components=None, copy=True, whiten=False),其中的主要原理及用法,这篇文章已经讲的非常好了http://blog.csdn.net/u012162613/article/details/42192293
要完成的了解这个函数,还需要彻底的了解主成分分析法。
这篇文章中主要调整的参数是保留的维度及explained_variance_,后者可以理解为对原始信息的保留程度。

1.2 LogisticRegression

具体参见
http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression
本文主要调整是它的惩罚因子C

1.3 axvline

matplotlib.pyplot.axvline(x=0, ymin=0, ymax=1, hold=None, **kwargs)
在图中话一条竖直(vertical)的线

1.4 plt.axes

plt.axes()文中的参数是一个形如[left, bottom, width, height]的列表,这些数值分别指定所创建的Axes对象相对于fig的位置和大小,取值范围都在0到1之间,当然如果不在0-1之间也可以 ,可以试试噢

1.5 numpy.logspace

http://docs.scipy.org/doc/numpy-1.6.0/reference/generated/numpy.logspace.html#numpy.logspace
numpy.logspace(start, stop, num=50, endpoint=True, base=10.0)
默认是以10为底(base) 10**start为 起始值 10 * *stop为中值值,num 为个数的序列
其他
pipeline GridSearchCV等前面102已经写过就不再赘述

2 代码

print(__doc__)


# Code source: Gaël Varoquaux
# Modified for documentation by Jaques Grobler
# License: BSD 3 clause


import numpy as np
import matplotlib.pyplot as plt

from sklearn import linear_model, decomposition, datasets
from sklearn.pipeline import Pipeline
from sklearn.grid_search 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 the PCA spectrum
pca.fit(X_digits)

plt.figure(1, figsize=(4, 3))
plt.clf()
plt.axes([.2, .2, .7, .7])
plt.plot(pca.explained_variance_, linewidth=2)
plt.axis('tight')
plt.xlabel('n_components')
plt.ylabel('explained_variance_')

###############################################################################
# Prediction

n_components = [20, 40, 64]
Cs = np.logspace(-4, 4, 3)

#Parameters of pipelines can be set using ‘__’ separated parameter names:

estimator = GridSearchCV(pipe,
                         dict(pca__n_components=n_components,
                              logistic__C=Cs))
estimator.fit(X_digits, y_digits)
print estimator.best_estimator_
plt.axvline(estimator.best_estimator_.named_steps['pca'].n_components,
            linestyle=':', label='n_components chosen')
plt.legend(prop=dict(size=12))
plt.show()

可以得到最佳参数为

Pipeline(steps=[('pca', PCA(copy=True, n_components=40, whiten=False)), ('logistic', LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
          verbose=0, warm_start=False))])
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值