class Program(): """ Object responsible for running the program and controlling the overall program flow. """ def __init__(self): np.random.seed(1) gamma = Gamma() gamma.set_estimators(2) self.classifier = BayesClassifier(2) self.classifier.add_class(Normal(), 0) self.classifier.add_class(gamma, 1) self.main() def main(self): """ Overall main function of the program. Generates data, trains model and measure accuracy of the model. """ x_train, y_train = self.generate_normal_data(600) x_test, y_test = self.generate_normal_data(300, plot=False) self.classifier.fit(x_train, y_train) acc = self.classifier.accuracy(x_test, y_test) print("accuracy: ", acc) def generate_normal_data(self, N, plot=True): """ Generate N data samples of normal distributed data for 1 class and gamma distributed data from 1 class. param N: total number of samples to generate type N: int param plot: True if the dataset is to be plotted type plot: bool, optional return: tuple of dataset and corresponding labels rtype: tuple """ mu = 23 sigma = 5 shape = 2 scale = 2 x1 = np.random.normal(mu, sigma, int(N / 3)) x2 = np.random.gamma(scale, shape, int(N / 3)) x = np.concatenate((x1, x2)) y = np.concatenate((np.zeros(int(N / 3)), np.ones(int(N / 3)))) if plot: plt.hist(x2, 50, density=True) plt.hist(x1, 50, density=True) plt.show() return x, y
def create_fit_model(self): # Create model classifier = BayesClassifier(2) classifier.add_class(MultivariateNormal(), 0) classifier.add_class(MultivariateNormal(), 1) x, y = self.create_data() # Train and return model classifier.fit(x, y) return classifier
class Program(): """ Object responsible for running the program and controlling the overall program flow. """ def __init__(self): np.random.seed(1) self.classifier = BayesClassifier(3) self.classifier.add_class(Normal(), 0) self.classifier.add_class(Normal(), 1) self.classifier.add_class(Normal(), 2) self.main() def main(self): """ Overall main function of the program. Generates data, trains model and measure accuracy of the model. """ x_train, y_train = self.generate_normal_data(60) x_test, y_test = self.generate_normal_data(30, plot=False) self.classifier.fit(x_train, y_train) acc = self.classifier.accuracy(x_test, y_test) print("accuracy: ", acc) def generate_normal_data(self, N, plot=True): """ Generate N data samples of normal distributed data for 3 classes. param N: total number of samples to generate type N: int param plot: True if the dataset is to be plotted type plot: bool, optional return: tuple of dataset and corresponding labels rtype: tuple """ mu1 = 2.5 mu2 = 4.3 mu3 = 6.2 sigma1 = 0.3 sigma2 = 0.5 sigma3 = 0.9 x1 = np.random.normal(mu1, sigma1, int(N / 3)) x2 = np.random.normal(mu2, sigma2, int(N / 3)) x3 = np.random.normal(mu3, sigma3, int(N / 3)) x = np.concatenate((x1, x2, x3)) y = np.concatenate( (np.zeros(int(N / 3)), np.ones(int(N / 3)), np.full((int(N / 3)), 2))) if plot: plt.figure(1, figsize=(8, 8)) plt.scatter(x1, np.zeros(x1.shape), s=120, facecolors='none', edgecolors='black', linewidth=3.0, label='Class 1') plt.scatter(x2, np.zeros(x2.shape), s=120, facecolors='none', edgecolors='blue', linewidth=3.0, label='Class 2') plt.scatter(x3, np.zeros(x3.shape), s=120, facecolors='none', edgecolors='red', linewidth=3.0, label='Class 3') plt.legend() plt.show() return x, y
class Program(): """ Object responsible for running the program and controlling the overall program flow. """ def __init__(self): self.classifier = BayesClassifier(2) self.classifier.add_class(MultivariateNormal(), 0) self.classifier.add_class(MultivariateNormal(), 1) self.main() def main(self): """ Overall main function of the program. Generates data, trains model and measure accuracy of the model. """ x_train, y_train = self.generate_multivariate_data(100, plot=True) x_test, y_test = self.generate_multivariate_data(50, plot=True) self.classifier.fit(x_train, y_train) acc = self.classifier.accuracy(x_test, y_test) print("accuracy: ", acc) def generate_multivariate_data(self, N, plot=True): """ Generate N multivariate gaussian samples param N: total number of samples to generate type N: int param plot: True if the dataset is to be plotted type plot: bool, optional return: tuple of dataset and corresponding labels rtype: tuple """ mu1 = np.array([1, 1]) mu2 = np.array([2.0, 2.0]) sigma = np.array([[0.2, 0.0], [0.0, 0.2]]) x1 = np.random.multivariate_normal(mu1, sigma, int(N / 2)) x2 = np.random.multivariate_normal(mu2, sigma, int(N / 2)) x = np.concatenate((x1, x2)) y = np.concatenate((np.zeros(int(N / 2)), np.ones(int(N / 2)))) if plot: plt.figure(1, figsize=(8, 8)) plt.scatter(x1[:, 0], x1[:, 1], s=120, facecolors='none', edgecolors='black', linewidth=3.0, label='Class 1') plt.scatter(x2[:, 0], x2[:, 1], s=120, facecolors='none', edgecolors='blue', linewidth=3.0, label='Class 2') plt.legend() plt.show() return x, y