Пример #1
0
def main():
    
    classifier1 = RandomForestClassifier(n_estimators = 100, max_features=0.5, max_depth=5.0)
    classifier2 = DecisionTreeClassifier(max_depth = 10, criterion = 'entropy', random_state = 0)
    classifier3 = KNeighborsClassifier(n_neighbors = 5, p = 2, metric = 'minkowski')
    classifier4 = SVC(kernel = 'rbf', C = 10.0, random_state = 0, gamma = 0.10)
    classifier5 = LogisticRegression(penalty = 'l2', C = 1.0, random_state = 0)
    classifier6 = GaussianNB()
        
    print("Reading in the training data")
    train = data_io.get_train_df()
    
    print ("Cleaning data. Check here for imputation, One hot encoding and factorization procedures..")
    train = FeatureConverter().clean_data(train)
    train.drop(['PassengerId'], axis = 1, inplace = True)
    #print train.head()
    train = train.values
    
    eclf = EnsembleClassifier(clfs = [classifier1, classifier2, classifier3, classifier5, classifier6], voting = 'hard')
    #eclf = EnsembleClassifier(clfs = [classifier2], voting = 'hard')    
    scores = cross_val_score(estimator = eclf, X = train[0:,1:], y = train[0:,0], cv = 10, scoring = 'roc_auc')
    
    print("Accuracy: %0.4f (+/- %0.3f)" % (scores.mean(), scores.std()))
    eclf.fit(train[0:,1:],train[0:,0])

    print("Saving the classifier")
    data_io.save_model(eclf)
Пример #2
0
def main():
    
    classifier1 = RandomForestClassifier(n_estimators = 100, max_features=0.5, max_depth=5.0)
    classifier2 = DecisionTreeClassifier(max_depth = 10, criterion = 'entropy', random_state = 0)
    classifier3 = KNeighborsClassifier(n_neighbors = 5, p = 2, metric = 'minkowski')
    classifier4 = SVC(kernel = 'rbf', C = 10.0, random_state = 0, gamma = 0.10)
    classifier5 = LogisticRegression(penalty = 'l2', C = 1.0, random_state = 0)
    classifier6 = GaussianNB()
    classifier7 = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
          
    print("Reading in the training data")
    train = data_io.get_train_df()
    
    print ("Cleaning data. Check here for imputation, One hot encoding and factorization procedures..")
    train = FeatureConverter().clean_data(train)
    train.drop(['Id'], axis = 1, inplace = True)
    #print train.head()
    train = train.values
    
    #eclf = EnsembleClassifier(clfs = [classifier1, classifier2, classifier3, classifier5, classifier6], voting = 'hard')
    #eclf = EnsembleClassifier(clfs = [classifier1], voting = 'hard')
    eclf = classifier3
    #scores = cross_val_score(estimator = eclf, X = train[0:,0:-1], y = train[0:,-1], cv = 10, scoring = 'roc_auc')
    
    #print("Accuracy: %0.4f (+/- %0.3f)" % (scores.mean(), scores.std()))
    eclf.fit(train[0:,0:-1],train[0:,-1])

#     importances = eclf.feature_importances_
#     indices = np.argsort(importances)[::-1]
#     for f in range(train[0:,0:-1].shape[1]):
#         print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
#         
    print("Saving the classifier")
    data_io.save_model(eclf)
 def __init__(self, gamma, above_epsilon, max_stack, step_size):
     self.gamma = gamma
     self.above_epsilon = above_epsilon
     self.curr_x = 0
     self.possible_actions = np.linspace(176, 464, 10)
     self.fc = FeatureConverter(max_stack, self.possible_actions)
     self.max_turns = max_stack - 1
     self.total_turns = []
     self.biggest_change = float('-inf')
     self.step_size = step_size
     self.all_rewards = []
     self.previous_action = 'EEEE'
Пример #4
0
def main():
    print("Loading the test data")
    classifier = data_io.load_model()
    
    print ("Load test data. And Clean..")
    test = data_io.get_test_df()
    test = FeatureConverter().clean_data(test)
    passengerIds = test['Id']
    test.drop(['Id'], axis = 1, inplace = True)
    test = test.values
    
    print("Making predictions") 
    predictions = classifier.predict(test).astype(int)
    #predictions = predictions.reshape(len(predictions), 1)
    
    print("Writing predictions to file")
    data_io.write_submission(predictions, passengerIds, ['Id', 'Cover_Type'])
Пример #5
0
def main():
    print("Loading the test data")
    classifier = data_io.load_model()

    print("Load test data. And Clean..")
    test = data_io.get_test_df()
    test = FeatureConverter().clean_data(test)
    passengerIds = test['Id']
    test.drop(['Id'], axis=1, inplace=True)
    test = test.values

    print("Making predictions")
    predictions = classifier.predict(test).astype(int)
    #predictions = predictions.reshape(len(predictions), 1)

    print("Writing predictions to file")
    data_io.write_submission(predictions, passengerIds, ['Id', 'Cover_Type'])
Пример #6
0
}

svc_params_grid = \
{
    'C': [0.001, 0.1, 1.0, 10.0, 100.0],
    'kernel': ['linear', 'rbf'],
    'gamma': [0.001, 0.1, 1.0, 10.0, 100.0]
}

if __name__=="__main__":
    
    print("Reading in the training data")
    train = data_io.get_train_df()
    
    print ("Cleaning data. Check here for imputation, One hot encoding and factorization procedures..")
    train = FeatureConverter().clean_data(train)
    train.drop(['PassengerId'], axis = 1, inplace = True)
    #print train.head()
    train = train.values
    
    grid_search = GridSearchCV(RandomForestClassifier(n_estimators = 100), rf_params_grid, cv = 5, verbose = 1)
    grid_search.fit(train[0:,1:],train[0:,0])
    
    print grid_search.best_params_
    
    grid_search = GridSearchCV(LogisticRegression(random_state = 0), lr_params_grid, cv = 5, verbose = 1)
    grid_search.fit(train[0:,1:],train[0:,0])
    
    print grid_search.best_params_
    
    grid_search = GridSearchCV(SVC(random_state = 0), svc_params_grid, cv = 5, verbose = 1)
