Author: Not specified Language: python
Description: Not specified Timestamp: 2018-06-11 02:55:58 +0000
View raw paste Reply
  1. import math
  2. import numpy as np
  3. import random
  4. from sklearn import linear_model
  5. import matplotlib.pyplot as plt
  6. from sklearn import preprocessing
  7. P_1 = 1/2.0
  8. P_2 = 1/2.0
  9. p_1 = 2/3.0
  10. p_2 = 1/6.0
  11. p_3 = 1/6.0
  12. J = 6
  13. M = 100
  14. D = 100
  15. mu_Z = 0
  16. sigma_Z = 1
  17. mu_noise = np.zeros(D)
  18. sigma_noise = 0.01*np.identity(D)
  19.  
  20. def draw_phi(M):
  21.     phi = np.zeros((M,D))
  22.     for i in range(M):
  23.         for d in range(D):
  24.             r_num = random.random()
  25.             if r_num > 0 and r_num <= P_1:
  26.                 phi[i][d] = 1
  27.             elif r_num>P_1 and r_num<=1:
  28.                 phi[i][d] = -1
  29.     return phi            
  30.  
  31. def draw_vector(D):
  32.     u = np.zeros(D)
  33.     for i in range(D):
  34.         r_num = random.random()
  35.         if r_num > 0 and r_num <= p_1:
  36.             u[i] = 0
  37.             continue
  38.         if r_num > p_1 and r_num <= p_1+p_2:
  39.             u[i] = 1
  40.             continue
  41.         if r_num > p_1+p_2 and r_num <= 1:
  42.             u[i] = -1
  43.             continue  
  44.     return u
  45.  
  46. u = np.zeros((J,D))
  47. for j in range(J):
  48.     u[j] = draw_vector(D)
  49.  
  50. for i in range(J):
  51.     u[i] = draw_vector(D)
  52.     for k in range(i):
  53.         dot_product = np.dot(u[i] , u[k])
  54.         if ((dot_product) <= 0 and (dot_product == 0)):
  55.             i = i+1
  56.             break
  57.  
  58. def draw_z():
  59.     return np.random.normal(mu_Z, sigma_Z, 2)
  60.  
  61. def draw_noise():
  62.     return np.random.multivariate_normal(mu_noise, sigma_noise, 1)
  63.  
  64. #Component 1
  65. def gen_component_1(u):
  66.     z = draw_z()
  67.     #n = draw_noise()
  68.     X_1 = u[0] + z[0] * u[1] + z[1] * u[2]
  69.     return X_1
  70.  
  71. #Component 2
  72. def gen_component_2(u):
  73.     z = draw_z()
  74.     #n = draw_noise()
  75.     X_2 = 2 * u[3] + math.sqrt(2) * z[0] * u[4] + z[1] * u[5]  
  76.     return X_2
  77.  
  78. #Component 3
  79. def gen_component_3(u):
  80.     z = draw_z()
  81.     #n = draw_noise()
  82.     X_3 = math.sqrt(2) * u[5] + z[0] * (u[0]+u[1]) + (1/math.sqrt(2)) * z[1] * u[4]  
  83.     return X_3
  84.  
  85.  
  86. N = 2000
  87. s = np.ndarray((N,D))
  88. x = np.ndarray((N,D))
  89. labels = np.zeros((N,3))
  90. sample1_num = 0
  91. sample2_num = 0
  92. sample3_num = 0
  93. for i in range(N):
  94.     r = np.random.randint(0,3)
  95.     if r ==0:
  96.         sample1_num += 1
  97.         n = draw_noise()
  98.         s[i,:]= gen_component_1(u)
  99.         x[i,:]= s[i,:] + n
  100.     elif r ==1:
  101.         sample2_num += 1
  102.         n = draw_noise()
  103.         s[i,:]= gen_component_2(u)
  104.         x[i,:]= s[i,:] + n
  105.     elif r ==2:
  106.         sample3_num += 1
  107.         n = draw_noise()
  108.         s[i,:]= gen_component_2(u)
  109.         x[i,:]= s[i,:] + n
  110.     labels[i,r] =1
View raw paste Reply