8 Replies - 29343 Views - Last Post: 17 December 2013 - 12:29 AM Rate Topic: -----

#1 nirath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

ValueError: setting an array element with a sequence (Python)

Posted 16 December 2013 - 08:57 PM

I want to plot a CCDF graph in Python by reading data from a text file.
Anyway, I got an error "ValueError: setting an array element with a sequence".

Can anyone help me?

Here is my code in python:

import matplotlib.pyplot as plt
import numpy as np

read = np.loadtxt('data.txt')
with open('data.txt') as f:
    a = [map(float, line.split()) for line in f]

sorted=np.sort(a)

x2 = []
y2 = []
y = 0
for x in sorted: 
    x2.extend([x,x])
    print x2
    y2.append(y)
    y += 1.0 / len(a)
    y2.append(y)

plt.plot(x2,y2)
plt.show()

And here is my text file:
9.500000000
2.500000000
3.500000000
4.500000000
6.500000000
1.500000000


Is This A Good Question/Topic? 0
  • +

Replies To: ValueError: setting an array element with a sequence (Python)

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,839
  • Joined: 19-March 11

Re: ValueError: setting an array element with a sequence (Python)

Posted 16 December 2013 - 09:19 PM

First thing to note - sorted() is the name of a built-in function. Probably don't want to clobber that name with your variable.

That's not your problem though. Your problem is here:

a = [map(float, line.split()) for line in f]



line.split() produces a list, so this is giving you a list of lists.

>>> with open ('data.txt') as f:
...     a = [map(float, line.split()) for line in f]
... 
>>> a
[[9.5], [2.5], [3.5], [4.5], [6.5], [1.5], []]



try line.strip() instead.
Was This Post Helpful? 0
  • +
  • -

#3 nirath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

Re: ValueError: setting an array element with a sequence (Python)

Posted 16 December 2013 - 09:35 PM

Hi Kiparsky. I had tried as you told me by using line.strip() instead. Anyway, I got this error "ValueError: could not convert string to float: ."

What should I do?
Was This Post Helpful? 0
  • +
  • -

#4 nirath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

Re: ValueError: setting an array element with a sequence (Python)

Posted 16 December 2013 - 09:41 PM

Hi Kiparsky. I had tried as you told me by using line.strip() instead. Anyway, I got this error "ValueError: could not convert string to float: ."

What should I do?
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,839
  • Joined: 19-March 11

Re: ValueError: setting an array element with a sequence (Python)

Posted 16 December 2013 - 10:00 PM

No need for the map

>>> with open('data.txt') as f:
... a = [float(line.strip()) for line in f]
...
>>> a
[9.5, 2.5, 3.5, 4.5, 6.5, 1.5]
>>>

View Postnirath, on 16 December 2013 - 11:41 PM, said:

Hi Kiparsky. I had tried as you told me by using line.strip() instead. Anyway, I got this error "ValueError: could not convert string to float: ."

What should I do?


This might also indicate a trailing blank line. "" doesn't represent a float, so it would give an error. Easiest fix in that case is to clean up your data file.
Was This Post Helpful? 0
  • +
  • -

#6 nirath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

Re: ValueError: setting an array element with a sequence (Python)

Posted 16 December 2013 - 10:08 PM

Thanks Jon. I had deleted "map". But I still got the same error "ValueError: could not convert string to float:".
What do you mean by "clean up my data file"? I don't understand it since I am new to Python.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11679
  • View blog
  • Posts: 19,839
  • Joined: 19-March 11

Re: ValueError: setting an array element with a sequence (Python)

Posted 16 December 2013 - 10:15 PM

Check if there's a blank line at the end of the data file. If so, just delete it.
Was This Post Helpful? 0
  • +
  • -

#8 nirath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

Re: ValueError: setting an array element with a sequence (Python)

Posted 16 December 2013 - 10:20 PM

Thanks again Jon. I did as you told me, but this time I got this error "TypeError: float() argument must be a string or a number". what should i do next?
Was This Post Helpful? 0
  • +
  • -

#9 nirath   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 13

Re: ValueError: setting an array element with a sequence (Python)

Posted 17 December 2013 - 12:29 AM

Hi Jon. Now i had solved this error problem after sitting in front of pc for 2 hours.
Really thanks for your answers. But the graph I got is CDF graph, not CCDF.
As my code: plot(x2,y2)
The value of y2 here is CDF
So what should I do if I want to get a CCDF graph?

Here is my code. Is it correct?

x2 = []
y2 = []
y = 0
ccdf = 0
for x in sorted: 
    x2.extend([x,x])
    y2.append(y)
    y += 1.0 / len(a)
    y2.append(y)
    ccdf = 1-y2
plt.plot(x2,ccdf)


After running this code, I got this error:
TypeError: unsupported operand type(s) for -: 'int' and 'list'

Any idea please...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1