导入仅在 Python 函数内部工作
Import Only Work Inside Python Function
背景信息:我正在使用 scikit-learn 开发一个模型。我使用 sklearn.cross_validation 模块将数据拆分为单独的训练集和测试集,如下所示:
def train_test_split(input_data):
from sklearn.cross_validation import train_test_split
### STEP 1: Separate y variable and remove from X
y = input_data['price']
X = input_data.copy()
X.drop('price', axis=1, inplace=True)
### STEP 2: Split into training & test sets
X_train, X_test, y_train, y_test =\
train_test_split(X, y, test_size=0.2, random_state=0)
return X_train, X_test, y_train, y_test
我的问题: 当我尝试在我的函数之外导入 sklearn.cross_validation 模块时,我收到以下错误:
from sklearn.cross_validation import train_test_split
def train_test_split(input_data):
### STEP 1: Separate y variable and remove from X
y = input_data['price']
X = input_data.copy()
X.drop('price', axis=1, inplace=True)
### STEP 2: Split into training & test sets
X_train, X_test, y_train, y_test =\
train_test_split(X, y, test_size=0.2, random_state=0)
return X_train, X_test, y_train, y_test
错误:
TypeError: train_test_split() got an unexpected keyword argument 'test_size'
知道为什么吗?
您正在从 sklear.cross_validation
导入函数 train_test_split
,然后用您的本地函数覆盖名称 train_test_split
。
尝试:
from sklearn.cross_validation import train_test_split as sk_train_test_split
def train_test_split(input_data):
### STEP 1: Separate y variable and remove from X
y = input_data['price']
X = input_data.copy()
X.drop('price', axis=1, inplace=True)
### STEP 2: Split into training & test sets
X_train, X_test, y_train, y_test =\
sk_train_test_split(X, y, test_size=0.2, random_state=0) # use the imported function instead of local one
return X_train, X_test, y_train, y_test
背景信息:我正在使用 scikit-learn 开发一个模型。我使用 sklearn.cross_validation 模块将数据拆分为单独的训练集和测试集,如下所示:
def train_test_split(input_data):
from sklearn.cross_validation import train_test_split
### STEP 1: Separate y variable and remove from X
y = input_data['price']
X = input_data.copy()
X.drop('price', axis=1, inplace=True)
### STEP 2: Split into training & test sets
X_train, X_test, y_train, y_test =\
train_test_split(X, y, test_size=0.2, random_state=0)
return X_train, X_test, y_train, y_test
我的问题: 当我尝试在我的函数之外导入 sklearn.cross_validation 模块时,我收到以下错误:
from sklearn.cross_validation import train_test_split
def train_test_split(input_data):
### STEP 1: Separate y variable and remove from X
y = input_data['price']
X = input_data.copy()
X.drop('price', axis=1, inplace=True)
### STEP 2: Split into training & test sets
X_train, X_test, y_train, y_test =\
train_test_split(X, y, test_size=0.2, random_state=0)
return X_train, X_test, y_train, y_test
错误:
TypeError: train_test_split() got an unexpected keyword argument 'test_size'
知道为什么吗?
您正在从 sklear.cross_validation
导入函数 train_test_split
,然后用您的本地函数覆盖名称 train_test_split
。
尝试:
from sklearn.cross_validation import train_test_split as sk_train_test_split
def train_test_split(input_data):
### STEP 1: Separate y variable and remove from X
y = input_data['price']
X = input_data.copy()
X.drop('price', axis=1, inplace=True)
### STEP 2: Split into training & test sets
X_train, X_test, y_train, y_test =\
sk_train_test_split(X, y, test_size=0.2, random_state=0) # use the imported function instead of local one
return X_train, X_test, y_train, y_test