from random import shuffle, random
from storage import init_db
from storage.models.fight import Fight
from storage.models.fighter import Fighter
from numpy import array, ndarray, asmatrix
from sklearn import svm


class SVMPredictor:
    def __init__(self, featurize):
        self.clf = svm.SVC(gamma=0.001, C=100.)
        self.featurize = featurize

    def learn(self, learning_set):
        target = array([f.outcome for f in learning_set])
        data = asmatrix([self.featurize(f) for f in learning_set])
        self.clf.fit(data, target)

    def predict(self, fight):
        featurized = self.featurize(fight)
        return self.clf.predict(featurized)[0]


def featurize(fight):
    return array(featurize_fighter(fight.fighter1, fight.event) + featurize_fighter(fight.fighter2, fight.event))


def featurize_fighter(fighter, event):
    return [fighter.height, fighter.reach]


class RandomPredictor:
    def learn(self, learning_set):
        pass

    def predict(self, fight):
        if random() < 0.5:
            return -1

        return 1


def cross_validate(predictor, fights):
    shuffle(fights)
    learning_ratio = 0.8
    n = len(fights)
    learning_size = int(n * learning_ratio)
    learning_set, validation_set = fights[:learning_size], fights[learning_size:]

    predictor.learn(learning_set)

    correct = 0
    validation_size = len(validation_set)
    for fight in validation_set:
        outcome = predictor.predict(fight)

        if outcome == fight.outcome:
            correct += 1

    return correct / float(validation_size)


if __name__ == "__main__":
    mysql_connection_string = "mysql+pymysql://{username}:{password}@{host}/{dbname}".format(
        username='tempuser',
        password='temppassword',
        host='localhost',
        dbname='ufcdb')

    memory_connection_string = 'sqlite:///:memory:'

    Session = init_db(mysql_connection_string, create=True)

    session = Session()

    fights = session.query(Fight).all()

    print(cross_validate(SVMPredictor(featurize), fights))

@@ -180,16 +180,27 @@
 asmatrix
+, transpose
 %0Afrom sk
@@ -400,16 +400,20 @@
         
+raw_
 target =
@@ -413,22 +413,16 @@
 arget = 
-array(
 %5Bf.outco
@@ -438,33 +438,32 @@
 in learning_set%5D
-)
 %0A        data = 
@@ -459,24 +459,66 @@
     
-data = asmatrix(
+target = array(raw_target).transpose()%0A        raw_data = 
 %5Bsel
@@ -554,16 +554,56 @@
 ing_set%5D
+%0A        data = asmatrix(array(raw_data)
 )%0A      
@@ -787,22 +787,16 @@
  return 
-array(
 featuriz
@@ -882,17 +882,16 @@
 t.event)
-)
 %0A%0A%0Adef f
@@ -932,46 +932,526 @@
     
-return %5Bfighter.height, fighter.reach%5D
+previous_fights = %5Bf for f in fighter.fights if f.event.date %3C event.date%5D%0A    wins = len(%5Bf for f in previous_fights if fighter_win(fighter, f)%5D)%0A    losses = len(previous_fights) - wins%0A    win_ratio_feature = 0.5%0A%0A    if len(previous_fights) != 0:%0A        win_ratio_feature = float(wins)/len(previous_fights)%0A%0A    return %5Bfighter.height, fighter.reach, win_ratio_feature%5D%0A%0A%0Adef fighter_win(fighter, f):%0A    return f.fighter1.ref == fighter.ref and f.outcome == 1 or f.fighter2.ref == fighter.ref and f.outcome == -1
 %0A%0A%0Ac

Add win ratio feature