TensorFlow多元回归预测房子信息

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np
import statsmodels.formula.api as smf
import tensorflow as tf
import matplotlib.pyplot as plt

house_data = pd.read_csv('F:\lcl\data.csv')
data = house_data.dropna()

#检验模型参数
# X = data.ix[:,0:5]
# Y = data.ix[:,6]
# fit = smf.OLS(Y,X).fit()
# print(fit.summary())

rng = np.random

learning_rate = 0.3
training_epochs = 1000

training_data = data.ix[0:20,0:6]
training_label = data.ix[0:20,6]
testing_data = data.ix[21:,0:6]
testing_label = data.ix[21:,6]

n_samples = training_data.shape[0]
X = tf.placeholder(tf.float32)
X2 = tf.placeholder(tf.float32)
X3 = tf.placeholder(tf.float32)
X4 = tf.placeholder(tf.float32)
X5 = tf.placeholder(tf.float32)
X6 = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

w = tf.Variable(rng.randn(),name="weights",dtype=tf.float32)
w2 = tf.Variable(rng.randn(),name="weights",dtype=tf.float32)
w3 = tf.Variable(rng.randn(),name="weights",dtype=tf.float32)
w4 = tf.Variable(rng.randn(),name="weights",dtype=tf.float32)
w5 = tf.Variable(rng.randn(),name="weights",dtype=tf.float32)
w6 = tf.Variable(rng.randn(),name="weights",dtype=tf.float32)
b = tf.Variable(rng.randn(),name="biases",dtype=tf.float32)

pred = tf.multiply(X,w)+tf.multiply(X2,w2)+tf.multiply(X3,w3)+tf.multiply(X4,w4)+tf.multiply(X5,w5)+tf.multiply(X6,w6)+b
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
#优化器
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()

with tf.Session() as sess:

    sess.run(init)

    for epoch in range(training_epochs):

        sess.run(optimizer,feed_dict={X: training_data['longitude'], X2: training_data['latitude'], \
                                      X3: training_data['price'], X4: training_data['buildingTypeId'], \
                                      X5: training_data['tradeTypeId'], X6: training_data['expectedDealPrice'], \
                                      Y: training_label})

    predict_y = sess.run(pred,feed_dict={X: testing_data['longitude'], X2: testing_data['latitude'], \
                                         X3: testing_data['price'], X4: testing_data['buildingTypeId'], \
                                         X5: testing_data['tradeTypeId'], X6: testing_data['expectedDealPrice']})

    print(predict_y)
    mse = tf.reduce_mean(tf.square((predict_y-testing_label)))
    print("MSE: %.4f" % sess.run(mse))

你可能感兴趣的:(Python语言)