日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python学习系列十三】Python机器学习库scikit-learn实现逻辑回归

發布時間:2025/4/16 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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

執行結果:


[ 0.15453444 0.00727297 0.63061708 0.2075755 ] LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,penalty='l2', random_state=None, solver='liblinear', tol=0.0001,verbose=0, warm_start=False)precision recall f1-score support0.0 0.66 0.87 0.75 2684981.0 0.66 0.35 0.45 188407avg / total 0.66 0.66 0.63 456905[[234680 33818][123182 65225]] finish all in 12.8994016037


《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的【Python学习系列十三】Python机器学习库scikit-learn实现逻辑回归的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。