Search code examples
pythonmatplotlibscikit-learnpolynomial-math

Points are connect out of order


I really have no idea why matplotlib connects dots on the plot in a random way: enter image description here It looks ok, only when I am plotting date with scatter() function: enter image description here

%matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline

np.random.seed(0)
n = 100
x = np.linspace(0,10,n) + np.random.randn(n)/5
y = np.sin(x)+x/6 + np.random.randn(n)/10

X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
plt.figure()
colors = ['teal', 'yellowgreen', 'gold', 'red']
lw = 2
plt.scatter(X_train, y_train, color='navy', s=30, marker='o', label="training points")

for count, degree in enumerate([1, 3, 6, 9]):
    model = make_pipeline(PolynomialFeatures(degree), Ridge())
    model.fit(X_train[:, np.newaxis], y_train)
    y_plot = model.predict(X_test[:, np.newaxis])
    plt.plot(X_test[:, np.newaxis], y_plot, color=colors[count], linewidth=lw, #np.sort(X_test)[:, np.newaxis]
             label="degree %d" % degree)
plt.legend(loc='lower right')

plt.show()

Solution

  • The values must be sorted with .sort_values.

    %matplotlib widget
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import Ridge
    from sklearn.preprocessing import PolynomialFeatures
    from sklearn.pipeline import make_pipeline
    
    np.random.seed(0)
    n = 100
    x = np.linspace(0,10,n) + np.random.randn(n)/5
    y = np.sin(x)+x/6 + np.random.randn(n)/10
    
    X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
    plt.figure()
    colors = ['teal', 'yellowgreen', 'gold', 'red']
    lw = 2
    plt.scatter(train_data[0].values, train_data[1].values, color='navy', s=30, marker='o', label="training points")
    
    # sorting values
    train_data = pd.DataFrame(data = [X_train, y_train]).T.sort_values(0)
    test_data = pd.DataFrame(data = [X_test, y_test]).T.sort_values(0)
    
    for count, degree in enumerate([1, 3, 6, 9]):
        model = make_pipeline(PolynomialFeatures(degree), Ridge())
        model.fit(train_data[0].values[:, np.newaxis], train_data[1].values)
        y_plot = model.predict(test_data[0].values[:, np.newaxis])
        plt.plot(test_data[0].values[:, np.newaxis], y_plot, color=colors[count], linewidth=lw, #np.sort(X_test)[:, np.newaxis]
                 label="degree %d" % degree)
    plt.legend(loc='lower right')
    
    plt.show()
    

    Result: enter image description here