转载自:https://blog.csdn.net/piaodexin/article/details/77774550
'''
sklean提供的pipeline来将多个学习器组成流水线,通常流水线的形式为:
将数据标准化的学习器---特征提取的学习器---执行预测的学习器
除了最后一个学习器之外,前面的所有学习器必须提供transform方法,该方法用于数据转化(例如:
归一化,正则化,以及特征提取
'''
from sklearn.datasets import load_digits
from sklearn import cross_validation
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
def test_Pipeline(data):
x_train,x_test,y_train,y_test=data
steps=[('Linear_SVM',LinearSVC(C=1,penalty='l1',dual=False)),
('LogisticRegression',LogisticRegression(C=1))]
pipeline=Pipeline(steps)
pipeline.fit(x_train,y_train)
print('name steps:',pipeline.named_steps)
print('Pipeline Score:',pipeline.score(x_test,y_test))
if __name__=='__main__':
data=load_digits()
X=data.data
y=data.target
test_Pipeline(cross_validation.train_test_split(X,y,test_size=0.25,
random_state=0,stratify=y))
from sklearn.datasets import load_digits
from sklearn import cross_validation
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
'''
工作流程:先进行pca降为,然后使用Logistic回归,来分类
'''
def test_Pipeline(data):
x_train,x_test,y_train,y_test=data
steps=[('PCA',PCA()),
('LogisticRegression',LogisticRegression(C=1))]
pipeline=Pipeline(steps)
pipeline.fit(x_train,y_train)
print('name steps:',pipeline.named_steps)
print('Pipeline Score:',pipeline.score(x_test,y_test))
if __name__=='__main__':
data=load_digits()
X=data.data
y=data.target
test_Pipeline(cross_validation.train_test_split(X,y,test_size=0.25,
random_state=0,stratify=y))
---------------------
作者:飘的心
来源:CSDN
原文:https://blog.csdn.net/piaodexin/article/details/77774550
版权声明:本文为博主原创文章,转载请附上博文链接!