def test_comp(self): # my_funcs = {"Abs": my_abs, "exp": my_exp, "log": my_log, 'cos': my_cos, 'sin': my_sin, # 'sqrt': my_sqrt, "Flat": my_flat, "Comp": my_comp, "Diff": my_diff, # "Quot": my_quot, "Self": my_self} a, b, c, dl, dn, dl2, dn2 = self.a, self.b, self.c, self.dl, self.dn, self.dl2, self.dn2 c5 = a.copy() c5 = Dim(np.array([c5] * 5)) my_funcs = self.dim_map func = my_funcs["MMul"] self.assertEqual(a, func(a)) self.assertEqual(b, func(b)) self.assertEqual(dl, func(dl)) assert func(dn).anyisnan() assert func(dn2).anyisnan() self.assertEqual(dl2**2, func(dl2)) self.assertEqual(Dim(2 * np.array(c)), func(c)) self.assertEqual(Dim(5 * np.array(c)), func(c5)) self.assertNotEqual(dl, func(3)) self.assertEqual(1, func(a).ndim) self.assertEqual(1, func(b).ndim) self.assertEqual(1, func(dl).ndim) self.assertEqual(1, func(dn).ndim) self.assertEqual(1, func(c).ndim) self.assertEqual(1, func(c5).ndim)
def test_mul(self): a, b, c, dl, dn = self.a, self.b, self.c, self.dl, self.dn self.assertEqual(a, a * 1) self.assertEqual(a, 1 * a) self.assertEqual(Dim(np.array(a) * 2), a * a) self.assertEqual(Dim(np.array([3, 4, 6, 8, 10, 12, 14])), a * b) self.assertNotEqual(Dim(np.array([2, 4, 6, 8, 10, 12, 14])), a * b) self.assertEqual(a, a * dl) self.assertEqual(a, dl * a) assert (a * dn).anyisnan() assert (dn * a).anyisnan() self.assertEqual(2, (1 * c).ndim) self.assertEqual(2, (c * 1).ndim) self.assertEqual(1, (1 * a).ndim) self.assertEqual(1, (a * 1).ndim) self.assertEqual(2, (a * c).ndim) self.assertEqual(1, (a * b).ndim) self.assertEqual(2, (c * dl).ndim) self.assertEqual(2, (c * b).ndim) self.assertEqual(2, (c * dn).ndim) self.assertEqual(2, (dn * c).ndim) self.assertEqual(1, (dn * dn).ndim) self.assertEqual(1, (dn * dl).ndim) self.assertEqual(1, (b * dl).ndim)
def test_dim_func(): a = 1 b = Dim(np.array([1, 2, 3, 4, 5])) c = Dim(np.array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])) d = Dim(np.array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])) e = Dim(np.array([[1, 2, 3, 4, 5] * self_grpup])) dim_func = funcs["dim_func"] dim_func(a) dim_func(b) dim_func(c) s = dim_func(d) s = dim_func(e)
def setUp(self): self.a = Dim([1, 2, 3, 4, 5, 6, 7]) self.b = Dim([2, 2, 3, 4, 5, 6, 7]) self.dl = dless self.dn = dnan self.c = Dim([[1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7]]) self.dl2 = Dim([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) self.dn2 = Dim( [[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]]) self.dim_map = dim_map()
def dimf(dim): if isinstance(dim, Dim): if dim.ndim == 1: return dim elif dim.shape[0] == 2: if keep: return Dim(operation(*dim)) else: return Dim(operation(*dim)) else: if is_jump: return dim else: return Dim(operation(*dim)) else: # number return dim
def test_pow(self): a, b, c, dl, dn = self.a, self.b, self.c, self.dl, self.dn self.assertEqual(Dim(np.array(a) * 2), a**2) self.assertEqual(Dim(np.array(a) / 2), a**0.5) assert (4**a).anyisnan() assert (a**b).anyisnan() self.assertEqual(1, (1**c).ndim) self.assertEqual(2, (c**1).ndim) self.assertEqual(1, (1**a).ndim) self.assertEqual(1, (a**1).ndim) self.assertEqual(1, (a**c).ndim) self.assertEqual(1, (a**b).ndim) self.assertEqual(1, (c**dl).ndim) self.assertEqual(1, (c**b).ndim) self.assertEqual(1, (c**dn).ndim) self.assertEqual(1, (dn**c).ndim) self.assertEqual(1, (dn**dn).ndim) self.assertEqual(1, (dn**dl).ndim) self.assertEqual(1, (b**dl).ndim)
def test_all(self): a, b, c, dl, dn, dl2 = self.a, self.b, self.c, self.dl, self.dn, self.dl2 c5 = a.copy() c5 = Dim(np.array([c5] * 5)) my_funcs = self.dim_map MMul = my_funcs["MMul"] MDiv = my_funcs["MDiv"] ln = my_funcs["ln"] assert ln(MDiv(dl2 + MMul(c))).anyisnan() assert MDiv(dl2 + MMul(c)).anyisnan() assert (dl2 + MMul(c)).anyisnan()
def test_div(self): a, b, c, dl, dn = self.a, self.b, self.c, self.dl, self.dn self.assertEqual(a, a / 1) self.assertEqual(Dim(-np.array(a)), 1 / a) self.assertEqual(dless, a / a) self.a = Dim([1, 2, 3, 4, 5, 6, 7]) self.b = Dim([2, 2, 3, 4, 5, 6, 7]) self.assertEqual(Dim(np.array([-1, 0, 0, 0, 0, 0, 0])), a / b) self.assertNotEqual(Dim(np.array([2, 4, 6, 8, 10, 12, 14])), a / b) self.assertEqual(a, a / dl) self.assertEqual(Dim(-np.array(a)), dl / a) assert (a / dn).anyisnan() assert (dn / a).anyisnan() assert (dn / dn).anyisnan() self.assertEqual(2, (1 / c).ndim) self.assertEqual(2, (c / 1).ndim) self.assertEqual(1, (1 / a).ndim) self.assertEqual(1, (a / 1).ndim) self.assertEqual(2, (a / c).ndim) self.assertEqual(1, (a / b).ndim) self.assertEqual(2, (c / dl).ndim) self.assertEqual(2, (c / b).ndim) self.assertEqual(2, (c / dn).ndim) self.assertEqual(2, (dn / c).ndim) self.assertEqual(1, (dn / dn).ndim) self.assertEqual(1, (dn / dl).ndim) self.assertEqual(1, (b / dl).ndim)
from bgp.functions.dimfunc import dless, Dim if __name__ == "__main__": # data data = load_boston() x = data["data"] y = data["target"] c = [6, 3, 4] # unit from sympy.physics.units import kg x_u = [kg] * 13 y_u = kg c_u = [dless, dless, dless] x, x_dim = Dim.convert_x(x, x_u, target_units=None, unit_system="SI") y, y_dim = Dim.convert_xi(y, y_u) c, c_dim = Dim.convert_x(c, c_u) t = time.time() # symbolset pset0 = SymbolSet() pset0.add_features(x, y, x_dim=x_dim, y_dim=y_dim, x_group=[[1, 2], [3, 4, 5]]) pset0.add_constants(c, c_dim=c_dim, c_prob=None) pset0.add_operations(power_categories=(2, 3, 0.5), categories=("Add", "Mul", "Sub", "Div", "exp"),
}, add_coef=True, inter_add=True, out_add=True, cal_dim=True, vector_add=True, personal_map=False) sl.fit() score = sl.score(x, y, "r2") print(i, sl.expr) y_pre = sl.predict(x) # break y_pre = si_transformer.scale_y * y_pre ssc = Dim.inverse_convert(y_dim, target_units=eV)[0] y_pre = y_pre * ssc p = BasePlot(font=None) p.scatter(Y, y_pre, strx='Experimental $E_{gap}$', stry='Calculated $E_{gap}$') import matplotlib.pyplot as plt plt.show() from sklearn.linear_model import LinearRegression lin = LinearRegression()
from bgp.skflow import SymbolLearning if __name__ == "__main__": # 数据 data = pd.read_csv("204-6.csv") data_np = data.values x = data_np[:, 1:] y = data_np[:, 0] # 量纲 from sympy.physics.units import kg, m, pa, J, mol, K from bgp.functions.dimfunc import Dim, dless # 由单位获得缩放因子和量纲 gpa_dim = Dim.convert_to_Dim(1e9 * pa, unit_system="SI") j_d_mol_dim = Dim.convert_to_Dim(1000 * J / mol, unit_system="SI") K_dim = Dim.convert_to_Dim(K, unit_system="SI") kg_d_m3_dim = Dim.convert_to_Dim(kg / m**3, unit_system="SI") # 忽视缩放因子 y_dim = dless x_dim = [ dless, gpa_dim[1], j_d_mol_dim[1], K_dim[1], dless, kg_d_m3_dim[1] ] # 符号集合 pset0 = SymbolSet() pset0.add_features(x, y, x_dim=x_dim, y_dim=y_dim) pset0.add_operations( power_categories=(2, 3, 0.5),
# r = np.corrcoef(np.vstack((pre_y, y)))[1, 0] # # error = np.mean(np.abs((y - pre_y) / y)) # # r2 = sl.score(x, y, "r2") # mae = sl.score(x, y, "neg_mean_absolute_error") # sl.loop.cpset.cv = 5 # r2_cv = sl.cv_result(refit=False) # print("r:{},error:{},r2:{},MAE:{},r2_cv:{}".format(r, error, r2, mae, r2_cv[0])) # # data = sl.loop.top_n(20, ascending=False) # st.end() # st.to_csv(data, file_new_name="top_n") if __name__ == "__main__": pa_factor, pa_dim = Dim.convert_to(10 * 6 * pa) ###########第一个########### """数据""" com_data = pd.read_csv(r'FCC.csv') x = com_data.iloc[:, :-1].values y = com_data.iloc[:, -1].values x, y = shuffle(x, y, random_state=0) st = Store("FCC_result_error_no_intercept") st.start() sl = SymbolLearning(loop=r'MultiMutateLoop', cal_dim=False, dim_type=pa_dim, pop=5000, gen=50, add_coef=True,