Polynomial Regression曲线绘制异常求助:如何正确处理难处理数据?
多项式回归绘图不符合预期,求解决方案
我在执行多项式回归时遇到问题,绘制出的曲线不符合预期。已查阅Stack Overflow上的同类问题,但未找到适用的解决方案。当前以reviews_per_month为自变量X,price为因变量y进行建模。
我尝试过排序X值,但问题仍未解决。以下是我的代码:
# Defining independent and dependent variables X = cph_listings_df[['reviews_per_month']].values.reshape(-1, 1) y = cph_listings_df['price'].values.reshape(-1, 1) # split into test and training X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) # scale features scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # Fitting Linear Regression to the dataset lin_reg = LinearRegression() lin_reg.fit(X, y) # Fitting Polynomial Regression to the dataset poly_model = PolynomialFeatures(degree=2) X_poly = poly_model.fit_transform(X) # transformer X to poly features pol_reg = LinearRegression() # instance of lineargression pol_reg.fit(X_poly, y) # train model y_predict = pol_reg.predict(X_poly) # predict on trained model plt.scatter(X, y, color='red') # red = actual data points plt.plot(X, y_predict , color='blue') plt.title('Polynomia Regression)') plt.xlabel('Number of reviews') plt.ylabel('Price') plt.show()
当前生成的曲线图:
老师要求的曲线图(手绘):
恳请有类似经验或成功处理过同类数据的人士提供解决方案。
内容的提问来源于stack exchange,提问作者BrunkageCoder
相关产品推荐
相关产品推荐

