Hello
I am currently trying to get my sea level predictor code to work but it seems there are some issues with the test and i’m getting a couple of failures.
here is my code:
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import linregress
import numpy as np
def draw_plot():
# Read data from file
df = pd.read_csv("epa-sea-level.csv")
# Create scatter plot
plt.scatter(df["Year"], df["CSIRO Adjusted Sea Level"])
# Create first line of best fit
slope, intercept, r_value, p_value, std_err = linregress(df["Year"], df["CSIRO Adjusted Sea Level"])
x1 = np.arange(df["Year"].min(),2050,1)
plt.plot(x1, intercept + slope*x1, "r")
# Create second line of best fit
x2=df["Year"][df["Year"] >= 2000]
y2=df.loc[(df["Year"] >= 2000), "CSIRO Adjusted Sea Level"]
xi=np.arange(x2.min(),2050,1)
slope, intercept, r_value, p_value, std_err = linregress(x2,y2)
plt.plot(xi, intercept + slope*xi, "g")
# Add labels and title
ax = plt.gca()
ax.set(xlabel = "Year", ylabel = "Sea Level (inches)", title = "Rise in Sea Level", xticks=[1850.0, 1875.0, 1900.0, 1925.0, 1950.0, 1975.0, 2000.0, 2025.0, 2050.0, 2075.0])
# Save plot and return data for testing (DO NOT MODIFY)
plt.savefig('sea_level_plot.png')
return plt.gca()
here is the result from repl:
F.F.
======================================================================
FAIL: test_plot_data_points (test_module.LinePlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-sea-level-predictor/test_module.py", line 30, in test_plot_data_points
self.assertEqual(actual, expected, "Expected different data points in scatter plot.")
AssertionError: Lists differ: [[188[26 chars]72441], [1882.0, -0.440944881], [1883.0, -0.23[2982 chars]951]] != [[188[26 chars]7244100000002], [1882.0, -0.440944881], [1883.[3226 chars]951]]
First differing element 1:
[1881.0, 0.220472441]
[1881.0, 0.22047244100000002]
Diff is 6114 characters long. Set self.maxDiff to None to see it. : Expected different data points in scatter plot.
======================================================================
FAIL: test_plot_lines (test_module.LinePlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-sea-level-predictor/test_module.py", line 37, in test_plot_lines
self.assertEqual(actual, expected, "Expected different line for second line of best fit.")
AssertionError: Lists differ: [7.06[42 chars]04435186, 7.560361677767105, 7.726788951098968[873 chars]3011] != [7.06[42 chars]04435242, 7.560361677767105, 7.726788951098968[873 chars]3011]
First differing element 2:
7.393934404435186
7.393934404435242
Diff is 1253 characters long. Set self.maxDiff to None to see it. : Expected different line for second line of best fit.
--------------------------------------------```
looking at the "CSIRO Adjusted Sea Level" data in the CSV file it seems to have a lot less decimals which is leading to my results not matching in the tests.
any suggestions how i can solve this.
thanks,