Пример #1
0
def test_lda_transform():
    # Test LDA transform.
    clf = lda.LDA(solver="svd", n_components=1)
    X_transformed = clf.fit(X, y).transform(X)
    assert_equal(X_transformed.shape[1], 1)
    clf = lda.LDA(solver="eigen", n_components=1)
    X_transformed = clf.fit(X, y).transform(X)
    assert_equal(X_transformed.shape[1], 1)
Пример #2
0
def test_lda_transform():
    # Test LDA transform.
    clf = lda.LDA(solver="svd", n_components=1)
    X_transformed = clf.fit(X, y).transform(X)
    assert_equal(X_transformed.shape[1], 1)
    clf = lda.LDA(solver="eigen", n_components=1)
    X_transformed = clf.fit(X, y).transform(X)
    assert_equal(X_transformed.shape[1], 1)

    clf = lda.LDA(solver="lsqr", n_components=1)
    clf.fit(X, y)
    msg = "transform not implemented for 'lsqr'"
    assert_raise_message(NotImplementedError, msg, clf.transform, X)
Пример #3
0
def binary_lda(training_feature_array, training_label_array,
               test_feature_array, test_label_array):
    """
    2クラス分類LDA(しかし多クラス分類で代用可能だった現実...)
    @param training_feature_array: トレーニング用データ
    @param training_label_array: トレーニング用データラベル
    @param test_feature_array: テスト用データ
    @param test_label_array: テスト用データラベル
    
    @return: 識別率, 識別結果のリスト, 識別されたクラスへの所属確率のリスト, LDAオブジェクト
    
    動作確認済
    """
    lda_obj = slda.LDA()
    lda_obj.fit(training_feature_array, training_label_array)
    
    print "test..."
    class_result = lda_obj.predict(test_feature_array)
    proba_result = lda_obj.predict_proba(test_feature_array)
    proba_max_result = np.max(proba_result, axis=1)
    try:
        precision = smet.accuracy_score(test_label_array, class_result)
        #precision, recall, fmeasure, sup = smet.precision_recall_fscore_support(test_label_array, class_result, 
        #                                                                        average='micro')    
    except DeprecationWarning, e:
        pass
Пример #4
0
 def buildModelQDA(self, outputFile, priorProbs=[0.5, 0.5]):
     classifier = lda.LDA(priors=priorProbs)
     classifier.fit(self.instances, self.classes)
     modelData = pickle.dumps(classifier)
     f = open(outputFile, "w")
     f.write(modelData)
     f.close()
Пример #5
0
def LDAclassification(train_set,test_set,train_ann,test_ann):

    global path, train_perc

    #Create z-scored data
    normalized_train = zscore(train_set)

    classifier = lda.LDA('lsqr')

    #train the LDA classifier
    classifier.fit(train_set, train_ann)

    #store the trained classifier
    if train_perc<1.0:
    	pickle.dump( classifier, open(path+"LDA_classifier.p", "wb+" ) )
        
    results = classifier.predict(test_set)
 
    #res2 = classifier.predict()


    cm = confusion_matrix(test_ann, results)

    print 'CONFUSION MATRIX = {}'.format(cm)
    return metrics(cm)
Пример #6
0
def fit_model(df, model='logistic_regression', max_depth=5):
	# split the target attribute from the explanatory variables
	y = df['clicks']
	x = df.drop(['clicks', 'paying_price'], axis=1)
	
	# fit the model
	if model == 'logistic_regression':
		m = linear_model.LogisticRegression()
		m.fit(x, y)
		df_coef = pd.DataFrame(columns=['attribute','logreg_coef'])
		df_coef['attribute'] = x.columns.values
		df_coef['logreg_coef'] = np.ravel(m.coef_)
		df_coef.to_csv(TABLES_DIR_PATH + 'logreg_coef.csv')
		print 'Logistic regression coefficients:'
		print df_coef
	elif model == 'tree':
		m = tree.DecisionTreeClassifier(max_depth=max_depth)
		m.fit(x, y)
	elif model == 'lda':
		m = lda.LDA()
		m.fit(x, y)
	elif model == 'naivebayes':
		m = naive_bayes.GaussianNB()
		m.fit(x, y)

	return m
Пример #7
0
def visualize(X, y, name):
    X2 = X.copy()
    X2.flat[::X.shape[1] + 1] += 0.01  # Make X invertible
    t0 = time()
    X_lda = lda.LDA(n_components=2).fit_transform(X2, y)
    plot_embedding(X_lda, y, name, "haha", True)
    plt.show()
Пример #8
0
def test_lda_predict():
    """
    LDA classification.

    This checks that LDA implements fit and predict and returns
    correct values for a simple toy dataset.
    """

    clf = lda.LDA()
    y_pred = clf.fit(X, y).predict(X)

    assert_array_equal(y_pred, y)

    # Assure that it works with 1D data
    y_pred1 = clf.fit(X1, y).predict(X1)
    assert_array_equal(y_pred1, y)

    # Test probas estimates
    y_proba_pred1 = clf.predict_proba(X1)
    assert_array_equal((y_proba_pred1[:, 1] > 0.5) + 1, y)
    y_log_proba_pred1 = clf.predict_log_proba(X1)
    assert_array_almost_equal(np.exp(y_log_proba_pred1), y_proba_pred1, 8)

    # Primarily test for commit 2f34950 -- "reuse" of priors
    y_pred3 = clf.fit(X, y3).predict(X)
    # LDA shouldn't be able to separate those
    assert_true(np.any(y_pred3 != y3))
def run_classification(args, features, labels):
    class_weight = {
        0: 1.0,
        1: float(len(labels) - sum(labels)) / float(sum(labels))
    }

    # Assemble data
    if args.classifier == 'lda':
        clf = lda.LDA()
    elif args.classifier == 'lsvm':
        clf = svm.LinearSVC()
    elif args.classifier == 'svm':
        clf = svm.SVC(kernel='rbf', class_weight=class_weight)
    elif args.classifier == 'rf':
        clf = ensemble.RandomForestClassifier()

    sensitivity_scorer = make_scorer(sensitivity, greater_is_better=True)
    specificity_scorer = make_scorer(specificity, greater_is_better=True)

    accuracies = cross_validation.cross_val_score(clf,
                                                  features,
                                                  labels,
                                                  cv=args.num_folds,
                                                  scoring='accuracy')
    sensitivities = cross_validation.cross_val_score(
        clf, features, labels, cv=args.num_folds, scoring=sensitivity_scorer)
    specificities = cross_validation.cross_val_score(
        clf, features, labels, cv=args.num_folds, scoring=specificity_scorer)

    return accuracies.mean(), sensitivities.mean(), specificities.mean()
Пример #10
0
def applyLDA(finalFeatures, finalAnswers, cvFeatures, testFeatures, n_components=200):
    print 'Applying LDA...'
    selLda = lda.LDA(n_components=n_components)
    finalFeatures = selLda.fit_transform(finalFeatures, finalAnswers)
    cvFeatures = selLda.transform(cvFeatures)
    testFeatures = selLda.transform(testFeatures)
    return (finalFeatures, cvFeatures, testFeatures)
Пример #11
0
def validate_feature_linear(features,
                            labels,
                            classes,
                            n_folds=5,
                            print_folds=True,
                            print_absolute=True,
                            print_logloss=True):
    kfold = cv.LabelKFold(labels, n_folds)
    model = lda.LDA()
    if print_absolute:
        score = cross_validation.cross_val_score(model,
                                                 features,
                                                 classes,
                                                 cv=kfold)
    if print_absolute: print("absolute scores")
    if print_folds: print("\tfolds:", score)
    if print_absolute: print("\tmean:", score.mean(), "std:", numpy.std(score))

    scores = score_calculation.loglossKFold(features,
                                            classes,
                                            model,
                                            kfold,
                                            given_kfold=True)
    if print_logloss: print("logloss scores")
    if print_folds: print("\tfolds", scores)
    if print_logloss:
        print("\tmean:", numpy.mean(scores), "std:", numpy.std(scores))
Пример #12
0
 def __init__(self, classifier):
     if classifier == 'svm':
         self.clf = svm.SCV()
     elif classifier == 'lda':
         self.clf = lda.LDA()
     elif classifier == 'qda':
         self.clf = qda.QDA()
