当我们必须预测分类(或离散)结果的值时,我们使用逻辑回归。我相信我们使用线性回归来预测给定输入值的结果值。
那么,这两种方法有什么不同呢?
当我们必须预测分类(或离散)结果的值时,我们使用逻辑回归。我相信我们使用线性回归来预测给定输入值的结果值。
那么,这两种方法有什么不同呢?
当前回答
只是补充一下之前的答案。
线性回归
Is meant to resolve the problem of predicting/estimating the output value for a given element X (say f(x)). The result of the prediction is a continuous function where the values may be positive or negative. In this case you normally have an input dataset with lots of examples and the output value for each one of them. The goal is to be able to fit a model to this data set so you are able to predict that output for new different/never seen elements. Following is the classical example of fitting a line to set of points, but in general linear regression could be used to fit more complex models (using higher polynomial degrees):
解决问题
线性回归有两种不同的求解方法:
法方程(直接解题方法) 梯度下降(迭代法)
逻辑回归
是为了解决分类问题,给定一个元素,你必须把它分成N个类别。典型的例子是,例如,给定一封邮件,将其分类为垃圾邮件,或者给定一辆车辆,查找它属于哪个类别(汽车、卡车、货车等)。基本上输出是一组有限的离散值。
解决问题
逻辑回归问题只能通过梯度下降来解决。一般来说,公式与线性回归非常相似,唯一的区别是使用不同的假设函数。在线性回归中,假设的形式为:
h(x) = theta_0 + theta_1*x_1 + theta_2*x_2 ..
其中是我们试图拟合的模型[1,x_1, x_2, ..]为输入向量。在逻辑回归中,假设函数是不同的:
g(x) = 1 / (1 + e^-x)
This function has a nice property, basically it maps any value to the range [0,1] which is appropiate to handle propababilities during the classificatin. For example in case of a binary classification g(X) could be interpreted as the probability to belong to the positive class. In this case normally you have different classes that are separated with a decision boundary which basically a curve that decides the separation between the different classes. Following is an example of dataset separated in two classes.
You can also use the below code to generate the linear regression curve q_df = details_df # q_df = pd.get_dummies(q_df) q_df = pd.get_dummies(q_df, columns=[ "1", "2", "3", "4", "5", "6", "7", "8", "9" ]) q_1_df = q_df["1"] q_df = q_df.drop(["2", "3", "4", "5"], axis=1) (import statsmodels.api as sm) x = sm.add_constant(q_df) train_x, test_x, train_y, test_y = sklearn.model_selection.train_test_split( x, q3_rechange_delay_df, test_size=0.2, random_state=123 ) lmod = sm.OLS(train_y, train_x).fit() lmod.summary() lmod.predict()[:10] lmod.get_prediction().summary_frame()[:10] sm.qqplot(lmod.resid,line="q") plt.title("Q-Q plot of Standardized Residuals") plt.show()
其他回答
在线性回归中,结果(因变量)是连续的。它可以有无限个可能值中的任意一个。在逻辑回归中,结果(因变量)只有有限数量的可能值。
例如,如果X包含以平方英尺为单位的房屋面积,而Y包含这些房屋的相应销售价格,您可以使用线性回归来预测销售价格作为房屋大小的函数。虽然可能的销售价格实际上可能没有任何值,但有很多可能的值,因此可以选择线性回归模型。
相反,如果你想根据房子的大小来预测房子是否会卖到20万美元以上,你会使用逻辑回归。可能的输出是Yes,房子将以超过20万美元的价格出售,或者No,房子不会。
非常同意以上的评论。 除此之外,还有一些不同之处
在线性回归中,残差被假设为正态分布。 在逻辑回归中,残差需要是独立的,但不是正态分布。
线性回归假设解释变量值的恒定变化导致响应变量的恒定变化。 如果响应变量的值代表概率(在逻辑回归中),则此假设不成立。
广义线性模型(GLM)不假设因变量和自变量之间存在线性关系。但在logit模型中,它假设link函数与自变量之间是线性关系。
线性回归和逻辑回归的基本区别是: 线性回归用于预测一个连续的或数值,但当我们寻找预测一个值,是分类逻辑回归进入画面。
二元分类采用逻辑回归。
Linear regression output as probabilities It's tempting to use the linear regression output as probabilities but it's a mistake because the output can be negative, and greater than 1 whereas probability can not. As regression might actually produce probabilities that could be less than 0, or even bigger than 1, logistic regression was introduced. Source: http://gerardnico.com/wiki/data_mining/simple_logistic_regression Outcome In linear regression, the outcome (dependent variable) is continuous. It can have any one of an infinite number of possible values. In logistic regression, the outcome (dependent variable) has only a limited number of possible values. The dependent variable Logistic regression is used when the response variable is categorical in nature. For instance, yes/no, true/false, red/green/blue, 1st/2nd/3rd/4th, etc. Linear regression is used when your response variable is continuous. For instance, weight, height, number of hours, etc. Equation Linear regression gives an equation which is of the form Y = mX + C, means equation with degree 1. However, logistic regression gives an equation which is of the form Y = eX + e-X Coefficient interpretation In linear regression, the coefficient interpretation of independent variables are quite straightforward (i.e. holding all other variables constant, with a unit increase in this variable, the dependent variable is expected to increase/decrease by xxx). However, in logistic regression, depends on the family (binomial, Poisson, etc.) and link (log, logit, inverse-log, etc.) you use, the interpretation is different. Error minimization technique Linear regression uses ordinary least squares method to minimise the errors and arrive at a best possible fit, while logistic regression uses maximum likelihood method to arrive at the solution. Linear regression is usually solved by minimizing the least squares error of the model to the data, therefore large errors are penalized quadratically. Logistic regression is just the opposite. Using the logistic loss function causes large errors to be penalized to an asymptotically constant. Consider linear regression on categorical {0, 1} outcomes to see why this is a problem. If your model predicts the outcome is 38, when the truth is 1, you've lost nothing. Linear regression would try to reduce that 38, logistic wouldn't (as much)2.
它们在解决解决方案方面非常相似,但正如其他人所说,一个(逻辑回归)是用于预测类别“适合”(Y/N或1/0),另一个(线性回归)是用于预测值。
所以如果你想预测你是否有癌症Y/N(或概率)-使用逻辑。如果你想知道你能活多少年,用线性回归吧!