ML -- End to End project split_train_test

Hello,

in below code… split_train_test is comprise of scikit-learn… ?

I am just wondering that why "import statement " of this function is not there in code…

For illustration only. Sklearn has train_test_split()

def split_train_test(data, test_ratio):
shuffled_indices = np.random.permutation(len(data))
test_set_size = int(len(data) * test_ratio)
test_indices = shuffled_indices[:test_set_size]
train_indices = shuffled_indices[test_set_size:]
return data.iloc[train_indices], data.iloc[test_indices]

train_set, test_set = split_train_test(housing, 0.2)
print(len(train_set), “train +”, len(test_set), “test”)

Hi @ns2000,

We have imported the function using this command in notebook

from sklearn.model_selection import train_test_split

We just have to import it once and it will be available in the notebook session.

Hope this helps

Thanks

It remembers what was imported as long as the notebook is running. If you restart the kernel or close the notebook, you need to run your import statements again and any variables declared for any libraries and variables you need loaded back in-memory.

2 Likes

Hi @Gopi_Raga,

Thanks for adding more details in the answer

2 Likes