Skip to content

Instantly share code, notes, and snippets.

@bcyoungV
Created April 18, 2021 10:57
Show Gist options
  • Save bcyoungV/b8e3a590297b718a56ea5927469e72b7 to your computer and use it in GitHub Desktop.
Save bcyoungV/b8e3a590297b718a56ea5927469e72b7 to your computer and use it in GitHub Desktop.
Regression Linear Simple --Bruno Casaca
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "code",
"source": "#Import required libraries\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom sklearn import datasets, linear_model\nfrom sklearn.metrics import mean_squared_error, r2_score\n",
"execution_count": 1,
"outputs": []
},
{
"metadata": {},
"cell_type": "code",
"source": "# Split the data into training/testing sets\nX_train =np.array([1,2,4,3,5,6,7,8,8])\nX_test = np.array([3,9,7,5])\n# Split the targets into training/testing sets\ny_train = np.array([1,3,6,4,5,5,6,7,6])\ny_test = np.array([5,7,5,3])\n#Plot the data to see how it looks\nplt.scatter(X_train, y_train, color='blue')\n",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "<matplotlib.collections.PathCollection at 0x7f380a2a90d0>"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": "<Figure size 432x288 with 1 Axes>",
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAWoAAAD4CAYAAADFAawfAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAOjklEQVR4nO3dTYwk9X3G8eeZHRD0GkQUWhFhPdPhghRZCotaJM5KKAHHAhuRHHIAtX2IIvXFiiCJZNnaQ+TD5BRZzslSa4ntyB0sm5cLSSyQDEqQYkjPspiXJQc7O8MC9jZKbIw7yov9y6F6xb70THfvVk39auf7kVrV9e+amker2mdr/lW144gQACCvlboDAAB2R1EDQHIUNQAkR1ED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p9Sb8m6VclHbT9qbL2T1Ffoul87+OShhHxRN15FjH9Mfc5SffUHGUnRyTdP533/Yaku2x/vd5Iu4uIt6fLM5KelHRHvYl2dVrS6XN+onpMRXFndq+k4xHxo7qDzPExSf8eEeOI+F9JT0j67bJ2TlFfgukFukcknYyIL9adZze227ZvmL6/VsUB9Ua9qWaLiM9HxKGI6Kj4cfc7EVHaWUnZbB+cXkzWdArh45LS3rUUET+U9KbtW6dDd0tKdwH8Ag8q+bTH1Lak37LdmvbD3SquXZUiTVFPf4nuv0i61fZp239cd6ZdHJH0aRVnfGdvH/pE3aF2cJOkZ21/T9K/qpijTn/bW0P8iqTnbb8s6UVJfx8R36450zx/Imk4PR5uk/SXNefZke2WpN9TcXaa2vSnlMckHZf0iopuLe1x8jS35wEAZktzRg0AmI2iBoDkKGoASI6iBoDkKGoASI6iBoDkKGoASO7/Aep5AfWfuAyDAAAAAElFTkSuQmCC\n"
},
"metadata": {
"needs_background": "light"
}
}
]
},
{
"metadata": {},
"cell_type": "code",
"source": "# Create linear regression object\nregr = linear_model.LinearRegression()\n\n# Train the model using the training sets\nregr.fit(X_train[:, np.newaxis], y_train[:, np.newaxis])\n\n# Make predictions using the testing set\ny_pred = regr.predict(X_test[:, np.newaxis])\n\n# The coefficients\nprint('Coefficients: \\n', regr.coef_)\nprint('Intercept: \\n',regr.intercept_ )\n# The mean squared error\nprint(\"Mean squared error: %.2f\"\n % mean_squared_error(y_test, y_pred))",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "code",
"source": "# Plot outputs\nallXvalues = np.concatenate((X_train,X_test), axis=0)\nallYvalues = np.concatenate((y_train,y_test), axis=0)\nplt.scatter(allXvalues, allYvalues, color='black')\nplt.scatter(X_test,y_pred, color='red')\nplt.plot(X_test, y_pred, color='blue', linewidth=3)\n\nplt.xticks(())\nplt.yticks(())\n\nplt.show()",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.10",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment