回归分析详解:一文说清回归、线性回归、非线性回归、Logistic回归

文章大纲:

  1. 1. 回归分析是什么?

  2. 2. 回归、线性回归、非线性回归、Logistic回归的不同?

  3. 3. 实际的应用例子与python代码实现

回归分析是什么?

回归分析是一种统计分析方法,用于预测一个连续变量与一个或多个其他变量之间的关系。回归分析通过建立模型来预测因变量(被预测变量)与自变量(预测变量)之间的关系,从而预测因变量的值。回归分析有多种形式,包括线性回归、非线性回归、Logistic回归等。

回归、线性回归、非线性回归、Logistic回归的不同?

回归:是一种统计分析方法,用于预测一个连续变量与一个或多个其他变量之间的关系。
线性回归:是回归分析的一种,假设因变量与自变量之间存在线性关系,即因变量与自变量的关系可以用一条直线来描述。
非线性回归:是回归分析的一种,假设因变量与自变量之间不存在线性关系,即因变量与自变量的关系不能用一条直线来描述,需要用更复杂的函数模型来描述。
Logisic回归:是回归分析的一种,专门用于预测二元分类结果(例如,预测一个人是否患有疾病),而不是连续变量。该模型建立在Sigmoid函数基础上,该函数可以将任意实数值映射到0~1范围内,表示某个事件的概率。

实际例子解释不同回归方法的应用场景

  • • 线性回归:该方法假设因变量和自变量之间存在线性关系。它最常见的应用场景是预测数值型变量,如预测房价根据面积、房龄等因素。

  • • 非线性回归:该方法假设因变量和自变量之间不存在线性关系,而是存在非线性关系。假如我们希望预测某商家的销售额,我们可以使用非线性回归分析,并通过分析广告费用、宣传渠道、季节等因素与销售额之间的关系,从而预测出销售额的变化趋势。非线性回归的模型可以更好地捕捉复杂的非线性关系,并对预测结果产生更大的影响。

  • • Logistic回归:常常用于二元分类问题,例如:预测某个用户是否会购买某种商品,或预测某个患者是否患有某种疾病。我们可以利用Logistic回归分析不同的因素,如年龄、性别、收入水平等,并预测其对购买行为或疾病状况的影响,从而得出预测结果。

Python代码示例

使用 Python 实现线性回归分析的代码示例:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Generate random data points
np.random.seed(0)
x = np.random.rand(1001)
y = 2 + 3 * x + np.random.rand(1001)

# Fit the data using Linear Regression
reg = LinearRegression().fit(x, y)

# Predict the output for new data points
y_pred = reg.predict(x)

# Plot the data and regression line
plt.scatter(x, y)
plt.plot(x, y_pred, color='red')
plt.show()

# Print the regression coefficients
print("Intercept: ", reg.intercept_)
print("Slope: ", reg.coef_)

使用scikit-learn库在Python中实现非线性回归分析的示例:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# Generating sample data
np.random.seed(0)
X = np.sort(5 * np.random.rand(801), axis=0)
y = np.sin(X).ravel()
y[::5] += 3 * (0.5 - np.random.rand(16))

# Fit a polynomial regression model
poly_features = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly_features.fit_transform(X)

lin_reg = LinearRegression()
lin_reg.fit(X_poly, y)

# Plot the prediction results
plt.scatter(X, y, color='blue')
sort_axis = np.argsort(X, axis=0)
plt.plot(X[sort_axis], lin_reg.predict(X_poly[sort_axis]), color='red')
plt.title("Non-Linear Regression")
plt.xlabel("X")
plt.ylabel("y")plt.show()

本示例使用多项式回归来拟合数据的非线性模型。的 PolynomialFeatures 类用于生成多项式特征,并且LinearRegression 类用于将线性回归模型拟合到多项式特征。对预测结果进行了绘制,并显示了X和y之间的非线性关系。

Python 实现Logistic回归分析的代码示例:

import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

# 导入数据
data = pd.read_csv("data.csv")

# 分离特征和标签
X = data.iloc[:,:-1]
y = data.iloc[:,-1]

# 分割训练数据和测试数据
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

# 训练模型
model = LogisticRegression(solver='lbfgs')
model.fit(X_train, y_train)

# 模型预测
y_pred = model.predict(X_test)

# 模型评估
score = model.score(X_test, y_test)print("准确率为:%.2f%%" % (score*100))

本文使用 文章同步助手 同步

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容