예제 #1
0
def knn_NCA(X_train, Y_train, X_test, K=1) -> list:
    """
    Reduce the dimensionalty of the dataset using the NCA method
    This is slower than using PCA or not using anything at all,
    but yields better results for now

    If the dataset sample is too large this takes really long to run
    """
    # Scale all the output using a standard scaler
    scaler = StandardScaler()
    X_train = scaler.fit_transform(X_train)
    X_test = scaler.transform(X_test)
    # Reduce the dimensionalty of the data using NCA
    nca = NeighborhoodComponentsAnalysis(2).fit(X_train, Y_train)
    X_train_nca = nca.transform(X_train)
    X_test_nca = nca.transform(X_test)

    X_train_nca = pd.DataFrame(X_train_nca)
    X_test_nca = pd.DataFrame(X_test_nca)

    # Classify using a KNN classifier
    clf = KNeighborsClassifier(n_neighbors=K, leaf_size=2)
    clf.fit(X_train_nca, Y_train)
    # Return the predicted results
    return clf.predict(X_test_nca)
예제 #2
0
 def __init__(self, X, y):
     # Initialize a fake NCA and variables needed to call the loss
     # function:
     self.fake_nca = NeighborhoodComponentsAnalysis()
     self.fake_nca.n_iter_ = np.inf
     self.X, y, _ = self.fake_nca._validate_params(X, y)
     self.same_class_mask = y[:, np.newaxis] == y[np.newaxis, :]
예제 #3
0
def knnGridSearch(X_train, Y_train, X_test, Y_test) -> list:
    """
    Used to run a grid search to find the best params for later usage
    Only runs if the param -grid is provided 
    """
    # Params used for the gird search
    grid_params = {
        'n_neighbors': [1, 3, 5],
    }
    # Scale all the output using a standard scaler
    scaler = StandardScaler()
    X_train = scaler.fit_transform(X_train)
    X_test = scaler.transform(X_test)
    # Reduce the dimensionalty of the data using NCA
    nca = NeighborhoodComponentsAnalysis(2).fit(X_train, Y_train)
    X_train_nca = nca.transform(X_train)
    X_test_nca = nca.transform(X_test)
    # Run the Grid search and print out the best params
    classifier = KNeighborsClassifier()
    gs = GridSearchCV(classifier, grid_params, verbose=1, cv=3, n_jobs=-1)
    gs.fit(X_train_nca, Y_train)
    print(gs.best_params_)
    # Score the best found params using a confusion matrix
    Y_pred = gs.predict(X_test_nca)
    print(confusion_matrix(Y_test, Y_pred))
예제 #4
0
def test_no_verbose(capsys):
    # assert by default there is no output (verbose=0)
    nca = NeighborhoodComponentsAnalysis()
    nca.fit(iris_data, iris_target)
    out, _ = capsys.readouterr()
    # check output
    assert (out == '')
예제 #5
0
 def __init__(self, X, y):
     self.loss = np.inf  # initialize the loss to very high
     # Initialize a fake NCA and variables needed to compute the loss:
     self.fake_nca = NeighborhoodComponentsAnalysis()
     self.fake_nca.n_iter_ = np.inf
     self.X, y, _ = self.fake_nca._validate_params(X, y)
     self.same_class_mask = y[:, np.newaxis] == y[np.newaxis, :]
예제 #6
0
def test_parameters_valid_types(param, value):
    # check that no error is raised when parameters have numpy integer or
    # floating types.
    nca = NeighborhoodComponentsAnalysis(**{param: value})

    X = iris_data
    y = iris_target

    nca.fit(X, y)
예제 #7
0
def test_one_class():
    X = iris_data[iris_target == 0]
    y = iris_target[iris_target == 0]

    nca = NeighborhoodComponentsAnalysis(max_iter=30,
                                         n_components=X.shape[1],
                                         init='identity')
    nca.fit(X, y)
    assert_array_equal(X, nca.transform(X))
예제 #8
0
    class LossStorer:
        def __init__(self, X, y):
            self.loss = np.inf  # initialize the loss to very high
            # Initialize a fake NCA and variables needed to compute the loss:
            self.fake_nca = NeighborhoodComponentsAnalysis()
            self.fake_nca.n_iter_ = np.inf
            self.X, y, _ = self.fake_nca._validate_params(X, y)
            self.same_class_mask = y[:, np.newaxis] == y[np.newaxis, :]

        def callback(self, transformation, n_iter):
            """Stores the last value of the loss function"""
            self.loss, _ = self.fake_nca._loss_grad_lbfgs(
                transformation, self.X, self.same_class_mask, -1.0)
예제 #9
0
def test_n_components():
    rng = np.random.RandomState(42)
    X = np.arange(12).reshape(4, 3)
    y = [1, 1, 2, 2]

    init = rng.rand(X.shape[1] - 1, 3)

    # n_components = X.shape[1] != transformation.shape[0]
    n_components = X.shape[1]
    nca = NeighborhoodComponentsAnalysis(init=init, n_components=n_components)
    assert_raise_message(
        ValueError, 'The preferred dimensionality of the '
        'projected space `n_components` ({}) does not match '
        'the output dimensionality of the given '
        'linear transformation `init` ({})!'.format(n_components,
                                                    init.shape[0]), nca.fit, X,
        y)

    # n_components > X.shape[1]
    n_components = X.shape[1] + 2
    nca = NeighborhoodComponentsAnalysis(init=init, n_components=n_components)
    assert_raise_message(
        ValueError, 'The preferred dimensionality of the '
        'projected space `n_components` ({}) cannot '
        'be greater than the given data '
        'dimensionality ({})!'.format(n_components, X.shape[1]), nca.fit, X, y)

    # n_components < X.shape[1]
    nca = NeighborhoodComponentsAnalysis(n_components=2, init='identity')
    nca.fit(X, y)
예제 #10
0
def test_nca_feature_names_out():
    """Check `get_feature_names_out` for `NeighborhoodComponentsAnalysis`."""

    X = iris_data
    y = iris_target

    est = NeighborhoodComponentsAnalysis().fit(X, y)
    names_out = est.get_feature_names_out()

    class_name_lower = est.__class__.__name__.lower()
    expected_names_out = np.array(
        [f"{class_name_lower}{i}" for i in range(est.components_.shape[1])],
        dtype=object,
    )
    assert_array_equal(names_out, expected_names_out)
예제 #11
0
def test_simple_example():
    """Test on a simple example.

    Puts four points in the input space where the opposite labels points are
    next to each other. After transform the samples from the same class
    should be next to each other.

    """
    X = np.array([[0, 0], [0, 1], [2, 0], [2, 1]])
    y = np.array([1, 0, 1, 0])
    nca = NeighborhoodComponentsAnalysis(
        n_components=2, init="identity", random_state=42
    )
    nca.fit(X, y)
    X_t = nca.transform(X)
    assert_array_equal(pairwise_distances(X_t).argsort()[:, 1], np.array([2, 3, 0, 1]))
예제 #12
0
def feature_reduction(x, y, n_components=2):
    from sklearn.pipeline import make_pipeline
    nca = make_pipeline(Normalizer(),
            NeighborhoodComponentsAnalysis(init='auto',
                            n_components=n_components, random_state=1))
    rx = nca.fit_transform(x,y)
    return rx, y
예제 #13
0
def test_auto_init(n_samples, n_features, n_classes, n_components):
    # Test that auto choose the init as expected with every configuration
    # of order of n_samples, n_features, n_classes and n_components.
    rng = np.random.RandomState(42)
    nca_base = NeighborhoodComponentsAnalysis(init='auto',
                                              n_components=n_components,
                                              max_iter=1,
                                              random_state=rng)
    if n_classes >= n_samples:
        pass
        # n_classes > n_samples is impossible, and n_classes == n_samples
        # throws an error from lda but is an absurd case
    else:
        X = rng.randn(n_samples, n_features)
        y = np.tile(range(n_classes), n_samples // n_classes + 1)[:n_samples]
        if n_components > n_features:
            # this would return a ValueError, which is already tested in
            # test_params_validation
            pass
        else:
            nca = clone(nca_base)
            nca.fit(X, y)
            if n_components <= min(n_classes - 1, n_features):
                nca_other = clone(nca_base).set_params(init='lda')
            elif n_components < min(n_features, n_samples):
                nca_other = clone(nca_base).set_params(init='pca')
            else:
                nca_other = clone(nca_base).set_params(init='identity')
            nca_other.fit(X, y)
            assert_array_almost_equal(nca.components_, nca_other.components_)
예제 #14
0
def test_verbose(init_name, capsys):
    # assert there is proper output when verbose = 1, for every initialization
    # except auto because auto will call one of the others
    rng = np.random.RandomState(42)
    X, y = make_blobs(n_samples=30, centers=6, n_features=5, random_state=0)
    regexp_init = r"... done in \ *\d+\.\d{2}s"
    msgs = {
        "pca": "Finding principal components" + regexp_init,
        "lda": "Finding most discriminative components" + regexp_init,
    }
    if init_name == "precomputed":
        init = rng.randn(X.shape[1], X.shape[1])
    else:
        init = init_name
    nca = NeighborhoodComponentsAnalysis(verbose=1, init=init)
    nca.fit(X, y)
    out, _ = capsys.readouterr()

    # check output
    lines = re.split("\n+", out)
    # if pca or lda init, an additional line is printed, so we test
    # it and remove it to test the rest equally among initializations
    if init_name in ["pca", "lda"]:
        assert re.match(msgs[init_name], lines[0])
        lines = lines[1:]
    assert lines[0] == "[NeighborhoodComponentsAnalysis]"
    header = "{:>10} {:>20} {:>10}".format("Iteration", "Objective Value",
                                           "Time(s)")
    assert lines[1] == "[NeighborhoodComponentsAnalysis] {}".format(header)
    assert lines[2] == ("[NeighborhoodComponentsAnalysis] {}".format(
        "-" * len(header)))
    for line in lines[3:-2]:
        # The following regex will match for instance:
        # '[NeighborhoodComponentsAnalysis]  0    6.988936e+01   0.01'
        assert re.match(
            r"\[NeighborhoodComponentsAnalysis\] *\d+ *\d\.\d{6}e"
            r"[+|-]\d+\ *\d+\.\d{2}",
            line,
        )
    assert re.match(
        r"\[NeighborhoodComponentsAnalysis\] Training took\ *"
        r"\d+\.\d{2}s\.",
        lines[-2],
    )
    assert lines[-1] == ""
def plot_nca_dim_reduction():
    n_neighbors = 3
    random_state = 0

    # Load Digits dataset
    X, y = datasets.load_digits(return_X_y=True)

    # Split into train/test
    X_train, X_test, y_train, y_test = \
        train_test_split(X, y, test_size=0.5, stratify=y,
                         random_state=random_state)

    dim = len(X[0])
    n_classes = len(np.unique(y))

    # Reduce dimension to 2 with PCA
    pca = make_pipeline(StandardScaler(),
                        PCA(n_components=2, random_state=random_state))

    # Reduce dimension to 2 with LinearDiscriminantAnalysis
    lda = make_pipeline(StandardScaler(),
                        LinearDiscriminantAnalysis(n_components=2))

    # Reduce dimension to 2 with NeighborhoodComponentAnalysis
    nca = make_pipeline(
        StandardScaler(),
        NeighborhoodComponentsAnalysis(n_components=2,
                                       random_state=random_state))

    # Use a nearest neighbor classifier to evaluate the methods
    knn = KNeighborsClassifier(n_neighbors=n_neighbors)

    # Make a list of the methods to be compared
    dim_reduction_methods = [('PCA', pca), ('LDA', lda), ('NCA', nca)]

    # plt.figure()
    for i, (name, model) in enumerate(dim_reduction_methods):
        plt.figure()
        # plt.subplot(1, 3, i + 1, aspect=1)

        # Fit the method's model
        model.fit(X_train, y_train)

        # Fit a nearest neighbor classifier on the embedded training set
        knn.fit(model.transform(X_train), y_train)

        # Compute the nearest neighbor accuracy on the embedded test set
        acc_knn = knn.score(model.transform(X_test), y_test)

        # Embed the data set in 2 dimensions using the fitted model
        X_embedded = model.transform(X)

        # Plot the projected points and show the evaluation score
        plt.scatter(X_embedded[:, 0], X_embedded[:, 1], c=y, s=30, cmap='Set1')
        plt.title("{}, KNN (k={})\nTest accuracy = {:.2f}".format(
            name, n_neighbors, acc_knn))
    plt.show()
예제 #16
0
def KNN(df, *args, **kwargs):
    unique_test_name = 'StandardScaler KNN GridSearchCV Optimised with SMOTE ENN'
    # Create a temporary folder to store the transformers of the pipeline
    cachedir = mkdtemp()
    memory = Memory(location=cachedir, verbose=10)

    y = df['QuoteConversion_Flag'].values
    IDs = df.Quote_ID
    X = df.drop(['QuoteConversion_Flag', 'Quote_ID'], axis=1).values

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.33,
                                                        random_state=42)

    param_grid = {
        'knn__n_neighbours': np.arange(3, 12),
        'knn__algorithm': ['ball_tree', 'kd_tree', 'brute'],
        'knn__leaf_size': np.arange(20, 30),
        'knn__p': [1, 2, 3, 4, 5],
        'nca__n_components': np.arange(2, 12),
        'nca__max_iter': np.arange(1000, 2000),
        'nca__tol': 10.0**-np.arange(1, 8),
    }

    # model classes
    nca = NeighborhoodComponentsAnalysis(random_state=42, warm_start=False)
    knn = KNeighborsClassifier(n_jobs=-1)

    model = [make_pipeline(StandardScaler(), nca, knn, memory=memory)]

    grid = GridSearchCV(model, param_grid, cv=1000, iid=False, n_jobs=-1)

    grid.fit(X_train, y_train)

    print("-----------------Best Param Overview--------------------")
    print("Best score: %0.4f" % grid.best_score_)
    print("Using the following parameters:")
    print(grid.best_params_)
    results = pd.DataFrame(grid.cv_results_)
    results.to_csv(unique_test_name + '_cv_results.csv', index=False)

    prediction = grid.predict(X_test)
    print("-----------------Scoring Model--------------------")
    print(classification_report(prediction, y_test))
    print(confusion_matrix(prediction, y_test), "\n")

    prediction = pd.DataFrame(data=prediction,
                              columns=['QuoteConversion_Flag'])
    results = pd.concat([IDs, prediction], axis=1)

    results.to_csv(unique_test_name + "ida_a3_13611165.csv", index=False)
    dump(grid, "MLP[{}].joblib".format(unique_test_name))

    # Delete the temporary cache before exiting
    rmtree(cachedir)
    return
