#-*- coding: utf-8 -*- import pandas as pd filename = 'C:/Users/Administrator/Desktop/demo/data/bankloan.xls' data = pd.read_excel(filename) x = data.iloc[:,:8].as_matrix() y = data.iloc[:,8].as_matrix() from sklearn.linear_model import LogisticRegression as LR from sklearn.linear_model import RandomizedLogisticRegression as RLR rlr = RLR() #建立随机逻辑回归模型,筛选变量 rlr.fit(x, y) #训练 rlr.get_support() #获取特征结果 print(u'通过随机逻辑回归筛选特征结束') print('有效特征为' +','.join(list(data.columns[rlr.get_support()]))) x = data[data.columns[rlr.get_support()]].as_matrix() #筛选好特征 lr = LR() #建立逻辑回归模型 lr.fit(x, y) #用筛选后的特征进行训练 print(u'通过逻辑回归训练结束') print(u'模型的平均正确率:%s' % lr.score(x, y)) #