messy scatter plot regression line: Python

后端 未结 1 1077
逝去的感伤
逝去的感伤 2021-01-28 11:47

In python 2.7.6, matlablib, scikit learn 0.17.0, When I make a polynomial regression lines on a scatter plot, the polynomial curve will be really messy like this:

相关标签:
1条回答
  • 2021-01-28 12:25

    While the plot is now correct, you messed up the pairing of X_test1 to y_test1 while sorting because you forgot to also sort y_test1 in the same way. The best solution is to sort right after the split. Then y_plot, which is computed later, will be automatically correct: (Here untested example using numpy as np)

    X_train1, X_test1, y_train1, y_test1 =             train_test_split(LSTAT,MEDV,test_size=0.3,random_state=1)
    
    sorted_index = np.argsort(X_test1)
    X_test1 = X_test1[sorted_index]
    y_test1 = y_test1[sorted_index]
    
    0 讨论(0)
提交回复
热议问题