机器学习学习吴恩达逻辑回归

In the previous stories, I had given an explanation of the program for implementation of various Regression models. As we move on to Classification, isn’t it surprising as to why the title of this algorithm still has the name, Regression. Let us understand the mechanism of the Logistic Regression and learn to build a classification model with an example.

在先前的故事中 ,我已经解释了用于实现各种回归模型的程序。 当我们继续进行分类时 ,为什么该算法的标题仍然具有名称Regression也不奇怪。 让我们了解Logistic回归的机制,并通过示例学习构建分类模型。

Logistic回归概述 (Overview of Logistic Regression)

Logistic Regression is a classification model that is used when the dependent variable (output) is in the binary format such as 0 (False) or 1 (True). Examples include such as predicting if there is a tumor (1) or not (0) and if an email is a spam (1) or not (0).

Logistic回归是一种分类模型,当因变量(输出)采用二进制格式(例如0(假)或1(真))时使用。 例如,例如预测是否有肿瘤(1)(0)和电子邮件是否为垃圾邮件(1)(0)。

The logistic function, also called as sigmoid function was initially used by statisticians to describe properties of population growth in ecology. The sigmoid function is a mathematical function used to map the predicted values to probabilities. Logistic Regression has an S-shaped curve and can take values between 0 and 1 but never exactly at those limits. It has the formula of 1 / (1 + e^-value).

统计学家最初使用逻辑函数(也称为S型函数)来描述生态学中人口增长的特性。 S形函数是用于将预测值映射到概率的数学函数。 Logistic回归具有S形曲线,并且可以采用0到1之间的值,但永远不能精确地处于那些极限。 它的公式为1 / (1 + e^-value)

Logistic Regression is an extension of the Linear Regression model. Let us understand this with a simple example. If we want to classify if an email is a spam or not, if we apply a Linear Regression model, we would get only continuous values between 0 and 1 such as 0.4, 0.7 etc. On the other hand, the Logistic Regression extends this linear regression model by setting a threshold at 0.5, hence the data point will be classified as spam if the output value is greater than 0.5 and not spam if the output value is lesser than 0.5.

Logistic回归是线性回归模型的扩展。 让我们用一个简单的例子来理解这一点。 如果我们要分类电子邮件是否为垃圾邮件,则应用线性回归模型,我们将只能获得0到1之间的连续值,例如0.4、0.7等。另一方面,逻辑回归可以扩展此线性通过将阈值设置为0.5来建立回归模型,因此,如果输出值大于0.5,则数据点将被归类为垃圾邮件;如果输出值小于0.5,则数据点将被归类为垃圾邮件。

In this way, we can use Logistic Regression to classification problems and get accurate predictions.

这样,我们可以使用Logistic回归对问题进行分类并获得准确的预测。

问题分析 (Problem Analysis)

To apply the Logistic Regression model in practical usage, let us consider a DMV Test dataset which consists of three columns. The first two columns consist of the two DMV written tests (DMV_Test_1 and DMV_Test_2) which are the independent variables and the last column consists of the dependent variable, Results which denote that the driver has got the license (1) or not (0).

为了在实际应用中应用Logistic回归模型,让我们考虑由三列组成的DMV测试数据集。 前两列包含两个DMV书面测试( DMV_Test_1DMV_Test_2 ),它们是自变量,最后一列包含因变量, 结果表示驱动程序已获得许可证(1)或没有获得许可证(0)。

In this, we have to build a Logistic Regression model using this data to predict if a driver who has taken the two DMV written tests will get the license or not using those marks obtained in their written tests and classify the results.

在这种情况下,我们必须使用此数据构建Logistic回归模型,以预测已参加两次DMV笔试的驾驶员是否会使用他们在笔试中获得的那些标记来获得驾照,然后对结果进行分类。

步骤1:导入库 (Step 1: Importing the Libraries)

As always, the first step will always include importing the libraries which are the NumPy, Pandas and the Matplotlib.

与往常一样,第一步将始终包括导入NumPy,Pandas和Matplotlib库。

import numpy as npimport matplotlib.pyplot as pltimport pandas as pd

步骤2:导入数据集 (Step 2: Importing the dataset)

In this step, we shall get the dataset from my GitHub repository as “DMVWrittenTests.csv”. The variable X will store the two “DMV Tests ”and the variable Y will store the final output as “Results. The dataset.head(5)is used to visualize the first 5 rows of the data.

在这一步中,我们将从GitHub存储库中获取数据集,名称为“ DMVWrittenTests.csv”。 变量X将存储两个“ DMV测试 ”,变量Y将最终输出存储为“ 结果 dataset.head(5)用于可视化数据的前5行。

dataset = pd.read_csv('https://raw.githubusercontent.com/mk-gurucharan/Classification/master/DMVWrittenTests.csv')X = dataset.iloc[:, [0, 1]].valuesy = dataset.iloc[:, 2].valuesdataset.head(5)>>DMV_Test_1   DMV_Test_2   Results34.623660    78.024693    030.286711    43.894998    035.847409    72.902198    060.182599    86.308552    179.032736    75.344376    1

步骤3:将资料集分为训练集和测试集 (Step 3: Splitting the dataset into the Training set and Test set)

In this step, we have to split the dataset into the Training set, on which the Logistic Regression model will be trained and the Test set, on which the trained model will be applied to classify the results. In this the test_size=0.25 denotes that 25% of the data will be kept as the Test set and the remaining 75% will be used for training as the Training set.

在这一步中,我们必须将数据集分为训练集和测试集,训练集将在该训练集上训练逻辑回归模型,测试集将在训练集上应用训练后的模型对结果进行分类。 在这种情况下, test_size=0.25表示将保留25%的数据作为测试集,而将剩余的75 %的数据用作培训集

from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

步骤4:功能缩放 (Step 4: Feature Scaling)

This is an additional step that is used to normalize the data within a particular range. It also aids in speeding up the calculations. As the data is widely varying, we use this function to limit the range of the data within a small limit ( -2,2). For example, the score 62.0730638 is normalized to -0.21231162 and the score 96.51142588 is normalized to 1.55187648. In this way, the scores of X_train and X_test are normalized to a smaller range.

这是一个附加步骤,用于对特定范围内的数据进行规范化。 它还有助于加快计算速度。 由于数据变化很大,我们使用此功能将数据范围限制在很小的限制(-2,2)内。 例如,将分数62.0730638标准化为-0.21231162,将分数96.51142588标准化为1.55187648。 这样,将X_train和X_test的分数归一化为较小的范围。

from sklearn.preprocessing import StandardScalersc = StandardScaler()X_train = sc.fit_transform(X_train)X_test = sc.transform(X_test)

步骤5:在训练集上训练Logistic回归模型 (Step 5: Training the Logistic Regression model on the Training Set)

In this step, the class LogisticRegression is imported and is assigned to the variable “classifier”. The classifier.fit() function is fitted with X_train and Y_train on which the model will be trained.

在此步骤中,将导入LogisticRegression类并将其分配给变量“ classifier”classifier.fit()函数配有X_trainY_train ,将在其上训练模型。

from sklearn.linear_model import LogisticRegressionclassifier = LogisticRegression()classifier.fit(X_train, y_train)

步骤6:预测测试集结果 (Step 6: Predicting the Test set results)

In this step, the classifier.predict() function is used to predict the values for the Test set and the values are stored to the variable y_pred.

在此步骤中, classifier.predict()函数用于预测测试集的值,并将这些值存储到变量y_pred.

y_pred = classifier.predict(X_test) y_pred

步骤7:混淆矩阵和准确性 (Step 7: Confusion Matrix and Accuracy)

This is a step that is mostly used in classification techniques. In this, we see the Accuracy of the trained model and plot the confusion matrix.

这是分类技术中最常用的步骤。 在此,我们看到了训练模型的准确性,并绘制了混淆矩阵。

The confusion matrix is a table that is used to show the number of correct and incorrect predictions on a classification problem when the real values of the Test Set are known. It is of the format

混淆矩阵是一个表,用于在已知测试集的实际值时显示有关分类问题的正确和不正确预测的数量。 它的格式

Source — Self
来源—自我

The True values are the number of correct predictions made.

True值是做出正确预测的次数。

from sklearn.metrics import confusion_matrixcm = confusion_matrix(y_test, y_pred)from sklearn.metrics import accuracy_score print ("Accuracy : ", accuracy_score(y_test, y_pred))cm>>Accuracy :  0.88>>array([[11,  0],       [ 3, 11]])

From the above confusion matrix, we infer that, out of 25 test set data, 22 were correctly classified and 3 were incorrectly classified. Pretty good for a start, isn’t it?

从上面的混淆矩阵中,我们推断出,在25个测试集数据中,有22个被正确分类,而3个被错误分类。 一开始很不错,不是吗?

步骤8:将实际值与预测值进行比较 (Step 8: Comparing the Real Values with Predicted Values)

In this step, a Pandas DataFrame is created to compare the classified values of both the original Test set (y_test) and the predicted results (y_pred).

在此步骤中,将创建一个Pandas DataFrame来比较原始测试集( y_test )和预测结果( y_pred )的分类值。

df = pd.DataFrame({'Real Values':y_test, 'Predicted Values':y_pred})df>> Real Values   Predicted Values1             10             00             00             01             11             11             01             10             01             10             00             00             01             11             01             10             01             11             01             10             00             01             11             10             0

Though this visualization may not be of much use as it was with Regression, from this, we can see that the model is able to classify the test set values with a decent accuracy of 88% as calculated above.

尽管这种可视化可能不像使用回归那样有用,但是从中我们可以看到,该模型能够以如上所述的88%的准确度对测试集值进行分类。

步骤9:可视化结果 (Step 9: Visualising the Results)

In this last step, we visualize the results of the Logistic Regression model on a graph that is plotted along with the two regions.

在最后一步中,我们在与两个区域一起绘制的图形上可视化Logistic回归模型的结果。

from matplotlib.colors import ListedColormapX_set, y_set = X_test, y_testX1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),             alpha = 0.75, cmap = ListedColormap(('red', 'green')))plt.xlim(X1.min(), X1.max())plt.ylim(X2.min(), X2.max())for i, j in enumerate(np.unique(y_set)):    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],                c = ListedColormap(('red', 'green'))(i), label = j)plt.title('Logistic Regression')plt.xlabel('DMV_Test_1')plt.ylabel('DMV_Test_2')plt.legend()plt.show()
Logistic Regression
逻辑回归

In this graph, the value 1 (i.e, Yes) is plotted in “Red” color and the value 0 (i.e, No) is plotted in “Green” color. The Logistic Regression line separates the two regions. Thus, any data with the two data points (DMV_Test_1 and DMV_Test_2) given, can be plotted on the graph and depending upon which region if falls in, the result (Getting the Driver’s License) can be classified as Yes or No.

在该图中,值1(即“是”)以“ 红色 ”颜色绘制,而值0(即“否”)以“ 绿色 ”颜色绘制。 Logistic回归线将两个区域分开。 因此,具有给定两个数据点(DMV_Test_1和DMV_Test_2)的任何数据都可以绘制在图形上,并且根据所落的区域,结果(获得驾驶执照)可以分类为是或否。

As calculated above, we can see that there are three values in the test set that are wrongly classified as “No” as they are on the other side of the line.

如上所述,我们可以看到测试集中有3个值被错误地归类为“否”,因为它们位于行的另一侧。

Logistic Regression
逻辑回归

结论— (Conclusion —)

Thus in this story, we have successfully been able to build a Logistic Regression model that is able to predict if a person is able to get the driving license from their written examinations and visualize the results.

因此,在这个故事中,我们已经成功地建立了Logistic回归模型,该模型可以预测一个人是否能够通过笔试获得驾照并将结果可视化。

I am also attaching the link to my GitHub repository where you can download this Google Colab notebook and the data files for your reference.

我还将链接附加到我的GitHub存储库中,您可以在其中下载此Google Colab笔记本和数据文件以供参考。

You can also find the explanation of the program for other Classification models below:

