def func(a,*args): if a[1]<=0: return 10000 KRR=KernelRidgeRegression(type="laplace") KRR.set_var(sigma=a[0], lambd=a[1]) KRR.fit(X,Y, "error") out1=KRR.rmse KRR.predict(Xv,Yv) out2=KRR.rmse print(str(a[1])+", "+str(a[0])+", "+str(out1)+", "+str(out2)+"\n", flush=True) return out2
def func(a,*args): if a[1]<=0: return 10000 KRR=KernelRidgeRegression(type="laplace") KRR.set_var(sigma=a[0], lambd=a[1]) KRR.fit(X,Y, "error") out1=KRR.rmse KRR.predict(Xv,Yv) out2=KRR.rmse with open(folder+filename, "a") as myfile: myfile.write(str(size)+", "+str(out1)+", "+str(out2)+"\n") return out2
def func(a, *args): if a[3]<=0: return 10000 try: KRR=KernelRidgeRegression(type="poly") KRR.set_var(c1=a[0],c2=a[1],d=a[2], lambd=a[3]) KRR.fit(X,Y, "error") out1=KRR.rmse KRR.predict(Xv,Yv) out2=KRR.rmse print(str(a[3])+", "+str(a[0])+", "+str(a[1])+", "+str(a[2])+", "+str(out1)+", "+str(out2)+"\n", flush=True) except numpy.linalg.linalg.LinAlgError: out1=10**9 out2=10**9 return out2
out = 1 lam_list = [10**n for n in range(-3, 5)] c_list = [10**n for n in range(-3, 7)] x = [-10**n for n in range(-1, 7)] x.reverse() c_list = x + c_list if method == 'linear': out_matrix_lin = np.zeros((len(c_list), len(lam_list))) for c in range(len(c_list)): for l in range(len(lam_list)): print(f'c={c_list[c]}, lambda={lam_list[l]}', flush=True) KRR = KernelRidgeRegression(type="linear") KRR.set_var(c1=c_list[c], lambd=lam_list[l]) KRR.fit(X, Y, "error") out = KRR.rmse print(out, flush=True) out_matrix_lin[c, l] = out with open(folder + "out_matrix_lin_fixed_faulty_matrix", 'wb') as file: pickle.dump([lam_list, c_list, out_matrix_lin], file) lam_list = [10**n for n in range(-3, 3)] c1_list = [10**n for n in range(-3, 3)] c2_list = [10**n for n in range(-2, 3)]
path, featureMatrixFileValidate, atomicSymbolsListFileValidate) elif Feature == "GP": largeFeatureMatrixValidate = advanced_large_matrix( path, featureMatrixFileValidate, atomicSymbolsListFileValidate) with open(path + energiesFileValidate, "rb") as pickleFile: energiesValidate = pickle.load(pickleFile) largeFeatureMatrixValidate.shape = (largeFeatureMatrixValidate.shape[0], -1) Xv = largeFeatureMatrixValidate Yv = np.array(energiesValidate) if method == 'linear': folder = folder + "lin/" KRR = KernelRidgeRegression(type="linear") KRR.set_var(c1=Args, lambd=lambd) KRR.fit(X, Y, "error") out1 = KRR.rmse KRR.predict(Xv, Yv) out2 = KRR.rmse print("\nTrain: " + str(out1) + " Validation: " + str(out2) + "\n", flush=True) elif method == 'polynomial': try: KRR = KernelRidgeRegression(type="poly") KRR.set_var(c1=c1_list[c1], c2=c2_list[c2], d=d_list[d], lambd=lambd) KRR.fit(X, Y, "error") out1 = KRR.rmse KRR.predict(Xv, Yv) out2 = KRR.rmse
#%% Load validation data featureMatrixFileValidate = "validate_featureMatrix.npy" atomicSymbolsListFileValidate = "validate_pickledAtomicSymbolsList.txt" energiesFileValidate = "validate_pickledEnergies.txt" largeFeatureMatrixValidate, mappedAtomicNumberValidate = simpleLargeMatrix(path,featureMatrixFileValidate, atomicSymbolsListFileValidate) with open(path+energiesFileValidate, "rb") as pickleFile: energiesValidate = pickle.load(pickleFile) #Flatten description largeFeatureMatrixValidate.shape = (largeFeatureMatrixValidate.shape[0], -1) X_v = largeFeatureMatrixValidate Y_v = np.array(energiesValidate) import time start=time.time() KRR=KernelRidgeRegression(type="laplace") KRR.fit(X,Y) Y_predict_val=KRR.predict(X_v,Y_v) print(KRR.rmse) rmse_class=np.sqrt(np.mean(np.square(Y_predict_val-Y_v))) print(rmse_class) end=time.time() print(end-start)