kingwall2020
kingwall2020

Reputation: 1

Incorrect x axis on Matplotlib when doing polynomial linear regression

The following code results in an x axis that ranges from 8 to 18. The data for the x axis actually ranges from 1,000 to 50 million. I would expect a log scale to show (10,000), (100,000), (1,000,000) (10,000,000) etc.

How do i fix the x axis?

dataset = pandas.DataFrame(Transactions, Price)
dataset = dataset.drop_duplicates()

import numpy as np
import matplotlib.pyplot as plt

X=dataset[['Transactions']]
y=dataset[['Price']]

log_X =np.log(X)

from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()

from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree=2)
X_poly = poly_reg.fit_transform(log_X)
pol_reg = LinearRegression()
pol_reg.fit(X_poly, y)

def viz_polymonial():
    plt.scatter(log_X, y, color='red')
    plt.plot(log_X, pol_reg.predict(poly_reg.fit_transform(log_X)), color='blue')
    plt.title('Price Curve')
    plt.xlabel('Transactions')
    plt.ylabel('Price')
    plt.grid(linestyle='dotted')
    plt.show()
    return
viz_polymonial()

Plot:

enter image description here

Upvotes: 0

Views: 119

Answers (1)

Jonatan Öström
Jonatan Öström

Reputation: 2609

You plot the values of log_X with log-scale. It's double-logged. Plot just X with log scale, or np.exp(log_X).

No you are not even using log-scale. Plot X wiht log-scale: plt.xscale("log"), not log_X with normal scale.

Upvotes: 1

Related Questions