<jupyter_start><jupyter_text>CODE TO PERFORM SIMPLE LINEAR REGRESSION Dr. Ryan @STEMplicity![image.png](attachment:image.png)  PROBLEM STATEMENT You own an ice cream business and you would like to create a model that could predict the daily revenue in dollars based on the outside air temperature (degC). You decide that a Linear Regression model might be a good candidate to solve this problem.  Data set:- Independant variable X: Outside Air Temperature- Dependant variable Y: Overall daily revenue generated in dollars  ![image.png](attachment:image.png)  STEP 1: LIBRARIES IMPORT<jupyter_code># Import Libraries
import pandas as pd  # dataframe manipulation
import numpy as np
import matplotlib.pyplot as plt # visualization
import seaborn as sns # advanced form of matplotlib visualization and statistical analysis
%matplotlib inline<jupyter_output><empty_output><jupyter_text>STEP 2: IMPORT DATASET<jupyter_code># import data
IceCream = pd.read_csv("IceCreamData.csv")
# preview a sample of the data
IceCream.head(100)
IceCream.tail()
IceCream.describe()
IceCream.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 500 entries, 0 to 499
Data columns (total 2 columns):
Temperature    500 non-null float64
Revenue        500 non-null float64
dtypes: float64(2)
memory usage: 7.9 KB<jupyter_text>STEP3: VISUALIZE DATASET<jupyter_code>sns.jointplot(x='Temperature', y='Revenue', data = IceCream, color = 'gray')
sns.pairplot(IceCream)
# "lmplot" draws a line for all the data points, but this is not the best fit line
sns.lmplot(x='Temperature', y='Revenue', data=IceCream)<jupyter_output><empty_output><jupyter_text>STEP4: CREATE TESTING AND TRAINING DATASET<jupyter_code>y = IceCream['Revenue']
X = IceCream[['Temperature']]
X
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
X_train.shape
# Check the shape of data
X_train.shape
y_train.shape
X_test.shape
y_test.shape<jupyter_output><empty_output><jupyter_text>STEP5: TRAIN THE MODEL<jupyter_code>from sklearn.linear_model import LinearRegression
# Instantiate a linear regression object
# There should be a "B0"
# false means no B0
regressor = LinearRegression(fit_intercept = True)
# fit the regressor object
regressor.fit(X_train,y_train)
# we us this to obtain the values of the parameters "m"=slope and "b"=intercept
# Slope
print('Linear Model Coefficient (m): ', regressor.coef_)
# intercept
print('Linear Model Coefficient (b): ', regressor.intercept_)<jupyter_output>Linear Model Coefficient (m):  [21.49001307]
Linear Model Coefficient (b):  43.39868968039724<jupyter_text>STEP6: TEST THE MODEL<jupyter_code># Preview/print our X_test data
X_test
# We apply our trained model "regressor" to the test data
y_predict = regressor.predict( X_test)

# the predicted "y_predict" for every "X_test" data 
y_predict
# Observes "y_test" values / Original dataset
y_test
# VISUALIZE TRAIN SET RESULTS
plt.scatter(X_train, y_train, color = 'gray')
plt.ylabel('Revenue [dollars]')
plt.xlabel('Temperature [degC]')
plt.title('Revenue Generated vs. Temperature @Ice Cream Stand(Training dataset)')
# plot of training dataset & the best fit line that goes through them -using the least squares method
plt.scatter(X_train, y_train, color = 'gray')
plt.plot(X_train, regressor.predict(X_train), color = 'red')
plt.ylabel('Revenue [dollars]')
plt.xlabel('Temperature [degC]')
plt.title('Revenue Generated vs. Temperature @Ice Cream Stand(Training dataset)')
# VISUALIZE TEST SET RESULTS
plt.scatter(X_test, y_test, color = 'gray')
plt.ylabel('Revenue [dollars]')
plt.xlabel('Hours')
plt.title('Revenue Generated vs. Hours @Ice Cream Stand(Test dataset)')
# plot of test dataset & the best fit line that goes through them - using the least squares method
plt.scatter(X_test, y_test, color = 'gray')
plt.plot(X_test, regressor.predict(X_test), color = 'red')
plt.ylabel('Revenue [dollars]')
plt.xlabel('Hours')
plt.title('Revenue Generated vs. Hours @Ice Cream Stand(Test dataset)')
# Predict for a given value of 
y_predict = regressor.predict(30)
y_predict
# Predicting for a given temperature
T = 40
revenue = regressor.predict(T)
revenue
T = int(input("What is Today's Temperature in Degrees Celcius?" ))
revenue = regressor.predict(T)
twoSigFigs = round(T, 2)
numToString = str(twoSigFigs)
print ("Revenue Forecast  for that Temperature " + numToString + " Dollars.")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10094424460431654, 'fraction_numerical': 0.015287769784172662, 'mean_word_length': 6.317434210526316, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '267173', 'license': 'MIT', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1461, 'n_words': 461, 'path': '11 Regression Algorithms/1. Simple Linear Regression - Py &R/Simple Linear Regression - Ice Cream Revenue Prediction.ipynb', 'repo_name': 'IshmaelAsabere/Machine_Learning-Various-Topics', 'size': '4448'}