Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. import numpy as np
  2. import random
  3. import os
  4. from scipy.spatial import distance
  5. from sklearn.model_selection import train_test_split
  6. from skopt import gp_minimize
  7. from scipy.optimize import minimize
  8. from sklearn.metrics import mean_squared_error
  9. from math import sqrt
  10. from scipy.stats.distributions import norm
  11. from sklearn.metrics.pairwise import euclidean_distances
  12. from scipy.spatial import distance
  13. from numpy import inf
  14. from sklearn.preprocessing import StandardScaler
  15. from sklearn.gaussian_process import GaussianProcessRegressor
  16. from sklearn.gaussian_process.kernels import (RBF, Matern, RationalQuadratic,
  17. ExpSineSquared, DotProduct,
  18. ConstantKernel,WhiteKernel)
  19.  
  20. from sklearn import metrics
  21. from sklearn.cluster import KMeans
  22. from multiprocessing import Process
  23. from multiprocessing import Pool
  24.  
  25. if __name__ == '__main__':
  26. with Pool(processes=4) as pool:
  27.  
  28. np.random.seed(1)
  29. np.set_printoptions(suppress=True)
  30.  
  31.  
  32. ################################ Here we load dataset ###################################################################################################
  33.  
  34. rmtest=np.loadtxt("test.dat",usecols=range(7)) # Load the test dataset
  35. X=rmtest
  36. kmeans = KMeans(n_clusters=4)
  37. kmeans.fit(X)
  38. y_kmeans = kmeans.predict(X)
  39. labels = kmeans.labels_
  40.  
  41. a = X[np.where(labels==0)]
  42. b = X[np.where(labels==1)]
  43. c = X[np.where(labels==2)]
  44. d = X[np.where(labels==3)]
  45.  
  46. print(a.shape)
  47. print(b.shape)
  48. print(c.shape)
  49. print(d.shape)
  50.  
  51.  
  52. armpredx=np.delete(a,6,1) # xvalues, in our case inverse distances
  53. armpredy=np.delete(a,np.s_[0:6],1) # energy values corresponding to the distances
  54.  
  55. brmpredx=np.delete(b,6,1) # xvalues, in our case inverse distances
  56. brmpredy=np.delete(b,np.s_[0:6],1) # energy values corresponding to the distances
  57.  
  58. crmpredx=np.delete(c,6,1) # xvalues, in our case inverse distances
  59. crmpredy=np.delete(c,np.s_[0:6],1) # energy values corresponding to the distances
  60.  
  61. drmpredx=np.delete(d,6,1) # xvalues, in our case inverse distances
  62. drmpredy=np.delete(d,np.s_[0:6],1) # energy values corresponding to the distances
  63.  
  64.  
  65.  
  66. sample=input("Enter number of training set points\n") # Number of points in our training set. You will be prompted to choose this.
  67. sample=int(sample)
  68.  
  69. np.random.seed(24)
  70.  
  71. apoints=a[np.random.choice(a.shape[0], sample, replace=False), :] # Training set based on number of points generated here.
  72. bpoints=b[np.random.choice(b.shape[0], sample, replace=False), :]
  73. cpoints=c[np.random.choice(c.shape[0], sample, replace=False), :]
  74. dpoints=d[np.random.choice(d.shape[0], sample, replace=False), :]
  75.  
  76. print("Total number of sample points for training is ",apoints.shape)
  77.  
  78.  
  79.  
  80. if os.path.exists('train.dat'): # we save the training set in train.dat
  81. os.remove('train.dat')
  82.  
  83. with open('train.dat', 'a') as l:
  84. np.savetxt(l, apoints,fmt='%1.6f',delimiter=' ')
  85.  
  86.  
  87. ainpx=np.delete(apoints,6,1) #inpx is the x-part of the training dataset
  88. ainpy=np.delete(apoints,np.s_[0:6],1) #inpy is the y-part of the training dataset
  89.  
  90. binpx=np.delete(bpoints,6,1) #inpx is the x-part of the training dataset
  91. binpy=np.delete(bpoints,np.s_[0:6],1) #inpy is the y-part of the training dataset
  92.  
  93. cinpx=np.delete(cpoints,6,1) #inpx is the x-part of the training dataset
  94. cinpy=np.delete(cpoints,np.s_[0:6],1) #inpy is the y-part of the training dataset
  95.  
  96. dinpx=np.delete(dpoints,6,1) #inpx is the x-part of the training dataset
  97. dinpy=np.delete(dpoints,np.s_[0:6],1) #inpy is the y-part of the training dataset
  98.  
  99. ########################### GPR done here#############################################################################################
  100. ## Instantiate a Gaussian Process model
  101. ##6 types of kernels represented.
  102.  
  103. k1 = 1**2* RBF(length_scale=0.01)
  104. k2=11**2*RationalQuadratic(length_scale=0.1, alpha=1.0, length_scale_bounds=(1e-05, 100000.0), alpha_bounds=(1e-05, 100000.0))
  105. k4=1**2*ExpSineSquared(length_scale=1.0, periodicity=1.0, length_scale_bounds=(1e-05, 100000.0), periodicity_bounds=(1e-05, 100000.0))
  106. k3 =Matern(length_scale=[0.1, 0.1, 0.1,0.1, 0.1, 0.1], length_scale_bounds=(1e-5, 1.0),nu=2.5)
  107. k5=ConstantKernel(constant_value=1.0, constant_value_bounds=(1e-05, 1000.0))
  108. k6=DotProduct(sigma_0=10, sigma_0_bounds=(1e-05, 100))
  109. k7=WhiteKernel(noise_level=0.1)
  110.  
  111. kernel= k5*k3*k6+k7 # we choose our kernel here.
  112. kernel1= k5*k3*k6+k7
  113. kernel2= k5*k3*k6+k7
  114. kernel3= k5*k3*k6+k7
  115.  
  116.  
  117. #optimisation step.
  118. gp = (GaussianProcessRegressor(kernel=kernel,n_restarts_optimizer=25))
  119. gp2 = (GaussianProcessRegressor(kernel=kernel1,n_restarts_optimizer=25))
  120. gp3 = (GaussianProcessRegressor(kernel=kernel2,n_restarts_optimizer=25))
  121. gp4 = (GaussianProcessRegressor(kernel=kernel3,n_restarts_optimizer=25))
  122.  
  123. agp=pool.apply(gp.fit,args=(ainpx,ainpy.reshape(-1,1)))
  124. bgp=pool.apply(gp.fit,args=(binpx,binpy.reshape(-1,1)))
  125. cgp=pool.apply(gp.fit,args=(cinpx,cinpy.reshape(-1,1)))
  126. dgp=pool.apply(gp.fit,args=(dinpx,dinpy.reshape(-1,1)))
  127. # #fitting step.
  128. # agp=pool.apply(gp.fit(ainpx, ainpy.reshape(-1,1))) # Here the GPR finds an optimal value of the kernel based on input x,y
  129. # bgp=pool.apply(gp2.fit(binpx, binpy.reshape(-1,1))) # Here the GPR finds an optimal value of the kernel based on input x,y
  130. # cgp=pool.apply(gp3.fit(cinpx, cinpy.reshape(-1,1))) # Here the GPR finds an optimal value of the kernel based on input x,y
  131. # dgp=pool.apply(gp4.fit(dinpx, dinpy.reshape(-1,1))) # Here the GPR finds an optimal value of the kernel based on input x,y
  132.  
  133. print("Fit done")
  134.  
  135. print("GPML kernel: %s" % agp.kernel_) # We print the kernel and the likelihood
  136. print("Log-marginal-likelihood: %.3f"
  137. % agp.log_marginal_likelihood(agp.kernel_.theta))
  138.  
  139. ######################################################### Generate RMSE values ###############################################################################################3
  140.  
  141. # name=str(len(ainpx)) #Generating a filename to save our final observations
  142. # filename = name+'_rmse'
  143.  
  144. # if os.path.exists(filename):
  145. # os.remove(filename)
  146. # f = open(filename , 'a')
  147.  
  148. # arm_pred, sigma = pool.apply(gp.predict,args=(armpredx, return_std=True)) #Generating prediction values for our test database
  149. # brm_pred, sigma1 = pool.apply(gp2.predict,args=(brmpredx, return_std=True)) #Generating prediction values for our test database
  150. # crm_pred, sigma2 = pool.apply(gp3.predict,args=(crmpredx, return_std=True)) #Generating prediction values for our test database
  151. # drm_pred, sigma3 = pool.apply(gp4.predict,args=(drmpredx, return_std=True)) #Generating prediction values for our test database
  152. # brm_pred, sigma1 = gp.predict(brmpredx, return_std=True) #Generating prediction values for our test database
  153. # crm_pred, sigma2 = gp.predict(crmpredx, return_std=True) #Generating prediction values for our test database
  154. # drm_pred, sigma3 = gp.predict(drmpredx, return_std=True) #Generating prediction values for our test database
  155.  
  156.  
  157. # # print("Observed value --- Predicted value---Absolute difference\n")
  158.  
  159. # armde=np.c_[armpredy, arm_pred,abs(armpredy-arm_pred)] #Generating an array to compare the original and predictedvalues
  160. # brmde=np.c_[brmpredy, brm_pred,abs(brmpredy-brm_pred)]
  161. # crmde=np.c_[crmpredy, crm_pred,abs(crmpredy-crm_pred)]
  162. # drmde=np.c_[drmpredy, drm_pred,abs(drmpredy-drm_pred)]
  163.  
  164. # # print (rmde)
  165.  
  166. # aerr=sqrt(mean_squared_error(armpredy, arm_pred)) #Generating an RMSE value for our testset
  167. # print("RMSE-A ",aerr)
  168.  
  169. # berr=sqrt(mean_squared_error(brmpredy, brm_pred)) #Generating an RMSE value for our testset
  170. # print("RMSE-B ",berr)
  171.  
  172. # cerr=sqrt(mean_squared_error(crmpredy, crm_pred)) #Generating an RMSE value for our testset
  173. # print("RMSE-C ",cerr)
  174.  
  175. # derr=sqrt(mean_squared_error(drmpredy, drm_pred)) #Generating an RMSE value for our testset
  176. # print("RMSE-D ",derr)
  177.  
  178. # # f.write("RMSE value is ")
  179. # # f.write(str(err))
  180. # # f.write("\n") # Saving the values to the file. We save the RMSE followed by the comparision array
  181. # # f.write("Observed value --- Predicted value---Absolute difference\n")
  182. # # np.savetxt(f, rmde,fmt='%1.6f',delimiter=' ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement