当我试图预测pri时,我得到了不准确的预测,NaN和无穷大的值

2022-12-01 04:51:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近开始学习Tensorflow,并设法编写了一些简单的代码来预测房价。我是机器学习新手,所以我还需要学很多东西,所以我需要你的帮助。这个程序的预测是不准确的,而且当我试图用最小均方误差的损失来代替交叉熵时,我得到了NaN和无穷大的值。你能告诉我哪里出错了吗?你知道吗

这是我的密码:

import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error

LEARNING_RATE = 0.003

home_input = pd.read_csv("kc_house_data.csv")

features = ["bedrooms", "bathrooms", "sqft_living", "sqft_lot", "floors", "waterfront", "view", "condition", "grade", "sqft_above", "sqft_basement", "yr_built", "yr_renovated", "zipcode", "lat", "long", "sqft_living15", "sqft_lot15"]
label = ["price"]

X = tf.placeholder(tf.float32, [None, 18])
Y = tf.placeholder(tf.float32, [None, 1])

W = tf.Variable(tf.ones([18, 1]))
b = tf.Variable(1.)

Y_ = tf.matmul(X, W) + b

cross_entropy = -tf.reduce_sum(Y*tf.log(Y_))

loss = tf.reduce_sum(tf.square(Y_ - Y))

optimizer = tf.train.GradientDescentOptimizer(LEARNING_RATE)
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    j = 1
    for i in range(1000):
        j = i * 10
        x_data = np.array(home_input[features][j:(j+10)])
        y_data = np.array(home_input[label][j:(j+10)])
        sess.run(train, feed_dict={X: x_data, Y: y_data})
        print(sess.run(Y_, feed_dict={X: x_data, Y: y_data}))

Tags: csvrunimporthomeinputdataratetfasnptrainpdlearningsesssqft
1条回答
网友
1楼 · 发布于 2022-12-01 04:51:31

将数据标准化,然后将其传递到网络将解决问题。为此,StandardScalerMinMaxScaler可能会对您有所帮助。你知道吗

from sklearn.preprocessing import StandardScaler
data = [[0, 0], [0, 0], [1, 1], [1, 1]]
scaler = StandardScaler()
new_data = scaler.fit_transform(data)
...
# feed new_data to the neural network