Berilganlarni intellektual tahlili fanidan Ko’p chiziqli regressiya
Download 238.13 Kb.
|
sarvinoz
- Bu sahifa navigatsiya:
- Koeffitsientlar qanchalik muhim
Misol:{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import math\n", "import statistics\n", "import pandas as pd\n", "from matplotlib import pyplot as plt\n", "import seaborn as sns\n", "import numpy as np\n", "from scipy import stats\n", "from sklearn.linear_model import LinearRegression\n", "from sklearn import metrics\n", "import scikitplot as skplt\n", "from sklearn import tree\n", "from sklearn.datasets import make_regression\n", "from sklearn.metrics import mean_squared_error\n", "import statsmodels.api as sm" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Download the data from http://www.saedsayad.com/datasets/BikeRental.zip\n", "trainset=pd.read_csv('C:\\\\Users\\\\Lenovo\\\\Documents\\\\Python Codes\\\\BikeRental\\\\bike_rental_train.csv')\n", "testset=pd.read_csv('C:\\\\Users\\\\Lenovo\\\\Documents\\\\Python Codes\\\\BikeRental\\\\bike_rental_test.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "trainset.shape\n", "testset.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "trainset.columns\n", "testset.columns" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# training\n", "cols=['temp', 'humidity', 'windspeed']\n", "tr=trainset.dropna(axis=0,how='any')\n", "X_train=tr[cols]\n", "y_train=tr['bike_rent_count']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mlr = LinearRegression()\n", "mlr.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# show the model parameters\n", "print(mlr.intercept_, mlr.coef_, mlr.score(X_train, y_train))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# testing\n", "cols=['temp', 'humidity', 'windspeed']\n", "ts=testset.dropna(axis=0,how='any')\n", "X_test=ts[cols]\n", "y_test=ts['bike_rent_count']\n", "y_pred = mlr.predict(X_test)\n", "print('R-squared of multiple linear regression on test set: {:.2f}'.format(mlr.score(X_test, y_test)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# mean squared error\n", "mse = mean_squared_error(y_test, y_pred)\n", "print(\"MSE: \", mse)\n", "print(\"RMSE: \", mse**(1/2.0)) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot original and predicted values\n", "x_ax = range(len(y_test))\n", "plt.plot(x_ax, y_test, label=\"original\")\n", "plt.plot(x_ax, y_pred, label=\"predicted\")\n", "plt.title(\"test and predicted data\")\n", "plt.xlabel('X-axis')\n", "plt.ylabel('Y-axis')\n", "plt.legend(loc='best',fancybox=True, shadow=True)\n", "plt.grid(True)\n", "plt.show() " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# residulas\n", "res=y_test-y_pred\n", "sns.histplot(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sns.boxplot(x=res, showmeans=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu, std = stats.norm.fit(res)\n", "fig, ax = plt.subplots()\n", "\n", "# plot the residuals\n", "sns.histplot(x=res, ax=ax, stat=\"density\", linewidth=0, kde=True)\n", "ax.set(title=\"Distribution of residuals\", xlabel=\"residual\")\n", "\n", "# plot corresponding normal curve\n", "xmin, xmax = plt.xlim() # the maximum x values from the histogram above\n", "x = np.linspace(xmin, xmax, 100) # generate some x values\n", "p = stats.norm.pdf(x, mu, std) # calculate the y values for the normal curve\n", "sns.lineplot(x=x, y=p, color=\"orange\", ax=ax)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Q-Q plot\n", "sm.qqplot(res, line='s')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# scatter plot\n", "plt.scatter(y_test,y_pred)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.11.0 64-bit", "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.11.0" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "e956c49ae0caa224b5fdd754cbc353d7e61c6c61f7329ee680c7f7b4f1b71a8e" } }, "nbformat": 4, "nbformat_minor": 2} Download 238.13 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling