Sea Level Predictor FAIL: test_plot_lines (test_module.LinePlotTestCase)

Hello, I’m having some trouble trying to fix this issue with my plot. Apparently there is a mismatch in shape and in the expected lines from the arrays. However, when looking at my graph I feel like it looks exactly as it is supposed to look like, as they ask for two lines predicting the sea level behavior for 2050.

This is my graph:

sea_level_plot

This is my code:

# Read data from file
df = pd.read_csv('epa-sea-level.csv')

# Create scatter plot
y = df['CSIRO Adjusted Sea Level']
x = df['Year']
plt.scatter(x, y)
plt.xlabel('Year')
plt.ylabel('Sea Level (inches)')
plt.title("Rise in Sea Level")

# Create first line of best fit
slp = linregress(x, y)
new = {'Year': 2050}
df = df.append(new, ignore_index=True)
y = df['CSIRO Adjusted Sea Level']
x = df['Year']
plt.plot(x, y, 'o')
plt.plot(x, slp[0] * x + slp[1])
plt.xlabel('Year')
plt.ylabel('Sea Level (inches)')
plt.title("Rise in Sea Level")

# Create second line of best fit
dfb = df[(df['Year'] >= 2000) & (df['Year'] < 2050)]
yb = dfb['CSIRO Adjusted Sea Level']
xb = dfb['Year']
slpb = linregress(xb, yb)
new = {'Year': 2050}
dfb = dfb.append(new, ignore_index=True)
yb = dfb['CSIRO Adjusted Sea Level']
xb = dfb['Year']
plt.plot(x, y, 'o')
plt.plot(x, slp[0] * x + slp[1])
plt.plot(xb, slpb[0] * xb + slpb[1])

# Add labels and title
plt.xlabel('Year')
plt.ylabel('Sea Level (inches)')
plt.title("Rise in Sea Level")

This is the error I’m getting:

AssertionError:
Arrays are not almost equal to 7 decimals
Expected different line for first line of best fit.
(shapes (135,), (171,) mismatch)
x: array([ 0. , 0.2204724, -0.4409449, -0.2322835, 0.5905512,
0.5314961, 0.4370079, 0.2165354, 0.2992126, 0.3622047,
0.4409449, 0.3740157, 0.5 , 0.6850394, 0.3031496,…
y: array([-0.542124 , -0.4790794, -0.4160349, -0.3529903, -0.2899457,
-0.2269011, -0.1638565, -0.1008119, -0.0377674, 0.0252772,
0.0883218, 0.1513664, 0.214411 , 0.2774556, 0.3405002,…

Ran 4 tests in 0.279s
FAILED (failures=1)

Challenge: Sea Level Predictor

Link to the challenge:

Hi @juanespal

I was just stuck with a similar problem and finally found the issue. Have a look at the test_plot_lines function in test_module.py: Here, all the points of the line that you plotted are compared to the expectation - and the expectation is that there is a value pair for all years 1880 to 2050 for the first line, and for all years 2000 to 2050 for the second line. So that’s why you get a shape mismatch. The test expects an array with 171 values, while you provided 135 only. While the plot looks perfectly fine, the underlying data is different.
Hope this helps!

3 Likes

Will definitely give it a try. Thanks for the help! Can’t help but think that the test should be changed, since it only seems to validate a single approach to a problem that can be solved in many different ways.

1 Like

Hello, I’ve managed to get it working. Had to do exactly as you said. Plotting each point was the way to go. Thank you so much!

Great :slight_smile:
I’m half way convinced that it’s part of the challenge to understand the test code… after all, a lot about working in programming is about reading other people’s code.

I’m getting a similar error.
“Expected different line for first line of best fit.
(shapes (170,), (171,) mismatch)”
How did you match the array lengths?

It is much easier if you open your own thread and post a link to your code.