我正在尝试从 CSV 数据中获得非线性回归,该数据可在此链接中获得: CSV 数据
我想使用多项式回归。问题是我从 TensorFlow 得到的结果是“无”。我找不到问题。我认为模型或成本函数有问题。有人可以帮忙吗?任何帮助,将不胜感激。
# importing modules
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import csv
import time
# defining the method for gathering data
# date_idx is the column number of date in the .CSV file
def read(filename, date_idx, date_parse, year, bucket=7):
# the amount of days in the year : 365 days
days_in_year = 365
# defining a dictionary for the frequency
freq = {}
# here we are calculating hao many buckets each frequency have?
# buckets = (which is 7, and by that we mean each frequency is 7 days)
# we are initializing each frequency with zero
for period in range(0, int(days_in_year / bucket)):
freq[period] = 0
# this opens the file in binary mode('rb' : 'r' for read, 'b' is for binary mode)
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
next(csvreader) # this escapes the first row since it consists of headers only
for row in csvreader:
if row[date_idx] == '': # each row consists of many columns but if the date is
continue # is unavailable there is no need to check the data
t = time.strptime(row[date_idx], date_parse) # converting to the input format
if t.tm_year == year and t.tm_yday < (days_in_year-1): # we want the data in specific year
freq[int(t.tm_yday / bucket)] += 1 # finding the frequency
return freq
# here i call the method to gather data for me
freq = read(r'C:\My Files\Programming\Python\TensorFlow\CallCenter\311_Call_Center_Tracking_Data__Archived_.csv',
0, '%m/%d/%Y', 2014)
# here we convert our dictionary into 2 arrays or lists in python
x_temp =[]
y_temp =[]
for key, value in freq.items():
x_temp.append(key)
y_temp.append(value)
x_data = np.asarray(x_temp)
y_data = np.asarray(y_temp)
# visualizing the data
plt.scatter(x_data,y_data)
plt.show()
# splitting data with ratio into 2 group : training and test
def split_dataset(x_dataset, y_dataset, ratio):
arr = np.arange(x_dataset.size)
np.random.shuffle(arr)
num_train = int(ratio*x_dataset.size)
x_train = x_dataset[arr[0:num_train]]
y_train = y_dataset[arr[0:num_train]]
x_test = x_dataset[arr[num_train:x_dataset.size]]
y_test = y_dataset[arr[num_train:y_dataset.size]]
return x_train,y_train,x_test,y_test
x_train, y_train, x_test, y_test = split_dataset(x_data,y_data, ratio=0.7)
# here we create some place holder for input and output of the session
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# defining global variables
learning_rate = 0.01
training_epochs = 100
num_coeffs = 5
# adding regularization (for later use)
#reg_lambda = 0.
# defining the coefficients of the polynomial
w = tf.Variable([0.]*num_coeffs, name='parameter')
# defining the model
def model(X,w):
terms = []
for i in range(num_coeffs):
term = tf.multiply(w[i], tf.pow(X, i))
terms.append(term)
return tf.add_n(terms)
y_model = model(X,w)
# defining the cost function
cost = tf.reduce_sum(tf.pow(Y-y_model,2))
# defining training method
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# initilizing all variables
init = tf.global_variables_initializer()
#runing the model
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
training_cost = sess.run(train_op, feed_dict={X:x_train, Y:y_train})
print(training_cost)
final_cost = sess.run(cost,feed_dict={X: x_test, Y:y_test})
print('Final cost = {}'.format(training_cost))