Python 执行多项式线性回归时Matplotlib上的x轴不正确
以下代码生成范围为8到18的x轴。x轴的数据实际上在1000万到5000万之间。我希望对数刻度显示(10000),(100000),(1000000)(10000000)等 如何固定x轴Python 执行多项式线性回归时Matplotlib上的x轴不正确,python,matplotlib,scikit-learn,Python,Matplotlib,Scikit Learn,以下代码生成范围为8到18的x轴。x轴的数据实际上在1000万到5000万之间。我希望对数刻度显示(10000),(100000),(1000000)(10000000)等 如何固定x轴 dataset = pandas.DataFrame(Transactions, Price) dataset = dataset.drop_duplicates() import numpy as np import matplotlib.pyplot as plt X=dataset[['Transac
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()
绘图:
使用对数刻度绘制
log_X
的值。它是双重记录的。用对数刻度仅绘制X,或np.exp(log\ux)
不,你甚至没有使用对数刻度。使用对数比例绘制X
:使用正常比例绘制plt.xscale(“log”)
,而不是log\u X