<jupyter_start><jupyter_text>Pre-procesamiento<jupyter_code>df_train = pd.read_csv('train_function.csv')
df_train.head()


X.shape, y.shape
len(df_train.index)

#X_train.shape, y_train.shape, X_valid.shape, y_valid.shape
data = df_train[['X','y']].to_numpy()
X, y = data[:,0], data[:,1]

#X = X**2
y = np.sqrt(y)

test_split = 0.8
N = len(df_train.index)
sz = int(test_split*N)
X_train, y_train = X[:sz], y[:sz]
X_valid, y_valid = X[sz:], y[sz:]

# y_train = np.sqrt(y_train) # No linearidad
# y_valid = np.sqrt(y_valid)



mean_X = np.mean(X_train)
std_X = np.std(X_train)

mean_y = np.mean(y_train)
std_y = np.std(y_train)

X_train = (X_train - mean_X)/std_X
y_train = (y_train - mean_y)/std_y

X_valid = (X_valid - mean_X)/std_X
y_valid = (y_valid - mean_y)/std_y

data_train = (X_train, y_train)
data_valid = (X_valid, y_valid)

w0, w1, c_train, c_valid = gradient_descent(data_train, data_valid, lr=0.1, ep=1e-8, max_iter=10000)
# plt.plot(c_train)
# plt.show()
# plt.plot(c_valid)
# plt.show()
# plt.scatter(X_train, y_train)
# plt.plot(np.arange(-1.6,2,0.1), w0 +w1*np.arange(-1.6,2,0.1)**2, 'r')
# plt.show()

plt.scatter(X_train, y_train)
plt.plot(np.arange(-1.2,2.4,0.1), [(w0 + w1 * x)[0] for x in np.arange(-1.2,2.4,0.1)], 'r')
plt.show()
df_test = pd.read_csv('test_function.csv')
len(df_test.index)
from sklearn.metrics import mean_absolute_error, mean_squared_error
data_test = df_test[['X','y']].to_numpy()
X_test, y_test = data_test[:,0], data_test[:,1]
#X_test = X_test**2
y_test = np.sqrt(y_test)

X_test = (X_test - mean_X)/std_X

y_pred = w0 + w1*X_test
y_pred = y_pred*std_y + mean_y
y_pred = y_pred**2  # sqrt
np.max(data_test[:,0])
data_test[:,0]
## USANDO SQRT(Y)
#plt.figure(figsize=(20,7))
plt.scatter(data_test[:,0], y_pred)
plt.plot(np.arange(0,240,1), [((w0 + w1*(x-mean_X)/std_X)[0]*std_y + mean_y)**2 for x in np.arange(0,240,1)], 'r')
#plt.plot(np.arange(0,240,1), [((w0 + w1*((x**2-mean_X)/std_X))[0]*std_y + mean_y) for x in np.arange(0,240,1)], 'r')
plt.show()
## USANDO X**2
plt.scatter(data_test[:,0], y_pred)
#plt.plot(np.arange(0,240,1), [((w0 + w1*(x**2-mean_X)/std_X)[0]*std_y + mean_y) for x in np.arange(0,240,1)], 'r')
plt.plot(np.arange(0,np.max(data_test[:,0])), (w0 + w1*(np.arange(0,np.max(data_test[:,0]))**2- mean_X)/std_X)*std_y + mean_y, 'r')
plt.show()
plt.scatter(y_pred, y_test)
plt.plot(np.arange(0,10000),np.arange(0,10000), 'r')
plt.show()
print(f"MAE: {mean_absolute_error(y_pred, y_test)}")
print(f"RMSE: {np.sqrt(mean_squared_error(y_pred, y_test))}")
print(f"MAE: {mean_absolute_error(y_pred, data_test[:,1])}")
print(f"RMSE: {np.sqrt(mean_squared_error(y_pred, data_test[:,1]))}")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22139117427075541, 'fraction_numerical': 0.04525056095736724, 'mean_word_length': 6.171581769436997, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '414483', 'license': 'MIT', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1330, 'n_words': 191, 'path': 'LR_hecha_en_clase.ipynb', 'repo_name': 'FrankCabello/Test', 'size': '2674'}