df = seaborn.load_dataset("mpg")
print(df.head())
print(df.info())
The output of the above Python statements will be like the following:
mpg cylinders displacement ... model_year origin name 0 18.0 8 307.0 ... 70 usa chevrolet chevelle malibu 1 15.0 8 350.0 ... 70 usa buick skylark 320 2 18.0 8 318.0 ... 70 usa plymouth satellite 3 16.0 8 304.0 ... 70 usa amc rebel sst 4 17.0 8 302.0 ... 70 usa ford torino [5 rows x 9 columns] <class 'pandas.core.frame.DataFrame'> RangeIndex: 398 entries, 0 to 397 Data columns (total 9 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 mpg 398 non-null float64 1 cylinders 398 non-null int64 2 displacement 398 non-null float64 3 horsepower 392 non-null float64 4 weight 398 non-null int64 5 acceleration 398 non-null float64 6 model_year 398 non-null int64 7 origin 398 non-null object 8 name 398 non-null object dtypes: float64(4), int64(3), object(2) memory usage: 28.1+ KB
As we can see the horsepower variable contains some null values. It has only 392 non-null values out of 398 rows. So, for simplicity, we will drop the rows that contain null values from the dataset.
df = df.dropna()
Now, we plot the scatterplot that shows the relationship between mpg and horsepower of a car.
pyplot.scatter(df["horsepower"], df["mpg"])
pyplot.xlabel("Horsepower")
pyplot.ylabel("MPG")
pyplot.savefig("poly-regression-scatter.png")
pyplot.close()
The graph looks like the following:








































0 Comments