コード例 #1
0
ファイル: test_lvq.py プロジェクト: webdiscover/neupy
    def test_lvq_initialization_exceptions(self):
        with self.assertRaises(ValueError):
            # n_sublcasses < n_classes
            algorithms.LVQ(n_inputs=2, n_subclasses=2, n_classes=3)

        with self.assertRaises(ValueError):
            # sum(prototypes_per_class) != n_subclasses
            algorithms.LVQ(n_inputs=2, n_subclasses=10, n_classes=3,
                           prototypes_per_class=[5, 3, 3])

        with self.assertRaises(ValueError):
            # len(prototypes_per_class) != n_classes
            algorithms.LVQ(n_inputs=2, n_subclasses=10, n_classes=3,
                           prototypes_per_class=[5, 5])
コード例 #2
0
 def test_lvq_with_odd_number_of_subclasses(self):
     lvqnet = algorithms.LVQ(
         n_inputs=2,
         n_subclasses=3,
         n_classes=2,
     )
     self.assertIn(lvqnet.prototypes_per_class, ([2, 1], [1, 2]))
コード例 #3
0
ファイル: test_lvq.py プロジェクト: webdiscover/neupy
    def test_lvq_weight_initialization_state(self):
        lvqnet = algorithms.LVQ(n_inputs=2, n_classes=2)
        self.assertFalse(lvqnet.initialized)

        lvqnet.train(np.random.random((10, 2)), np.random.random(10).round(),
                     epochs=1)
        self.assertTrue(lvqnet.initialized)

        lvqnet = algorithms.LVQ(n_inputs=2, n_classes=3,
                                weight=np.random.random((2, 3)))
        self.assertTrue(lvqnet.initialized)

        lvqnet = algorithms.LVQ(n_inputs=2, n_classes=3,
                                weight=init.Normal())
        self.assertTrue(lvqnet.initialized)
        self.assertEqual(lvqnet.weight.shape, (2, 3))
コード例 #4
0
    def test_lvq_with_custom_set_of_prototypes(self):
        lvqnet = algorithms.LVQ(
            n_inputs=2,
            n_subclasses=4,
            n_classes=2,
            prototypes_per_class=[3, 1],
        )

        lvqnet.train(self.data, self.target, epochs=3)
        self.assertGreater(lvqnet.errors.last(), 0)

        lvqnet.train(self.data, self.target, epochs=30)
        self.assertEqual(lvqnet.errors.last(), 0)
コード例 #5
0
ファイル: test_lvq.py プロジェクト: kradygithub/neupy
    def test_simple_lvq(self):
        lvqnet = algorithms.LVQ(
            n_inputs=2,
            n_subclasses=4,
            n_classes=2,
            shuffle_data=True,
        )

        lvqnet.train(self.data, self.target, epochs=100)
        predicted_target = lvqnet.predict(self.data)

        self.assertEqual(lvqnet.errors.last(), 0)
        np.testing.assert_array_equal(predicted_target, self.target[:, 0])
コード例 #6
0
    def test_lvq_with_disabled_step_reduction(self):
        lvqnet = algorithms.LVQ(
            n_inputs=2,
            n_subclasses=4,
            n_classes=2,
            n_updates_to_stepdrop=None,
        )

        n_expected_updates = 0
        for i in range(10):
            n_expected_updates += len(self.data)
            lvqnet.train(self.data, self.target, epochs=1)
            self.assertAlmostEqual(lvqnet.training_step, lvqnet.step)

        self.assertEqual(n_expected_updates, lvqnet.n_updates)
コード例 #7
0
    def test_lvq_step_reduction(self):
        lvqnet = algorithms.LVQ(
            n_inputs=2,
            n_subclasses=4,
            n_classes=2,
            minstep=0.1,
            step=1.1,
            n_updates_to_stepdrop=200,
        )

        n_expected_updates = 0
        for i in range(10):
            n_expected_updates += len(self.data)
            lvqnet.train(self.data, self.target, epochs=1)
            expected_step = 1.1 - (i + 1) * 0.1
            self.assertAlmostEqual(lvqnet.training_step, expected_step)

        self.assertEqual(n_expected_updates, lvqnet.n_updates)
        self.assertEqual(lvqnet.training_step, lvqnet.minstep)
コード例 #8
0
    def test_compare_lvq_and_lvq21(self):
        dataset = datasets.load_iris()
        data, target = dataset.data, dataset.target

        # Prepare the same weights for the fair comparison
        lvq = algorithms.LVQ(n_inputs=4, n_subclasses=3, n_classes=3)
        lvq.train(data, target, epochs=1)
        prepared_lvq_weights = lvq.weight

        compare_networks(
            algorithms.LVQ,
            partial(algorithms.LVQ21, epsilon=0.1),
            data=[data, target],
            epochs=10,
            show_comparison_plot=False,
            n_inputs=4,
            n_subclasses=3,
            n_classes=3,
            weight=prepared_lvq_weights,
        )
コード例 #9
0
    def test_lvq_training_exceptions(self):
        lvqnet = algorithms.LVQ(n_inputs=2, n_subclasses=4, n_classes=2)

        with self.assertRaises(NotTrained):
            lvqnet.predict(np.array([1, 2]))

        with self.assertRaises(ValueError):
            input_train = np.array([
                [1, 2],
                [3, 4],
                [4, 5],
            ])
            target_train = np.array([0, 1, 0])
            # len(input_train) <= n_subclasses
            lvqnet.train(input_train, target_train)

        with self.assertRaises(ValueError):
            input_train = np.array([
                [1, 2],
                [3, 4],
                [5, 6],
                [7, 8],
                [9, 10],
            ])
            target_train = np.array([0, 0, 0, 0, 1])
            # there are should be 3 or more samples for
            # class 1, got only 1
            lvqnet.train(input_train, target_train)

        with self.assertRaises(ValueError):
            input_train = np.array([
                [1, 2],
                [3, 4],
                [4, 5],
                [5, 6],
                [67, 8],
            ])
            target_train = np.array([0, 1, 0, 1, 2])
            # 3 unique classes instead of 2 expected
            lvqnet.train(input_train, target_train)
コード例 #10
0
    def test_compare_lvq_and_lvq3(self):
        dataset = datasets.load_iris()
        data, target = dataset.data, dataset.target

        # Prepare the same weights for the fair comparison
        lvq = algorithms.LVQ(n_inputs=4, n_subclasses=6, n_classes=3)
        lvq.train(data, target, epochs=1)
        prepared_lvq_weights = lvq.weight

        compare_networks(
            algorithms.LVQ,
            partial(algorithms.LVQ3, epsilon=0.4),
            data=[data, target],
            epochs=100,
            show_comparison_plot=False,
            n_inputs=4,
            n_subclasses=6,
            n_classes=3,
            prototypes_per_class=[4, 1, 1],
            step=0.001,
            weight=prepared_lvq_weights,
        )
コード例 #11
0
"""
Created on Thu Nov  7 23:40:37 2019

@author: abhishek
"""
import numpy as np
import pandas as pd
from neupy import algorithms
import matplotlib.pyplot as plt
from sklearn import preprocessing
import pickle

from sklearn import datasets
import numpy as np

classifier = algorithms.LVQ(n_inputs=4, n_classes=3)
iris = datasets.load_iris()

x = iris.data
y = iris.target.reshape(-1, 1)

print(x[:5])
print(y[:5])
print(x.shape)
# print(y.head())

print(y.shape)
colors = (0, 0, 0)
data = pd.DataFrame(iris.data)

plt.scatter(data[0], y, c="red", alpha=0.5)
コード例 #12
0
data1.columns = [
    'sample_code_number', 'clump_thickness', 'uniformity_of_cell_size',
    'uniformity_of_cell_shape', 'marginal_adhesion',
    'single_epithelial_cell_size', 'bare_nuclei', 'bland_chromatin',
    'normal_nucleoli', 'mitoses', 'class'
]
feature_columns1 = [
    'clump_thickness', 'uniformity_of_cell_size', 'uniformity_of_cell_shape',
    'marginal_adhesion', 'single_epithelial_cell_size', 'bare_nuclei',
    'bland_chromatin', 'normal_nucleoli', 'mitoses'
]

missingRemovedData1 = data1[data1['bare_nuclei'] !=
                            '?']  # remove rows with missing data

X1 = missingRemovedData1[feature_columns1]
y1 = missingRemovedData1['class']

# split X and y into training and teting sets
X1_train, X1_test, y1_train, y1_test = train_test_split(X1, y1, test_size=0.35)

lvqnet = algorithms.LVQ(n_inputs=9, n_classes=2)
lvqnet.train(X1_train, y1_train, epochs=100)
y1_pred = lvqnet.predict(X1_test)

from sklearn import metrics

print("Accuracy:  %.2f%%", metrics.accuracy_score(y1_test, y1_pred) * 100.0)

# In[ ]: