示例#1
0
def test_fastfood():
    """test that Fastfood fast approximates kernel on random data"""
    # compute exact kernel
    gamma = 10.0
    kernel = rbf_kernel(X, Y, gamma=gamma)

    sigma = np.sqrt(1 / (2 * gamma))

    # approximate kernel mapping
    ff_transform = Fastfood(sigma, n_components=1000, random_state=42)

    pars = ff_transform.fit(X)
    X_trans = pars.transform(X)
    Y_trans = ff_transform.transform(Y)

    kernel_approx = np.dot(X_trans, Y_trans.T)

    print("approximation:", kernel_approx[:5, :5])
    print("true kernel:", kernel[:5, :5])
    assert_array_almost_equal(kernel, kernel_approx, decimal=1)
    pl.title("Training: %i" % label)
    if index > 3:
        break

# To apply an classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))
gamma = 0.001
sigma = np.sqrt(1 / (2 * gamma))
number_of_features_to_generate = 1000
train__idx = range(n_samples // 2)
test__idx = range(n_samples // 2, n_samples)

# map data into featurespace
rbf_transform = Fastfood(sigma=sigma,
                         n_components=number_of_features_to_generate)
data_transformed_train = rbf_transform.fit_transform(data[train__idx])
data_transformed_test = rbf_transform.transform(data[test__idx])

# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma=gamma)
linear_classifier = svm.LinearSVC()
linear_classifier_transformation = svm.LinearSVC()

# We learn the digits on the first half of the digits
classifier.fit(data[train__idx], digits.target[train__idx])
linear_classifier.fit(data[train__idx], digits.target[train__idx])

# Run the linear classifier on the mapped data.
linear_classifier_transformation.fit(data_transformed_train,
                                     digits.target[train__idx])
    digits.target[n_samples // 2 :],
)
# data_test = scaler.transform(data_test)

# fix model parameters:
GAMMA = 0.2
SIGMA = np.sqrt(1 / (2 * GAMMA))

# Create a classifier: a support vector classifier
kernel_svm = svm.SVC(gamma=GAMMA)
linear_svm = svm.LinearSVC()

# create pipeline from kernel approximation
# and linear svm
feature_map_fastfood = Fastfood(
    sigma=SIGMA, tradeoff_mem_accuracy="mem", random_state=1
)
feature_map_fourier = RBFSampler(gamma=GAMMA, random_state=1)
feature_map_nystroem = Nystroem(gamma=GAMMA, random_state=1)
fastfood_approx_svm = pipeline.Pipeline(
    [("feature_map", feature_map_fastfood), ("svm", svm.LinearSVC())]
)

fourier_approx_svm = pipeline.Pipeline(
    [("feature_map", feature_map_fourier), ("svm", svm.LinearSVC())]
)

nystroem_approx_svm = pipeline.Pipeline(
    [("feature_map", feature_map_nystroem), ("svm", svm.LinearSVC())]
)
示例#4
0
def test_fastfood_enforce_dimensionality_constraint(message, input_, expected):
    d, n = input_
    output = Fastfood._enforce_dimensionality_constraints(d, n)
    assert expected == output, message