1
$\begingroup$

I've seen a couple of posts on how to make a simple graph that looks something like this:

enter image description here

The code for this would be as follows:

# Create linear regression object
regr = linear_model.LinearRegression()

# Train the model using the training sets
regr.fit(X_train, Y_train)

# Plot outputs
plt.plot(X_test, regr.predict(X_test), color='red',linewidth=3)

I've been working on building a similar thing but using the USA_Housing dataset from Kaggle.

My code is as follows:

# plot the graph
plt.scatter(X_test[0], y_test, color = "black")

plt.plot(X_test[0], y_hat)
plt.xticks(())
plt.yticks(())
plt.show()

Which results in the following:

enter image description here

X_test[0] is just one of the independent variables (income).

Shouldn't this just make a simple line through the prediction points?

R-squared is around 91 so the fit is good, but I guess I'm confused as to why it would look like this.

$\endgroup$

2 Answers 2

2
$\begingroup$

pyplot defaults to '-' for linestyle argument, which means a line that connects through all of the dots, so to produce a scatter plot, whose linestyle are data points instead write

plt.plot(X_test[0], y_hat, '.')

$\endgroup$
2
$\begingroup$

Generally plotting functions connects the points that are consecutive in the array. So, first sort those points and then plot.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.