(一)、获取数据,处理数据
# -*- coding: utf-8 -*-
#(一)加载数据
import numpy as np
import urllib
url = "http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data"
raw_data = urllib.urlopen(url)
dataset = np.loadtxt(raw_data,delimiter=',')#读入数据,用逗号分隔
X = dataset[:,0:7] #把0到7列全部行存入X
y = dataset[:,8] #第八列存入y
#(二)数据归一化
from sklearn import preprocessing #调用sklearn机器学习库,预处理数据
scaled_X = preprocessing.scale(X)
normalized_X = preprocessing.normalize(X)
standardized_X = preprocessing.scale(X)
#(三)特征选择
from sklearn import metrics
from sklearn.ensemble import ExtraTreesClassifier
model = ExtraTreesClassifier()
model.fit(X,y)
print(model.feature_importances_)#反馈每个特征重要程度
(二)常见算法的使用
(1)逻辑回归
from sklearn import metrics
from sklearn.ensemble import ExtraTreesClassifier
model = ExtraTreesClassifier()
model.fit(X,y)
print(model.feature_importances_)#反馈每个特征重要程度
from sklearn.linear_model import LogisticRegression #回归模型中调用逻辑回归
model = LogisticRegression()
model.fit(X,y)
print ('MODEL')
print model
#进行预测
expected = y #正确答案,期望
predicted = model.predict(X)
#训练模型
print ('Result')
print (metrics.classification_report(expected,predicted))#输出结果
print (metrics.confusion_matrix(expected,predicted))