9

I've been following this tutorial I found online about speech analysis in Deep Learning, it kept giving me the nameerror. i'm quite new to python, so I'm not sure on how to define it. But then train_test_split is a method by default to split the data, train_test_split is imported.

Here is the code:

'''

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 '''

it gave an error:

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
peterh
  • 11,875
  • 18
  • 85
  • 108
silvermaze
  • 133
  • 1
  • 1
  • 3

1 Answers1

11

Probably you haven't installed sklearn Pip install sklearn If you already have done that, then try: from sklearn.cross_validation import train_test_split

Capie
  • 976
  • 1
  • 8
  • 20
  • 1
    i put from sklearn.cross_validation import train_test_split as you suggested and ran it, error came out ModuleNotFoundError: No module named 'sklearn.cross_validation' – silvermaze Dec 15 '19 at 22:40
  • Did you run the `pip install sklearn`? You obviously don't have it installed – Capie Dec 15 '19 at 22:56
  • [This answer is outdated](https://stackoverflow.com/a/34844352/5033247) – Smart Manoj Dec 03 '20 at 13:49
  • 16
    In 2020, I had to use `from sklearn.model_selection import train_test_split` – lizziepika Jan 25 '21 at 22:11
  • https://stackoverflow.com/questions/30667525/importerror-no-module-named-sklearn-cross-validation We need to use `model_selection` – ABCD Feb 06 '21 at 12:01