def cross_validation(c, data_set, kernel_func=lambda x, y: x.dot(y.T), times=10): error_rates = [] for _ in xrange(times): train_set, test_set = training_and_test_sets(data_set) X_tr = numpy.array([test.features[0] for test in train_set]) Y_tr = numpy.array([test.answer for test in train_set]) X_test = numpy.array([test.features[0] for test in test_set]) Y_test = numpy.array([test.answer for test in test_set]) alpha, beta = smo2(c, X_tr, Y_tr, kernel_func=kernel_func) rate = error_rate(*get_answers_and_actual(X_tr, Y_tr, X_test, Y_test, alpha, beta, kernel_func)) print rate error_rates.append(rate) return sum(error_rates) / float(len(error_rates))
def cross_validation(C, data_set, times=10): error_rates = [] for _ in xrange(times): train_set, test_set = training_and_test_sets(data_set) X_tr = np.array([test.features[0] for test in train_set]) Y_tr = np.array([1 if test.answer == 1 else 0 for test in train_set]) X_test = np.array([test.features[0] for test in test_set]) Y_test = np.array([1 if test.answer == 1 else 0 for test in test_set]) cost_function = logistic_cost_function(X_tr, Y_tr, C) omega = np.zeros((1, len(X_tr[0]), )) result = minimize(cost_function, omega, jac=None) # jac) omega = result.x rate = error_rate(*get_answers_and_actual(X_test, Y_test, omega)) print rate error_rates.append(rate) return sum(error_rates) / float(len(error_rates))
def cross_validation(C, data_set, times=10): error_rates = [] for _ in xrange(times): train_set, test_set = training_and_test_sets(data_set) X_tr = np.array([test.features[0] for test in train_set]) Y_tr = np.array([1 if test.answer == 1 else 0 for test in train_set]) X_test = np.array([test.features[0] for test in test_set]) Y_test = np.array([1 if test.answer == 1 else 0 for test in test_set]) cost_function = logistic_cost_function(X_tr, Y_tr, C) omega = np.zeros(( 1, len(X_tr[0]), )) result = minimize(cost_function, omega, jac=None) # jac) omega = result.x rate = error_rate(*get_answers_and_actual(X_test, Y_test, omega)) print rate error_rates.append(rate) return sum(error_rates) / float(len(error_rates))