示例#1
0
    def test_validation(self):
        with self.assertRaises(ValueError):
            # More clusters than samples
            nw = RBFKMeans(n_clusters=1000)
            nw.train(self.data, epsilon=1e-5)

        with self.assertRaises(ValueError):
            # Number of clusters the same as number of samples
            nw = RBFKMeans(n_clusters=self.data.shape[0])
            nw.train(self.data, epsilon=1e-5)

        with self.assertRaises(ValueError):
            # One cluster
            nw = RBFKMeans(n_clusters=1)
            nw.train(self.data, epsilon=1e-5)
示例#2
0
    def test_classification(self):
        result = np.array([
            [0.228, 0.312],
            [0.48166667,  0.76666667],
        ])

        nw = RBFKMeans(n_clusters=2)
        nw.train(self.data, epsilon=1e-5)
        self.assertTrue(np.all(result == np.round(nw.centers, 8)))

        if self.draw_plot:
            classes = nw.predict(self.data)

            for i, center in enumerate(nw.centers):
                positions = np.argwhere(classes[:, 0] == i)
                class_data = np.take(self.data, positions[:, 0], axis=0)
                plt.plot(class_data[:, 0], class_data[:, 1], 'o')

            for center in nw.centers:
                plt.plot(center[0], center[1], 'kx')

            plt.axis([0, 1, 0, 1])
            plt.show()
示例#3
0
import numpy as np
from neupy.algorithms import RBFKMeans
from mnist import MNIST

mnistData=MNIST('./mnistData')
imgTrain,lblTrain=mnistData.load_training()
imgTest,lblTest=mnistData.load_testing()
        

data = np.array(imgTrain)


rbfk_net = RBFKMeans(n_clusters=40, verbose=False)
rbfk_net.train(data, epsilon=1e-5)
print len(rbfk_net.centers)

new_data = np.array(imgTest[1])
print rbfk_net.predict(new_data).shape
X = df.drop(['Grade'], axis=1)
Y = df.Grade

#Using Built in train test split function in sklearn
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)

# RBF and kmeans clustering by class
#Number of prototypes
#prototypes = int(input("Number of seed points:"))
prototypes = 12

#Finding cluster centers
df_cluster = X_train
df_cluster[
    'Grade'] = Y_train  #Reproduce original data but only with training values
rbfk_net = RBFKMeans(
    n_clusters=prototypes)  #Chose number of clusters that you want
rbfk_net.train(df_cluster, epsilon=1e-5)
center = pd.DataFrame(rbfk_net.centers)

# Turn the centers into prototypes values needed
X_prototypes = center.iloc[:, 0:-1]
Y_prototypes = center.iloc[:,
                           -1]  #Y_prototypes is the last column of center since 'Grade' is the last feature added to center.

#Train GRNN
GRNNet = GRNN(std=0.1)
GRNNet.train(X_prototypes, Y_prototypes)

# Cross validataion
score = cross_val_score(GRNNet, X_train, Y_train, scoring='r2', cv=5)
print("")
#print("")

ClassD_df = data_train[data_train['Letter'].isin(['D'])]
ClassD_data = ClassD_df.drop(['Letter'], axis=1)
#print (ClassD_data)
#print("")

# RBF and kmeans clustering by class

#Number of prototypes
#prototypes = int(input("Number of prototypes:"))
prototypes = 4
eps = 1e-5

#Finding cluster centers
rbfk_netA = RBFKMeans(
    n_clusters=prototypes)  #Chose number of clusters that you want
rbfk_netA.train(ClassA_data, epsilon=eps)
center_classA = pd.DataFrame(rbfk_netA.centers)

rbfk_netB = RBFKMeans(n_clusters=prototypes)
rbfk_netB.train(ClassB_data, epsilon=eps)
center_classB = pd.DataFrame(rbfk_netB.centers)

rbfk_netC = RBFKMeans(n_clusters=prototypes)
rbfk_netC.train(ClassC_data, epsilon=eps)
center_classC = pd.DataFrame(rbfk_netC.centers)

#Defining my Y_train
letter_classA = ['A'] * prototypes
letter_classB = ['B'] * prototypes
letter_classC = ['C'] * prototypes