import numpy as np from sklearn.neural_network import MLPClassifier class AlgorithmMLP(): # method: AlgorithmKNN::constructor # # arguments: # win_input: GUI input display # win_output: GUI output display # win_log: GUI process log # # return: none # def __init__(self, win_input, win_output, win_log, nlayers, solver): # create class data # AlgorithmMLP.__CLASS_NAME__ = self.__class__.__name__ # copy the inputs into class data # self.input_d = win_input self.output_d = win_output self.log_d = win_log self.nlayers = nlayers self.solver = solver # method: AlgorithmKNN::initialize # # arguments: None # # return: True # # initialize variables for KNN # def initialize(self, data): # initialize variables # self.data = data self.classes = len(self.data) self.mlp = MLPClassifier(hidden_layer_sizes=self.nlayers, solver=self.solver) self.X = np.empty((0, 0)) return True # method: AlgorithmKNN::run_algo # # arguments: None # # return: True # # run algorithm steps # def run_algo(self, data): # calc everything and display stats self.initialize(data) self.train() return True def create_labels(self): labels = [] count = 0 d = self.input_d.class_info for i in d: total_samples = len(d[i][1]) labels = labels + [count] * total_samples count += 1 labels = np.array(labels) return labels def train(self): labels = self.create_labels() data = np.vstack((self.data)) self.mlp.fit(data,labels) def predict(self, ax, X): X = np.concatenate(X, axis=0) X = np.reshape(X, (-1, 2)) res = (ax.canvas.axes.get_xlim()[1] - ax.canvas.axes.get_ylim()[0]) / 100 x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, res), np.arange(y_min, y_max, res)) Z = self.mlp.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) return xx, yy, Z def prediction_classifier(self, data): data = np.array([data]) prediction = self.mlp.predict(data) return prediction[0]