<jupyter_start><jupyter_text>Cross Validation<jupyter_code>from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score, KFold
def build_logistic_regression_model():
    model = Sequential()
    model.add(Dense(1, input_shape=(100,), activation='sigmoid'))
    model.compile(SGD(lr=0.5),
                  'binary_crossentropy',
                  metrics=['accuracy'])
    return model

model = KerasClassifier(build_fn=build_logistic_regression_model, epochs=25, verbose=0)
cv = KFold(5,shuffle=True)  # defining a 5-fold cross validation 

scores = cross_val_score(model, np.random.randn(4000,100), np.random.randint(2,size=(4000)), cv=cv)
print(scores)  # accuracies
print(scores.mean())
print(scores.std())<jupyter_output>[ 0.50375  0.51125  0.53     0.50125  0.49   ]
0.50725
0.0132617872099
{'fraction_non_alphanumeric': 0.11452810180275716, 'fraction_numerical': 0.07104984093319194, 'mean_word_length': 5.012738853503185, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '226351', 'license': 'MIT', 'n_tokens_mistral': 376, 'n_tokens_neox': 331, 'n_words': 61, 'path': 'ml/Train Test Split and Cross Validation.ipynb', 'repo_name': 'disooqi/jupyter-notebooks', 'size': '943'}