8

Here I am jus extracting a csv file and reading the "TV"values, calculating average and printing using tensorflow. But I am getting "AttributError" list has no attribute 'size' ". Can anyone please help me? Thanks in advance.

 import tensorflow as tf
 import pandas
 csv = pandas.read_csv("Advertising.csv")["TV"]
 t = tf.constant(list(csv))
 r = tf.reduce_mean(t)
 sess = tf.Session()
 s = list(csv).size
 fill = tf.fill([s],r)
 f = sess.run(fill)
 print(f)
6
  • 3
    Try this: s = len(csv) instead of s = list(csv).size Commented Jul 14, 2017 at 8:36
  • 1
    Nope, lists don't have an attribute size. The normal way to get the length of a sized object is to use len(object) instead. Commented Jul 14, 2017 at 8:37
  • @Martijn Pieters. In that case it gives the result [147.04249573 147.04249573 147.04249573 147.04249573 147.04249573 147.04249573 147.04249573 147.04249573 147.04249573 147.04249573........................] Commented Jul 14, 2017 at 8:47
  • 1
    @Raghavi: then please do include sample data and expected outcomes. If you have a pandas Series object, then just use the .size attribute on that directly. Pandas and numpy translate most actions into actions on the contents, which is why they are an exception to the normal len() usage. Commented Jul 14, 2017 at 8:54
  • 3
    @Raghavi, pandas.read_csv("Advertising.csv")["TV"] returns a Pandas.Series object and len(Pandas.Series) returns its length. I can't imagine how can it return a list... Commented Jul 14, 2017 at 8:55

1 Answer 1

6

As summary of the discussion in the comments, here are valid ways of getting a length of the column in csv:

$ csv = pandas.read_csv("Advertising.csv")
$ print type(csv), len(csv)
<class 'pandas.core.frame.DataFrame'> 10

$ series = csv["TV"]
$ print type(series), len(series)
<class 'pandas.core.series.Series'> 10

$ as_list = list(series)
$ print type(as_list), len(as_list)
<type 'list'> 10

And here's how to calculate the average (without tensorflow session):

$ import numpy as np
$ print np.mean(series)
1.2
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.