【问题标题】:Incorrect x axis on Matplotlib when doing polynomial linear regression进行多项式线性回归时,Matplotlib 上的 x 轴不正确
【发布时间】:2020-10-26 01:51:26
【问题描述】:

以下代码生成的 x 轴范围为 8 到 18。x 轴的数据实际上范围为 1000 到 5000 万。我希望对数刻度显示 (10,000)、(100,000)、(1,000,000) (10,000,000) 等。

如何固定 x 轴?

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()

剧情:

【问题讨论】:

    标签: python matplotlib scikit-learn


    【解决方案1】:

    您使用对数刻度绘制 log_X 的值。它是双记录的。仅用对数比例绘制 X,或 np.exp(log_X)

    不,您甚至没有使用对数刻度。使用对数刻度绘制 Xplt.xscale("log"),而不是使用正常刻度绘制 log_X

    【讨论】:

      猜你喜欢
      • 2018-08-27
      • 1970-01-01
      • 2021-03-19
      • 2021-03-11
      • 2020-05-22
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多