<jupyter_start><jupyter_text>What does the following code do?<jupyter_code>d = pd.read_csv("data/dataset_0.csv")
fig, ax = plt.subplots()
ax.plot(d.x,d.y,'o')<jupyter_output><empty_output><jupyter_text>What does the following code do?<jupyter_code>def linear(x,a,b):
    return a + b*x<jupyter_output><empty_output><jupyter_text>What does the following code do?<jupyter_code>def linear(x,a,b):
    return a + b*x

def linear_r(param,x,y):
    return linear(x,param[0],param[1]) - y<jupyter_output><empty_output><jupyter_text>What does the following code do?<jupyter_code>def linear_r(param,x,y):                     # copied from previous cell
    return linear(x,param[0],param[1]) - y   # copied from previous cell

param_guesses = [1,1]
fit = scipy.optimize.least_squares(linear_r,param_guesses,
                                   args=(d.x,d.y))
fit_a = fit.x[0]
fit_b = fit.x[1]
sum_of_square_residuals = fit.cost<jupyter_output><empty_output><jupyter_text>What does the following code do?+ What the heck is `linspace`?+ What are we plotting?<jupyter_code>x_range = np.linspace(np.min(d.x),np.max(d.x),100)

fig, ax = plt.subplots()
ax.plot(d.x,d.y,"o")
ax.plot(x_range,linear(x_range,fit_a,fit_b))<jupyter_output><empty_output><jupyter_text>Put together<jupyter_code>def linear(x,a,b):
    """Linear model of x using a (slope) and b (intercept)"""
    return a + b*x

def linear_r(param,x,y):
    """Residuals function for linear"""
    return linear(x,param[0],param[1]) - y

fig, ax = plt.subplots()

# Read data
d = pd.read_csv("data/dataset_0.csv")
ax.plot(d.x,d.y,'o')

# Perform regression
param_guesses = [1,1]
fit = scipy.optimize.least_squares(linear_r,param_guesses,args=(d.x,d.y))
fit_a = fit.x[0]
fit_b = fit.x[1]
sum_of_square_residuals = fit.cost

# Plot result
x_range = np.linspace(np.min(d.x),np.max(d.x),100)
ax.plot(x_range,linear(x_range,fit_a,fit_b))

fit<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1574267782426778, 'fraction_numerical': 0.011506276150627616, 'mean_word_length': 5.272131147540984, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '270754', 'license': 'Unlicense', 'n_tokens_mistral': 821, 'n_tokens_neox': 807, 'n_words': 132, 'path': 'chapters/02_regression/01_fitting-with-least-squares.ipynb', 'repo_name': 'lperezmo/pythonic-science', 'size': '1912'}