绘制机器学习预测模型的回归线,使用Python。

huangapple go评论61阅读模式
英文:

Plot the regression line of the machine learning prediction model in python

问题

我想在下面的数据散点图上绘制机器学习模型的回归线,我到目前为止尝试过的是:

首先,选择任意两个依赖变量并将数据拆分为训练集和测试集。

from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_train = train_test_split(features, labels, test_size=0.25, random_state=42)
GradientBoostingRegressor().fit(X_train, y_train).predict(X_test)

绘制机器学习预测模型的回归线,使用Python。

英文:

I would like to plot the regression line of the machine learning model on the data scatter plot as shown below:

绘制机器学习预测模型的回归线,使用Python。

This is what I have tried so far:

Firstly, take any two dependent variables and split the data to train and test the model.


from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_train = train_test_split(features, labels, test_size = 0.25, random_state = 42)
GradientBoostingRegressor().fit(X_train, y_train).predict(X_test)

答案1

得分: 2

以下是您要翻译的内容:

在训练模型后对输入范围进行预测并按如下方式绘制图形

import matplotlib.pyplot as plt
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split
import numpy as np
a, b = 10, 2
features = np.random.randn(1000, 1)
labels  = a * (features + np.random.randn(*features.shape)) + (b + np.random.randn(*features.shape))

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size = 0.25, random_state = 42)
model = GradientBoostingRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(features.min()-0.5, features.max() + 0.5, 1000)
plt.scatter(features, labels, alpha=0.1)
plt.plot(x_range, model.predict(x_range.reshape(-1, 1)), label="GradientBoostingRegressor()",
          linewidth=2)
plt.xlabel("Features")
plt.ylabel("Label")
plt.title("Title Here")
plt.legend()
plt.show()

希望这对您有帮助!

英文:

After training model, predict its output on the input range, and plot it as follows:

import matplotlib.pyplot as plt
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import train_test_split
import numpy as np
a, b = 10, 2
features = np.random.randn(1000, 1)
labels  = a * (features + np.random.randn(*features.shape)) + (b + np.random.randn(*features.shape))

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size = 0.25, random_state = 42)
model = GradientBoostingRegressor()
model.fit(X_train, y_train)
x_range = np.linspace(features.min()-0.5, features.max() + 0.5, 1000)
plt.scatter(features, labels, alpha=0.1)
plt.plot(x_range, model.predict(x_range.reshape(-1, 1)), label="GradientBoostingRegressor()",
          linewidth=2)
plt.xlabel("Features")
plt.ylabel("Label")
plt.title("Title Here")
plt.legend()
plt.show()

huangapple
  • 本文由 发表于 2023年5月17日 18:50:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定