class Q_learner:
    def __init__(self, gamma, above_epsilon, max_stack, step_size):
        self.gamma = gamma
        self.above_epsilon = above_epsilon
        self.curr_x = 0
        self.possible_actions = np.linspace(176, 464, 10)
        self.fc = FeatureConverter(max_stack, self.possible_actions)
        self.max_turns = max_stack - 1
        self.total_turns = []
        self.biggest_change = float('-inf')
        self.step_size = step_size
        self.all_rewards = []
        self.previous_action = 'EEEE'
        # state_dict architecture: upper_level is height (so number of turns),
        # second is positions of objects in bottom-up height order, third is actions,
        # final level is reward.

    def train(self, iter):
        #screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), display=1)
        #pygame.display.set_caption('test')
        #clock = pygame.time.Clock()
        self.env = Environment()
        self.biggest_change = float('-inf')
        success_bool = True
        first_loop = True
        turn = -1
        self.curr_state = []
        curr_reward = []
        self.init_state = self.env.body_list[0].position
        while success_bool:
            turn += 1
            action = self.choose_move_train(first_loop, turn)
            obj_list, sizes, reward, success_bool = self.env.play_action(
                action, (32, 32))
            self.curr_state = [self.init_state]
            for box in obj_list[1:]:
                self.curr_state.append(box.position)
            # print(self.curr_state)
            if first_loop:
                next_move = [reward, self.curr_state, action]
                first_loop = False
            else:
                update_move = next_move[:]
                next_move = [reward, self.curr_state, action]
                self.update_val(next_move, update_move, turn)
            if turn == self.max_turns:
                success_bool = False
            curr_reward.append(reward)
            # if iter % 100 == 0:
            # self.update_screen()

        if turn == 0:
            update_move = next_move[:]

        #self.all_rewards.append(max(curr_reward))
        #self.total_turns.append(turn)
        self.final_value_update(reward, update_move, turn)

    def test(self, iter, show_screen):
        #screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), display=1)
        #pygame.display.set_caption('test')
        #clock = pygame.time.Clock()
        self.env = Environment()
        self.biggest_change = float('-inf')
        success_bool = True
        first_loop = True
        turn = -1
        self.curr_state = []
        curr_reward = []
        self.init_state = self.env.body_list[0].position
        while success_bool:
            turn += 1
            action = self.choose_move_test(first_loop, turn)
            obj_list, sizes, reward, success_bool = self.env.play_action(
                action, (32, 32))
            self.curr_state = [self.init_state]
            first_loop = False
            for box in obj_list[1:]:
                self.curr_state.append(box.position)
            if turn == self.max_turns:
                success_bool = False
            curr_reward.append(reward)
            # if iter % 100 == 0:
            if show_screen:
                self.update_screen()
        self.all_rewards.append(max(curr_reward))
        self.total_turns.append(turn)

    def choose_move_train(self, first_loop, turn):
        possible_actions = self.possible_actions.tolist()
        if random.uniform(0, 1) > self.above_epsilon:
            action = False
            prev_reward = float('-inf')
            for move in possible_actions:
                move_reward = self.fc.predict(self.curr_state, move, turn)
                if move_reward > prev_reward:
                    action = move
                    prev_reward = move_reward
        else:

            action = np.random.choice(self.possible_actions)

        return action

    def choose_move_test(self, first_loop, turn):
        possible_actions = self.possible_actions.tolist()
        action = False
        prev_reward = float('-inf')
        for move in possible_actions:
            move_reward = self.fc.predict(self.curr_state, move, turn)
            if move_reward > prev_reward:
                action = move
                prev_reward = move_reward

        return action

    def update_val(self, next_move, update_move, turn):
        old_theta = self.fc.theta.copy()
        #max_action = float('-inf')
        #for move in self.possible_actions:
        #    a = self.fc.predict(next_move[1], move, turn)
        #    if a > max_action:
        #        max_action = a

        update_g = self.step_size * (next_move[0] + self.gamma * self.fc.predict(next_move[1], next_move[2], turn) -
                                      self.fc.predict(update_move[1], update_move[2], turn)) * \
                                      self.fc.grad(update_move[1], update_move[2], turn)
        self.fc.theta += update_g
        self.biggest_change = max(self.biggest_change,
                                  np.abs(self.fc.theta - old_theta).sum())

    def final_value_update(self, reward, update_move, turn):
        old_theta = self.fc.theta.copy()
        update_g = self.step_size * (reward - self.fc.predict(update_move[1], update_move[2], turn)) * \
                         self.fc.grad(update_move[1], update_move[2], turn)

        self.fc.theta += update_g

        self.biggest_change = max(self.biggest_change,
                                  np.abs(self.fc.theta - old_theta).sum())

    def update_screen(self):
        screen.fill((0, 0, 0, 0))
        iterator = 0
        for iterator, body in enumerate(self.env.body_list):
            # print(len(self.env.body_list))
            for fixture in body.fixtures:
                # The fixture holds information like density and friction,g
                # and also the shape.
                shape = fixture.shape

                # Naively assume that this is a polygon shape. (not good normally!)
                # We take the body's transform and multiply it with each
                # vertex, and then convert from meters to pixels with the scale
                # factor.
                vertices = [(body.transform * v) * PPM for v in shape.vertices]

                # But wait! It's upside-down! Pygame and Box2D orient their
                # axes in different ways. Box2D is just like how you learned
                # in high school, with positive x and y directions going
                # right and up. Pygame, on the other hand, increases in the
                # right and downward directions. This means we must flip
                # the y components.
                vertices = [(v[0], SCREEN_HEIGHT - v[1]) for v in vertices]

                pygame.draw.polygon(screen, self.env.colour_list[iterator],
                                    vertices)
        SARSA_agent.env.world.Step(TIME_STEP, 10, 10)

        # Flip the screen and try to keep at the target FPS
        pygame.display.flip()
        clock.tick(TARGET_FPS)
Пример #8
0
svc_params_grid = \
{
    'C': [0.001, 0.1, 1.0, 10.0, 100.0],
    'kernel': ['linear', 'rbf'],
    'gamma': [0.001, 0.1, 1.0, 10.0, 100.0]
}

if __name__ == "__main__":

    print("Reading in the training data")
    train = data_io.get_train_df()

    print(
        "Cleaning data. Check here for imputation, One hot encoding and factorization procedures.."
    )
    train = FeatureConverter().clean_data(train)
    train.drop(['PassengerId'], axis=1, inplace=True)
    #print train.head()
    train = train.values

    grid_search = GridSearchCV(RandomForestClassifier(n_estimators=100),
                               rf_params_grid,
                               cv=5,
                               verbose=1)
    grid_search.fit(train[0:, 1:], train[0:, 0])

    print grid_search.best_params_

    grid_search = GridSearchCV(LogisticRegression(random_state=0),
                               lr_params_grid,
                               cv=5,