-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutomaticProcess.py
80 lines (60 loc) · 1.71 KB
/
AutomaticProcess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 14 15:09:16 2018
@author: Administrator
"""
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.pipeline import FeatureUnion
from sklearn.linear_model import LogisticRegression
#选择前最好的几种特征
from sklearn.feature_selection import SelectKBest
'''
数据准备和生成模型的Pipeline
糖尿病数据
'''
#读取数据
filename='diabetes.csv'
names=['preg','plas','pres','skin','test','mass','pedi','age','class']
data=pd.read_csv(filename)
#将数据分为输入数据和输出数据
array=data.values
X=array[:,0:8]
y=array[:,8]
num_folds=10
seed=7
kfold=KFold(n_splits=num_folds,random_state=seed)
steps=[]
steps.append(('Standardize',StandardScaler()))
steps.append(('lda',LinearDiscriminantAnalysis()))
model=Pipeline(steps)
result=cross_val_score(model,X,y,cv=kfold)
print('Pipeline LDA:',result.mean())
'''
使用Pipeline特征选择
'''
array=data.values
X=array[:,0:8]
y=array[:,8]
num_folds=10
seed=7
kfold=KFold(n_splits=num_folds,random_state=seed)
#生成FeatureUnion
features=[]
features.append(('pca',PCA()))
#选择前最好的6种特征
features.append(('select_best',SelectKBest(k=6)))
#生成Pipeline
steps=[]
steps.append(('feature_union',FeatureUnion(features)))
steps.append(('logistic',LogisticRegression()))
model=Pipeline(steps)
result=cross_val_score(model,X,y,cv=kfold)
print('使用Pipeline特征选择',result.mean())