0

I am trying to plot the trained curve in matplotlib. However I am getting this thing: enter image description here

The scatter works fine: enter image description here

How can I create the curve using plot?

3 Answers 3

1

It may be that the order of your X_train data is wrong. Try to sort them out. For instance, if X_train is just a list of numbers, you could say:

X_train.sort()
Sign up to request clarification or add additional context in comments.

2 Comments

If that didn't work, you can try adding this: linestyle = 'None' right after the Marker function in plt.plot.
It works! Thank you so much for this! X_train is pandas Dataframe with a single column and I just had to do this X_train.sort_values(by="age")
1

You can plot a smooth line curve by first determining the spline curve’s coefficients using the scipy.interpolate.make_interp_spline():

import numpy as np
import numpy as np
from scipy.interpolate import make_interp_spline
import matplotlib.pyplot as plt
 
# Dataset
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([20, 30, 5, 12, 39, 48, 50, 3])
 
X_Y_Spline = make_interp_spline(x, y)
 
# Returns evenly spaced numbers
# over a specified interval.
X_ = np.linspace(x.min(), x.max(), 500)
Y_ = X_Y_Spline(X_)
 
# Plotting the Graph
plt.plot(X_, Y_)
plt.title("Plot Smooth Curve Using the scipy.interpolate.make_interp_spline() Class")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

Result:

enter image description here

Comments

1

It seems, that you have unsorted values in X_train. For instance, if

In  [1]: X_train
Out [1]: array([30, 20, 50, 40])

then

In  [2]: model.predict_proba(X_train)
Out [2]: array([0.2, 0.1, 0.8, 0.5])

Here, plt.plot will try to plot lines from point [30, 0.2] to point [20, 0.1], then from [20, 0.1] to [50, 0.8], then from [50, 0.8] to [40, 0.5].

Thus, the solution to your problem is to sort X_train before plotting =)

import numpy as np
X_train_sorted = np.sort(X_train)
y_train_sorted = model.predict_proba(X_train_sorted)
plt.scatter(X_train_sorted, y_train_sorted)
plt.plot(X_train_sorted, y_train_sorted)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.