为什么会出现NameError:名称'train_test_split'未定义?

时间:2019-12-15 21:56:48

标签: python deep-learning jupyter-notebook nameerror

我一直在关注我在网上找到的有关深度学习中语音分析的教程,但该教程一直给我起名字错误。我对python很陌生,所以我不确定如何定义它。但是默认情况下,train_test_split是一种拆分数据的方法,train_test_split是导入的

代码如下:

'''

import numpy as np
import pandas as pd 
import os
import seaborn as sns
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('fivethirtyeight')
from tqdm import tqdm
print(os.listdir("../input"))

from keras import Sequential
from keras import optimizers
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential,Model
from keras.layers import LSTM, Dense, Bidirectional, Input,Dropout,BatchNormalization,CuDNNLSTM, GRU, CuDNNGRU, Embedding, GlobalMaxPooling1D, GlobalAveragePooling1D, Flatten
from keras import backend as K
from keras.engine.topology import Layer
from keras import initializers, regularizers, constraints
from sklearn.model_selection import KFold, cross_val_score, train_test_split

train = pd.read_json('C:/Users/User/Downloads/dont-call-me-turkey/train.json')
display(train.shape)

train.head()

train_train, train_val = train_test_split(train, random_state = 42)
xtrain = [k for k in train_train['audio_embedding']]
ytrain = train_train['is_turkey'].values
xval = [k for k in train_val['audio_embedding']]
yval = train_val['is_turkey'].values '''

它给出了一个错误:

NameError                                 Traceback (most recent call last)
<ipython-input-19-1e07851e6519> in <module>
----> 1 train_train, train_val = train_test_split(train, random_state = 42)
      2 xtrain = [k for k in train_train['audio_embedding']]
      3 ytrain = train_train['is_turkey'].values
      4 xval = [k for k in train_val['audio_embedding']]
      5 yval = train_val['is_turkey'].values

NameError: name 'train_test_split' is not defined

1 个答案:

答案 0 :(得分:0)

可能您尚未安装sklearn Pip install sklearn 如果您已经这样做,请尝试: from sklearn.cross_validation import train_test_split