<jupyter_start><jupyter_text>Linear Regression Using Scipy<jupyter_code>%matplotlib inline
x = [5,10,20,8,4,6,12,15]
y = [27,46,73,40,30,28,46,59]

data_list = [tpl for tpl in zip(x,y)]
data_list
import numpy as np

income = []
sales = []
for x,y in sorted(data_list):
    income.append(x)
    sales.append(y)
    
income = np.array(income)
sales = np.array(sales)
income
sales
import matplotlib.pyplot as plt
import scipy.stats as stats

fig = plt.figure()

slope, intercept, r_value, p_value, std_err = stats.linregress(income, sales)
x = range(0,26)
y = slope * x + intercept

axes = fig.add_axes([0, 0, 1, 1]) # left, bottom, width, height (range 0 to 1)
axes.scatter(income, sales)
axes.plot(x,y)
plt.show()<jupyter_output><empty_output><jupyter_text>y-intercept:<jupyter_code>intercept<jupyter_output><empty_output><jupyter_text>slope:<jupyter_code>slope
y = slope * income + intercept
y<jupyter_output><empty_output><jupyter_text>Prediction at income = 15:<jupyter_code>slope * 15 + intercept
residual = sales - y
residual
fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1]) # left, bottom, width, height (range 0 to 1)

axes.spines['right'].set_color('none')
axes.spines['top'].set_color('none')

axes.xaxis.set_ticks_position('bottom')
axes.spines['bottom'].set_position(('data',0)) # set position of x spine to x=0

axes.yaxis.set_ticks_position('left')
axes.spines['left'].set_position(('data',0))   # set position of y spine to y=0

axes.scatter(income, residual)
axes.grid(True)
plt.show()<jupyter_output><empty_output><jupyter_text>Using scikit-learn **NOTE:** With scikit-learn, we must make our arrays 2D array, not 1D array via .reshape(-1, 1)<jupyter_code>from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(income.reshape(-1,1), sales.reshape(-1,1))
regressor.intercept_
regressor.coef_<jupyter_output><empty_output><jupyter_text>Prediction:<jupyter_code>regressor.predict([[15]])
regressor.predict([[15]])[0][0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15211970074812967, 'fraction_numerical': 0.032418952618453865, 'mean_word_length': 6.897637795275591, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '525983', 'license': 'MIT', 'n_tokens_mistral': 856, 'n_tokens_neox': 785, 'n_words': 154, 'path': 'jupyter_notebooks/statistics/Linear_Regression_Scipy.ipynb', 'repo_name': 'manual123/Nacho-Jupyter-Notebooks', 'size': '2005'}