np.random.seed(31337) X_train, y_train, X_test, y_test, scaler = cf.get_normalised_data() basic_svm = SGDClassifier(loss="hinge", penalty="l2", l1_ratio=0.0, random_state=31337, n_jobs=5) # From the Scipy docs: to sample a random variable Y such that Y=exp(X) where X~N(mu,sigma), use # scipy.stats.lognormal(s=sigma, scale=np.exp(mu)) random_search = RandomizedSearchCV(basic_svm, param_distributions={'alpha': sp_lognormal(s=2, scale=np.exp(-4))}, n_iter=20, verbose=1) random_search.fit(X_train, y_train) print("Chosen: ", random_search.best_params_["alpha"]) print("Best CV score: ", random_search.best_score_) chosen_svm = random_search.best_estimator_ os.makedirs("output/svc", exist_ok=True) labels = cf.get_label_names() for i in range(10): # Don't forget to rescale the hyperplanes to get human-readable versions---the l2 penalty makes # them close to the origin, so they look indistinguishable. this_hyperplane = 127*(chosen_svm.coef_[i]/np.max(np.abs(chosen_svm.coef_[i]))) + scaler.mean_ cf.plot_image(this_hyperplane, "output/svc/archetype " + labels[i] + ".png") preds = chosen_svm.predict(X_test) print("Test score: %.3f" % chosen_svm.score(X_test, y_test))
alpha_grid = [ 5.0, 1.0, 0.5, 1e-1, 5e-2, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6 ] cv_results = [] for alpha in alpha_grid: print(alpha) mean_score, std_score, svm = evaluate_svm(alpha) cv_results.append({"alpha": alpha, "mean": mean_score, "std": std_score, "svm": svm}) best_score = np.max([x['mean'] for x in cv_results]) best_score_index = np.where([x['mean'] >= best_score for x in cv_results])[0][0] onesd_at_best = cv_results[best_score_index]['std'] score_to_beat = best_score - onesd_at_best for i in range(best_score_index-1, -1, -1): if cv_results[i]['mean'] > score_to_beat: best_score_index = i chosen_alpha = cv_results[best_score_index]["alpha"] print("Chosen: ", chosen_alpha) print("Best CV score: ", cv_results[best_score_index]["mean"]) chosen_svm = cv_results[best_score_index]["svm"] os.makedirs("output/svc", exist_ok=True) for i in range(10): this_hyperplane = 127*(chosen_svm.coef_[i]/np.max(np.abs(chosen_svm.coef_[i]))) + scaler.mean_ cf.plot_image(this_hyperplane, "output/svc/archetype " + str(i) + ".png") preds = chosen_svm.predict(X_test) print("Test score: %.3f" % chosen_svm.score(X_test, y_test))