# 'estimator__sgd__alpha': sp_randint(0.00001, 0.000001), # 'estimator__sgd__loss': ('hinge', 'modified_huber', 'squared_hinge', 'perceptron'), # 'estimator__sgd__penalty': ('l2', 'elasticnet'), # 'estimator__sgd__n_iter': sp_randint(10, 80), # } if __name__ == "__main__": # multiprocessing requires the fork to happen in a __main__ protected # block # find the best parameters for both the feature extraction and the # classifier for i in range(0, 20): p = classifier2.predictor() print (p.trainExamples) grid_search = RandomizedSearchCV(p.classif, parameters, n_jobs=-1, verbose=1, error_score=0) print("Performing grid search...") print("parameters:") pprint(parameters) t0 = time() grid_search.fit(p.trainExamples, p.mlb.fit_transform(util2.getCorrectGenres(p.trainExamples))) print("done in %0.3fs" % (time() - t0)) print() print("Best score: %0.3f" % grid_search.best_score_) print("Best parameters set:") best_parameters = grid_search.best_estimator_.get_params() for param_name in sorted(parameters.keys()): print("\t%s: %r" % (param_name, best_parameters[param_name])) print ("###########################################################################")
def learnPredictor(self, numbers=False): train_genres = self.mlb.fit_transform(util2.getCorrectGenres(self.trainExamples)) self.classif.fit(self.trainExamples, train_genres) return train_genres
import classifier2, classifier3, util2, movie_genre, os, sys nlrloss = [] nlraprecision = [] plrloss = [] plraprecision = [] nscoreone = 0 nscoretwo = 0 correctness = 0 for i in range(0,10): p = classifier2.predictor() p.learnPredictor() n_predicted = p.predict() correct = p.mlb.transform(util2.getCorrectGenres(p.testExamples)) ny_score = np.array(n_predicted) y_true = np.array(correct) nscoreone += label_ranking_loss(y_true, ny_score) nscoretwo += label_ranking_average_precision_score(y_true, ny_score) correctness += util2.printCorrectness(p.mlb, p.testExamples, n_predicted, correct) print "LABEL RANKING LOSS: " + str(float(nscoreone)/10) print "LABEL RANKING AVERAGE PRECISION: " + str(float(nscoretwo)/10) print "CORRECTNESS: " + str(float(correctness)/10) # util2.printAccuracyByGenre(p.mlb, p.testExamples, n_predicted, correct) # util2.printOutput(p.mlb, p.testExamples, n_predicted, correct)