调用python的sklearn实现Logistic Reression算法

本文介绍了如何利用python的sklearn库实现Logistic Regression算法,详细讲解了模型的使用和参数设置。通过LogisticRegression类的fit和predict方法进行训练和预测。尽管默认参数已能满足需求,但作者表达了对自定义权重向量输出的兴趣,这促使他考虑自行实现LR算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调用python的sklearn实现Logistic Reression算法

    

    先说如何实现,其中的导入数据库和类、方法的关系,之前不是很清楚,现在知道了。。。   


from numpy import * 
from sklearn.datasets import load_iris     # import datasets

# load the dataset: iris
iris = load_iris() 
samples = iris.data
#print samples 
target = iris.target 

# import the LogisticRegression
from sklearn.linear_model import LogisticRegression 

classifier = LogisticRegression()  # 使用类,参数全是默认的
classifier.fit(samples, target)  # 训练数据来学习,不需要返回值

x = classifier.predict([5, 3, 5, 2.5])  # 测试数据,分类返回标记

print x 

#其实导入的是sklearn.linear_model的一个类:LogisticRegression, 它里面有许多方法
#常用的方法是fit(训练分类模型)、predict(预测测试样本的标记)

#不过里面没有返回LR模型中学习到的权重向量w,感觉这是一个缺陷


    上面使用的


classifier = LogisticRegression()  # 使用类,参数全是默认的

是默认的,所有的参数全都是默认的,其实我们可以自己设置许多。这需要用到官方给定的参数说明,如下:

sklearn.linear_model.LogisticRegression

class  sklearn.linear_model. LogisticRegression ( penalty='l2'dual=Falsetol=0.0001C=1.0fit_intercept=True, intercept_scaling=1class_weight=Nonerandom_state=None )

Logistic Regression (aka logit, MaxEnt) classifier.

In the multiclass case, the training algorithm uses a one-vs.-all (OvA) scheme, rather than the “true” multinomial LR.

This class implements L1 and L2 regularized logistic regression using the liblinear library. It can handle both dense and sparse input. Use C-ordered arrays or CSR matrices containing 64-bit floats for optimal performance; any other input format will be converted (and copied).

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值