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\data1.csv').astype(np.float32)

data = house_data.dropna()

learning_rate = 0.3
training_epochs = 1000

training_data = data.ix[0:20,0:6]
training_data = np.asarray(training_data).reshape(21,6)

training_label = data.ix[0:20,6]
training_label = np.asarray(training_label).reshape(21,1)

testing_data = data.ix[21:,0:6]
testing_data = np.asarray(testing_data).reshape(7,6)
testing_label = data.ix[21:,6]
testing_label = np.asarray(testing_label).reshape(7,1)

n_samples = training_data.shape[0]
X = tf.placeholder(tf.float32,[None,6])
Y = tf.placeholder(tf.float32,[None,1])

w = tf.Variable(tf.random_uniform((6,1),-1.0,1.0),name="weights",dtype=tf.float32)
b = tf.Variable(tf.random_uniform((1,1)),name="biases",dtype=tf.float32)


pred = tf.add(tf.matmul(X,w),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,Y: training_label})

    predict_y = sess.run(pred, feed_dict={X: testing_data})

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

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