<jupyter_start><jupyter_text>Modelado<jupyter_code>df
y=df["mean_flag"]
df2=df.drop(columns=["mean_flag","created_date","counts"])
df2
from sklearn.model_selection import TimeSeriesSplit
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
#primero separamos los datos de entrenamiento y prueba
x_train, x_test, y_train, y_test= train_test_split(df2,y,test_size=0.8)
#separamos los primeros 70% de los datos para entrenar
X_train = df2[:int(df2.shape[0]*0.7)].to_numpy()
X_test = df2[int(df2.shape[0]*0.7):].to_numpy()
y_train = y[:int(df2.shape[0]*0.7)].to_numpy()
y_test = y[int(df2.shape[0]*0.7):].to_numpy()
X_train.shape
#partimos los datos con temporal cv

tscv=TimeSeriesSplit(n_splits=5)

for tr_index, val_index in tscv.split(X_train):
    X_tr, X_val=X_train[tr_index], X_train[val_index]
    y_tr, y_val = y_train[tr_index], y_train[val_index]
model=RandomForestClassifier(max_depth=10,criterion='gini',n_estimators=100,n_jobs=-1)
model.fit(X_tr,y_tr)
#PRedecimos sobre datos de validacion
preds=model.predict(X_val)
model.score(X_train,y_train)
model.score(X_val,y_val)
model.score(X_test,y_test)
1-sum(abs(preds-y_val))/len(preds)
#guarda pickle
import pickle
modelo=pickle.dumps(model)
#load pickle
desde_pickle=pickle.loads(modelo)
#usa el pickle
desde_pickle.score(X_test,y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13435003631082063, 'fraction_numerical': 0.023965141612200435, 'mean_word_length': 9.132352941176471, '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': '503794', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 599, 'n_tokens_neox': 588, 'n_words': 90, 'path': 'scripts/model/model.ipynb', 'repo_name': 'dapivei/data-product-architecture-final-project', 'size': '1377'}