You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.1 KiB
129 lines
3.1 KiB
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ccd1edc5",
|
|
"metadata": {},
|
|
"source": [
|
|
"### In order to create a regression model using the Linnerud dataset, you first load the data, then select one of the exercise variables (for example, sit-ups) as the input feature and one of the physiological variables (for example, waistline) as the output variable, fit a regression model such as LinearRegression, and then assess it and, if desired, plot the relationship; you carry out this procedure for each combination of exercise and physiological variables that you are interested in.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "4fc227b7",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "plaintext"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from sklearn.datasets import load_linnerud\n",
|
|
"\n",
|
|
"data = load_linnerud(as_frame=True)\n",
|
|
"X = data.data # exercises\n",
|
|
"y = data.target # physiological measures"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "fad548fe",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "plaintext"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"X_situps = X[[\"Situps\"]] # 2D DataFrame\n",
|
|
"y_waist = y[\"Waist\"] # Series"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "325f344e",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "plaintext"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from sklearn.model_selection import train_test_split\n",
|
|
"\n",
|
|
"X_train, X_test, y_train, y_test = train_test_split(\n",
|
|
" X_situps, y_waist, test_size=0.25, random_state=42\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "930b8b74",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "plaintext"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from sklearn.linear_model import LinearRegression\n",
|
|
"\n",
|
|
"model = LinearRegression()\n",
|
|
"model.fit(X_train, y_train)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "71cee5ee",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "plaintext"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from sklearn.metrics import mean_squared_error, r2_score\n",
|
|
"\n",
|
|
"y_pred = model.predict(X_test)\n",
|
|
"mse = mean_squared_error(y_test, y_pred)\n",
|
|
"r2 = r2_score(y_test, y_pred)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e2533995",
|
|
"metadata": {
|
|
"vscode": {
|
|
"languageId": "plaintext"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import matplotlib.pyplot as plt\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"plt.scatter(X_situps, y_waist, label=\"Data\")\n",
|
|
"x_vals = np.linspace(X_situps.min(), X_situps.max(), 100).reshape(-1, 1)\n",
|
|
"plt.plot(x_vals, model.predict(x_vals), color=\"red\", label=\"Fit\")\n",
|
|
"plt.xlabel(\"Situps\")\n",
|
|
"plt.ylabel(\"Waist\")\n",
|
|
"plt.legend()\n",
|
|
"plt.show()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|