Giter Site home page Giter Site logo

linear-regression's Introduction

Linear-Regression

Linear Regression gives the linear relationship between the set of input variables (x) and the single output variable (y). More specifically, that the target or Output variable (y) can be calculated from a linear combination of the input variables (x).

Applying Linear Regression on Ecommerce Dataset:

Initially we import the necessary libraries as follows:

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

pandas is used to import data and perform alalysis, numpy for mathematical operations,matplotlib & seaborn for data visualization.

Exploratory Data Analysis:

After successfully loading the data with df = pd.read_csv('Ecommerce Customers') command, we get overview of the data by displaying the first 5 rows using df.head() or df.sample(5) to select randomly 5 rows.

.describe() method gives the discriptive statastics of dataset's distribution excluding NaN (Non Numerical) values. Using .info() method, we learn the shape of object types of our data and we see that there are 500 rows of data with no null values where 5 columns are of float datatype and 3 are Object datatype.

sns.heatmap(df.corr(),annot=True) gives the correlation of columns.

image

From the above correlation heatmap we can interpret that 'Length of Membership' and 'Time on App' is 81% & 50% correlated with 'Yearly Amount Spent' respectively.

sns.pairplot(df) plots multiple pairwise bivariate distributions in the dataset.

pairplot

We could see a linear relation between 'Length of Membership' and 'Yearly Amount Spent' also between 'Time on App' and 'Yearly Amount Spent'.

As there are no missing values we can now proceed to Build the model.

Training LinearRegression Model:

Firstly we split Features (X) and Lables (y) as below.

X = df[['Avg. Session Length', 'Time on App','Time on Website', 'Length of Membership']]
y = df['Yearly Amount Spent']

Then we split into test and train datasets using the following code

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size = 0.3,random_state = 101)

Well now we are ready to fit the Linear Model using LinearRegression

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(x_train,y_train)

The training for our linear regression model is completed. The complexity of using more than one independent variable to predict a dependent variable by fitting a best linear relationship is carried out by LinearRegression.fit(x_train,y_train) method.

Prediction:

We predict the values for our testing set (x_test) and save it in the variable y_pred

y_pred = lr.predict(x_test)

We now check the accuracy score to check how well our model has performed.

from sklearn.metrics import r2_score
r2_score(y_test,y_pred)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.