<jupyter_start><jupyter_text>Fitting SImple Linear Regression Model using sklearn.linear_model.LinearRegression<jupyter_code>from sklearn.linear_model import LinearRegression
linear_reg = LinearRegression()
linear_reg.fit(X = computers_df[["Units"]], y= computers_df[["Minutes"]])
# OR
#model.fit(X = computers_df.loc[:,["Units"]], y= computers_df.loc[:,["Minutes"]])<jupyter_output><empty_output><jupyter_text>Fetching the coefficients<jupyter_code>print("Intercept:", linear_reg.intercept_)
print("Coefficients:",linear_reg.coef_)<jupyter_output>Intercept: [4.16165414]
Coefficients: [[15.50877193]]<jupyter_text>Making predictions<jupyter_code>predictions = linear_reg.predict(computers_df[["Units"]])
predictions<jupyter_output><empty_output><jupyter_text>Visualizing the Fitted Model<jupyter_code>plt.figure(figsize=(12,7))
plt.scatter(x= "Units", y="Minutes", data = computers_df)
plt.plot(computers_df["Units"], predictions, 'r')
plt.title("Linearly fitted model")
plt.xlabel("Number of units")
plt.ylabel("Time taken in minutes")
plt.show()<jupyter_output><empty_output><jupyter_text>Computing various types of errors (Evaluation metrics) 1. Mean Squared Error (MSE)<jupyter_code>from sklearn.metrics import mean_squared_error
mse = mean_squared_error(computers_df["Minutes"], predictions)
print("Mean Squared Error of the model : ", mse)
#manually
np.mean((computers_df["Minutes"] - predictions.ravel())**2)
# OR
#np.mean(np.square(computers_df["Units"] - predictions.ravel()))<jupyter_output><empty_output><jupyter_text>2. Root Mean Squared Error (RMSE)<jupyter_code>rmse = np.sqrt(mean_squared_error(computers_df["Minutes"], predictions))
print("Root Mean Squared Error of the model : ", rmse)<jupyter_output>Root Mean Squared Error of the model :  4.991767300317866<jupyter_text>3. Mean Absolute Error (MAE)<jupyter_code>from sklearn.metrics import mean_absolute_error
mae = mean_absolute_error(computers_df["Minutes"], predictions)
print("Mean Absolute Error of the model : ", mae)
#manually
np.mean(np.abs(computers_df["Minutes"] - predictions.ravel()))<jupyter_output><empty_output><jupyter_text>4. Mean Absolute Percentage Error (MAPE)<jupyter_code>mape = np.mean(np.abs((computers_df["Minutes"] - predictions.ravel()) / predictions.ravel())) * 100
print("Mean Absolute Error of the model : ", mape)<jupyter_output>Mean Absolute Error of the model :  5.960744353707668<jupyter_text>Linear Regression Assumption 1. Validating the Linearity Assumption<jupyter_code>plt.figure(figsize=(12,7))
plt.scatter(x= "Units", y="Minutes", data = computers_df)
plt.title("Scatter plot of Units vs. Minutes")
plt.xlabel("Number of units")
plt.ylabel("Time taken in minutes")
plt.show()
sns.residplot(predictions.reshape(-1),computers_df["Minutes"],lowess=True,
                                  line_kws={'color': 'red', 'lw': 1, 'alpha': 1})
plt.xlabel("Fitted values")
plt.title('Residual plot')<jupyter_output><empty_output><jupyter_text>2. Validating the Normality Assumption<jupyter_code>residuals = computers_df["Minutes"] - predictions.reshape(-1)
plt.figure(figsize=(7,7))
stats.probplot(residuals, dist="norm", plot=plt)
plt.title("Normal Q-Q Plot")<jupyter_output><empty_output><jupyter_text>3. Validating Homoscedasticity Assumption<jupyter_code>model_norm_residuals_abs_sqrt=np.sqrt(np.abs(residuals))
plt.figure(figsize=(7,7))
sns.regplot(predictions.reshape(-1), model_norm_residuals_abs_sqrt,
              scatter=True,
              lowess=True,
              line_kws={'color': 'red', 'lw': 1, 'alpha': 0.8})
plt.ylabel("Standarized residuals")
plt.xlabel("Fitted value")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14471140568903618, 'fraction_numerical': 0.022093344380005524, 'mean_word_length': 8.077694235588972, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '180670', 'license': 'MIT', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1266, 'n_words': 215, 'path': '08 Machine Learning/Regression/Simple_Linear_Regression.ipynb', 'repo_name': 'Anil-matcha/ISTE-Examly-Sessions', 'size': '3621'}