【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
【Python学习系列十三】Python机器学习库scikit-learn实现逻辑回归相关推荐
- Python学习系列 -- 改善 Python 程序的 91 个建议
转载自:https://zhuanlan.zhihu.com/p/32817459 自己写Python也有四五年了,一直是用自己的"强迫症"在维持自己代码的质量,除了Google的 ...
- python机器学习库keras——线性回归、逻辑回归、一般逻辑回归
全栈工程师开发手册 (作者:栾鹏) python数据挖掘系列教程 线性回归 import numpy as npfrom keras.models import Sequential from ker ...
- Python数据分析学习系列 十三 Python建模库介绍
Python数据分析学习系列 十三 Python建模库介绍 资料转自(GitHub地址):https://github.com/wesm/pydata-book 有需要的朋友可以自行去github下载 ...
- 机器学习与Scikit Learn学习库
摘要: 本文介绍机器学习相关的学习库Scikit Learn,包含其安装及具体识别手写体数字案例,适合机器学习初学者入门Scikit Learn. 在我科研的时候,机器学习(ML)是计算机科学领域中最 ...
- Python学习系列(六)(模块)
Python学习系列(六)(模块) Python学习系列(五)(文件操作及其字典) 一,模块的基本介绍 1,import引入其他标准模块 标准库:Python标准安装包里的模块. 引入模块的几种方式: ...
- Python: 学习系列之七:模块、PIPY及Anaconda
系列 Python: 学习系列之一:Python能做什么 Python: 学习系列之二:基础介绍(int/float/string/range/list/tuple/dict/set) Python: ...
- Python学习系列(五)(文件操作及其字典)
Python学习系列(五)(文件操作及其字典) Python学习系列(四)(列表及其函数) 一.文件操作 1,读文件 在以'r'读模式打开文件以后可以调用read函数一次性将文件内容全部读出 ...
- python学习[第十三篇] 条件和循环
python学习[第十三篇] 条件和循环 if语句 单一if 语句 if语句有三个部分构成,关键字if本身,判断结果真假的条件表达式,以及表达式为真或非0是执行的代码 if expression: e ...
- 【python脚本系列】利用mido库解析midi文件
[python脚本系列]利用mido库解析midi文件 Hallo大家好~~我是Lampard猿奋 昨天写了关于Midi数字化乐器接口的文章,介绍了midi是一组代表音乐参数( ...
最新文章
- fiddler手机模拟器抓包_利用Fiddler对Android模拟器网络请求进行抓包
- 写一函数,将两个字符串连接
- Oracle收集用户的权限
- 基金份额净值估值是什么?
- 2016设置方框的尺寸_手把手教你用Word打印标准尺寸的身份证“复印件”
- Java替换中使用正则表达式实现中间模糊匹配
- java8读流_JAVA: Java8流逐行读取文件
- 演示6:python音乐下载器
- 计算机usb接口打开方法,电脑USB接口被禁用如何开启?
- No thread-bound request found: Are you referring to request attributes outside of an actual web requ
- 高红梅:第三章 第二节 身份焦虑与英雄梦
- 异常解决:在实体引用中, 实体名称必须紧跟在 ‘‘ 后面
- 强化学习:Actor-Critic、SPG、DDPG、MADDPG
- linux设置多线程优先级
- 风云三国2.4问鼎天下修改作弊大全
- python安装xgboost的方法
- 我的计算机学习体系计划
- servlce和tomcat
- Android平台值得关注的开源项目
- 学习笔记:python人脸识别4-考勤系统
热门文章
- oracle 入参含多个值,oracle几个检查点参数的含义与区别
- 理论应用实例水杯_PID理解起来很难?系统讲解PID控制及参数调节,理论加实际才好...
- 基础正规表示法字符汇整 (characters)
- 了解JavaScript核心精髓(三)
- [daily] 使用diff和patch打补丁
- [ACM] hdu 1228 A+B (字符串处理)
- 深度探索c++对象模型读书笔记:Data语意学-Data Member的绑定
- installshield学习笔记
- 2016/12summary
- 一种基于openflow的虚拟化层软件flowvisor的API测试