def lambda_handler(event, context): # TODO implement json_data = json.loads(event['body']) preprocess = Preprocess(json_data=json_data) preprocess.scale_points(calculate_scale=False) pose_objects = preprocess.new_pose_objects features = [] features_obj = Features(pose_objects=pose_objects) features_obj.compute_features() features = features_obj.get_features() # pca_model = pickle.load(open('pca.pkl', 'rb')) # reduced_feature_matrix = pca_model.transform(features) s3 = boto3.resource('s3') svm_classifier = pickle.loads( s3.Bucket("gesture-recognition").Object("SVM_model.pkl").get() ['Body'].read()) logreg_classifier = pickle.loads( s3.Bucket("gesture-recognition").Object("LogReg_model.pkl").get() ['Body'].read()) lda_classifier = pickle.loads( s3.Bucket("gesture-recognition").Object("LDA_model.pkl").get() ['Body'].read()) random_forest_classifier = pickle.loads( s3.Bucket("gesture-recognition").Object("RForest_model.pkl").get() ['Body'].read()) prediction_rf = random_forest_classifier.predict(features) prediction_svm = svm_classifier.predict(features) prediction_lda = lda_classifier.predict(features) prediction_logreg = logreg_classifier.predict(features) data = { "1": prediction_svm[0], "2": prediction_logreg[0], "3": prediction_lda[0], "4": prediction_rf[0] } return {'statusCode': 200, 'body': json.dumps(data)}
def get_data(): files = os.listdir('./MealNoMealData') meal_data_files = [] no_meal_data_files = [] for file in files: if 'Nomeal' in file: no_meal_data_files.append(os.path.join('./MealNoMealData', file)) else: meal_data_files.append(os.path.join('./MealNoMealData', file)) data = [] labels = [] for meal_data_file, no_meal_data_file in zip(meal_data_files, no_meal_data_files): preprocess_obj = Preprocess(meal_data_file) meal_df = preprocess_obj.get_dataframe() meal_features = Features(meal_df) meal_features.compute_features() # temp_meal_features = meal_features.pca_decomposition().tolist() temp_meal_features = meal_features.get_features() labels += [1] * len(temp_meal_features) preprocess_obj_ = Preprocess(no_meal_data_file) no_meal_df = preprocess_obj_.get_dataframe() no_meal_features = Features(no_meal_df) no_meal_features.compute_features() no_meal_features_ = no_meal_features.get_features() # no_meal_final_features = meal_features.pca.transform(no_meal_features_).tolist() no_meal_final_features = no_meal_features_ labels += [0] * len(no_meal_features_) for no_meal_feature in no_meal_final_features: temp_meal_features.append(no_meal_feature) for meal_no_meal_feature in temp_meal_features: data.append(meal_no_meal_feature) return data, labels
class Dummy(object): """ Dummy AI """ def __init__(self): """ Init function""" self.cls = choice # random.choice classifier self.feat = Features() def choose_card(self, metadata): """ This method choose a card randomly. Input: metadata['cards'] : Cards in hand metadata['seat_id'] : The ID of the seat metadata['table'] : Cards on the table Output: Return the best card to play ! """ features_list = self.feat.compute_features(metadata) chosen_feature = self.cls(features_list) stdout.write('[%s' % chosen_feature) return {'color': chosen_feature[0], 'number': chosen_feature[1]}
from preprocessing import Preprocess from classification import Classification import pickle from features import Features import json import os preprocess = Preprocess() preprocess.scale_points() pose_objects = preprocess.new_pose_objects features = [] features_obj = Features(pose_objects=pose_objects) features_obj.compute_features() # reduced_feature_matrix = features_obj.compute_pca() # print(reduced_feature_matrix) # print(len(reduced_feature_matrix),len(reduced_feature_matrix[0])) # X = reduced_feature_matrix X = features_obj.get_features() Y = [obj.label for obj in pose_objects] print(len(X), len(Y)) clf_rforest = Classification('RForest', X, Y) clf_rforest.get_classifier_object() clf_rforest.get_metrics() pickle.dump(clf_rforest.get_classifier(), open('RForest_model.pkl', 'wb')) print()
import pickle from preprocessing import Preprocess from features import Features import numpy as np import pandas as pd test_file_name = input("Please enter the test file name: ") preprocess_obj = Preprocess(test_file_name) test_file_dataframe = preprocess_obj.get_dataframe() test_file_features_obj = Features(test_file_dataframe) test_file_features_obj.compute_features() test_file_features = test_file_features_obj.get_features() # print(len(test_file_features)) # Random Forest random_forest_clf = pickle.load(open('RForest_model.pkl', 'rb')) y_pred = random_forest_clf.predict(test_file_features) print('Saving the output of RandomForest classifier prediction') rforest_dataframe = pd.DataFrame(y_pred, columns=['Meal/NoMeal']) rforest_dataframe.to_csv('RForest_output.csv') # AdaBoost adaboost_clf = pickle.load(open('Adaboost_model.pkl', 'rb')) y_pred = adaboost_clf.predict(test_file_features) print('Saving the output of AdaBoost classifier prediction') adaboost_dataframe = pd.DataFrame(y_pred, columns=['Meal/NoMeal']) adaboost_dataframe.to_csv('Adaboost_output.csv') # XGBoost XGBoost_clf = pickle.load(open('XGBoost_model.pkl', 'rb'))
class Neophyte(object): """ Less dumb AI """ def __init__(self, cls_path=None): """ Init function""" if cls_path is not None: self.cls = load_cls(cls_path) else: self.cls = None self.feat = Features() self.percent_training = 80 def choose_card(self, metadata): """ This method choose a card randomly. Input: metadata['cards'] : Cards in hand metadata['seat_id'] : The ID of the seat metadata['table'] : Cards on the table Output: Return the best card to play ! """ features_list = self.feat.compute_features(metadata) best_indice_index = get_indice( array(self.cls.predict_proba(features_list))).argmax() chosen_feature = features_list[best_indice_index] stdout.write('[%s' % chosen_feature) return {'color': chosen_feature[0], 'number': chosen_feature[1]} def compute_features(self, training_sample): """ This function extract features from the Training sample. """ features = training_sample[:, :-1] scores_resized = map(resize_scores, training_sample[:, -1]) return features, scores_resized def train(self, training_sample): """ This function train a classifier with the Training sample. """ features, scores = self.compute_features(training_sample) # Training # cls = sk.linear_model.LogisticRegression(C=0.01) # cls = svm.SVC(C=1, probability=True, verbose=True) cls = ensemble.RandomForestClassifier(n_estimators=200, max_features=None, n_jobs=-1) # cls = sk.ensemble.AdaBoostClassifier(n_estimators=200) # cls = sk.ensemble.GradientBoostingClassifier(n_estimators=100, \ # learning_rate=0.9, max_depth=10, random_state=0) # cls = sk.ensemble.GradientBoostingRegressor(n_estimators=100, \ # learning_rate=0.1, max_depth=1, random_state=0, loss='ls') # cls = sk.neighbors.KNeighborsclassifier(n_neighbors=1) print 'start cls.fit' cls.fit(features, scores) print 'stop cls.fit' return cls def verify(self, classifier, testing_sample): """ This function verify the classifier's prediction with the Testing sample. """ _, scores = self.compute_features(testing_sample) scores_proba = classifier.predict_proba(testing_sample[:, :-1]) scores_prediction = get_indice(scores_proba) return sum(abs(scores_prediction - scores)) / len(scores) def classifier_generator(self): """ This function generate a classifier. It splits raw data into two sets : - Training sample : 80% - Testing sample : 20% """ # preload_into_pickles() tricks_raw = read_from_pickles() best_classifier = None best_classifier_indice = 99 indice = 0 nb_loop = 5 # It trains 'nb_loop' classifiers for i in range(nb_loop): shuffle(tricks_raw) nb_training_tricks = len(tricks_raw) * self.percent_training / 100 training_sample = tricks_raw[:nb_training_tricks] testing_sample = tricks_raw[nb_training_tricks:] classifier = self.train(training_sample) indice = self.verify(classifier, testing_sample) print '%s/%s (%s)' % (i + 1, nb_loop, indice) if indice < best_classifier_indice: best_classifier = classifier best_classifier_indice = indice save_cls(best_classifier, '%s/%s.%s.cls' % \ (CLS_DIRECTORY, DATA_FILE, best_classifier_indice)) print 'INDICE : %s' % best_classifier_indice
from preprocessing import Preprocess from features import Features cgm_meal_pat_1_preprocess = Preprocess('./MealNoMealData/mealData1.csv') cgm_meal_pat_1 = cgm_meal_pat_1_preprocess.get_dataframe() print(cgm_meal_pat_1) cgm_meal_pat_1_feature_obj = Features(cgm_meal_pat_1) cgm_meal_pat1_features = cgm_meal_pat_1_feature_obj.compute_features() cgm_meal_pat1_final_features = cgm_meal_pat_1_feature_obj.pca_decomposition()