Tensorflow: Simple Linear Regression using CSV data

2024/10/11 22:19:54

I am an extreme beginner at tensorflow, and i was tasked to do a simple linear regression using my csv data which contains 2 columns, Height & State of Charge(SoC), where both values are float. In CSV file, Height is the first col while SoC is the second col.

Using Height i'm suppose to predict SoC

I'm completely lost as to what i have to add in the "Fit all training data" portion of the code. I've looked at other linear regression models and their codes are mind boggling, such as this one:

with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):sess.run(training_step,feed_dict={X:train_x,Y:train_y})cost_history = np.append(cost_history,sess.run(cost,feed_dict={X: train_x,Y: train_y}))#calculate mean square error 
pred_y = sess.run(y_, feed_dict={X: test_x})
mse = tf.reduce_mean(tf.square(pred_y - test_y))
print("MSE: %.4f" % sess.run(mse)) #plot cost
plt.plot(range(len(cost_history)),cost_history)
plt.axis([0,training_epochs,0,np.max(cost_history)])
plt.show()fig, ax = plt.subplots()
ax.scatter(test_y, pred_y)
ax.plot([test_y.min(), test_y.max()], [test_y.min(), test_y.max()], 'k--', lw=3)
ax.set_xlabel('Measured')
ax.set_ylabel('Predicted')
plt.show()

I've just been able to get data from my CSV file without error using this guide:

TensorFlow: Reading and using data from CSV file

Full Code:

import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
rng = np.randomfrom numpy import genfromtxt
from sklearn.datasets import load_boston# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50
n_samples = 221X = tf.placeholder("float") # create symbolic variables
Y = tf.placeholder("float")filename_queue = tf.train.string_input_producer(["battdata.csv"],shuffle=False)reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(filename_queue)# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[1.], [1.]]
col1, col2= tf.decode_csv(value, record_defaults=record_defaults)
features = tf.stack([col1])# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")# Construct a linear model
pred = tf.add(tf.multiply(col1, W), b) # XW + b <- y = mx + b  where W is gradient, b is intercept# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-col2, 2))/(2*n_samples)# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)# Initializing the variables
init = tf.global_variables_initializer()with tf.Session() as sess:# Start populating the filename queue.coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(coord=coord)sess.run(init)# Fit all training datafor epoch in range(training_epochs):_, cost_value = sess.run([optimizer,cost])for (x, y) in zip(col2, col1):sess.run(optimizer, feed_dict={X: x, Y: y})#Display logs per epoch stepif (epoch+1) % display_step == 0:c = sess.run(cost, feed_dict={X: col2, Y:col1})print( "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \"W=", sess.run(W), "b=", sess.run(b))print("Optimization Finished!")training_cost = sess.run(cost, feed_dict={X: col2, Y: col1})print ("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')#Graphic displayplt.plot(train_X, train_Y, 'ro', label='Original data')plt.plot(train_X, sess.run(W) * col2 + sess.run(b), label='Fitted line')plt.legend()plt.show()coord.request_stop()coord.join(threads)

Error:

INFO:tensorflow:Error reported to Coordinator: , Attempted to use a closed Session.--------------------------------------------------------------------------- TypeError Traceback (most recent calllast) in ()8 for epoch in range(training_epochs):9 _, cost_value = sess.run([optimizer,cost])---> 10 for (x, y) in zip(*col1, col2):11 sess.run(optimizer, feed_dict={X: x, Y: y})12

C:\Users\Shiina\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.pyin iter(self)514 TypeError: when invoked.515 """--> 516 raise TypeError("'Tensor' object is not iterable.")517 518 def bool(self):

TypeError: 'Tensor' object is not iterable.

Answer

The error is because your are trying to iterate over tensors in for (x, y) in zip(col2, col1) which is not allowed. The other issues with the code is that you have input pipeline queues setup and then your also trying to feed in through feed_dict{}, which is wrong. Your training part should look something like this:

with tf.Session() as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
sess.run(init)# Fit all training data
for epoch in range(training_epochs):_, cost_value = sess.run([optimizer,cost])#Display logs per epoch stepif (epoch+1) % display_step == 0:c = sess.run(cost)print( "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \"W=", sess.run(W), "b=", sess.run(b))print("Optimization Finished!")training_cost = sess.run(cost)print ("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')#Plot data after completing training
train_X = []
train_Y = []
for i in range(input_size): #Your input data size to loop through onceX, Y = sess.run([col1, pred]) # Call pred, to get the prediction with the updated weightstrain_X.append(X)train_Y.append(y)#Graphic display
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.legend()
plt.show()coord.request_stop()
coord.join(threads)
https://en.xdnf.cn/q/118269.html

Related Q&A

How to resolve positional index error in python while solving a condition in python?

I have the following data and I am trying the following code: Name Sensex_index Start_Date End_Date AAA 0.5 20/08/2016 25/09/2016 AAA 0.8 26/08/2016 …

Google Calendar API: Insert multiple events (in Python)

I am using the Google Calendar API, and have successfully managed to insert a single event into an authorized primary calendar, but I would like to hard code multiple events that, when executed, would …

Remove special characters from column headers

I have a dictionary (data_final) of dataframes (health, education, economy,...). The dataframes contain data from one xlsx file. In one of the dataframes (economy), the column names have brackets and s…

Python Flask application getting OPTIONS instead of POST

I have a python Flask listener waiting on port 8080. I expect another process to make a series of POSTs to this port.The code for listener is as follows.#!/usr/bin/env python2 from __future__ import pr…

Raspberry pi:convert fisheye image to normal image using python

I have attached the USB webcam with raspberry pi to capture image and write code to send it using mail. It captures image using fswebcam commamnd so code for capture image in python script is :subproce…

modifying python daemon script, stop does not return OK (but does kill the process)

Following on from the previous post, the script now start and stops the python script (and only that particular script) correctly but does not report the OK back to the screen...USER="root" A…

fulfill an empty dataframe with common index values from another Daframe

I have a daframe with a series of period 1 month and frequency one second.The problem the time step between records is not always 1 second.time c1 c2 2013-01-01 00:00:01 5 3 2013-01-0…

How to mix numpy slices to list of indices?

I have a numpy.array, called grid, with shape:grid.shape = [N, M_1, M_2, ..., M_N]The values of N, M_1, M_2, ..., M_N are known only after initialization.For this example, lets say N=3 and M_1 = 20, M_…

Visualize strengths and weaknesses of a sample from pre-trained model

Lets say Im trying to predict an apartment price. So, I have a lot of labeled data, where on each apartment I have features that could affect the price like:city street floor year built socioeconomic s…

Scrapy get result in shell but not in script

one topic again ^^ Based on recommendations here, Ive implemented my bot the following and tested it all in shell :name_list = response.css("h2.label.title::text").extract()packaging_list = r…