已知一个数,但这个数不是最对的,最对的可能在它附近,想要从附近随机生成一群数,放入模型中暴力查找最对的那个值。
以下是代码片段
n = 800 # 个数
m = 2 # 角度,只有2才是均匀的,1为半圆,以此类推,2的倍数与2相同
p1 = 0.2 # 幅度 x 轴半径
p2 = 0.3 # 幅度 y 轴半径
K = 0.2 # 圆心坐标 x
V = 15 # 圆心坐标 y
t = np.random.random(size=n) *m * np.pi -np.pi
x1 = np.cos(t)
x2 = np.sin(t)
for i in range(n):
len = np.sqrt(np.random.random())
x1[i] = (x1[i] * len) * p1 + K
x2[i] = (x2[i] * len) * p2 + V
plt.figure(figsize=(10,10))
plt.scatter(x1,x2,marker='.',color='r')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Random Scatter')
plt.show()
用这个功能找最优秀的kv
位于def_jili的seek_best2(self)
def seek_best2(self):
"""
kv 继续查找最优的kv
尝试以点为圆心继续找最优kv 效果不佳
"""
df1 = self.df1df2()[0]
df2 = self.df1df2()[1]
t1 = self.t1t2()[0]
t2 = self.t1t2()[1]
dfs = self.seek_best()
df_kv = dfs.head(1)
k_1 = df_kv.iloc[0, 2]
v_1 = df_kv.iloc[0, 3]
df_empty = pd.DataFrame(columns=["mean", "r2", "k", "v"])
n = 50 # 个数
m = 2 # 角度,只有2才是均匀的,1为半圆,以此类推,2的倍数与2相同
p1 = 0.2 # 幅度 x 轴半径
p2 = 0.2 # 幅度 y 轴半径
tw = np.random.random(size=n) * m * np.pi - np.pi
km = np.cos(tw) # 圆心坐标 x
vr = np.sin(tw) # 圆心坐标 y
number = 0
for i in range(n):
len = np.sqrt(np.random.random())
km[i] = (km[i] * len) * p1 + k_1
vr[i] = (vr[i] * len) * p2 + v_1
x = self.x
nf1 = cmore(df1[t1:t2], km[i], vr[i], x)
tm = timemove(t1, t2, vr[i], x)
t3 = tm[0]
t2 = pd.to_datetime(t2)
t3 = pd.to_datetime(t3)
t2 = t2.strftime("%Y-%m-%d %H:%M:%S")
t3 = t3.strftime("%Y-%m-%d %H:%M:%S")
nf2 = nf1.set_index(tm)
mean1 = mean_absolute_error(nf2[t3:t2], df2[t3:t2]) # mse
nf_2 = nf2[t3:t2]
df_2 = df2[t3:t2]
index_nf_2 = nf_2.index
df_22 = df_2.set_index(index_nf_2)
dfkk = pd.concat([nf_2, df_22], axis=1)
corr = dfkk.corr(method="spearman")
corr1 = corr.iloc[0, 1]
df_empty.loc[number] = [mean1, corr1, km[i], vr[i]]
number = number + 1
dfs = df_empty.sort_values(by=["mean"])
return dfs.head()