This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print('R2 score:', olsmod.rsquared) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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_) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Visualize the data using scatter plot and histogram | |
| sns.set_palette('colorblind') | |
| sns.pairplot(data=df_pie, height=3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
NewerOlder