<jupyter_start><jupyter_text>Simple linear regression  Import the relevant libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
import seaborn as sns
sns.set()<jupyter_output><empty_output><jupyter_text>Load the data<jupyter_code># Load the data from a .csv in the same folder
data = pd.read_csv('1.01. Simple linear regression.csv')
# Check what's inside this data frame
data
# This method gives us very nice descriptive statistics.
data.describe()<jupyter_output><empty_output><jupyter_text>Create your first regression  Define the Problem: SAT = Critical Reading + Mathamatics + Writing GPA = Grade point Average from University We will create a linear regression model which predicts GPA based on SAT scores Regression Equation y_hat = b0 +b1x1  Define the dependent and the independent variables<jupyter_code># Following the regression equation, our dependent variable (y) is the GPA
y = data ['GPA']
# Similarly, our independent variable (x) is the SAT score
x1 = data ['SAT']<jupyter_output><empty_output><jupyter_text>Explore the data<jupyter_code># Plot a scatter plot (first we put the horizontal axis, then the vertical axis)
plt.scatter(x1,y)
# Name the axes
plt.xlabel('SAT', fontsize = 25)
plt.ylabel('GPA', fontsize = 25)
# Show the plot
plt.show()<jupyter_output><empty_output><jupyter_text>Regression itself<jupyter_code># Add a constant. Essentially, we are adding a new column (equal in lenght to x), which consists only of 1s
x_constant = sm.add_constant(x)
# Fit the model, according to the OLS (ordinary least squares) method with a dependent variable y and an idependent x
results = sm.OLS(y,x_constant).fit()
# Print a nice summary of the regression. That's one of the strong points of statsmodels -> the summaries
results.summary()<jupyter_output>C:\Users\yorkc\Anaconda3\envs\python_r_jupyter\lib\site-packages\numpy\core\fromnumeric.py:2495: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)<jupyter_text>Reading the Above Table Dependent Variable is Dep. Variable:	GPA	 The Model is:	OLS (Ordinary Least Sqaures) The method is: Least Squares	 R-sqaured is a measure of how good the regression is. Generally 0.2 - 0.9 is good for casual sciences. Coef from const above is b0. So b0 = 0.2750 Coef of SAT above is b1. So b1 = 0.0017  Y_hat = 0.2750 + 0.0017(x1) GPA = 0.2750 + 0.0017 * SAT     std err shows the accuracy of each variable; the lower the better t is t-value A P>|t|  < 0.500 shows that the variable is significant<jupyter_code># Create a scatter plot
plt.scatter(x1,y)
# Define the regression equation, so we can plot it later
y_hat = 0.0017*x1 + 0.275  #formula from above
# Plot the regression line against the independent variable (SAT)
fig = plt.plot(x1,y_hat, lw=4, c='red', label ='regression line')
# Label the axes
plt.xlabel('SAT', fontsize = 25)
plt.ylabel('GPA', fontsize = 25)
plt.show()<jupyter_output><empty_output><jupyter_text>Wrong Ways to Graph Below<jupyter_code>plt.scatter(x1,y)
yhat = 0.0017*x1 + 0
fig = plt.plot(x1,yhat, lw=4, c='green', label ='regression line')
plt.xlabel('SAT', fontsize = 20)
plt.ylabel('GPA', fontsize = 20)
plt.xlim(0)
plt.ylim(0)
plt.show()
plt.scatter(x1,y)
yhat = 0*x1 + 0.275
fig = plt.plot(x1,yhat, lw=4, c='red', label ='regression line')
plt.xlabel('SAT', fontsize = 20)
plt.ylabel('GPA', fontsize = 20)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1026811180832858, 'fraction_numerical': 0.030519110096976613, 'mean_word_length': 5.330324909747293, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '438345', 'license': 'MIT', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1187, 'n_words': 418, 'path': 'Part_5_Advanced_Statistical_Methods_(Machine_Learning)/Linear Regression/Simple Linear Regression - GPA Problem/Simple linear regression - Basics with GPA Regression Formula.ipynb', 'repo_name': 'cyork95/The-Data-Science-Course-2020', 'size': '3506'}