Beispiel #1
0
    def extraction(self, data):
        # extracting 40 features using sparse filtering
        sp = SparseFiltering(self.num_feature_extract)
        feature_set = sp.fit_transform(data)

        # fig = plt.figure()
        # ax = fig.add_subplot(111, projection='3d')
        # ax.scatter(feature_set[:, 0], feature_set[:, 1], feature_set[:, 2], c=label[:, 0])
        # ax.set_xlabel('feature 1')
        # ax.set_ylabel('feature 2')
        # ax.set_zlabel('feature 3')
        # plt.title('SP visulization')
        # plt.show()

        return feature_set
pred = clf.predict(XXtrainIn)
#save the classifier
_ = joblib.dump(clf, 'random_forest_trained.pkl', compress =9)

labels = ytrain.squeeze()

report_accuracy(pred,labels, ' RF Raw Train')
pred = clf.predict(XXtest)
labels = ytest.squeeze()
report_accuracy(pred,labels, ' RF Raw Test')

#SparseFiltering
n_features = 64   # How many features are learned

estimator = SparseFiltering(n_features=n_features,
                            maxfun=200,  # The maximal number of evaluations of the objective function
                            iprint=-1)  # after how many function evaluations is information printed
                                        # by L-BFGS. -1 for no information
print("sparse yapıldı")
#model2 = RandomForestClassifier(n_estimators=20, max_depth=100, criterion='entropy', max_features='sqrt',max_leaf_nodes=30,oob_score=False)

train_features = estimator.fit_transform(XXtrainIn)
rfr = RandomForestClassifier(n_estimators=51, class_weight=wtrain, max_depth=15)
rfr.fit(train_features,ytrain.ravel())
pred = rfr.predict(train_features)
report_accuracy(pred,ytrain.ravel(), 'Sparse RF Train')

test_features = estimator.transform(XXtest)
pred = rfr.predict(test_features)
report_accuracy(pred,ytest.ravel(), 'Sparse RF Test')
faces_centered = \
    faces_centered.reshape(n_samples, 64, 64)  # Reshaping to 64*64 pixel images

print("Dataset consists of %d faces" % n_samples)

###############################################################################
# Extract n_patches patches randomly from each image
patches = [extract_patches_2d(faces_centered[i], (patch_width, patch_width),
                              max_patches=n_patches, random_state=i)
              for i in range(n_samples)]
patches = np.array(patches).reshape(-1, patch_width * patch_width)

###############################################################################
#
estimator = SparseFiltering(n_features=n_features, maxfun=maxfun, iprint=iprint)
features = estimator.fit_transform(patches)

# #############################################################################
# Plot weights of features
pl.figure(0, figsize=(12, 10))
pl.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.95,
                  wspace=0.1, hspace=0.4)
for i in range(estimator.w_.shape[0]):
    pl.subplot(int(np.sqrt(n_features)), int(np.sqrt(n_features)), i + 1)
    pl.pcolor(estimator.w_[i].reshape(patch_width, patch_width),
              cmap=pl.cm.gray)
    pl.xticks(())
    pl.yticks(())
    pl.title("Feature %4d" % i)
#
# row_sums = test_X.sum(axis=1).astype(float)
# test_X = np.true_divide(test_X, row_sums[:, np.newaxis])

###############################################################################
# Dictionary Learning
n_components = 700
n_samples_training = 20000

print("\nSparse Coding Dictionary Learning")
# pca = RandomizedPCA(n_components=n_dcomponents).fit(train_X)
# dl = KSVDSparseCoding(n_components, n_nonzero_coefs=70, preserve_dc=False, approx=False, max_iter=5, verbose=1)
# dl.fit(train_X[0:n_samples_training])

estimator = SparseFiltering(n_features=n_components,
                            maxfun=500,  # The maximal number of evaluations of the objective function
                            iprint=10)  # after how many function evaluations is information printed
# by L-BFGS. -1 for no information
features = estimator.fit_transform(train_X[0:n_samples_training])
print "features.shape", features.shape
print "estimator.w_.shape", estimator.w_.shape

plt.figure(figsize=(12, 10))
for i in range(estimator.w_.shape[0]):
    plt.subplot(int(np.sqrt(n_features)), int(np.sqrt(n_features)), i + 1)
    plt.pcolor(estimator.w_[i].reshape(w, h),
               cmap=plt.cm.gray, vmin=estimator.w_.min(),
               vmax=estimator.w_.max())
    plt.xticks(())
    plt.yticks(())
    plt.title("")
Beispiel #5
0
faces_centered = \
    faces_centered.reshape(n_samples, 64, 64)  # Reshaping to 64*64 pixel

print("Dataset consists of %d faces" % n_samples)

###############################################################################
# Extract n_patches patches randomly from each image
patches = [
    extract_patches_2d(faces_centered[i], (patch_width, patch_width),
                       max_patches=n_patches,
                       random_state=i) for i in range(n_samples)
]
patches = np.array(patches).reshape(-1, patch_width * patch_width)

###############################################################################
estimator = \
    SparseFiltering(n_features=n_features, maxfun=maxfun, iprint=iprint)
features = estimator.fit_transform(patches)

# #############################################################################
# Plot weights of features
pl.figure(0, figsize=(12, 10))
pl.subplots_adjust(left=0.01,
                   bottom=0.01,
                   right=0.99,
                   top=0.95,
                   wspace=0.1,
                   hspace=0.4)
for i in range(estimator.w_.shape[0]):
    pl.subplot(int(np.sqrt(n_features)), int(np.sqrt(n_features)), i + 1)
    pl.pcolor(estimator.w_[i].reshape(patch_width, patch_width),
              cmap=pl.cm.gray)