【Python学习系列十三】Python机器学习库scikit-learn实现逻辑回归
生活随笔
收集整理的這篇文章主要介紹了
【Python学习系列十三】Python机器学习库scikit-learn实现逻辑回归
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
邏輯回歸基礎知識可參考:http://blog.csdn.net/fjssharpsword/article/details/54580552
python內部算法已經實現了,最重要是理解y=f(x)的最小化損失函數并通過梯度下降法求解參數。
這里通過Python機器學習庫scikit-learn實現,代碼如下:
# -*- coding: utf-8 -*-import numpy as np import urllib from sklearn import preprocessing from sklearn.feature_selection import RFE from sklearn.linear_model import LogisticRegression from sklearn import metrics from sklearn.ensemble import ExtraTreesClassifier import timedef main(): #數據加載# load the CSV file as a numpy matrixdataset = np.loadtxt('D:\sample.csv', delimiter=",")# separate the data from the target attributesX = dataset[:,0:4]y = dataset[:,4]#數據標準化# normalize the data attributesnormalized_X = preprocessing.normalize(X)# standardize the data attributesstandardized_X = preprocessing.scale(X)#特征選取#model = LogisticRegression()#create the RFE model and select 3 attributes#rfe = RFE(model, 4)#rfe = rfe.fit(X, y)# summarize the selection of the attributes#print(rfe.support_)#print(rfe.ranking_)model = ExtraTreesClassifier()model.fit(X, y)# display the relative importance of each attributeprint(model.feature_importances_)#模型訓練model = LogisticRegression()model.fit(X, y)print(model)#模型預測# make predictionsexpected = ypredicted = model.predict(X)# summarize the fit of the modelprint(metrics.classification_report(expected, predicted))print(metrics.confusion_matrix(expected, predicted))#執行 if __name__ == '__main__': start = time.clock() main() end = time.clock() print('finish all in %s' % str(end - start)) sample.csv數據特點如下:最后一列是標簽 32,0,445,5,0 68,1,415,5,0 44,1,235,30,1 40,0,444,5,0 83,0,466,5,0 52,1,573,5,0 33,0,445,5,0執行結果:
總結
以上是生活随笔為你收集整理的【Python学习系列十三】Python机器学习库scikit-learn实现逻辑回归的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python学习系列十二】Python
- 下一篇: 【Python学习系列十四】IPytho