예제 #17
0
def dim_reduc(X_train, Y_train, X_test, Y_test, K=1) -> None:
    """
    Compare PCA, kernel PCA, and NCA dimensionalty reduction.
    Slightly modified version of this code:
    https://scikit-learn.org/stable/auto_examples/neighbors/plot_nca_dim_reduction.html
    Only runs if the -dim argument is provided
    KernelPCA and standard PCA give the same results
    While NCA seems to have a slight edge
    """
    X = pd.concat([X_train, X_test])
    Y = Y_train + Y_test
    random_state = 0
    # Reduce dimension to 2 with PCA
    pca = make_pipeline(StandardScaler(),
                        PCA(n_components=2, random_state=random_state))

    # Reduce dimension to 2 with NeighborhoodComponentAnalysis
    nca = make_pipeline(
        StandardScaler(),
        NeighborhoodComponentsAnalysis(n_components=2,
                                       random_state=random_state))
    # Reduce the dimensionalty using Kernel PCA
    kernel_pca = make_pipeline(StandardScaler(),
                               KernelPCA(2, random_state=random_state))

    # Use a nearest neighbor classifier to evaluate the methods
    knn = KNeighborsClassifier(n_neighbors=K)

    # Make a list of the methods to be compared
    dim_reduction_methods = [('PCA', pca), ('NCA', nca),
                             ('KernelPCA', kernel_pca)]

    # plt.figure()
    for i, (name, model) in enumerate(dim_reduction_methods):
        plt.figure()
        # plt.subplot(1, 3, i + 1, aspect=1)
        # Fit the method's model
        model.fit(X_train, Y_train)
        # Fit a nearest neighbor classifier on the embedded training set
        knn.fit(model.transform(X_train), Y_train)
        # Compute the nearest neighbor accuracy on the embedded test set
        acc_knn = knn.score(model.transform(X_test), Y_test)
        print(name, acc_knn)
        # Embed the data set in 2 dimensions using the fitted model
        X_embedded = model.transform(X)
        # Plot the projected points and show the evaluation score
        plt.scatter(
            X_embedded[:, 0],
            X_embedded[:, 1],
            c=Y,
            s=30,
            cmap='Set1',
        )
        plt.title("KNN with {}\np={}".format(name, round(acc_knn, 3)))
        plt.savefig("figs/KNN_{}.png".format(name))

    plt.show()
예제 #18
0
def test_transformation_dimensions():
    X = np.arange(12).reshape(4, 3)
    y = [1, 1, 2, 2]

    # Fail if transformation input dimension does not match inputs dimensions
    transformation = np.array([[1, 2], [3, 4]])
    with pytest.raises(ValueError):
        NeighborhoodComponentsAnalysis(init=transformation).fit(X, y)

    # Fail if transformation output dimension is larger than
    # transformation input dimension
    transformation = np.array([[1, 2], [3, 4], [5, 6]])
    # len(transformation) > len(transformation[0])
    with pytest.raises(ValueError):
        NeighborhoodComponentsAnalysis(init=transformation).fit(X, y)

    # Pass otherwise
    transformation = np.arange(9).reshape(3, 3)
    NeighborhoodComponentsAnalysis(init=transformation).fit(X, y)
예제 #19
0
 def knn_nca(self, X_train, X_test, y_train, y_test):
     start = time.time()
     nca = NeighborhoodComponentsAnalysis(random_state=1)
     knn = KNeighborsClassifier(n_neighbors=3)
     nca_pipe = Pipeline([('nca', nca), ('knn', knn)])
     nca_pipe.fit(X_train, y_train)
     self.app_metrics.nca_knnPerf = time.time() - start
     score = '{}%'.format(nca_pipe.score(X_test, y_test) * 100)
     print('\nKNN & NCA: {}'.format(score))
     self.app_metrics.nca_knnScore = score
예제 #20
0
def test_verbose(init_name, capsys):
    # assert there is proper output when verbose = 1, for every initialization
    # except auto because auto will call one of the others
    rng = np.random.RandomState(42)
    X, y = make_blobs(n_samples=30, centers=6, n_features=5, random_state=0)
    regexp_init = r'... done in \ *\d+\.\d{2}s'
    msgs = {
        'pca': "Finding principal components" + regexp_init,
        'lda': "Finding most discriminative components" + regexp_init
    }
    if init_name == 'precomputed':
        init = rng.randn(X.shape[1], X.shape[1])
    else:
        init = init_name
    nca = NeighborhoodComponentsAnalysis(verbose=1, init=init)
    nca.fit(X, y)
    out, _ = capsys.readouterr()

    # check output
    lines = re.split('\n+', out)
    # if pca or lda init, an additional line is printed, so we test
    # it and remove it to test the rest equally among initializations
    if init_name in ['pca', 'lda']:
        assert re.match(msgs[init_name], lines[0])
        lines = lines[1:]
    assert lines[0] == '[NeighborhoodComponentsAnalysis]'
    header = '{:>10} {:>20} {:>10}'.format('Iteration', 'Objective Value',
                                           'Time(s)')
    assert lines[1] == '[NeighborhoodComponentsAnalysis] {}'.format(header)
    assert lines[2] == ('[NeighborhoodComponentsAnalysis] {}'.format(
        '-' * len(header)))
    for line in lines[3:-2]:
        # The following regex will match for instance:
        # '[NeighborhoodComponentsAnalysis]  0    6.988936e+01   0.01'
        assert re.match(
            r'\[NeighborhoodComponentsAnalysis\] *\d+ *\d\.\d{6}e'
            r'[+|-]\d+\ *\d+\.\d{2}', line)
    assert re.match(
        r'\[NeighborhoodComponentsAnalysis\] Training took\ *'
        r'\d+\.\d{2}s\.', lines[-2])
    assert lines[-1] == ''
 def test_sklearn_nca_default(self):
     model, X_test = fit_classification_model(
         NeighborhoodComponentsAnalysis(random_state=42), 3)
     model_onnx = convert_sklearn(
         model,
         "NCA", [("input", FloatTensorType((None, X_test.shape[1])))],
         target_opset=TARGET_OPSET)
     self.assertIsNotNone(model_onnx)
     dump_data_and_model(X_test,
                         model,
                         model_onnx,
                         basename="SklearnNCADefault")
예제 #22
0
def test_warm_start_validation():
    X, y = make_classification(
        n_samples=30,
        n_features=5,
        n_classes=4,
        n_redundant=0,
        n_informative=5,
        random_state=0,
    )

    nca = NeighborhoodComponentsAnalysis(warm_start=True, max_iter=5)
    nca.fit(X, y)

    X_less_features, y = make_classification(
        n_samples=30,
        n_features=4,
        n_classes=4,
        n_redundant=0,
        n_informative=4,
        random_state=0,
    )
    msg = (f"The new inputs dimensionality ({X_less_features.shape[1]}) "
           "does not match the input dimensionality of the previously learned "
           f"transformation ({nca.components_.shape[1]}).")
    with pytest.raises(ValueError, match=re.escape(msg)):
        nca.fit(X_less_features, y)
예제 #23
0
    def _update_KNN_NCA(self):
        self.knn = KNeighborsClassifier(n_neighbors=self.K)
        self.nca = NeighborhoodComponentsAnalysis(n_components=self.NCA_dim)
        self.model.eval()

        labels = list()
        images = None
        for label in range(0, len(self.exemplar_sets)):
            image, _ = self.train_dataset.get_images_by_indexes(
                self.exemplar_sets[label])
            image = torch.stack(image).to(DEVICE)
            with torch.no_grad():
                image = torch.nn.functional.normalize(
                    self.model.features_extractor(image)).cpu()
            if label == 0:
                images = image
            else:
                images = torch.cat((images, image), 0)
            labels.extend([self.classes[label]] * len(image))
        # plt.figure()
        self.nca.fit(images, labels)
        self.knn.fit(self.nca.transform(images), labels)
예제 #24
0
def ml_basis():
    print('Welcome to the world of machine learning!')
    X, y = load_iris(return_X_y=True)
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        stratify=y,
                                                        test_size=0.7,
                                                        random_state=42)
    nca = NeighborhoodComponentsAnalysis(random_state=42)
    knn = KNeighborsClassifier(n_neighbors=3)
    nca_pipe = Pipeline([('nca', nca), ('knn', knn)])
    nca_pipe.fit(X_train, y_train)
    print(nca_pipe.score(X_test, y_test))
예제 #25
0
def test_toy_example_collapse_points():
    """Test on a toy example of three points that should collapse

    We build a simple example: two points from the same class and a point from
    a different class in the middle of them. On this simple example, the new
    (transformed) points should all collapse into one single point. Indeed, the
    objective is 2/(1 + exp(d/2)), with d the euclidean distance between the
    two samples from the same class. This is maximized for d=0 (because d>=0),
    with an objective equal to 1 (loss=-1.).

    """
    rng = np.random.RandomState(42)
    input_dim = 5
    two_points = rng.randn(2, input_dim)
    X = np.vstack([two_points, two_points.mean(axis=0)[np.newaxis, :]])
    y = [0, 0, 1]

    class LossStorer:
        def __init__(self, X, y):
            self.loss = np.inf  # initialize the loss to very high
            # Initialize a fake NCA and variables needed to compute the loss:
            self.fake_nca = NeighborhoodComponentsAnalysis()
            self.fake_nca.n_iter_ = np.inf
            self.X, y, _ = self.fake_nca._validate_params(X, y)
            self.same_class_mask = y[:, np.newaxis] == y[np.newaxis, :]

        def callback(self, transformation, n_iter):
            """Stores the last value of the loss function"""
            self.loss, _ = self.fake_nca._loss_grad_lbfgs(
                transformation, self.X, self.same_class_mask, -1.0)

    loss_storer = LossStorer(X, y)
    nca = NeighborhoodComponentsAnalysis(random_state=42,
                                         callback=loss_storer.callback)
    X_t = nca.fit_transform(X, y)
    print(X_t)
    # test that points are collapsed into one point
    assert_array_almost_equal(X_t - X_t[0], 0.)
    assert abs(loss_storer.loss + 1) < 1e-10
예제 #26
0
    class TransformationStorer:
        def __init__(self, X, y):
            # Initialize a fake NCA and variables needed to call the loss
            # function:
            self.fake_nca = NeighborhoodComponentsAnalysis()
            self.fake_nca.n_iter_ = np.inf
            self.X, y, _ = self.fake_nca._validate_params(X, y)
            self.same_class_mask = y[:, np.newaxis] == y[np.newaxis, :]

        def callback(self, transformation, n_iter):
            """Stores the last value of the transformation taken as input by
            the optimizer"""
            self.transformation = transformation
def nearest_neighbours_classifier(training_data):
    print('Generating the data model for a nearest neighbours classifier . . .\n')
    X = util.drop_target_variable(training_data)
    y = util.retrieve_target_variable(training_data)
    X_train, X_test, y_train, y_test = train_test_split(X, y,stratify=y, test_size=0.1, random_state=1)
    nca = NeighborhoodComponentsAnalysis(random_state=42)
    knn = KNeighborsClassifier(n_neighbors=3)
    knn=knn.fit(X_train, y_train)
    nca_pipe = Pipeline([('nca', nca), ('knn', knn)])
    nca_pipe.fit(X_train, y_train)
    print('The data model for nearest neighbours classifier has been generated successfully!\n')
    util.save_data_model(knn,'nearest_neighbours_classifier')
    return knn;
예제 #28
0
def test_finite_differences():
    """Test gradient of loss function

    Assert that the gradient is almost equal to its finite differences
    approximation.
    """
    # Initialize the transformation `M`, as well as `X` and `y` and `NCA`
    rng = np.random.RandomState(42)
    X, y = make_classification()
    M = rng.randn(rng.randint(1, X.shape[1] + 1), X.shape[1])
    nca = NeighborhoodComponentsAnalysis()
    nca.n_iter_ = 0
    mask = y[:, np.newaxis] == y[np.newaxis, :]

    def fun(M):
        return nca._loss_grad_lbfgs(M, X, mask)[0]

    def grad(M):
        return nca._loss_grad_lbfgs(M, X, mask)[1]

    # compute relative error
    rel_diff = check_grad(fun, grad, M.ravel()) / np.linalg.norm(grad(M))
    np.testing.assert_almost_equal(rel_diff, 0., decimal=5)
예제 #29
0
def test_warm_start_validation():
    X, y = make_classification(n_samples=30,
                               n_features=5,
                               n_classes=4,
                               n_redundant=0,
                               n_informative=5,
                               random_state=0)

    nca = NeighborhoodComponentsAnalysis(warm_start=True, max_iter=5)
    nca.fit(X, y)

    X_less_features, y = make_classification(n_samples=30,
                                             n_features=4,
                                             n_classes=4,
                                             n_redundant=0,
                                             n_informative=4,
                                             random_state=0)
    assert_raise_message(
        ValueError, 'The new inputs dimensionality ({}) does not '
        'match the input dimensionality of the '
        'previously learned transformation ({}).'.format(
            X_less_features.shape[1], nca.components_.shape[1]), nca.fit,
        X_less_features, y)
예제 #30
0
def test_expected_transformation_shape():
    """Test that the transformation has the expected shape."""
    X = iris_data
    y = iris_target

    class TransformationStorer:
        def __init__(self, X, y):
            # Initialize a fake NCA and variables needed to call the loss
            # function:
            self.fake_nca = NeighborhoodComponentsAnalysis()
            self.fake_nca.n_iter_ = np.inf
            self.X, y, _ = self.fake_nca._validate_params(X, y)
            self.same_class_mask = y[:, np.newaxis] == y[np.newaxis, :]

        def callback(self, transformation, n_iter):
            """Stores the last value of the transformation taken as input by
            the optimizer"""
            self.transformation = transformation

    transformation_storer = TransformationStorer(X, y)
    cb = transformation_storer.callback
    nca = NeighborhoodComponentsAnalysis(max_iter=5, callback=cb)
    nca.fit(X, y)
    assert transformation_storer.transformation.size == X.shape[1]**2
            ax.plot(*line, c=cm.Set1(y[j]),
                    linewidth=5*thickness[j])


# we consider only point 3
i = 3

# Plot bonds linked to sample i in the original space
relate_point(X, i, ax)
ax.set_title("Original points")
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.axis('equal')

# Learn an embedding with NeighborhoodComponentsAnalysis
nca = NeighborhoodComponentsAnalysis(max_iter=30, random_state=random_state)
nca = nca.fit(X, y)

# Plot the points after transformation with NeighborhoodComponentsAnalysis
plt.figure()
ax2 = plt.gca()

# Get the embedding and find the new nearest neighbors
X_embedded = nca.transform(X)

relate_point(X_embedded, i, ax2)

for i in range(len(X)):
    ax2.text(X_embedded[i, 0], X_embedded[i, 1], str(i),
             va='center', ha='center')
    ax2.scatter(X_embedded[i, 0], X_embedded[i, 1], s=300, c=cm.Set1(y[i]),