def runWorker(): # import DistML.DistML as worker if not ps.isWorker(): return from model.logistic_regression import LogisticRegression # TODO split data for diff worker data = datasets.load_iris() X = normalize(data.data[data.target != 0]) y = data.target[data.target != 0] y[y == 1] = 0 y[y == 2] = 1 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, seed=1) clf = LogisticRegression(gradient_descent=True) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy)
def predict(): if request.method == 'GET': try: try: # Data is sent via a .py script like in test_and_debug/test_api.py data = json.loads(request.json) school = data['school'] astronomy = float( data['astronomy']) # -1000 < astronomy < 1000 herbology = float(data['herbology']) # -10 < herbology < 10 ancient_runes = float( data['ancient_runes']) # 250 < ancient_runes < 750 except: # Data is sent via an url (e.g. in Chrome) # http://127.0.0.1:5000/predict?school=hogwarts&astronomy=-800&herbology=-2&ancient_runes=300 data = request.args school = data.get('school') astronomy = float(data.get('astronomy')) herbology = float(data.get('herbology')) ancient_runes = float(data.get('ancient_runes')) X = np.array([[astronomy, herbology, ancient_runes]]) if school == 'hogwarts': logreg = LogisticRegression(path_to_beta='results/beta.json') sc = Scaling(X, path_to_scaling='results/scaling.json') else: logreg = LogisticRegression( path_to_beta='results/beta_%s.json' % school) sc = Scaling(X, path_to_scaling='results/scaling_%s.json' % school) sc.transform() prediction = logreg.predict(X_to_predict=sc.X) return jsonify({ 'house': prediction[0][0], 'probas': prediction[1][0] }) except ValueError: return "Please enter values in the correct format: {\"school\":str, \"astronomy\":-1000<float<1000, \"herbology\":-10<float<10, \"ancient_runes\":250<float<750}."
class Model: def __init__(self, classifier_type, classifier_params, kernel_name, kernel_params, id_model=0): # define kernel self.kernel = Kernel(kernel_name, kernel_params) self.tag_kernel = str( id_model ) + "_kernel_" + kernel_name # kernel tag used to kernel mat saves # define classifier if classifier_type == "svm": self.classifier = SVM(classifier_params) elif classifier_type == "l_regression": self.classifier = LogisticRegression(lambda_regularisation=0.01) # kernel mat self.kernel_mat_train = None self.kernel_mat_val = None self.kernel_mat_test = None # load save kernel matrix self.do_save_kernel = kernel_params["save_kernel"] self.save_name = kernel_params["save_name"] if kernel_params["load_kernel"]: self.kernel_mat_train = load_object("train_" + self.tag_kernel + "_" + kernel_params["load_name"]) self.kernel_mat_val = load_object("val_" + self.tag_kernel + "_" + kernel_params["load_name"]) self.kernel_mat_test = load_object("test_" + self.tag_kernel + "_" + kernel_params["load_name"]) if self.kernel_mat_train is None: print("## kernel load failed: kernel not found") else: print("## kernel matrix loaded") def fit(self, X, y, X_train_repres=None): """ fit the model use X_train_repres if filled as it is faster """ self.X_train = X self.deal_with_kernel_mat_train(X_train_repres) self.classifier.fit(self.kernel_mat_train, y) def predict(self, X_test, X_test_repres=None, X_train_repres=None): """ predict from X_test for spectrum and mismatch only X_test_repres : representation of X_test in RKHS X_train_repres : representation of X_train in RKHS use X_train_repres, X_test_repres if filled as it is faster """ self.deal_with_kernel_mat_test(X_test, X_test_repres, X_train_repres) return self.classifier.predict(self.kernel_mat_test) def predict_val(self, X_val, X_val_repres=None, X_train_repres=None): """ predict from X_test for spectrum and mismatch only X_val_repres : representation of X_val in RKHS X_train_repres : representation of X_train in RKHS use X_train_repres, X_test_repres if filled as it is faster """ self.deal_with_kernel_mat_val(X_val, X_val_repres, X_train_repres) return self.classifier.predict(self.kernel_mat_val) def predict_train(self): """ to compute the train loss """ return self.classifier.predict(self.kernel_mat_train) ## functions to calculate or load Kernel matrix and save it if needed def deal_with_kernel_mat_train(self, X_train_repres): """ compute kernel matrix and save it if do_save_kernel """ if self.kernel_mat_train is None: # compute kernel self.kernel_mat_train = self.kernel.get_kernel_mat( self.X_train, self.X_train, X_train_repres=X_train_repres) # save it or not dump_object(self.kernel_mat_train, "train_" + self.tag_kernel + "_" + self.save_name, self.do_save_kernel) def deal_with_kernel_mat_test(self, X_test, X_test_repres=None, X_train_repres=None): """ compute kernel matrix and save it if do_save_kernel """ if self.kernel_mat_test is None: # compute kernel self.kernel_mat_test = self.kernel.get_kernel_mat( X_test, self.X_train, test=True, X_test_repres=X_test_repres, X_train_repres=X_train_repres) # save it or not dump_object(self.kernel_mat_test, "test_" + self.tag_kernel + "_" + self.save_name, self.do_save_kernel) def deal_with_kernel_mat_val(self, X_val, X_val_repres, X_train_repres=None): """ compute kernel matrix and save it if do_save_kernel """ if self.kernel_mat_val is None: # compute kernel self.kernel_mat_val = self.kernel.get_kernel_mat( X_val, self.X_train, test=True, X_test_repres=X_val_repres, X_train_repres=X_train_repres) # save it or not dump_object(self.kernel_mat_val, "val_" + self.tag_kernel + "_" + self.save_name, self.do_save_kernel) def set_c_svm(self, c_svm): """ set c_svm in SVM classifier """ self.classifier.C = c_svm return self