Files
FYS-STK4155/doc/MathFoundationML/PythonCode_Regressions.ipynb
T
Morten Hjorth-Jensen 69cf4fa816 adding notes
2024-01-07 17:01:32 +01:00

85 lines
2.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Documents for the Linear, Ridge and Lasso regressions in the sklearn package:\n",
"\n",
"Linear: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html\n",
"\n",
"Ridge: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html\n",
"\n",
"Lasso: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lasso.html"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.15631421 2.24887843]\n"
]
}
],
"source": [
"from sklearn import linear_model\n",
"import numpy as np\n",
"\n",
"# Form the coefficient matrix of the linear system\n",
"X_transpose = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])\n",
"\n",
"\n",
"# The underlying linear function is y = 1 * x_1 + 2 * x_2 + epsilon\n",
"epsilon = np.random.rand(4,)\n",
"y = np.dot(X_transpose, np.array([1, 2])) + epsilon\n",
"\n",
"\n",
"# Various regression models\n",
"#clf = linear_model.LinearRegression()\n",
"#clf = linear_model.Ridge(alpha=0.1)\n",
"clf = linear_model.Lasso(alpha=0.1)\n",
"\n",
"# Fit the model to the data\n",
"clf.fit(X_transpose, y)\n",
"\n",
"\n",
"# Print the regression coefficients\n",
"print(clf.coef_)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}