├── Gussian.py ├── Gussian_Sampling.py ├── Gussian_Sampling2.py ├── README.md └── yolo_segment_train1.zip /Gussian.py: -------------------------------------------------------------------------------- 1 | # Define the desired distribution to sample from 2 | # GUSSIAN SAMPLING... 3 | 4 | # Import Libraries 5 | 6 | import matplotlib.pyplot as plt 7 | import numpy as np 8 | 9 | d = 2 # Number of dimensions 10 | mean = np.matrix([[0.], [1.]]) 11 | covariance = np.matrix([ 12 | [1, 0.8], 13 | [0.8, 1] 14 | ]) 15 | 16 | # Compute the Decomposition: 17 | A = np.linalg.cholesky(covariance) 18 | 19 | # Sample X from standard normal 20 | n = 50 # Samples to draw 21 | Z = np.random.normal(size=(d, n)) 22 | 23 | # Apply the transformation 24 | X = A.dot(Z) + mean 25 | 26 | # Plot the samples and the distribution 27 | fig, ax = plt.subplots(figsize=(6, 4.5)) 28 | # Plot bivariate distribution 29 | x1, x2, p = generate_surface(mean, covariance, d) 30 | con = ax.contourf(x1, x2, p, 33, cmap=cm.YlGnBu) 31 | # Plot samples 32 | ax.plot(Y[0,:], Y[1,:], 'ro', alpha=.6, 33 | markeredgecolor='k', markeredgewidth=0.5) 34 | ax.set_xlabel('y1', fontsize=13) 35 | ax.set_ylabel('y2', fontsize=13) 36 | ax.axis([-2.5, 2.5, -1.5, 3.5]) 37 | ax.set_aspect('equal') 38 | ax.set_title('Samples from bivariate normal distribution') 39 | cbar = plt.colorbar(con) 40 | cbar.ax.set_ylabel('density: p(y1, y2)', fontsize=13) 41 | plt.show() 42 | -------------------------------------------------------------------------------- /Gussian_Sampling.py: -------------------------------------------------------------------------------- 1 | # Import Libreries 2 | 3 | 4 | import numpy as np 5 | from mpl_toolkits.mplot3d import Axes3D 6 | from matplotlib import cm 7 | import matplotlib.pyplot as plt 8 | from scipy.stats import multivariate_normal 9 | import math 10 | 11 | def get_gaussian_random(): 12 | m = 0 13 | while m == 0: 14 | m = round(np.random.random() * 100) 15 | 16 | numbers = np.random.random(int(m)) 17 | summation = float(np.sum(numbers)) 18 | gaussian = (summation - m / 2) / math.sqrt(m / 12.0) 19 | return gaussian 20 | 21 | def learn_mean_cov(pts): 22 | learned_mean = np.matrix([[0.0], [0.0]]) 23 | learned_cov = np.zeros((2, 2)) 24 | count = len(pts) 25 | for pt in pts: 26 | learned_mean += pt 27 | learned_cov += pt * pt.transpose() 28 | 29 | learned_mean /= count 30 | learned_cov /= count 31 | learned_cov -= learned_mean * learned_mean.transpose() 32 | return learned_mean, learned_cov 33 | 34 | 35 | def generate_known_gaussian(dimensions, count): 36 | ret = [] 37 | for i in range(count): 38 | current_vector = [] 39 | for j in range(dimensions): 40 | g = get_gaussian_random() 41 | current_vector.append(g) 42 | ret.append(tuple(current_vector)) 43 | return ret 44 | 45 | def sample(count, c): 46 | known = generate_known_gaussian(2, count) 47 | target_mean = np.matrix([[2.0], [8.0]]) 48 | target_cov = np.matrix([[0.25, 0.25], 49 | [0.25, 0.5]]) 50 | 51 | [eigenvalues, eigenvectors] = np.linalg.eig(target_cov) 52 | l = np.matrix(np.diag(np.sqrt(eigenvalues))) 53 | Q = np.matrix(eigenvectors) * l 54 | x1_tweaked = [] 55 | x2_tweaked = [] 56 | tweaked_all = [] 57 | for i, j in known: 58 | original = np.matrix([[i], [j]]).copy() 59 | tweaked = (Q * original) + target_mean 60 | x1_tweaked.append(float(tweaked[0])) 61 | x2_tweaked.append(float(tweaked[1])) 62 | tweaked_all.append(tweaked) 63 | if c == 0: 64 | mu = np.array([2.0, 3.0]) 65 | X, Y = np.meshgrid(x1_tweaked, x2_tweaked) 66 | pos = np.dstack((X, Y)) 67 | rv = multivariate_normal(mu, target_cov) 68 | Z = rv.pdf(pos) 69 | fig = plt.figure() 70 | ax = fig.add_subplot(111, projection='3d') 71 | ax.plot_surface(X, Y, Z) 72 | return tweaked_all 73 | 74 | known = generate_known_gaussian(2,100) 75 | data = np.asarray(known) 76 | np.random.seed(0) 77 | x = np.random.rand(100, 1) 78 | y = 2 + 3 * x + np.random.rand(100, 1) 79 | print(x.shape) 80 | mean1 = (2, 3) 81 | cov1 = ([0, 0.5], 82 | [0.5, 0.25]) 83 | Ax1 = np.random.multivariate_normal(mean1, cov1, 100) 84 | 85 | 86 | temps = data[:, 0] 87 | rentals = data[:, 1] 88 | 89 | plt.scatter(temps, rentals, marker='x', color='red') 90 | plt.xlabel('Normalized Temperature in C') 91 | plt.ylabel('Bike Rentals in 1000s') 92 | 93 | def compute_cost(X, y, theta): 94 | return np.sum(np.square(np.matmul(X, theta) - y)) / (2 * len(y)) 95 | 96 | 97 | theta = np.zeros(2) 98 | X = np.column_stack((np.ones(len(temps)), temps)) 99 | y = rentals 100 | cost = compute_cost(X, y, theta) 101 | 102 | print('theta:', theta) 103 | print('cost:', cost) 104 | 105 | def gradient_descent(X, y, alpha, iterations): 106 | theta = np.zeros(2) 107 | m = len(y) 108 | 109 | for i in range(iterations): 110 | t0 = theta[0] - (alpha / m) * np.sum(np.dot(X, theta) - y) 111 | t1 = theta[1] - (alpha / m) * np.sum((np.dot(X, theta) - y) * X[:, 1]) 112 | theta = np.array([t0, t1]) 113 | return theta 114 | 115 | iterations = 5000 116 | alpha = 0.1 117 | 118 | theta = gradient_descent(X, y, alpha, iterations) 119 | cost = compute_cost(X, y, theta) 120 | 121 | print("theta:", theta) 122 | print('cost:', compute_cost(X, y, theta)) 123 | 124 | plt.scatter(temps, rentals, marker='x', color='red') 125 | plt.xlabel('Normalized Temperature in C') 126 | plt.ylabel('Bike Rentals in 1000s') 127 | samples = np.linspace(min(temps), max(temps)) 128 | plt.plot(samples, theta[0] + theta[1] * samples) 129 | 130 | Xs, Ys = np.meshgrid(np.linspace(-5, 5, 50), np.linspace(-40, 40, 50)) 131 | Zs = np.array([compute_cost(X, y, [t0, t1]) for t0, t1 in zip(np.ravel(Xs), np.ravel(Ys))]) 132 | Zs = np.reshape(Zs, Xs.shape) 133 | 134 | fig = plt.figure(figsize=(7, 7)) 135 | ax = fig.gca(projection="3d") 136 | ax.set_xlabel(r't0') 137 | ax.set_ylabel(r't1') 138 | ax.set_zlabel(r'cost') 139 | ax.view_init(elev=25, azim=40) 140 | ax.plot_surface(Xs, Ys, Zs, cmap=cm.rainbow) 141 | 142 | ax = plt.figure().gca() 143 | ax.plot(theta[0], theta[1], 'r*', label='Solution Found') 144 | CS = plt.contour(Xs, Ys, Zs, np.logspace(-10, 10, 50), label='Cost Function') 145 | plt.clabel(CS, inline=1, fontsize=10) 146 | plt.title("Contour Plot of Cost Function") 147 | plt.xlabel("w0") 148 | plt.ylabel("w1") 149 | plt.legend(loc='best') 150 | plt.show() 151 | -------------------------------------------------------------------------------- /Gussian_Sampling2.py: -------------------------------------------------------------------------------- 1 | # Import Libraries 2 | 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from mpl_toolkits.mplot3d import Axes3D 6 | from matplotlib import cm 7 | 8 | data_x = np.linspace(1.0, 10.0, 100)[:, np.newaxis] 9 | data_y = np.sin(data_x) + 0.1 * np.power(data_x, 2) + 0.5 * np.random.randn(100, 1) 10 | data_x /= np.max(data_x) 11 | data_x = np.hstack((np.ones_like(data_x), data_x)) 12 | order = np.random.permutation(len(data_x)) 13 | portion = 20 14 | test_x = data_x[order[:portion]] 15 | test_y = data_y[order[:portion]] 16 | train_x = data_x[order[portion:]] 17 | train_y = data_y[order[portion:]] 18 | 19 | def get_gradient(w, x, y): 20 | y_estimate = x.dot(w).flatten() 21 | error = (y.flatten() - y_estimate) 22 | mse = (1.0/len(x))*np.sum(np.power(error, 2)) 23 | gradient = -(1.0/len(x)) * error.dot(x) 24 | return gradient, mse 25 | 26 | w = np.random.randn(2) 27 | alpha = 0.5 28 | tolerance = 1e-5 29 | 30 | # Perform Gradient Descent 31 | iterations = 1 32 | 33 | while True: 34 | gradient, error = get_gradient(w, train_x, train_y) 35 | new_w = w - alpha * gradient 36 | 37 | # Stopping Condition 38 | if np.sum(abs(new_w - w)) < tolerance: 39 | print 40 | "Converged." 41 | break 42 | 43 | # Print error every 50 iterations 44 | if iterations % 100 == 0: 45 | print 46 | "Iteration: %d - Error: %.4f" % (iterations, error) 47 | iterations += 1 48 | w = new_w 49 | 50 | print 51 | "w =", w 52 | print 53 | "Test Cost =", get_gradient(w, test_x, test_y)[1] 54 | plt.plot(data_x[:,1], data_x.dot(w), c='g', label='Model') 55 | plt.scatter(train_x[:,1], train_y, c='b', label='Train Set') 56 | plt.scatter(test_x[:,1], test_y, c='r', label='Test Set') 57 | plt.grid() 58 | plt.legend(loc='best') 59 | plt.xlabel('X') 60 | plt.ylabel('Y') 61 | plt.figure() 62 | w1 = np.linspace(-w[1]*3, w[1]*3, 300) 63 | w0 = np.linspace(-w[0]*3, w[0]*3, 300) 64 | J_vals = np.zeros(shape=(w1.size, w0.size)) 65 | 66 | for t1, element in enumerate(w1): 67 | for t2, element2 in enumerate(w0): 68 | wT = [0, 0] 69 | wT[1] = element 70 | wT[0] = element2 71 | J_vals[t1, t2] = get_gradient(wT, train_x, train_y)[1] 72 | 73 | plt.scatter(w[0], w[1], marker='*', color='r', s=40, label='Solution Found') 74 | CS = plt.contour(w0, w1, J_vals, np.logspace(-10,10,50), label='Cost Function') 75 | plt.clabel(CS, inline=1, fontsize=10) 76 | plt.title("Contour Plot of Cost Function") 77 | plt.xlabel("w0") 78 | plt.ylabel("w1") 79 | plt.legend(loc='best') 80 | 81 | fig = plt.figure(figsize=(7, 7)) 82 | ax = fig.gca(projection="3d") 83 | ax.set_xlabel(r't0') 84 | ax.set_ylabel(r't1') 85 | ax.set_zlabel(r'cost') 86 | ax.view_init(elev=25, azim=40) 87 | ax.plot_surface(w0, w1, J_vals, cmap=cm.rainbow) 88 | 89 | plt.show() 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Gussian_Sampling 2 | ## Gussian Sampling Generation 3 | 4 | Multivariate Gaussian distribution is a fundamental concept in statistics and machine learning that finds applications in various fields, including data analysis, image processing, and natural language processing. It is a probability distribution that describes the probability of multiple random variables being correlated with each other. The process of generating random samples from a multivariate Gaussian distribution can be challenging, particularly when the dimensionality of the data is high. In this post, we will explore the topic of sampling from a multivariate Gaussian distribution and provide Python code examples to help you understand and implement this concept. 5 | 6 | 7 | Gaussian sampling generation in Python refers to the process of generating random numbers from a Gaussian (normal) distribution using the NumPy library. The Gaussian distribution is a continuous probability distribution defined by its mean (μ) and standard deviation (σ). 8 | 9 | To generate Gaussian samples in Python, you can use the numpy.random.normal() function. Here is an example: 10 | 11 | ```bash 12 | import numpy as np 13 | 14 | # Set the mean and standard deviation of the Gaussian distribution 15 | mean = 0 # Mean of the distribution 16 | std_dev = 1 # Standard deviation of the distribution 17 | 18 | # Generate 100 random samples from the Gaussian distribution 19 | samples = np.random.normal(mean, std_dev, 100) 20 | 21 | # Print the generated samples 22 | print(samples) 23 | ``` 24 | In the above code, the numpy.random.normal() function takes three arguments: the mean, standard deviation, and the number of samples to generate. It returns an array of random numbers drawn from the specified Gaussian distribution. 25 | 26 | You can customize the mean and standard deviation values based on your requirements. Additionally, you can generate different numbers of samples by changing the third argument of the numpy.random.normal() function. 27 | 28 | Remember to import the numpy library at the beginning of your script to use the numpy.random.normal() function. 29 | The goal of this project is to generate Gaussian samples in 2-D from uniform samples, the latter of which can be readily generated using built-in random number generators in most computer languages. 30 | -------------------------------------------------------------------------------- /yolo_segment_train1.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AISoltani/Gussian_Sampling/34bf9de40e1fd0c0db0f8d9c521f57d8c96d95bb/yolo_segment_train1.zip --------------------------------------------------------------------------------