gpt4 book ai didi

python - 导入仅在 Python 函数内部工作

转载 作者:行者123 更新时间:2023-11-28 22:33:53 25 4
gpt4 key购买 nike

背景信息:我正在使用 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

关于python - 导入仅在 Python 函数内部工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39466228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com