未定义名称“ linear_model”

时间:2018-10-11 03:27:28

标签: python scikit-learn linear-regression

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression

df = pd.read_excel('C:\Data.xlsx')
#print (df.head())

x_train = df['Father'].values[0:15]
y_train = df['Son'].values[0:15]

x_test = df['Father'].values[15:]
#print (x_test)

lm = linear_model.LinearRegression()
lm.fit(x_train, y_train)

-----------------------------------------

lm = linear_model.LinearRegression()
NameError: name 'linear_model' is not defined

2 个答案:

答案 0 :(得分:2)

在导入这样的模块时:

import foo

您需要这样调用函数:

foo.bar()

但是在导入这样的模块时:

from foo import bar

您调用如下函数:

bar()

答案 1 :(得分:1)

将其放入您的代码中:

import sklearn
from sklearn import linear_model