My code
//
import numpy as np
import pandas as pd
import matplotlib.pyplot as pyplot
import pickle as pk
from sklearn import linear_model
from sklearn.utils import shuffle
from matplotlib import style
data = pd.read_csv("student-mat.csv", sep=";")
data = data[["G1", "G2", "G3", "studytime", "failures", "absences"]]
print(data.head())
predict = "G3"
x = np.array(data.drop(predict, axis=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)
acc = linear.score(x_test, y_test)
print(acc)
with open("studentmodel.pickle", "wb") as f:
pickle.dump(linear,f)
pickle_in = open("studentmodel.pickle", "rb")
linear = pickle.load(pickle_in)
print("coefficient: n", linear.coef_)
print("intercept: n", linear.intercept_)`
whenever i run this code, it throws me an error that says the name sklearn is not defined. however, this is odd because i imported the right sklearn libraries.
The error
NameError Traceback (most recent call last)
Cell In[1], line 14
12 x = np.array(data.drop(predict, axis=1))
13 y = np.array(data[predict])
---> 14 x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size=0.1)
15 linear = linear_model.LinearRegression()
16 linear.fit(x_train, y_train)
NameError: name 'sklearn' is not defined
New contributor
Fionn Lenaghan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.