Example #1
0
 def compute(self, X, y):
     # turn data into a row vector (needed for libsvm)
     X = asRowMatrix(X)
     y = np.asarray(y)
     self.svm.fit(X, y)
     self.y = y
     print(self.__repr__())
Example #2
0
 def compute(self, X, y):
     self.logger.debug("SVM TRAINING (C=%.2f,gamma=%.2f,p=%.2f,nu=%.2f,coef=%.2f,degree=%.2f)" % (self.param.C, self.param.gamma, self.param.p, self.param.nu, self.param.coef0, self.param.degree))
     # turn data into a row vector (needed for libsvm)
     X = asRowMatrix(X)
     y = np.asarray(y)
     problem = svm_problem(y, X.tolist())        
     self.svm = svm_train(problem, self.param)
     self.y = y
Example #3
0
 def compute(self, X, y):
     self.logger.debug("SVM TRAINING (C=%.2f,gamma=%.2f,p=%.2f,nu=%.2f,coef=%.2f,degree=%.2f)" % (self.param.C, self.param.gamma, self.param.p, self.param.nu, self.param.coef0, self.param.degree))
     # turn data into a row vector (needed for libsvm)
     X = asRowMatrix(X)
     y = np.asarray(y)
     problem = svm_problem(y, X.tolist())        
     self.svm = svm_train(problem, self.param)
     self.y = y
Example #4
0
def grid_search(model, X, y, tuned_parameters):
    # Check if the Classifier in the Model is actually an SVM:
    if not isinstance(model.classifier, SVM):
        raise TypeError("classifier must be of type SVM!")
    # First compute the features for this SVM-based model:
    features = model.feature.compute(X, y)
    # Turn the List of Features into a matrix with each feature as Row:
    Xrow = asRowMatrix(features)
    # Split the dataset in two equal parts
    X_train, X_test, y_train, y_test = train_test_split(Xrow,
                                                        y,
                                                        test_size=0.5,
                                                        random_state=0)
    # Define the Classifier:
    scores = ['precision', 'recall']
    # Evaluate the Model:
    for score in scores:
        print("# Tuning hyper-parameters for %s" % score)
        print()

        clf = GridSearchCV(SVC(C=1),
                           tuned_parameters,
                           cv=5,
                           scoring='%s_macro' % score)
        clf.fit(X_train, y_train)

        print("Best parameters set found on development set:")
        print()
        print(clf.best_params_)
        print()
        print("Grid scores on development set:")
        print()
        means = clf.cv_results_['mean_test_score']
        stds = clf.cv_results_['std_test_score']
        for mean, std, params in zip(means, stds, clf.cv_results_['params']):
            print("%0.3f (+/-%0.03f) for %r" % (mean, std * 2, params))
        print()

        print("Detailed classification report:")
        print()
        print("The model is trained on the full development set.")
        print("The scores are computed on the full evaluation set.")
        print()
        y_true, y_pred = y_test, clf.predict(X_test)
        print(classification_report(y_true, y_pred))
        print()
Example #5
0
 def compute(self, X, y):
     XR = asRowMatrix(X);
     y = np.asarray(y)
     self._mean = {};
     self._cov = {};
     self._trans = {};
     self._const = {}; 
     unique_labels = np.unique(y);
     for label in unique_labels:
         groupX = XR[y == label, :];
         self._mean[label] = np.squeeze(np.asarray(groupX.mean(axis=0)));
         self._cov[label] = np.zeros(self._mean[label].shape);
         _cov = np.cov(groupX, rowvar=0);
         self._cov[label], self._trans[label] = np.linalg.eig(_cov);
         self._cov[label] = np.asarray(self._cov[label].real, dtype=np.float64);
         self._cov[label] = np.abs(self._cov[label]);
         self._cov[label][self._cov[label] < self.EPS] = self.EPS;
         self._trans[label] = np.asarray(self._trans[label].real, dtype=np.float64);
         self._const[label] = np.log(self._cov[label]).sum();
Example #6
0
 def compute(self, X, y):
     X = asRowMatrix(X)
     y = np.asarray(y)
     self.svm.fit(X, y)
     self.y = y
Example #7
0
 def compute(self, X, y):
     X = asRowMatrix(X)
     y = np.asarray(y)
     self.svm.fit(X, y)
     self.y = y