4

i am implementing simple polynomial regression to predict time for a video given its size, and it's my own dataset. Now for some reason, i am getting multiple traces for my plot.

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('estSize.csv')
X = dataset.iloc[:, 0].values.reshape(-1,1)
y = dataset.iloc[:, 1].values.reshape(-1,1)

from sklearn.linear_model import LinearRegression

# Fitting Polynomial Regression to the dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 2)
X_poly = poly_reg.fit_transform(X)
poly_reg.fit(X_poly, y)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, y)

# Visualising the Polynomial Regression results
plt.scatter(X, y, color = 'red')
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = 'blue')
plt.show()

This is my output

5
  • What is the question?
    – desertnaut
    Aug 7, 2018 at 17:46
  • change your second last line to : plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)),"b-").
    – Krishna
    Aug 7, 2018 at 17:48
  • That didnt not work, i still have multiple lines Aug 7, 2018 at 18:02
  • 2
    Is your data ordered with respect to the X values? Aug 7, 2018 at 18:12
  • 2
    As @Marathon55 points out, the data must be ordered from smallest to largest value. This is easily shown by making a line plot of totally random data that is not ordered. Aug 7, 2018 at 18:17

1 Answer 1

5

Your data needs to be ordered with respect to the predictor.

After the line

dataset = pd.read_csv('estSize.csv')

Add this line:

dataset = dataset.sort_values(by=['col1'])

Where col1 is your column header for the file-size values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.