Tensorflow Keras房价预估

Tensorflow Keras房价预估.

这是本人练手的一段代码,拿本地一小区的房价做的一个模型训练,数据较少,纯为示范。

import tensorflow as tf
import numpy as np
from tensorflow import keras

model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[2])])

# 优化器选 adam,损失函数选MSE
model.compile(optimizer='adam', loss='mean_squared_error')

# 房子面积和卧室个数的数据,面积以平方计
# [101,3], [85,2], [125,3], [51,1], [80,2]
# 房子价格的数据
# [81.7, 68.6, 100.9, 41.1, 64.6] ,基本合8000一平方

xs = np.array([[101,3], [85,2], [125,3], [51,1], [80,2]], dtype=float)
ys = np.array([81.7, 68.6, 100.9, 41.1, 64.6], dtype=float)

model.fit(xs, ys, epochs=500)

model.evaluate(xs, ys)

# [60,1]需要reshape下,不然会报错
test = np.array([60,1], dtype=float).reshape(1, -1)

print(model.predict(test))

最终预估60平方1个卧室的房价是48.3,基本符合实际。

Leave a Comment

豫ICP备19001387号-1