微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用黄金价格数据库使用Tensorflow进行回归

如何解决使用黄金价格数据库使用Tensorflow进行回归

我正在尝试建立回归模型,以从5月2日至10月2日的数据集中预测黄金价格。

首先,我列出了112天后做了一个X:

x_data = np.arange(1,113,1)
x_df = pd.DataFrame(data=x_data,columns = ['X'])

然后我使用来自 第二个是5月2日至10月2日的黄金价格:https://drive.google.com/file/d/1tFDgfIJof03vA0VGiqgRsDBZsOdmec2g/view?usp=sharing
价格为“ terakhir”的美元。

terakhir的价格转换为USD之后,我使用

进行连接
emas_df = pd.read_csv(r"C:\Users\Tila\Documents\BOT\dataemas.csv")
for index in range(0,len(emas_df["terakhir"])):
    emas_df["terakhir"][index] = emas_df["terakhir"][index].replace(",","")
    
my_data = pd.concat([x_df,emas_df],axis=1)
my_data["terakhir"] = pd.to_numeric(my_data["terakhir"])
terakhir = my_data["terakhir"]
y_true = (terakhir.values).T

然后我使用X绘制了X和黄金价格“ terakhir”之间的散点图

my_data.sample(n=50).plot(kind='scatter',x='X',y='terakhir')

这是回归代码的开始,我使用代码

batch_size = 2
m = tf.Variable(0.45453853)
b = tf.Variable(0.4537617)
xph = tf.placeholder(tf.float32,[batch_size])
yph = tf.placeholder(tf.float32,[batch_size])
y_model = m*xph + b
error = tf.reduce_sum(tf.square(yph-y_model))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0001)
train = optimizer.minimize(error)
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    
    batches = 50
    for i in range(batches):
        
        rand_ind = np.random.randint(len(x_data),size=batch_size)
        
        Feed = {xph:x_data[rand_ind],yph:y_true[rand_ind]}
        
        sess.run(train,Feed_dict = Feed)
        
    model_m,model_b = sess.run([m,b])   

然后,我将回归与实际的分散数据一起绘制

y_hat = x_data*model_m + model_b

my_data.sample(n=50).plot(kind='scatter',y='terakhir')
plt.plot(x_data,y_hat,'r')

但是我总是得到不正确的回归预测,如图所示

failed prediction

关于如何使回归精确的想法?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。