Пример #13
0
def test_lda_orthogonality():
    # arrange four classes with their means in a kite-shaped pattern
    # the longer distance should be transformed to the first component, and
    # the shorter distance to the second component.
    means = np.array([[0, 0, -1], [0, 2, 0], [0, -2, 0], [0, 0, 5]])

    # We construct perfectly symmetric distributions, so the LDA can estimate
    # precise means.
    scatter = np.array([[0.1, 0, 0], [-0.1, 0, 0], [0, 0.1, 0], [0, -0.1, 0],
                        [0, 0, 0.1], [0, 0, -0.1]])

    X = (means[:, np.newaxis, :] + scatter[np.newaxis, :, :]).reshape((-1, 3))
    y = np.repeat(np.arange(means.shape[0]), scatter.shape[0])

    # Fit LDA and transform the means
    clf = lda.LDA(solver="svd").fit(X, y)
    means_transformed = clf.transform(means)

    d1 = means_transformed[3] - means_transformed[0]
    d2 = means_transformed[2] - means_transformed[1]
    d1 /= np.sqrt(np.sum(d1**2))
    d2 /= np.sqrt(np.sum(d2**2))

    # the transformed within-class covariance should be the identity matrix
    assert_almost_equal(np.cov(clf.transform(scatter).T), np.eye(2))

    # the means of classes 0 and 3 should lie on the first component
    assert_almost_equal(np.abs(np.dot(d1[:2], [1, 0])), 1.0)

    # the means of classes 1 and 2 should lie on the second component
    assert_almost_equal(np.abs(np.dot(d2[:2], [0, 1])), 1.0)
Пример #14
0
def run_classification(args, features1, features2, features3=None):
    # Assemble data
    if features3 is None:
        features = np.concatenate((features1, features2))
        labels = np.concatenate(
            (np.zeros(len(features1)), np.ones(len(features2))))
    else:
        features = np.concatenate((features1, features2, features3))
        labels = np.concatenate(
            (np.zeros(len(features1)), np.ones(len(features2)),
             2 * np.ones(len(features3))))

    if args.classifier == 'lda':
        # priors_second = float(sum(labels)) / float(len(labels))
        # priors = [1 - priors_second, priors_second]
        # clf = lda.LDA(priors=priors)
        clf = lda.LDA()
    elif args.classifier == 'lsvm':
        clf = svm.LinearSVC()
    elif args.classifier == 'svm':
        class_weight = {
            0: 1.0,
            1: float(len(labels) - sum(labels)) / float(sum(labels))
        }
        clf = svm.SVC(kernel='rbf', class_weight=class_weight)
    elif args.classifier == 'rf':
        clf = ensemble.RandomForestClassifier()

    sensitivity_scorer = make_scorer(sensitivity, greater_is_better=True)
    specificity_scorer = make_scorer(specificity, greater_is_better=True)

    accuracies = 0.0
    sensitivities = 0.0
    specificities = 0.0
    for _ in range(args.num_runs):
        accuracies += cross_validation.cross_val_score(clf,
                                                       features,
                                                       labels,
                                                       cv=args.num_folds,
                                                       scoring='accuracy')
        sensitivities += cross_validation.cross_val_score(
            clf,
            features,
            labels,
            cv=args.num_folds,
            scoring=sensitivity_scorer)
        specificities += cross_validation.cross_val_score(
            clf,
            features,
            labels,
            cv=args.num_folds,
            scoring=specificity_scorer)
    accuracies /= args.num_runs
    sensitivities /= args.num_runs
    specificities /= args.num_runs

    return accuracies.mean(), sensitivities.mean(), specificities.mean()
