<jupyter_start><jupyter_text>$$\hat\theta = (X^TX)^{-1}X^Ty$$<jupyter_code>(2,100)(100,2)=> (2,2)(2,100) => (2,100)(100,1) => (2,1)
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
m = 100
X = 2 * np.random.rand(m, 1)
y = 3 + 4*X + 1.5*np.random.randn(m, 1)
plt.plot(X, y, "b.")
plt.xlabel("$X_1$", fontsize=14)
plt.ylabel("$y$", fontsize=14, rotation=0)
plt.axis([0, 2, 0, 15])
plt.grid(True)
plt.show()
X_b = np.c_[np.ones((m,1)), X]
theta = np.linalg.inv(X_b.T @ X_b) @ X_b.T @ y
theta
X_new = [[0], [0.75], [1.5], [2]]
X_new_b = np.c_[np.ones((4,1)), X_new]
y_predict = X_new_b @ theta
y_predict
plt.plot(X, y, "b.")
plt.plot(X_new, y_predict, "r-", linewidth=2, label="Predictions")
plt.xlabel("$X_1$", fontsize=14)
plt.ylabel("$y$", fontsize=14, rotation=0)
plt.legend(loc="upper left", fontsize=14)
plt.axis([0, 2, 0, 15])
plt.grid(True)
plt.show()<jupyter_output><empty_output><jupyter_text>Using sklearn library<jupyter_code>from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X, y)
model.intercept_, model.coef_
model.predict(X_new)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2225177304964539, 'fraction_numerical': 0.06205673758865248, 'mean_word_length': 6.283870967741936, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '326139', 'license': 'MIT', 'n_tokens_mistral': 578, 'n_tokens_neox': 525, 'n_words': 95, 'path': '.ipynb_checkpoints/04 - Linear Regression Methods-checkpoint.ipynb', 'repo_name': 'OswaldoCC17/imk-2021-2-aprendizaje-automatico', 'size': '1128'}