Python 基于黄金价格数据库的Tensorflow回归

Python 基于黄金价格数据库的Tensorflow回归,python,tensorflow,machine-learning,regression,linear-regression,Python,Tensorflow,Machine Learning,Regression,Linear Regression,我正试图建立一个回归模型,从5月2日到10月2日的数据集预测黄金价格 首先,我将X作为112天的列表编号: x_data = np.arange(1,113,1) x_df = pd.DataFrame(data=x_data,columns = ['X']) 然后我使用来自 第二个是从5月2日到10月2日的黄金价格: 价格以美元为单位,单位为“特拉赫希尔” 将terakhir的价格转换为美元后,我使用 emas_df = pd.read_csv(r"C:\Users\Tila\Do

我正试图建立一个回归模型,从5月2日到10月2日的数据集预测黄金价格

首先,我将X作为112天的列表编号:

x_data = np.arange(1,113,1)
x_df = pd.DataFrame(data=x_data,columns = ['X'])
然后我使用来自 第二个是从5月2日到10月2日的黄金价格:
价格以美元为单位,单位为“特拉赫希尔”

将terakhir的价格转换为美元后,我使用

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和黄金价格“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',x='X',y='Terakhir')
plt.plot(x_data,y_hat,'r')
但我总是得到不正确的回归预测,从图中可以看出


关于如何使我的回归精确,有什么想法吗?

请不要将Python代码作为Javascript片段发布(编辑)!必须使用tensorflow吗?因为如果你只想做线性回归,那可能是一个更好的解决方案。
y_hat = x_data*model_m + model_b

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