7 examples of 'import train test split' in Python

Every line of 'import train test split' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
9def train_test_split(fileName,type=1):
10 header = ['user_id', 'item_id', 'rating', 'timestamp']
11 if(type==1):
12 df = pd.read_csv(fileName, sep='\t', names=header)
13 else:
14 df = pd.read_csv(fileName, sep='::', names=header,engine = 'python')
15 n_users = df.user_id.unique().shape[0]
16 users = df.user_id.max()
17 n_items = df.item_id.unique().shape[0]
18 items = df.item_id.max()
19
20 print 'Number of users = ' + str(n_users) + ' | Number of movies = ' + str(n_items)
21 print 'The biggest ID of users = ' + str(users) + ' | The biggest ID of movies = ' + str(items)
22 #
23 train_data, test_data = cv.train_test_split(df, test_size=0.1)
24 train_data = pd.DataFrame(train_data)
25 test_data = pd.DataFrame(test_data)
26 #Create two user-item matrices, one for training and another for testing
27 train_data_matrix = np.zeros((users, items))
28 for line in train_data.itertuples():
29 train_data_matrix[line[1]-1, line[2]-1] = line[3]
30
31 test_data_matrix = np.zeros((users, items))
32 for line in test_data.itertuples():
33 test_data_matrix[line[1]-1, line[2]-1] = line[3]
34 return train_data_matrix,test_data_matrix
23def train_valid_test_split(SOURCE_DATA_DIR, TARGET_DATA_DIR, train_size=0.8, valid_size=0.1,
24 COMBINE_FOLDERS=None, SELECT_FOLDERS=None):
25 """
26 Usage:
27 SOURCE_DATA_DIR = "data/ClothingAttributeDataset/images/"
28 TARGET_DATA_DIR = "data/"
29
30 train_valid_test_split(SOURCE_DATA_DIR, TARGET_DATA_DIR)
31 """
32 if COMBINE_FOLDERS is None:
33 COMBINE_FOLDERS = dict()
34 for folder_name in ["train", "test", "valid"]:
35 rmtree(os.path.join(TARGET_DATA_DIR, folder_name), ignore_errors=True)
36 os.makedirs(os.path.join(TARGET_DATA_DIR, folder_name))
37
38 # Split records by 80-20 between Train and Validation Set
39 filenames = np.random.permutation(glob(os.path.join(SOURCE_DATA_DIR, "*.jpg")))
40
41 train_idx = int(len(filenames) * train_size)
42 test_idx = int(len(filenames) * (train_size+valid_size))
43 for idx, filename in enumerate(filenames):
44 target_name = filename.split("/")[-1]
45 if idx < train_idx:
46 target_filepath = os.path.join(TARGET_DATA_DIR, "train", target_name)
47 elif idx < test_idx:
48 target_filepath = os.path.join(TARGET_DATA_DIR, "valid", target_name)
49 else:
50 target_filepath = os.path.join(TARGET_DATA_DIR, "test", target_name)
51 copyfile(filenames[idx], target_filepath)
75def _get_adapted_dataset(split):
76 """ Gets the adapted dataset for the experiments
77
78 Args :
79 split (str): train or test
80 Returns :
81 (tuple): images and labels
82 """
83 dataset = _get_dataset()
84 key_img = 'x_' + split
85 key_lbl = 'y_' + split
86
87 if split != 'train':
88 dataset[key_img], dataset[key_lbl] = _adapt(dataset[key_img],
89 dataset[key_lbl])
90
91 return (dataset[key_img], dataset[key_lbl])
36def prepare_data():
37 data = io.loadmat(raw_filename)
38 df = pd.DataFrame(dict(
39 spectra=data['Int_ABQR'].tolist(),
40 solute=data['Gamme_ABQR'].ravel(),
41 vial=data['Vial_ABQR'].ravel(),
42 concentration=data['Conc_ABQR'].ravel(),
43 molecule=data['Molecule_ABQR'].ravel()))
44 skf = ShuffleSplit(n_splits=2, test_size=held_out_test_size,
45 random_state=random_state)
46 train_is, test_is = list(skf.split(df))[0]
47 df_train = df.iloc[train_is]
48 df_test = df.iloc[test_is]
49 df_train.to_csv(train_filename, index=False)
50 df_test.to_csv(test_filename, index=False)
9def train_test_split(X, y, train_percentage=0.8):
10 '''
11 Very simple splitting into train and test data. Works for
12 any input shape without dependencies, but is a bit restricted.
13 '''
14 cut_idx = int(floor(X.shape[0] * 0.80))
15 X_train, X_test = X[:cut_idx], X[cut_idx:]
16 y_train, y_test = y[:cut_idx], y[cut_idx:]
17 print("Number of train samples", X_train.shape[0])
18 print("Number of test samples", X_test.shape[0])
19
20 return (X_train, y_train), (X_test, y_test)
82def my_train_split(ds, y):
83 return ds, skorch.dataset.Dataset(corpus.valid[:200], y=None)
35def split_data(df):
36 X = df.drop('Y', axis=1).values
37 y = df['Y'].values
38
39 X_train, X_test, y_train, y_test = train_test_split(
40 X, y, test_size=0.2, random_state=0)
41 data = {"train": {"X": X_train, "y": y_train},
42 "test": {"X": X_test, "y": y_test}}
43 return data

Related snippets