英文:
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)
英文:
I would like to plot the regression line of the machine learning model on the data scatter plot as shown below:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论