1
from matplotlib import pyplot as plt


data = pd.read_csv("student-mat.csv", sep=";")

data = data[["G1", "G2", "G3", "studytime", "failures", "absences"]]

predict = "G3"

X = np.array(data.drop([predict], 1))
y = np.array(data[predict])

x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size = 0.1)

linear = linear_model.LinearRegression()

linear.fit(x_train, y_train)
accuracy = linear.score(x_test, y_test)
print(accuracy*100)

print('Coefficient: \n', linear.coef_)
print('Intercept: \n', linear.intercept_)

predictions = linear.predict(x_test)

for x in range(len(predictions)):
    print(predictions[x], x_test[x], y_test[x])
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
thunderOP
  • 13
  • 1
  • 1
    This question is not reproducible without **data**. Please see [How to provide a reproducible copy of your DataFrame using `df.head(30).to_clipboard(sep=',')`](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. If you don't include an mre, it is likely the question will be downvoted, closed, and deleted. – Trenton McKinney Sep 17 '21 at 17:13
  • 1
    [`seaborn.regplot`](https://seaborn.pydata.org/generated/seaborn.regplot.html) – Trenton McKinney Sep 17 '21 at 17:14

2 Answers2

0

Example (similar to above comment):

import seaborn as sns
sns.regplot(x=y_test, y=predictions, ci=None, color="r")
Joe
  • 97
  • 10
0

You can use the following code to plot the line chart using matplotlib:

a = linear.coef_
b = linear.intercept_
plt.plot(x_train, a*x_train + b)

or another possible solution is :

m = linear.coef_
b = linear.intercept_
regression_line = [(m*x)+b for x in x_train]
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
plt.scatter(x_train,y_train,color='#003F72')
plt.plot(x_train, regression_line)
plt.show()
Raha Moosavi
  • 527
  • 4
  • 19