from sklearn.svm import LinearSVC from sklearn.pipeline import Pipeline from sklearn import neighbors, datasets from sklearn.datasets import load_digits from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split def load_diabetes(): #使用 scikit-learn 自带的一个糖尿病病人的数据集 diabetes = datasets.load_diabetes() # 拆分成训练集和测试集,测试集大小为原始数据集大小的 1/4 return train_test_split(diabetes.data,diabetes.target,test_size=0.25,random_state=0) #数据预处理流水线Pipeline模型 def test_Pipeline(X_train,X_test,y_train,y_test): steps=[("Linear_SVM",LinearSVC(C=1,penalty='l1',dual=False)),("LogisticRegression",LogisticRegression(C=1))] pipeline=Pipeline(steps) pipeline.fit(X_train,y_train) print("Named steps:",pipeline.named_steps) print("Pipeline Score:",pipeline.score(X_test,y_test)) # 获取分类数据 X_train,X_test,y_train,y_test=load_diabetes() # 调用 test_Pipeline test_Pipeline(X_train,X_test,y_train,y_test)