What is wrong with this code?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1690 Views - Last Post: 19 November 2012 - 08:55 AM Rate Topic: -----

#1 Billyzitwark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 12

What is wrong with this code?

Posted 17 November 2012 - 05:51 PM

What is wrong with this code? I get an error saying need more than 1 value to unpack? Something dealing with my line.split?

  
from rpy import r

movieID = []
numberofratings = []
AverageRating = []

open_file = open("ratings.txt")
for line in open_file:
movieID, numberofratings, avgRatings = line.split("|")
movieID.append(movieID)
numberofratings.append(numberofratings)
AverageRating.append(avgRatings)

r.png("ratings.txt" , width=400, height=350);
r.plot(x=numberofratings, y=AverageRating, xlab="Number of Ratings",
ylab = "Average Rating", main = "Scatter Plot with Least Squares
Fit");

leastsquarefit = r.lsfit(numberofratings, AverageRating)

gradient = leastsquarefit['coefficients']['X'];
yintercept = leastsquarefit['coefficients']['Intercep…

r.abline(a=yintercept, b=gradient, col="red");
r.dev_off();


This post has been edited by Atli: 17 November 2012 - 06:01 PM
Reason for edit:: Fixed the [code] ... [/code] tags.


Is This A Good Question/Topic? 1
  • +

Replies To: What is wrong with this code?

#2 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 124
  • Joined: 08-May 11

Re: What is wrong with this code?

Posted 17 November 2012 - 06:09 PM

line.split is going to return one list of items. So basically the solution to your problem is that you need to set only one variable to equal line.split... and then use that variable (call it x for example) as a list so x[0] = movieid, x[1] = numberofratings, and x[2] = avgratings

hope this helped

edit: disregard what i said above. I was wrong, you can do it the way you're doing it, but you're also using two variables with the same name. Try changing that

This post has been edited by alexr1090: 17 November 2012 - 06:19 PM

Was This Post Helpful? 1
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,979
  • Joined: 08-June 10

Re: What is wrong with this code?

Posted 17 November 2012 - 06:11 PM

I don't know about that, but there are a couple of odd things about that code.

  • The indentation is wrong. Last time I used it, Python required proper indentation. (One of the things I liked best about the language.)

  • Line #22. What's going on with the end of that line?


Also, if you're going to use semi-colons, then use semi-colons, but be consistent about it. Don't use them for some lines and not for other lines. - I'd suggest not using them at all. Waste of space for Python code, really.
Was This Post Helpful? 1
  • +
  • -

#4 Billyzitwark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 12

Re: What is wrong with this code?

Posted 17 November 2012 - 06:16 PM

My problem was in line 9 specifically, the other code is correct I believe.
Was This Post Helpful? 0
  • +
  • -

#5 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: What is wrong with this code?

Posted 17 November 2012 - 06:53 PM

Post the data file. If any line, when split on pipes, contains more or less than three values, you get an error.

(too many values to unpack if len(line.split("|")) > 3, need more than 3 values to unpack if len(line.split("|")) < 3

a bad fix would be wrapping the content in a try: everything except: continue statement

It is probably blank lines in the data. Check for that before doing anything else (IE, at the top of the loop) like so:
if not line:
    continue




thanks for showing rpy! never knew that existed. probably more fun than matplotlib.

This post has been edited by Python_4_President: 17 November 2012 - 07:13 PM

Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,881
  • Joined: 19-March 11

Re: What is wrong with this code?

Posted 17 November 2012 - 08:12 PM

View PostAtli, on 17 November 2012 - 08:11 PM, said:

I don't know about that, but there are a couple of odd things about that code.

  • The indentation is wrong. Last time I used it, Python required proper indentation. (One of the things I liked best about the language.)

  • Line #22. What's going on with the end of that line?


Also, if you're going to use semi-colons, then use semi-colons, but be consistent about it. Don't use them for some lines and not for other lines. - I'd suggest not using them at all. Waste of space for Python code, really.



To add to the nitpicking, pick a variable name convention and stick with it.

	movieID = []
	numberofratings = []
	AverageRating = []


Three different caps conventions in three lines? WTF?

View PostPython_4_President, on 17 November 2012 - 08:53 PM, said:

If any line, when split on pipes, contains more or less than three values, you get an error.


I guarantee this is the problem.
Was This Post Helpful? 0
  • +
  • -

#7 Billyzitwark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 12

Re: What is wrong with this code?

Posted 17 November 2012 - 11:53 PM

See file attached, I made some changes but I still am getting an error message with r.plot now..

View PostBillyzitwark, on 17 November 2012 - 11:51 PM, said:

See file attached, I made some changes but I still am getting an error message with r.plot now..


 
#STEP 4
from rpy import r

movieID = []
rating = []
AverageRating = []

open_file = open("ratings.txt")
for line in open_file:
    movieid = line.split("|")
    ratings = line.split("|")
    avgRatings = line.split("|")
    movieID.append(movieid)
    rating.append(ratings)
    AverageRating.append(avgRatings)

print "Writing output graph to file: scatterplot.png"
print "To view it in Linux, you can use the File Browser and double-click."
print "For command-line viewing, use the gthumb image viewer as follows:"
print "% gthumb scatterplot.png"



Here is my problem:
r.png("ratings.txt" , width=400, height=350);
r.plot(x=rating, y=AverageRating, xlabel="Number of Ratings", ylabel = "Average Rating", main = "Scatter Plot with Least Squares Fit");

leastsquarefit = r.lsfit(rating, AverageRating)

gradient = leastsquarefit['coefficients']['X'];
yintercept = leastsquarefit['coefficients']['Intercept'];

r.abline(a=yintercept, b=gradient, col="red");
r.dev_off();





Was This Post Helpful? 0
  • +
  • -

#8 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: What is wrong with this code?

Posted 18 November 2012 - 01:16 AM

Looks like the documentation for R kinda sucks, but I'm going to go out on a limb here and say that the x argument probably doesn't expect a list of lists.


At the beginning of the program you say,

rating = []



that means that rating is a list. Rating has an append method, which you use to append another list (the one you get when you split the line)

Speaking of splitting lines, what the heck is going on?

You were on the right track originally, but now you're splitting the same line the same way three times and trying to treat it differently.


Let's get back to where we met up at. Here's your original code on anti-biotics and cough suppressant. Sadly, we could not save your latest code. It died of nonsense:
  
from rpy import r

movie_ids= []
ratings = []
average_ratings = []

open_file = open("ratings.txt")
for line in open_file:
    #the line below this comment will skip lines that contain nothing but whitespace characters.
    if not line: continue

    #the line below this will say, "Try to do what follows"
    try:
        movie_id, rating, average_rating = line.split("|")
        #the line below this will say, "IN the event we could not do that"
    except:
        #the line below this says, "Then skip the rest and continuing executing the loop from the top
        continue

    movie_ids.append(movie_id)
    ratings.append(rating)
    average_ratings.append(average_rating)


if movie_ids and ratings and average_ratings:
    r.png("ratings.txt" , width=400, height=350);
    r.plot(x=ratings, y=average_ratings, xlab="Number of Ratings",
    ylab = "Average Rating", main = "Scatter Plot with Least Squares Fit");

    leastsquarefit = r.lsfit(ratings, average)

    gradient = leastsquarefit['coefficients']['X'];
    yintercept = leastsquarefit['coefficients']['Intercept']
    r.abline(a=yintercept, b=gradient, col="red");
    r.dev_off();
else:
    print "Failed to get what we needed to plot the data."




This post has been edited by Python_4_President: 18 November 2012 - 01:23 AM

Was This Post Helpful? 1
  • +
  • -

#9 Billyzitwark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 12

Re: What is wrong with this code?

Posted 18 November 2012 - 09:46 AM

Thank you very much, however I seem to get an error still with r.plot. It says there is an invalid multibyte string...
Was This Post Helpful? 0
  • +
  • -

#10 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: What is wrong with this code?

Posted 18 November 2012 - 10:14 AM

I wish I could see the data you attached, but I think Jon stole it.

Can you attach it better this time?

In the mean time, try this one.

MOVIE1|10|4
MOVIE2|11|3
MOVIE3|10|6
MOVIE4|9|5
MOVIE5|6|7
MOVIE6|6|4
MOVIE7|7|5
MOVIE8|11|2


Was This Post Helpful? 0
  • +
  • -

#11 Billyzitwark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 12

Re: What is wrong with this code?

Posted 18 November 2012 - 10:23 AM

Here is the ratings and the code I have thus far:

from rpy import r

movie_ids = []
ratings = []
average_ratings = []

open_file = open("ratings.txt")
for line in open_file:
    if not line: continue
    try:
        movie_id, rating, average_rating = line.split("|")
    except:
        continue
    movie_ids.append(movie_id)
    ratings.append(rating)
    average_ratings.append(average_rating)


print "Writing output graph to file: scatterplot.png"
print "To view it in Linux, you can use the File Browser and double-click."
print "For command-line viewing, use the gthumb image viewer as follows:"
print "% gthumb scatterplot.png"

r.png("scatterplot.png" , width=400, height=350);
r.plot(x=ratings, y=average_ratings, xlab="Number of Ratings", ylab = "Average Rating",
        main = "Scatter Plot with Least Squares Fit");

leastsquarefit = r.lsfit(ratings, average_ratings)

gradient = leastsquarefit['coefficients']['X'];
yintercept = leastsquarefit['coefficients']['Intercept'];

r.abline(a=yintercept, b=gradient, col="red");
r.dev_off();






Also ignore the difference in file name, I do realize that would cause an error too.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#12 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: What is wrong with this code?

Posted 18 November 2012 - 10:28 AM

Wow! I was wrong!

It wasn't a blank line, it was '...'!

Further, my text editor put 4 of the last 5 files for the average rating as - nan, which is probably not a good sign.


Use this data instead (the "..." should be fine)
1|452|3.87832
2|131|3.20611
3|90|3.03333
...
1675|1|3
1676|1|2
1677|1|3
1678|1|1
1679|1|3
1680|1|2
1681|1|3
1682|0|0
1683|0|0
1684|1|3
1685|0|0
1686|0|0


Was This Post Helpful? 0
  • +
  • -

#13 Billyzitwark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 12

Re: What is wrong with this code?

Posted 18 November 2012 - 10:29 AM

Something is wrong with the leastsquarefit code, line 28. I fixed the other error.

I dont think the data set is wrong, instead I am not sure why it is not producing a least square regression line, but it is outputting a graph.
Was This Post Helpful? 0
  • +
  • -

#14 Billyzitwark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 12

Re: What is wrong with this code?

Posted 18 November 2012 - 10:41 AM

Just kidding it was my NaN's. Is there a way to plot the regression line without omitting them?
Was This Post Helpful? 0
  • +
  • -

#15 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: What is wrong with this code?

Posted 18 November 2012 - 03:56 PM

Try replacing NaN with 0?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2