{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd \n", "from sklearn.linear_model import LinearRegression\n", "from sklearn.model_selection import train_test_split\n", "import sklearn.metrics as sm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.read_excel(\"https://github.com/masterfloss/data/blob/main/socialmedia.xlsx?raw=true\")\n", "X=df[['Linux','Chrome','Twitter']]\n", "Y=df[['Facebook']]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X_train, X_test, y_train, y_test = train_test_split(X, Y,test_size=0.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear_regressor = LinearRegression() \n", "#linear_regressor = LinearRegression(fit_intercept = False) \n", "results=linear_regressor.fit(X_train, y_train)\n", "y_test_pred=results.predict(X_test)\n", "y_train_pred=results.predict(X_train)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"R2 score =\", round(sm.r2_score(y_test, y_test_pred), 2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"R2 score =\", round(sm.r2_score(y_train,y_train_pred), 2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Regression with all data\n", "\n", "linear_regressor = LinearRegression() \n", "results=linear_regressor.fit(X, Y)\n", "Y_pred=results.predict(X)\n", "print(\"R2 score =\", round(sm.r2_score(Y, Y_pred), 2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "results.coef_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "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.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }