コード例 #1
0
ファイル: test_kmeans.py プロジェクト: bioidiap/bob.learn.em
def test_KMeansMachine():
    # Test a KMeansMachine

    means = np.array([[3, 70, 0], [4, 72, 0]], "float64")
    test_val = np.array([3, 70, 1], "float64")
    test_arr = np.array([[3, 70, 1], [5, 72, 0]], "float64")

    for transform in (to_numpy, to_dask_array):
        means, test_val, test_arr = transform(means, test_val, test_arr)

        # Initializes a KMeansMachine
        km = KMeansMachine(2)
        km.centroids_ = means

        # Distance and closest mean
        np.testing.assert_equal(km.transform(test_val)[0], np.array([1]))
        np.testing.assert_equal(km.transform(test_val)[1], np.array([6]))

        index = km.predict(test_val)
        assert index == 0

        indices = km.predict(test_arr)
        np.testing.assert_equal(indices, np.array([0, 1]))

        # Check __eq__ and is_similar_to
        km2 = KMeansMachine(2)
        assert km != km2
        assert not km.is_similar_to(km2)
        km2 = copy.deepcopy(km)
        assert km == km2
        assert km.is_similar_to(km2)
        km2.centroids_[0, 0] += 1
        assert km != km2
        assert not km.is_similar_to(km2)
コード例 #2
0
ファイル: test_kmeans.py プロジェクト: 183amir/bob.learn.em
def test_KMeansMachine():
  # Test a KMeansMachine

  means = numpy.array([[3, 70, 0], [4, 72, 0]], 'float64')
  mean  = numpy.array([3,70,1], 'float64')

  # Initializes a KMeansMachine
  km = KMeansMachine(2,3)
  km.means = means
  assert km.shape == (2,3)

  # Sets and gets
  assert (km.means == means).all()
  assert (km.get_mean(0) == means[0,:]).all()  
  assert (km.get_mean(1) == means[1,:]).all()
  km.set_mean(0, mean)
  assert (km.get_mean(0) == mean).all()

  # Distance and closest mean
  eps = 1e-10

  assert equals( km.get_distance_from_mean(mean, 0), 0, eps)
  assert equals( km.get_distance_from_mean(mean, 1), 6, eps)  
  
  (index, dist) = km.get_closest_mean(mean)
  
  assert index == 0
  assert equals( dist, 0, eps)
  assert equals( km.get_min_distance(mean), 0, eps)

  # Loads and saves
  filename = str(tempfile.mkstemp(".hdf5")[1])
  km.save(bob.io.base.HDF5File(filename, 'w'))
  km_loaded = KMeansMachine(bob.io.base.HDF5File(filename))
  assert km == km_loaded

  # Resize
  km.resize(4,5)
  assert km.shape == (4,5)

  # Copy constructor and comparison operators
  km.resize(2,3)
  km2 = KMeansMachine(km)
  assert km2 == km
  assert (km2 != km) is False
  assert km2.is_similar_to(km)
  means2 = numpy.array([[3, 70, 0], [4, 72, 2]], 'float64')
  km2.means = means2
  assert (km2 == km) is False
  assert km2 != km
  assert (km2.is_similar_to(km)) is False

  # Clean-up
  os.unlink(filename)
コード例 #3
0
def test_KMeansMachine():
    # Test a KMeansMachine

    means = numpy.array([[3, 70, 0], [4, 72, 0]], 'float64')
    mean = numpy.array([3, 70, 1], 'float64')

    # Initializes a KMeansMachine
    km = KMeansMachine(2, 3)
    km.means = means
    assert km.shape == (2, 3)

    # Sets and gets
    assert (km.means == means).all()
    assert (km.get_mean(0) == means[0, :]).all()
    assert (km.get_mean(1) == means[1, :]).all()
    km.set_mean(0, mean)
    assert (km.get_mean(0) == mean).all()

    # Distance and closest mean
    eps = 1e-10

    assert equals(km.get_distance_from_mean(mean, 0), 0, eps)
    assert equals(km.get_distance_from_mean(mean, 1), 6, eps)

    (index, dist) = km.get_closest_mean(mean)

    assert index == 0
    assert equals(dist, 0, eps)
    assert equals(km.get_min_distance(mean), 0, eps)

    # Loads and saves
    filename = str(tempfile.mkstemp(".hdf5")[1])
    km.save(bob.io.base.HDF5File(filename, 'w'))
    km_loaded = KMeansMachine(bob.io.base.HDF5File(filename))
    assert km == km_loaded

    # Resize
    km.resize(4, 5)
    assert km.shape == (4, 5)

    # Copy constructor and comparison operators
    km.resize(2, 3)
    km2 = KMeansMachine(km)
    assert km2 == km
    assert (km2 != km) is False
    assert km2.is_similar_to(km)
    means2 = numpy.array([[3, 70, 0], [4, 72, 2]], 'float64')
    km2.means = means2
    assert (km2 == km) is False
    assert km2 != km
    assert (km2.is_similar_to(km)) is False

    # Clean-up
    os.unlink(filename)