Last active
January 16, 2020 06:28
-
-
Save mruv/81186da46104614ca8853c9eb6e6b644 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import pandas as pd | |
from sklearn.model_selection import StratifiedKFold | |
def tf_cross_val_score(X, y, x_col_nms, n_classes): | |
""" | |
Takes features, number of classes, labels and feature vector names and returns 3 validation scores. | |
*X* Numpy ndarray - features | |
*y* Numpy ndarray - labels | |
*x_col_nms* a list() - feature dataset column names | |
*n_classes* int - number of classes | |
""" | |
scores = [] | |
kfold = StratifiedKFold(n_splits = 3, random_state = 42) | |
for train_index, test_index in kfold.split(X, y): | |
X_train_df, X_test_df = pd.DataFrame(data = X[train_index], columns = x_col_nms),\ | |
pd.DataFrame(data = X[test_index], columns = x_col_nms) | |
y_train, y_test = y[train_index], y[test_index] | |
clfr = tf.estimator.DNNClassifier( | |
feature_columns = columns_feat, | |
hidden_units=[10, 10], n_classes = n_classes) | |
clfr.train(input_fn = lambda : train_function(X_train_df, y_train, 1000), steps = 100) | |
scores.append(clfr.evaluate(input_fn = lambda : evaluation_function(X_test_df, y_test, 1000))['accuracy']) | |
return pd.np.array(scores) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment