Supervised Learning-----Logistic Regression

Logistic Regression

Introduction

Logistic回归是一种二元分类方法。 它可以将数据集中的点划分为两个不同的类或类别。 为简单起见,我们称它们为A类和B类。模型将给出给定点属于B类的概率。如果它低(低于50%),那么我们将它归类为A类。否则,它 属于B类。同样重要的是要注意逻辑回归比具有阈值的线性回归更好用于此目的,因为阈值必须手动设置,这是不可行的。 逻辑回归将改为创建一种S曲线(使用sigmoid函数),这也有助于显示确定性,因为逻辑回归的输出不仅仅是1或0。 这是标准逻辑函数,请注意输出始终在0和1之间,但永远不会达到这些值中的任何一个。

When to Use

对于需要在两个类别之间进行分类的情况,Logistic回归非常有用。 一些好的例子被接受和拒绝的申请人以及竞争中的胜利或失败。 这是一个数据示例表,可以作为逻辑回归的一个很好的候选者。

请注意,学生的成功是由输入决定的,而值是二进制的,因此逻辑回归对这种情况很有效。

How does it work?

Logistic回归使用输入的线性组合,因此多个信息源可以控制模型的输出。 模型的参数是各种特征的权重,并表示它们对结果的相对重要性。 在下面的等式中,您应该识别线性回归中使用的公式。 逻辑回归在其基础上是从线性预测变换到0和1之间的概率的变换。

与线性回归一样,β值是权重,x值是变量输入。 此公式给出了输入属于B类的概率,这是逻辑回归模型的目标。

Multinomial Logistic Regression

到目前为止,我们一直在讨论两种不同输出的情况,例如通过或失败。 但是,如果有两个以上可能的输出怎么办? 数字分类示例怎么样,输出可以是0到9之间的任何数字?

那么,有一种方法可以通过逻辑回归处理它。 当使用scikit-learn库时,如示例代码所示,该设施已经存在。 使用scikit-learn,您可以使用多项模式并在训练数据中提供任意数量的类。 您可以将该方法视为创建多个模型并比较它们的概率,但具体细节超出了本课程的范围。

Code

查看我们的存储库中的逻辑回归示例example

from sklearn.linear_model import LogisticRegression
import numpy as np
import random

#defines the classification for the training data.
def true_classifier(i):
    if i >= 700:
        return 1
    return 0

#Generate a random dataset which includes random scores from 0 to 1000.
x = np.array([ random.randint(0,1000) for i in range(0,1000) ])

#The model will expect a 2D array, so we must reshape
#For the model, the 2D array must have rows equal to the number of samples,
#and columns equal to the number of features.
#For this example, we have 1000 samples and 1 feature.
x = x.reshape((-1, 1))

#For each point, y is a pass/fail for the grade. The simple threshold is arbitrary,
#and can be changed as you would like. Classes are 1 for success and 0 for failure
y = [ true_classifier(x[i][0]) for i in range(0,1000) ]


#Again, we need a numpy array, so we convert.
y = np.array(y)

#Our goal will be to train a logistic regression model to do pass/fail to the same threshold.
model = LogisticRegression(solver='liblinear')

#The fit method actually fits the model to our training data
model = model.fit(x,y)

#Create 100 random samples to try against our model as test data
samples = [random.randint(0,1000) for i in range(0,100)]
#Once again, we need a 2d Numpy array
samples = np.array(samples)
samples = samples.reshape(-1, 1)

#Now we use our model against the samples.  output is the probability, and _class is the class.
_class = model.predict(samples)
proba = model.predict_proba(samples)

num_accurate = 0

#Finally, output the results, formatted for nicer viewing.
#The format is [<sample value>]: Class <class number>, probability [ <probability for class 0> <probability for class 1>]
#So, the probability array is the probability of failure, followed by the probability of passing.
#In an example run, [7]: Class 0, probability [  9.99966694e-01   3.33062825e-05]
#Means that for value 7, the class is 0 (failure) and the probability of failure is 99.9%
for i in range(0,100):
    if (true_classifier(samples[i])) == (_class[i] == 1):
        num_accurate = num_accurate + 1
    print("" + str(samples[i]) + ": Class " + str(_class[i]) + ", probability " + str(proba[i]))
#skip a line to separate overall result from sample output
print("")
print(str(num_accurate) +" out of 100 correct.")

结果:

在该示例中,scikit-learn和numpy用于训练简单的逻辑回归模型。 该模型是基本的,但可扩展。 通过逻辑回归,可以将更多特征无缝地添加到数据集中,简单地作为2D阵列中的列。

代码创建表示训练输入的2D数组,在这种情况下,它是1000 x 1,因为有1000个样本和1个特征。 这些输入的分数为1000分。还会创建一个训练输出数组,基于阈值,分类为1表示传递,0表示失败。 然后,scikit-learn的LogisticRegression类用于将逻辑回归分类器拟合到数据中。 之后,下一步是使用不同的数据集测试准确性。 因此,我们创建另外100个随机样本进行测试,并使用该模型预测它们。

Motivation

为什么要使用逻辑回归? Logistic回归非常适合二元分类,或者分为两类。 Logistic回归也是一种相对简单的方法,利用输入的加权和,类似于线性回归。 逻辑回归也很有用,因为它给出了连续值,表示给定分类正确的概率。 出于这些原因,倡导者说逻辑回归应该是数据科学界首先学到的东西。

Conclusion

Logistic回归建立在线性回归的基础上,将其用途扩展到分类。 虽然它无法分类为两个以上的类,但它仍然有效,并且易于实现。 将逻辑回归视为第一个思考通过/失败方法。 当您只需要数据的通过/失败概率时,逻辑回归是最简单且可能是最佳选择。

机器学习库使用Logistic回归非常简单。 查看存储库中的示例代码并按照说明进行操作。 基本思想是将训练数据作为输入和分类对提供,并且模型将自动构建。 与往常一样,请记住此存储库概述部分中提到的基础知识,因为没有针对机器学习的万无一失的方法。

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九妹123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值