Skip to content

Instantly share code, notes, and snippets.

View rafiag's full-sized avatar

Rafi Atha rafiag

View GitHub Profile
print('R2 score:', olsmod.rsquared)
X = df_pie[['price', 'advertising']]
X = sm.add_constant(X) # adding a constant
olsmod = sm.OLS(df_pie['pie_sales'], X).fit()
print(olsmod.summary())
# Prepare data
X = df_pie[['price', 'advertising']].values.reshape(-1,2)
Y = df_pie['pie_sales']
# Create range for each dimension
x = X[:, 0]
y = X[:, 1]
z = Y
xx_pred = np.linspace(4, 9, 30) # range of price values
# Values to predict
price = input('What is the price of the pie? \n')
advertising = input('How much money are you going to spend for advertising? \n')
try:
print('We predict {:.0f} pies will be sold if we sold the pie at ${} and spend ${} at advertising.'.format(
model.predict([[float(price), float(advertising)]])[0],
price,
advertising))
except ValueError:
# Set independent and dependent variables
X = df_pie[['price', 'advertising']]
y = df_pie['pie_sales']
# Initialize model from sklearn and fit it into our data
regr = linear_model.LinearRegression()
model = regr.fit(X, y)
print('Intercept:', model.intercept_)
print('Coefficients:', model.coef_)
# Visualize the data using scatter plot and histogram
sns.set_palette('colorblind')
sns.pairplot(data=df_pie, height=3)
# Import libraries
## Basic libs
import pandas as pd
import numpy as np
import warnings
## Building Model
from sklearn import linear_model
from scipy import stats
import statsmodels
import statsmodels.api as sm