<commit_before>from pnet.layer import SupervisedLayer
from pnet.layer import Layer
import numpy as np
import amitgroup as ag
from sklearn.svm import LinearSVC

@Layer.register('svm-classification-layer')
class SVMClassificationLayer(SupervisedLayer):
    def __init__(self, C=1.0, settings={}):
        self._penalty = C
        self._svm = None

    @property
    def trained(self):
        return self._svm is not None

    @property
    def classifier(self):
        return True;

    def extract(self,X):
        Xflat = X.reshape((X.shape[0], -1))
        return self._svm.predict(Xflat) 

    def train(self, X, Y, OriginalX = None):
        Xflat = X.reshape((X.shape[0], -1))
        svc = LinearSVC(C=self._penalty)
        svc.fit(Xflat, Y)
        self._svm = svc

    def save_to_dict(self):
        d = {}
        d['svm'] = self._svm
        d['penalty'] = self._penalty
        return d

    @classmethod
    def load_from_dict(cls, d):
        obj = cls(C=d['penalty'])
        obj._svm = d['svm']
        return obj
<commit_msg>add cv procedure to svm classification<commit_after>from __future__ import division, print_function, absolute_import
from pnet.layer import SupervisedLayer
from pnet.layer import Layer
import numpy as np
import amitgroup as ag
from sklearn.svm import LinearSVC
from sklearn import cross_validation

@Layer.register('svm-classification-layer')
class SVMClassificationLayer(SupervisedLayer):
    def __init__(self, C=1.0, settings={}):
        self._penalty = C
        self._settings = settings
        self._svm = None

    @property
    def trained(self):
        return self._svm is not None

    @property
    def classifier(self):
        return True;

    def extract(self,X):
        Xflat = X.reshape((X.shape[0], -1))
        return self._svm.predict(Xflat) 

    def train(self, X, Y):
        Xflat = X.reshape((X.shape[0], -1))
        if self._penalty is None:
            Cs = 10**np.linspace(-1, -4, 10)
            avg_scores = np.zeros(len(Cs))
            for i, C in enumerate(Cs):
                clf = LinearSVC(C=C)
                scores = cross_validation.cross_val_score(clf, Xflat, Y, cv=5)
                avg_scores[i] = np.mean(scores)
                print('C', C, 'scores', scores, 'avg', np.mean(scores))

            Ci = np.argmax(avg_scores)
            C = Cs[Ci]

            clf = LinearSVC(C=C)
            clf.fit(Xflat, Y)

        else:
            # Cross-validate the penalty
            clf = LinearSVC(C=self._penalty, random_state=self._settings.get('seed', 0))
            clf.fit(Xflat, Y)

        self._svm = clf 

    def save_to_dict(self):
        d = {}
        d['svm'] = self._svm
        d['settings'] = self._settings
        d['penalty'] = self._penalty
        return d

    @classmethod
    def load_from_dict(cls, d):
        obj = cls(C=d['penalty'])
        obj._settings = d['settings']
        obj._svm = d['svm']
        return obj

{'fraction_non_alphanumeric': 0.11854619565217392, 'fraction_numerical': 0.006793478260869565, 'mean_word_length': 2.215065502183406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7464959', 'n_tokens_mistral': 1019, 'n_tokens_neox': 954, 'n_words': 233}