def Make_X_UsingCopula_xi(self): Xvar = [] varr = list(zip(*self.ListNonDominated)) xx = rd.sample( self.ListNonDominated , len(self.ListNonDominated)/2) # demi de meilleur sol and random yy = [item for item in self.ListNonDominated if item not in xx] if len(xx)>len(yy): xx.pop() # test egality if len(yy)>len(xx): yy.pop() for j in range(self.ndimension): xm = [] ym = [] for i in range(len(yy)): # youo are here xm.append(xx[i][j]) ym.append(yy[i][j]) x = np.array(xm) y = np.array(ym) foo = Copula(x, y, family='frank') # gumbel, clayton, frank self.C[j] = deepcopy(foo) XX, YY = foo.generate_xy(self.Tbit) X1 = XX.tolist() Y1 = YY.tolist() Xvar.append(X1+Y1) Zvar = zip(*Xvar) Indexs = [] for index in range(len(Zvar)): # test du contraintes # if ( ( Zvar[index][0]<0 or Zvar[index][0]>1) or any(map(lambda x:True if (x < -5 or x > 5) else False ,Zvar[index][1:])) == True ): if ( any(map(lambda x:True if (x < self.xmin or x > self.xmax) else False ,Zvar[index])) == True ): Indexs.append(Zvar[index]) for index in range(len(Indexs)): Zvar.remove(Indexs[index]) return Zvar
def Make_X_UsingCopula2(self): Xvar = [] # Varrtmp = self.Choice() varr = list(zip(*self.ListNonDominated)) xx = rd.sample( self.ListNonDominated , len(self.ListNonDominated)/2) # demi de meilleur sol yy = [item for item in self.ListNonDominated if item not in xx] if len(xx)>len(yy): xx.pop() # test egality if len(yy)>len(xx): yy.pop() for j in range(m): xm = [] ym = [] for i in range(len(yy)): # youo are here rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr xm.append(deepcopy(xx[i][j])) ym.append(deepcopy(yy[i][j])) x = np.array(xm) y = np.array(ym) foo = Copula(x, y, family='frank') # gumbel, clayton, frank self.C[j] = deepcopy(foo) XX, YY = foo.generate_xy(200) X1 = XX.tolist() Y1 = YY.tolist() Xvar.append(X1+Y1) Zvar = zip(*Xvar) Indexs = [] for index in range(len(Zvar)): if ( any(map(lambda x:True if (x < 0 or x >1) else False ,Zvar[index])) == True ): Indexs.append(Zvar[index]) for index in range(len(Indexs)): Zvar.remove(Indexs[index]) return Zvar
def Make_X_UsingCopula2(self): indexes = [self.Constrained.index(b) for b in self.ListNonDominated] self.ListNonDominatedP = self.Load_PModel(indexes) Xvar = [] xx = rd.sample(self.ListNonDominatedP, len(self.ListNonDominatedP) / 2) # demi de meilleur sol and random yy = [item for item in self.ListNonDominatedP if item not in xx] if len(xx) > len(yy): xx.pop() # test egality if len(yy) > len(xx): yy.pop() for j in range(m): xm = [] ym = [] for i in range(len(yy)): xm.append(deepcopy(xx[i][j])) ym.append(deepcopy(yy[i][j])) x = np.array(xm) y = np.array(ym) foo = Copula(x, y, family='frank') # gumbel, clayton, frank self.C[j] = deepcopy(foo) XX, YY = foo.generate_xy(150) X1 = XX.tolist() Y1 = YY.tolist() Xvar.append(X1 + Y1) Zvar = zip(*Xvar) Zvar = Zvar + self.ListNonDominatedP self.P = Zvar new = self.isContrainte(self.Lecture(Zvar)) # self.P = self.P + self.ListNonDominatedP return new
def Make_X_UsingCopula_half(self): Xvar = range(self.ndimension) # pour que la liste puisse contenir 30 elements # Varrtmp = self.Choice() varr = list(zip(*self.ListNonDominated)) indexs = rd.sample(range(self.ndimension),self.ndimension) xx = rd.sample( self.ListNonDominated , len(self.ListNonDominated)/2) # demi de meilleur sol yy = [item for item in self.ListNonDominated if item not in xx] if len(xx)>len(yy): xx.pop() # test egality if len(yy)>len(xx): yy.pop() k = 0 for j in range(self.ndimension/2): xm = deepcopy(varr[k]) ym = deepcopy(varr[k+1]) x = np.array(xm) y = np.array(ym) foo = Copula(x, y, family='frank') # gumbel, clayton, frank self.C[j] = deepcopy(foo) XX, YY = foo.generate_xy(self.Tbit) X1 = XX.tolist() Y1 = YY.tolist() Xvar[k] = deepcopy(X1) Xvar[k+1] = deepcopy(Y1) k = k + 2 Zvar = zip(*Xvar) Indexs = [] for index in range(len(Zvar)): # test du contraintes # if ( ( Zvar[index][0]<0 or Zvar[index][0]>1) or any(map(lambda x:True if (x < self.xmin or x > self.xmax) else False ,Zvar[index][1:])) == True ): if ( any(map(lambda x:True if (x < self.xmin or x > self.xmax ) else False ,Zvar[index])) == True ): Indexs.append(Zvar[index]) for index in range(len(Indexs)): Zvar.remove(Indexs[index]) return Zvar
def Make_X_UsingCopula2(self): Xvar = range(30) # pour que la liste puisse contenir 30 elements # Varrtmp = self.Choice() varr = list(zip(*self.ListNonDominated)) indexs = rd.sample(range(30),30) xx = rd.sample( self.ListNonDominated , len(self.ListNonDominated)/2) # demi de meilleur sol yy = [item for item in self.ListNonDominated if item not in xx] if len(xx)>len(yy): xx.pop() # test egality if len(yy)>len(xx): yy.pop() k = 0 for j in range(m/2): xm = deepcopy(varr[k]) ym = deepcopy(varr[k+1]) x = np.array(xm) y = np.array(ym) foo = Copula(x, y, family='frank') # gumbel, clayton, frank self.C[j] = deepcopy(foo) XX, YY = foo.generate_xy(200) X1 = XX.tolist() Y1 = YY.tolist() Xvar[k] = deepcopy(X1) Xvar[k+1] = deepcopy(Y1) k = k + 2 Zvar = zip(*Xvar) Indexs = [] for index in range(len(Zvar)): # test du contraintes if ( any(map(lambda x:True if (x < 0 or x >1) else False ,Zvar[index])) == True ): Indexs.append(Zvar[index]) for index in range(len(Indexs)): Zvar.remove(Indexs[index]) return Zvar
def Normal(): from copulalib.copulalib import Copula deviation = 5 N_points = 10000 N_Features = 200 size_init_data = 100 Data_array_Ref = np.zeros((N_points, N_Features)) Data_array_Test = np.zeros((N_points, N_Features)) # Let us create the numpy array for i in range(0, N_Features): # Generate random (normal distributed) numbers:: x = np.random.normal(size=size_init_data) y = (x) + np.random.normal(size=size_init_data) # Make the instance of Copula class with x, y and clayton family:: foo = Copula(x, y, family='clayton') X1, Y1 = foo.generate_xy(N_points) #print X1.shape #print Y1.shape print "The iteration going on is", i X2 = X1 + deviation Y2 = Y1 + deviation # plot_copula(X1,Y1,X2,Y2,'Copula_parabola_T') Data_array_Ref[:, i] = X1.copy() Data_array_Ref[:, i] = Y1.copy() Data_array_Test[:, i] = X2.copy() Data_array_Test[:, i] = Y2.copy() i = i + 1 np.savetxt("foo_Ref.csv", Data_array_Ref, delimiter=",") np.savetxt("foo_Test.csv", Data_array_Test, delimiter=",")
def Make_X_UsingCopula2(self): indexes = [self.Constrained.index(b) for b in self.ListNonDominated ] self.ListNonDominatedP = self.Load_PModel(indexes) Xvar = [] xx = rd.sample( self.ListNonDominatedP , len(self.ListNonDominatedP)/2) # demi de meilleur sol and random yy = [item for item in self.ListNonDominatedP if item not in xx] if len(xx)>len(yy): xx.pop() # test egality if len(yy)>len(xx): yy.pop() for j in range(m): xm = [] ym = [] for i in range(len(yy)): xm.append(deepcopy(xx[i][j])) ym.append(deepcopy(yy[i][j])) x = np.array(xm) y = np.array(ym) foo = Copula(x, y, family='frank') # gumbel, clayton, frank self.C[j] = deepcopy(foo) XX, YY = foo.generate_xy(150) X1 = XX.tolist() Y1 = YY.tolist() Xvar.append(X1+Y1) Zvar = zip(*Xvar) Zvar = Zvar + self.ListNonDominatedP self.P = Zvar new = self.isContrainte(self.Lecture(Zvar)) # self.P = self.P + self.ListNonDominatedP return new
def Test_RBF(): deviation = 40 N_points = 1000 N_Features = 2 size_init_data = 100 # Declare the data arrays Data_array_Ref = np.zeros((N_points, N_Features)) Data_array_Test = np.zeros((N_points, N_Features)) # Generate random (normal distributed) numbers:: x = np.random.normal(size=size_init_data) y = np.power(x, 1) + np.exp(x) + np.random.normal(size=size_init_data) # Make the instance of Copula class with x, y and clayton family:: foo = Copula(x, y, family='clayton') X1, Y1 = foo.generate_xy(N_points) # Plot the Data_array_Ref[:, 0] = X1.copy() Data_array_Ref[:, 1] = Y1.copy() Data_array_Test[:, 0] = X1.copy() + deviation Data_array_Test[:, 1] = Y1.copy() + deviation # plot the copula # plot_copula(Data_array_Ref[:,0], Data_array_Ref[:,1], Data_array_Test[:,0], Data_array_Test[:,1], 'Copula_parabola_T'); start_time = time.time() for gam in [0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 0.5, 1, 10]: MD_test = np.zeros((N_points, 2)) MD_normal = np.zeros((N_points, 2)) alphas, lambdas = stepwise_kpca(Data_array_Ref, gamma=gam, n_components=2) for i in range(0, N_points): MD_normal[i][:] = project_x(Data_array_Ref[i][:], Data_array_Ref, gamma=gam, alphas=alphas, lambdas=lambdas) for i in range(0, N_points): MD_test[i][:] = project_x(Data_array_Test[i][:], Data_array_Ref, gamma=gam, alphas=alphas, lambdas=lambdas) np.savetxt("Trans_normal" + str(gam) + ".csv", MD_normal, delimiter=",") np.savetxt("Trans_Test" + str(gam) + ".csv", MD_test, delimiter=",") print("--- %s Seconds ---" % (time.time() - start_time)) print("--- %s Gamma ---" % gam)
def Make_X_UsingCopula2(self): Xvar = [] # Varrtmp = self.Choice() varr = list(zip(*self.ListNonDominated)) xx = rd.sample(self.ListNonDominated, len(self.ListNonDominated) / 2) # demi de meilleur sol and random yy = [item for item in self.ListNonDominated if item not in xx] if len(xx) > len(yy): xx.pop() # test egality if len(yy) > len(xx): yy.pop() for j in range(m): xm = [] ym = [] for i in range( len(yy) ): # youo are here rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr xm.append(deepcopy(xx[i][j])) ym.append(deepcopy(yy[i][j])) x = np.array(xm) y = np.array(ym) foo = Copula(x, y, family='frank') # gumbel, clayton, frank self.C[j] = deepcopy(foo) XX, YY = foo.generate_xy(200) X1 = XX.tolist() Y1 = YY.tolist() Xvar.append(X1 + Y1) Zvar = zip(*Xvar) Indexs = [] for index in range(len(Zvar)): # test du contraintes # if ( any(map(lambda x:True if (x < -5 or x >5) else False ,Zvar[index][1:])) == True ) or (Zvar[index][0]<0 or Zvar[index][0] > 1): # pour f4 if (any( map(lambda x: True if (x < 0 or x > 1) else False, Zvar[index])) == True): Indexs.append(Zvar[index]) for index in range(len(Indexs)): Zvar.remove(Indexs[index]) return Zvar
def copulalib_test(): x = numpy.random.normal(size=100) y = 2.5 * x + numpy.random.normal(size=100) #Make the instance of Copula class with x, y and clayton family:: print "copulalib test >>>", Copula(x, y, family='clayton')
def Weibull(): N_points = 100 N_Features = 10 size_init_data = 100 Data_array_Ref = np.zeros((N_points, N_Features)) Data_array_Test = np.zeros((N_points, N_Features)) # Let us create the numpy array for i in range(0, N_Features): # Generate random (normal distributed) numbers:: x = np.random.weibull(10, size=size_init_data) y = np.power(x, 2) + np.random.weibull(10, size=size_init_data) # Make the instance of Copula class with x, y and clayton family:: foo = Copula(x, y, family='frank') X1, Y1 = foo.generate_xy(N_points) #print X1.shape X2 = X1 Y2 = Y1 #plot_copula(X1,Y1,X2,Y2,'Copula_parabola_T') Data_array_Ref[:, i] = X1.copy() Data_array_Ref[:, i] = Y1.copy() Data_array_Test[:, i] = X2.copy() Data_array_Test[:, i] = Y2.copy() i = i + 1 np.savetxt("foo_Ref.csv", Data_array_Ref, delimiter=",") np.savetxt("foo_Test.csv", Data_array_Test, delimiter=",") ########################################################### from numpy import genfromtxt dev = np.random.rand(100, 1) * 1000 dev = np.sort(dev, axis=None) Data_array_Ref = genfromtxt('foo_Ref.csv', delimiter=',') Data_array_Test = genfromtxt('foo_Test.csv', delimiter=',') Data_array_Test = Data_array_Test + 40 # Calculate Distance between Class 2 and Class 1 row_ref = Data_array_Ref.shape[0] column_ref = Data_array_Ref.shape[1] row_test = Data_array_Test.shape[0] column_test = Data_array_Test.shape[1] for gam in [0.000001, 0.00001, 0.0001, 0.1]: Res = [] for element in dev: # Declare the variables print("--- %s True Distance ---" % (element)) Data_array_Ref = genfromtxt('foo_Ref.csv', delimiter=',') Data_array_Test = genfromtxt('foo_Test.csv', delimiter=',') Data_array_Test = Data_array_Test + element mean_normal = [] std_normal = [] # Declare the Matrices Normalized_matrix_test_Data_class_1 = np.zeros( (row_ref, column_ref)) Normalized_matrix_test_Data_class_2 = np.zeros( (row_test, column_test)) # First Calculate the mean and the standard deviation\ for i in range(0, column_ref): a = np.array(Data_array_Ref[i]) mean_normal.append(a.mean()) std_normal.append(a.std()) #Calcualte Normalized matrix for j in range(0, column_ref): for i in range(0, row_ref): Normalized_matrix_test_Data_class_1[i, j] = ( Data_array_Ref[i, j] - mean_normal[j]) / std_normal[j] for j in range(0, column_test): for i in range(0, row_test): Normalized_matrix_test_Data_class_2[i, j] = ( Data_array_Test[i, j] - mean_normal[j]) / std_normal[j] numberFlag = 1 CurrentFile = [] start_time = time.time() MD, CurrentFile = Initialization_kernel( Normalized_matrix_test_Data_class_1, Normalized_matrix_test_Data_class_2, 'Simulated_Data', 1, 2, numberFlag, CurrentFile, gam) print("--- %s seconds ---" % (time.time() - start_time)) Res.append(MD.mean()) print "The mean of Class 2 from Ref is", MD.mean() print "The resulting distances are", Res print("--- %s seconds ---" % (time.time() - start_time)) colors = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c"] Data_plot = np.zeros((len(Res), 2)) Data_plot[:, 0] = np.array(Res).copy() Data_plot[:, 1] = np.array(dev).copy() np.savetxt("foo_data_plot_Weibull" + str(gam) + ".csv", Data_plot, delimiter=",") plot_copula_time(Res, dev, str(gam), 'Weibull1' + str(gam), colors[0])
def GammachangeNormal(): deviation = [30] N_points = 10000 N_Features = 100 size_init_data = 1000 Data_array_Ref = np.zeros((N_points, N_Features)) Data_array_Test = np.zeros((N_points, N_Features)) # Let us create the numpy array for i in range(0, N_Features): # Generate random (normal distributed) numbers:: x = np.random.normal(size=size_init_data) y = x + np.random.normal(size=size_init_data) # Make the instance of Copula class with x, y and clayton family:: foo = Copula(x, y, family='clayton') X1, Y1 = foo.generate_xy(N_points) #print X1.shape #print Y1.shape print "The iteration going on is", i X2 = X1 Y2 = Y1 #plot_copula(X1,Y1,X2,Y2,'Copula_parabola_T') Data_array_Ref[:, i] = X1.copy() Data_array_Ref[:, i] = Y1.copy() Data_array_Test[:, i] = X2.copy() Data_array_Test[:, i] = Y2.copy() i = i + 1 np.savetxt("foo_Ref.csv", Data_array_Ref, delimiter=",") np.savetxt("foo_Test.csv", Data_array_Test, delimiter=",") from numpy import genfromtxt #dev= np.random.rand(100,1)*1000; #dev=np.sort(dev, axis=None); Data_array_Ref = genfromtxt('foo_Ref.csv', delimiter=',') Data_array_Test = genfromtxt('foo_Test.csv', delimiter=',') Data_array_Test = Data_array_Test + 40 # Calculate Distance between Class 2 and Class 1: row_ref = Data_array_Ref.shape[0] column_ref = Data_array_Ref.shape[1] row_test = Data_array_Test.shape[0] column_test = Data_array_Test.shape[1] MD = traditional_MTS(Data_array_Ref, Data_array_Test) a1 = (np.random.rand(50, 1) / 10000).tolist( ) # [0.000001,0.000002,0.000003,0.000005,0.000009,0.00001,0.00002,0.00004,0.00006,0.00008,0.00009,0.0001,0.0002,0.0003,0.0004,0.0006,0.0008,0.001,0.002,0.003,0.004]; a2 = (np.random.rand(50, 1) / 1000).tolist() a3 = (np.random.rand(50, 1) / 100).tolist() gamma_array = np.asarray(a1 + a2 + a3) # Declare the variables Data_array_Ref = genfromtxt('foo_Ref.csv', delimiter=',') Data_array_Test = genfromtxt('foo_Test.csv', delimiter=',') Data_array_Test = Data_array_Test + 30 mean_normal = [] std_normal = [] # Declare the Matrices Normalized_matrix_test_Data_class_1 = np.zeros((row_ref, column_ref)) Normalized_matrix_test_Data_class_2 = np.zeros((row_test, column_test)) # First Calculate the mean and the standard deviation\ for i in range(0, column_ref): a = np.array(Data_array_Ref[i]) mean_normal.append(a.mean()) std_normal.append(a.std()) #Calcualte Normalized matrix for j in range(0, column_ref): for i in range(0, row_ref): Normalized_matrix_test_Data_class_1[ i, j] = (Data_array_Ref[i, j] - mean_normal[j]) / std_normal[j] for j in range(0, column_test): for i in range(0, row_test): Normalized_matrix_test_Data_class_2[ i, j] = (Data_array_Test[i, j] - mean_normal[j]) / std_normal[j] Res = [] numberFlag = 1 P = np.sort(gamma_array, axis=None) for gam in P: CurrentFile = [] start_time = time.time() MD, CurrentFile = Initialization_kernel( Normalized_matrix_test_Data_class_1, Normalized_matrix_test_Data_class_2, 'Simulated_Data', 1, 2, numberFlag, CurrentFile, gam) print("--- %s seconds ---" % (time.time() - start_time)) Res.append(MD.mean()) print "The mean of Class 2 from Ref is", MD.mean() colors = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c"] Data_plot = np.zeros((len(Res), P)) Data_plot[:, 0] = np.array(Res).copy() Data_plot[:, 1] = np.array(dev).copy() np.savetxt("foo_data_plot_Normal_gamma_change" + str(gam) + ".csv", Data_plot, delimiter=",") plot_copula_time(Res, P, 'NGDM-HDR Vs Gamma', 'GammaNorm_1' + str(gam), colors[3])