Пример #15
0
def get_classifier(classifier_str):
    '''
    This functions maps the classifier string classifier_str to the
    corresponding classifier object with the default paramers set.
    '''

    # SVC
    if (classifier_str == 'linearsvc'):
        cl = svm.LinearSVC(**svm_default_param)
    elif (classifier_str == 'svc_linear'):
        libsvm_default_param['kernel'] = 'linear'
        cl = svm.SVC(**libsvm_default_param)
    elif (classifier_str == 'svc_rbf'):
        libsvm_default_param['kernel'] = 'rbf'
        cl = svm.SVC(**libsvm_default_param)
    # polynomial, sigmoid kernel
    # nuSVC
    # Nearest Neighbors (euclidian distance used by default)
    elif (classifier_str == 'kn_uniform'):
        kn_default_param['weights'] = 'uniform'
        cl = neighbors.KNeighborsClassifier(**kn_default_param)
    elif (classifier_str == 'kn_distance'):
        kn_default_param['weights'] = 'distance'
        cl = neighbors.KNeighborsClassifier(**kn_default_param)
    elif (classifier_str == 'rn_uniform'):
        rn_default_param['weights'] = 'uniform'
        cl = neighbors.RadiusNeighborsClassifier(**rn_default_param)
    elif (classifier_str == 'rn_distance'):
        rn_default_param['weights'] = 'distance'
        cl = neighbors.RadiusNeighborsClassifier(**rn_default_param)
    elif (classifier_str == 'nc'):
        cl = neighbors.NearestCentroid()
    # LDA and QDA, priors are by default set to 1/len(class) for each class
    elif (classifier_str == 'lda'):
        cl = lda.LDA()
    elif (classifier_str == 'qda'):
        cl = qda.QDA()
    # Gaussion naive bayes
    # from the code it is unclear how priors are set
    elif (classifier_str == 'gnb'):
        cl = naive_bayes.GaussianNB()
    elif (classifier_str == 'mnb'):
        cl = naive_bayes.MultinomialNB()
    elif (classifier_str == 'bnb'):
        cl = naive_bayes.BernoulliNB()
    # Decision tree
    elif (classifier_str == 'dtree'):
        cl = tree.DecisionTreeClassifier()
    elif (classifier_str == 'rforest'):
        cl = ensemble.RandomForestClassifier()
    else:
        # raise error if classifier not found
        raise ValueError('Classifier not implemented: %s' % (classifier_str))

    return (cl)
Пример #16
0
    def lda_predict(self, unlabelled):
        ''' Use Linear Discriminant Analysis for classification.
        WARNING: Will only work when we have multiple data samples
        for each dataset (i.e., two for left, two for right, etc.)'''
        self.clf = lda.LDA()
        self.clf.fit(self.dataset, self.targets)

        unlabelled = np.array(unlabelled)
        unlabelled = unlabelled.flatten()

        target = self.clf.predict(unlabelled)
        return self.positions[target[0]]
Пример #17
0
def test_lda_coefs():
    # Test if the coefficients of the solvers are approximately the same.
    n_features = 2
    n_classes = 2
    n_samples = 1000
    X, y = make_blobs(n_samples=n_samples,
                      n_features=n_features,
                      centers=n_classes,
                      random_state=11)

    clf_lda_svd = lda.LDA(solver="svd")
    clf_lda_lsqr = lda.LDA(solver="lsqr")
    clf_lda_eigen = lda.LDA(solver="eigen")

    clf_lda_svd.fit(X, y)
    clf_lda_lsqr.fit(X, y)
    clf_lda_eigen.fit(X, y)

    assert_array_almost_equal(clf_lda_svd.coef_, clf_lda_lsqr.coef_, 1)
    assert_array_almost_equal(clf_lda_svd.coef_, clf_lda_eigen.coef_, 1)
    assert_array_almost_equal(clf_lda_eigen.coef_, clf_lda_lsqr.coef_, 1)
def svmf(index_train , index_test):
    cnt = 0
    err = 0
    clf = lda.LDA(gamma = 'scale')
    X = fea_copy[index_train]
    y = gnd[index_train]
    clf.fit(X, y.ravel())
    for i in range(len(index_test)):
        if clf.predict(fea_copy[[index_test[i]]]) != gnd[index_test[i]]:
            err += 1
        cnt += 1
    return cnt,err
Пример #19
0
def test_lda_predict():
    """Test LDA classification.

    This checks that LDA implements fit and predict and returns correct values
    for simple toy data.
    """
    for test_case in solver_shrinkage:
        solver, shrinkage = test_case
        clf = lda.LDA(solver=solver, shrinkage=shrinkage)
        y_pred = clf.fit(X, y).predict(X)
        assert_array_equal(y_pred, y, 'solver %s' % solver)

        # Assert that it works with 1D data
        y_pred1 = clf.fit(X1, y).predict(X1)
        assert_array_equal(y_pred1, y, 'solver %s' % solver)

        # Test probability estimates
        y_proba_pred1 = clf.predict_proba(X1)
        assert_array_equal((y_proba_pred1[:, 1] > 0.5) + 1, y,
                           'solver %s' % solver)
        y_log_proba_pred1 = clf.predict_log_proba(X1)
        assert_array_almost_equal(np.exp(y_log_proba_pred1), y_proba_pred1, 8,
                                  'solver %s' % solver)

        # Primarily test for commit 2f34950 -- "reuse" of priors
        y_pred3 = clf.fit(X, y3).predict(X)
        # LDA shouldn't be able to separate those
        assert_true(np.any(y_pred3 != y3), 'solver %s' % solver)

    # Test invalid shrinkages
    clf = lda.LDA(solver="lsqr", shrinkage=-0.2231)
    assert_raises(ValueError, clf.fit, X, y)
    clf = lda.LDA(solver="eigen", shrinkage="dummy")
    assert_raises(ValueError, clf.fit, X, y)
    clf = lda.LDA(solver="svd", shrinkage="auto")
    assert_raises(NotImplementedError, clf.fit, X, y)
    # Test unknown solver
    clf = lda.LDA(solver="dummy")
    assert_raises(ValueError, clf.fit, X, y)
Пример #20
0
    def __init__(self, name='default'):
        self.clf = lda.LDA()
        self.scaler = preprocessing.StandardScaler()
        self.selector = None
        self.transform = None
        self.extractor = HardieExtractor()
        self.feature_set = None
        self.network = None

        self.name = name
        self.roi_size = 64
        self.dataset_type = 'numpy'
        self.file = None # datasets_file
Пример #21
0
def plot_simple_demo_lda():
    pylab.clf()
    fig = pylab.figure(num=None, figsize=(10, 4))
    pylab.subplot(121)

    title = "Original feature space"
    pylab.title(title)
    pylab.xlabel("$X_1$")
    pylab.ylabel("$X_2$")

    good = x1 > x2
    bad = ~good

    x1g = x1[good]
    x2g = x2[good]
    pylab.scatter(x1g, x2g, edgecolor="blue", facecolor="blue")

    x1b = x1[bad]
    x2b = x2[bad]
    pylab.scatter(x1b, x2b, edgecolor="red", facecolor="white")

    pylab.grid(True)

    pylab.subplot(122)

    X = np.c_[(x1, x2)]

    lda_inst = lda.LDA(n_components=1)
    Xtrans = lda_inst.fit_transform(X, good)

    Xg = Xtrans[good]
    Xb = Xtrans[bad]

    pylab.scatter(Xg[:, 0],
                  np.zeros(len(Xg)),
                  edgecolor="blue",
                  facecolor="blue")
    pylab.scatter(Xb[:, 0],
                  np.zeros(len(Xb)),
                  edgecolor="red",
                  facecolor="white")
    title = "Transformed feature space"
    pylab.title(title)
    pylab.xlabel("$X'$")
    fig.axes[1].get_yaxis().set_visible(False)

    pylab.grid(True)

    pylab.autoscale(tight=True)
    filename = "lda_demo.png"
    pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
Пример #22
0
def test_lda_explained_variance_ratio():
    # Test if the sum of the normalized eigen vectors values equals 1
    n_features = 2
    n_classes = 2
    n_samples = 1000
    X, y = make_blobs(n_samples=n_samples,
                      n_features=n_features,
                      centers=n_classes,
                      random_state=11)

    clf_lda_eigen = lda.LDA(solver="eigen")

    clf_lda_eigen.fit(X, y)

    assert_almost_equal(clf_lda_eigen.explained_variance_ratio_.sum(), 1.0, 3)
Пример #23
0
def test_lda_scaling():
    # Test if classification works correctly with differently scaled features.
    n = 100
    rng = np.random.RandomState(1234)
    # use uniform distribution of features to make sure there is absolutely no
    # overlap between classes.
    x1 = rng.uniform(-1, 1, (n, 3)) + [-10, 0, 0]
    x2 = rng.uniform(-1, 1, (n, 3)) + [10, 0, 0]
    x = np.vstack((x1, x2)) * [1, 100, 10000]
    y = [-1] * n + [1] * n

    for solver in ('svd', 'lsqr', 'eigen'):
        clf = lda.LDA(solver=solver)
        # should be able to separate the data perfectly
        assert_equal(
            clf.fit(x, y).score(x, y), 1.0, 'using covariance: %s' % solver)
Пример #24
0
    def train(self, labels, data):
        self.labels = np.array(labels)  # [class, ..., class]
        self.classes = np.array(np.unique(labels))
        self.data = np.array(data, dtype='float')

        self.ints_labels = {}
        for i in range(0, len(self.classes)):
            self.ints_labels[self.classes[i]] = i

        ints = np.zeros([len(self.labels)])
        for i in range(0, len(self.labels)):
            ints[i] = self.ints_labels[self.labels[i]]

        Xnorm = self.normalize(self.data)
        self.pca = PCA(n_components=N_COMPONENTS)
        self.pca.fit(Xnorm)
        self.pca_data = self.pca.transform(Xnorm)

        self.classifier = lda.LDA()
        self.classifier.fit(self.pca_data, ints)
Пример #25
0
    def apply(self):
        transformed = components = None
        if self.data:
            self.data = Continuize(Impute(self.data))
            lda = skl_lda.LDA(solver='eigen', n_components=2)
            X = lda.fit_transform(self.data.X, self.data.Y)
            dom = Domain([
                ContinuousVariable('Component_1'),
                ContinuousVariable('Component_2')
            ], self.data.domain.class_vars, self.data.domain.metas)
            transformed = Table(dom, X, self.data.Y, self.data.metas)
            transformed.name = self.data.name + ' (LDA)'
            dom = Domain(self.data.domain.attributes,
                         metas=[StringVariable(name='component')])
            metas = np.array([[
                'Component_{}'.format(i + 1)
                for i in range(lda.scalings_.shape[1])
            ]],
                             dtype=object).T
            components = Table(dom, lda.scalings_.T, metas=metas)
            components.name = 'components'

        self.send("Transformed data", transformed)
        self.send("Components", components)
Пример #26
0
def multiclass_lda(training_feature_array, training_label_array,
               test_feature_array, test_label_array):
    """
    多クラス分類LDA
    @param training_feature_array: トレーニング用データ
    @param training_label_array: トレーニング用データラベル
    @param test_feature_array: テスト用データ
    @param test_label_array: テスト用データラベル
    
    @return: 全体識別率, 識別結果のリスト, 識別されたクラスへの所属確率のリスト, LDAオブジェクト

    動作確認済
    """
    multi_lda_obj = OneVsRestClassifier(slda.LDA())
    multi_lda_obj.fit(training_feature_array, training_label_array)
    
    print "test..."
    class_result = multi_lda_obj.predict(test_feature_array)
    proba_result = multi_lda_obj.predict_proba(test_feature_array)    
    proba_max_result = np.max(proba_result, axis=1)
    try:
        precision = smet.accuracy_score(test_label_array, class_result)
    except DeprecationWarning, e:
        pass
Пример #27
0
client = OSCClient()
client.connect(('127.0.0.1', 9999))

recent = []
window = blackman(N)
vectors = []
labels = []

def getAmplitude(data):
	freq = sfft.fft(data * window)
	nyquist = len(freq)/2
	return np.absolute(freq[:nyquist])

clf = SVC(probability=True, kernel = 'rbf')
model = lda.LDA(n_components=2)
projection = []
p_min = None
p_max = None
predicted = None

def buildLDA(vectors, labels):
	global model, projection, p_min, p_max
	if not len(vectors):
		print('No data to learn from.')
		return
	t0 = time()
	X = np.array(map(getAmplitude, vectors))
	y = np.array(labels)
	X.flat[::X.shape[1] + 1] += 0.01  # Make X invertible
	projection = model.fit_transform(X, y)
    
def splitMeanAngleFeatures(image, splits = 2, threshold = 10):
    result = splitMeanAngle(image, splits, threshold).flatten()
    return result

print("Loading images")
#images, classes = loader.loadProblematicImagesAndClasses()
images, classes = loader.loadTrainingAndClasses()
amount = len(images)

print("Making thumbnails")

thumbsize = 50
thumbs = [misc.imresize(x,(thumbsize, thumbsize)) for x in images]

print("Calculating features")
#features = list(map(extractor.calculateNormalizedColorFeatures, images))
splits = 5
features = numpy.zeros([len(images), splits * splits])
for i in range(amount):
    if(i%10 ==0):print(i, "/", amount)
    features[i] = splitMeanAngleFeatures(thumbs[i], splits)
    
print("Producing KFold indexes")
kfold = cv.KFold(amount, n_folds = 5, shuffle = True)
#model = neighbors.KNeighborsClassifier(n_neighbors = 1)
model = lda.LDA()
score = cross_validation.cross_val_score(model, features, classes, cv=kfold)
print(score)
print(score.mean())
    
Пример #29
0
print "Computing PCA projection"
t0 = time()
X_pca = decomposition.RandomizedPCA(n_components=2).fit_transform(X)
plot_embedding(
    X_pca, "Principal Components projection of the digits (time %.2fs)" %
    (time() - t0))

#----------------------------------------------------------------------
# Projection on to the first 2 linear discriminant components

print "Computing LDA projection"
X2 = X.copy()
X2.flat[::X.shape[1] + 1] += 0.01  # Make X invertible
t0 = time()
X_lda = lda.LDA(n_components=2).fit_transform(X2, y)
plot_embedding(
    X_lda, "Linear Discriminant projection of the digits (time %.2fs)" %
    (time() - t0))

#----------------------------------------------------------------------
# Isomap projection of the digits dataset
print "Computing Isomap embedding"
t0 = time()
X_iso = manifold.Isomap(n_neighbors, n_components=2).fit_transform(X)
print "Done."
plot_embedding(X_iso,
               "Isomap projection of the digits (time %.2fs)" % (time() - t0))

#----------------------------------------------------------------------
# Locally linear embedding of the digits dataset
Пример #30
0
            X_train, Y_train, X_test, Y_test = preprocess_dataset(X_train, Y_train, X_test, Y_test, streams=(self.streams != 'none'), config=self.args.preprocess_dataset, mode='hdf5')
            X_train, Y_train = self._split_data_pos_neg(X_train, Y_train)
            X_test, Y_test = self._split_data_pos_neg(X_test, Y_test)
            std = np.std(X_train[0])
            '''
            #self.helper_model = ae.swwae_augment(self.network.network, X_train, Y_train, X_test, Y_test, finetune_epochs=nb_epoch, multipliers=self.multipliers, layerwise_epochs=self.lw_epochs, decoder_epochs=self.dec_epochs, lr=self.lr, model_name=self.init)
            self.helper_model = ae.swwae_augment_hdf5(self.network.network, self.network.generator, X_train, Y_train, X_test, Y_test, finetune_epochs=nb_epoch, multipliers=self.multipliers, layerwise_epochs=self.lw_epochs, decoder_epochs=self.dec_epochs, lr=self.lr, model_name=self.init)

        else:
            print "Preprocessor {}".format(self.network.preprocessor)
            print "Generator {}".format(self.network.generator)
            self.helper_model = ae.swwae_augment_hdf5(self.network.network, self.network.generator, X_train, Y_train, X_test, Y_test, finetune_epochs=nb_epoch, multipliers=self.multipliers, layerwise_epochs=self.lw_epochs, decoder_epochs=self.dec_epochs, lr=self.lr, model_name=self.init)

    def fit_transform_bovw(self, rois_tr, pred_blobs_tr, blobs_tr, rois_te, pred_blobs_te, blobs_te,  model):
        X_tr, Y_tr = classify.create_training_set_from_feature_set(rois_tr, pred_blobs_tr, blobs_tr)
        X_te, Y_te = classify.create_training_set_from_feature_set(rois_te, pred_blobs_te, blobs_te)
        model.fit(X_tr)
        V_tr = []
        for rois in rois_tr:
            V_tr.append(model.transform(rois))
        V_te = []
        for rois in rois_te:
            V_te.append(model.transform(rois))
        return np.array(V_tr), np.array(V_te)

# optimized
opt_classifiers = {'svm':svm.SVC(probability=True, C=0.0373, gamma=0.002), 'lda':lda.LDA()}
# default
classifiers = {'linear-svm':svm.SVC(kernel='linear', C=1, probability=True), 'svm':svm.SVC(kernel='rbf', probability=True), 'lda':lda.LDA(), 'knn': neighbors.KNeighborsRegressor(), 'rf': ensemble.RandomForestClassifier(), 'hik':svm.SVC(kernel=pk.regular.Min(), C=1, probability=True)}
reductors = {'none':None, 'pca':decomposition.PCA(n_components=0.99999999999, whiten=True), 'lda':selection.SelectFromModel(lda.LDA())}