Esempio n. 1
0
    def test_weights(self):
        """Testea los weights"""

        # Primero, caso en el que considere todos los vecinos, y en caso
        # de empate deberia dar el primero.
        classifier = metnum.KNNClassifier(12, "uniform")

        X_train = np.array([
            [20, 20],
            [21, 20],
            [20, 22],
            [22, 20],
            [0, 0],
            [1, 0],
            [0, 1],
            [1, 1],
            [5, 5],
            [6, 5],
            [5, 6],
            [6, 6],
        ])

        y_train = np.array([
            [9],
            [9],
            [9],
            [9],
            [0],
            [0],
            [0],
            [0],
            [5],
            [5],
            [5],
            [5],
        ])

        classifier.fit(X_train, y_train)

        X = np.array([
            [15, 15],  # deberia dar 9
        ])

        got = classifier.predict(X)
        assertAllClose(self, np.array([0.0]), got)

        # prediciendo tomando como weights la distancia,
        # deberia dar el valor correcto
        classifier = metnum.KNNClassifier(12, "distance")
        classifier.fit(X_train, y_train)

        got = classifier.predict(X)
        assertAllClose(self, np.array([9.0]), got)
def run_no_pca(dfTrain, dataTest):
    dataTrain = dfTrain[dfTrain.columns[1:]].values
    labelTrain = dfTrain["label"].values.reshape(-1, 1)
    dataTest = dataTest.values

    clf = metnum.KNNClassifier(k)
    clf.fit(dataTrain, labelTrain)
    pred = clf.predict(dataTest)

    return pred
def run_pca(dfTrain, dataTest):
    dataTrain = dfTrain[dfTrain.columns[1:]].values
    labelTrain = dfTrain["label"].values.reshape(-1, 1)
    dataTest = dataTest.values

    pca = metnum.PCA(alpha)
    pca.fit(dataTrain)

    dataTrain = pca.transform(dataTrain)
    dataTest = pca.transform(dataTest)

    clf = metnum.KNNClassifier(k)
    clf.fit(dataTrain, labelTrain)
    pred = clf.predict(dataTest)

    return pred
Esempio n. 4
0
    def test_little(self):
        """Testea un caso en R2"""
        classifier = metnum.KNNClassifier(3, "uniform")

        X_train = np.array([
            [0, 0],
            [1, 0],
            [0, 1],
            [1, 1],
            [5, 5],
            [6, 5],
            [5, 6],
            [6, 6],
            [20, 20],
            [21, 20],
            [20, 22],
            [22, 20],
        ])

        y_train = np.array([
            [0],
            [0],
            [0],
            [0],
            [5],
            [5],
            [5],
            [5],
            [9],
            [9],
            [9],
            [9],
        ])

        classifier.fit(X_train, y_train)

        X = np.array([
            [15, 15],  # 9
            [10, 10],  # 5
            [2, 2],  # 0
        ])

        want = np.array([9.0, 5.0, 0.0])
        got = classifier.predict(X)

        assertAllClose(self, want, got)
Esempio n. 5
0
    def test_same(self):
        """Testea que predecir la matriz de entrenamiento de el mismo res"""
        classifier = metnum.KNNClassifier(1, "uniform")

        X_train = np.array([
            [1, 2, 3],
            [6, 6, 6],
            [0, 1, 0],
        ])

        y_train = np.array([
            [0],
            [2],
            [3],
        ])

        classifier.fit(X_train, y_train)

        y = classifier.predict(X_train)

        self.assertTrue(np.allclose(y.reshape(3, 1), y_train))