您还可以在下面找到其他分类模型的程序说明:

  • Logistic Regression逻辑回归
  • K-Nearest Neighbors (KNN) Classification (Coming Soon)K最近邻居(KNN)分类(即将推出)
  • Support Vector Machine (SVM) Classification (Coming Soon)支持向量机(SVM)分类(即将推出)
  • Naive Bayes Classification (Coming Soon)朴素贝叶斯分类(即将推出)
  • Random Forest Classification (Coming Soon)随机森林分类(即将推出)

We will come across the more complex models of Regression, Classification and Clustering in the upcoming articles. Till then, Happy Machine Learning!

在接下来的文章中,我们将介绍更复杂的回归,分类和聚类模型。 到那时,快乐机器学习!

翻译自: https://towardsdatascience.com/machine-learning-basics-logistic-regression-890ef5e3a272

机器学习学习吴恩达逻辑回归


http://www.taodudu.cc/news/show-863723.html

相关文章:

  • 软件测试 测试停止标准_停止正常测试
  • cloud 部署_使用Google Cloud AI平台开发,训练和部署TensorFlow模型
  • 机器学习多元线性回归_过度简化的机器学习(1):多元回归
  • 机器学习与分布式机器学习_机器学习的歧义
  • 单词嵌入_单词嵌入与单词袋:推荐系统的奇怪案例
  • 自然语言处理综述_自然语言处理
  • 来自天秤座的梦想_天秤座:单线全自动机器学习
  • 数据增强 数据集扩充_数据扩充的抽象总结
  • 贝叶斯优化神经网络参数_贝叶斯超参数优化:神经网络,TensorFlow,相预测示例
  • 如何学习 azure_Azure的监督学习
  • t-sne 流形_流形学习[t-SNE,LLE,Isomap等]变得轻松
  • 数据库课程设计结论_结论
  • 摘要算法_摘要
  • 数据库主从不同步_数据从不说什么
  • android 揭示动画_遗传编程揭示具有相互作用的多元线性回归
  • 检测和语义分割_分割和对象检测-第5部分
  • 如何在代码中将menu隐藏_如何在40行代码中将机器学习用于光学/光子学应用
  • pytorch实现文本分类_使用变形金刚进行文本分类(Pytorch实现)
  • python 机器学习管道_构建机器学习管道-第1部分
  • pandas数据可视化_5利用Pandas进行强大的可视化以进行数据预处理
  • 迁移学习 迁移参数_迁移学习简介
  • div文字自动扩充_文字资料扩充
  • ml是什么_ML,ML,谁是所有人的冠军?
  • 随机森林分类器_建立您的第一个随机森林分类器
  • Python中的线性回归:Sklearn与Excel
  • 机器学习中倒三角符号_机器学习的三角误差
  • 使用Java解决您的数据科学问题
  • 树莓派 神经网络植入_使用自动编码器和TensorFlow进行神经植入
  • opencv 运动追踪_足球运动员追踪-使用OpenCV根据运动员的球衣颜色识别运动员的球队
  • 犀牛建模软件的英文语言包_使用tidytext和textmineR软件包在R中进行主题建模(

机器学习学习吴恩达逻辑回归_机器学习基础:逻辑回归相关推荐

  1. 学习—吴恩达《机器学习》—手敲代码_准备工作之基于Ubuntu系统的 Anaconda(python环境)搭建

    题记--初听不识曲中意,再听已是曲中人. 序曲 一直以来想找个机会与时间去了解一下机器学习.与此同时,吴恩达博士的名字一直在耳边回响,却不知为何如此响彻.后来,在couresa上看到了吴恩达博士的&l ...

  2. 机器学习【吴恩达|周志华|李宏毅|算法】清单 #收藏#

    网络转自:https://blog.csdn.net/julialove102123/article/details/78729602 系列学习记录: 1.吴恩达机器学习系列: 2.李宏毅机器学习课程 ...

  3. 机器学习【吴恩达|周志华|李宏毅|算法】清单

    系列学习记录: 1.吴恩达机器学习系列: 2.李宏毅机器学习课程: 3.周志华 西瓜书: 4.十大算法练习: 5.系列学习资源: 周志华:机器学习书籍 吴恩达 : CS229n机器学习系列 李宏毅教授 ...

  4. 深度学习-吴恩达-笔记-2-神经网络的编程基础

    目录 二分类 逻辑回归 逻辑回归的代价函数 梯度下降法 导数 计算图 使用计算图求导 逻辑回归中的梯度下降 m个样本的梯度下降 向量化 向量化的更多例子 向量化逻辑回归 向量化逻辑回归的梯度输出 Py ...

  5. 机器学习-吴恩达-笔记-13-大规模机器学习

    目录 大型数据集的学习 批量梯度下降 随机梯度下降 小批量梯度下降 随机梯度下降收敛 在线学习 映射化简和数据并行 [此为本人学习吴恩达的机器学习课程的笔记记录,有错误请指出!] 大型数据集的学习 如 ...

  6. 机器学习-吴恩达-笔记-6-应用机器学习的建议

    目录 评估一个假设(假设函数) 模型选择和交叉验证集 诊断偏差和方差 正则化和偏差/方差 学习曲线 决定下一步做什么 [此为本人学习吴恩达的机器学习课程的笔记记录,有错误请指出!] 当我们运用训练好了 ...

  7. 深度学习-吴恩达-笔记-7-超参数调试、Batch正则化和程序框架

    目录 调试处理 为超参数选择合适的范围 超参数调试实践: Pandas VS Caviar 归一化网络的激活函数 将 Batch Norm 拟合进神经网络 Batch Norm 为什么奏效? 测试时的 ...

  8. 深度学习-吴恩达-笔记-5-深度学习的实践层面

    目录 训练.验证.测试集 偏差.方差 机器学习基础 正则化 为什么正则化有利于预防过拟合 dropout正则化 理解dropout 其它正则化方法 归一化输入 梯度消失/梯度爆炸 神经网络的权重初始化 ...

  9. 深度学习-吴恩达-笔记-6-优化算法

    目录 Mini-batch 梯度下降 理解 mini-batch 梯度下降法 指数加权平均数 理解指数加权平均数 指数加权平均的偏差修正 动量梯度下降法 ( Gradient descent with ...

最新文章

  1. 《人工智能爱好者俱乐部》祝大家元旦快乐!
  2. 虚拟社会建设需要“网络社工”助力
  3. 怎么查看python是多少位_python+位数
  4. 关于Verilog中begin···end语句执行顺序
  5. hibernate mysql分页_求struts+hibernate实现mysql分页的详细代码
  6. sql 删除数据_从零开始学SQL:是什么、如何安装、基本语法、表格(创建、删除、更新)、数据(插入、删除、更新)...
  7. java使用tar算法压缩解压缩文件、数据流、byte[]字节数组
  8. 小程序模板订阅操作【小程序订阅消息】
  9. 5分钟在大米云上基于WordPress搭建一个博客
  10. 2021好看小说推荐(持续更新中)
  11. WPF游戏开发——小鸡快跑
  12. shell base64 会自动换行问题
  13. slice,splice和split的区别
  14. VRChat模型上传需要注意些什么?
  15. 3、微信小程序-通信
  16. 《Pro SQL Server Internals, 2nd edition》 作者:Dmitri KorotkevitchP55-58 P62-65 P68-69
  17. Android入门之简易计算器(一)
  18. Kubernetes 三大探针及探针方式
  19. 数学建模的基本办法和步骤 ##数模学习1
  20. docker介绍说明,docker配置代理加速器、docker使用国内镜像仓库

热门文章

  1. 百度地图infoWindow圆角处理
  2. (一)Builder(建造者)模式
  3. schedule vs scheduleAtFixedRate
  4. Android全工程编译不过问题汇总
  5. 解析Infopath生成的XSN结构
  6. 云计算与SaaS的关系 :新计算时代将来临
  7. 虚拟机上php5.6安装教程,手把手安装linux虚拟机教程
  8. Struts 2的OGNL表达式
  9. ajax headers 参数有什么用_动态爬虫(ajax)-爬取bilibili热门视频信息
  10. put url带参数_避免自己写的 url 被diss!建议看看这篇RESTful API简明教程!