正在关注EdX上关于在数据科学中使用Python编程的课程。当使用给定的函数绘制我的线性回归模型的结果时,我不确定是否定义的函数drawLine不正确,或者我的建模过程中有其他错误。
这里是定义的功能def drawLine(model, X_test, y_test, title, R2):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(X_test, y_test, c='g', marker='o')
ax.plot(X_test, model.predict(X_test), color='orange', linewidth=1, alpha=0.7)
title += " R2: " + str(R2)
ax.set_title(title)
print(title)
print("Intercept(s): ", model.intercept_)
plt.show()
这是我写的代码import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import linear_model
from sklearn.model_selection import train_test_split
matplotlib.style.use('ggplot') # Look Pretty
# Reading in data
X = pd.read_csv('Datasets/College.csv', index_col=0)
# Wrangling data
X.Private = X.Private.map({'Yes':1, 'No':0})
# Splitting data
roomBoard = X[['Room.Board']]
accStudent = X[['Accept']]
X_train, X_test, y_train, y_test = train_test_split(roomBoard, accStudent, test_size=0.3, random_state=7)
# Training model
model = linear_model.LinearRegression()
model.fit(X_train, y_train)
score = model.score(X_test, y_test)
# Visualise results
drawLine(model, X_test, y_test, "Accept(Room&Board)", score)