Sitemap
Raksheshashank

Raksheshashank

Linear Regression Model tutorial

Feb 20, 2022

--

Waiter_Tips_Prediction_with_Machine_Learning

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go

df=pd.read_csv(“tips.csv”)
df.head()

fig=px.scatter(data_frame=df,x=’total_bill’,y=’tip’,
size=”size”,color=’day’,trendline=’ols’)
fig.show()

fig=px.scatter(data_frame=df,x=’total_bill’,y=’tip’,
size=”size”,color=’sex’,trendline=’ols’)
fig.show()

Get Raksheshashank’s stories in your inbox

Join Medium for free to get updates from this writer.

fig=px.scatter(data_frame=df,x=’total_bill’,y=’tip’,
size=”size”,color=’time’,trendline=’ols’)
fig.show()

figure=px.pie(df,
values=’tip’,
names=’sex’,hole=0.5 )
figure.show()

figure=px.pie(df,
values=’tip’,
names=’smoker’,hole=0.5)
figure.show()

df[‘sex’]= df[‘sex’].map({“Female”:10,”Male”:20})
df[‘smoker’] =df[‘smoker’].map({“No”:1,”Yes”:0})
df[‘day’]= df[‘day’].map({“Thur”:0,”Fri”:1,”Sat”:2,”Sun”:3})
df[‘time’]= df[‘time’].map({“Lunch”:1,”Dinner”:0})
df.head()

x = np.array(df[[“total_bill”, “sex”, “smoker”, “day”,
“time”, “size”]])
y = np.array(df[“tip”])

from sklearn.model_selection import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(x, y,
test_size=0.2,
random_state=42)
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(xtrain, ytrain)

features = np.array([[20, 0, 1, 1, 1, 4]])
model.predict(features)

--

--