Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将数据拆分为随机集_Python - Fatal编程技术网

Python将数据拆分为随机集

Python将数据拆分为随机集,python,Python,我想把我的数据分成两个随机集。我已经完成了第一部分: ind = np.random.choice(df.shape[0], size=[int(df.shape[0]*0.7)], replace=False) X_train = df.iloc[ind] 现在我想选择所有索引'notinind来创建我的测试集。你能告诉我怎么做吗 我以为会的 X_test = df.iloc[-ind] 但很明显,这不是尝试这种纯Python方法 ind_inversed = list(set(range

我想把我的数据分成两个随机集。我已经完成了第一部分:

ind = np.random.choice(df.shape[0], size=[int(df.shape[0]*0.7)], replace=False)
X_train = df.iloc[ind]
现在我想选择所有索引'notin
ind
来创建我的测试集。你能告诉我怎么做吗

我以为会的

X_test = df.iloc[-ind]

但很明显,这不是

尝试这种纯Python方法

ind_inversed = list(set(range(df.shape[0])) - set(ind))
X_test = df.iloc[ind_inversed]

尝试这种纯Python方法

ind_inversed = list(set(range(df.shape[0])) - set(ind))
X_test = df.iloc[ind_inversed]
检查
test\u train\u split()

文档中的示例:

>>> import numpy as np
>>> from sklearn.model_selection import train_test_split
>>> X, y = np.arange(10).reshape((5, 2)), range(5)
>>> X
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
>>> list(y)
[0, 1, 2, 3, 4]

>>>

>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, test_size=0.33, random_state=42)
...
>>> X_train
array([[4, 5],
       [0, 1],
       [6, 7]])
>>> y_train
[2, 0, 3]
>>> X_test
array([[2, 3],
       [8, 9]])
>>> y_test
[1, 4]
在您的情况下,您可以这样做:

larger, smaller = test_train_split(df, test_size=0.3)
检查
test\u train\u split()

文档中的示例:

>>> import numpy as np
>>> from sklearn.model_selection import train_test_split
>>> X, y = np.arange(10).reshape((5, 2)), range(5)
>>> X
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
>>> list(y)
[0, 1, 2, 3, 4]

>>>

>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, test_size=0.33, random_state=42)
...
>>> X_train
array([[4, 5],
       [0, 1],
       [6, 7]])
>>> y_train
[2, 0, 3]
>>> X_test
array([[2, 3],
       [8, 9]])
>>> y_test
[1, 4]
在您的情况下,您可以这样做:

larger, smaller = test_train_split(df, test_size=0.3)

另一种获得70-30列车测试分割的方法是生成索引,将其洗牌,然后将其分割为70-30部分

ind = np.arange(df.shape[0])
np.random.shuffle(ind)
X_train = df.iloc[ind[:int(0.7*df.shape[0])],:]
X_test = df.iloc[ind[int(0.7*df.shape[0]):],:]

我建议将
pandas.dataframe
转换为一个数字矩阵,并使用scikit learn的
train\u test\u split
进行拆分,除非您真的想这样做。

获得70-30列车测试拆分的另一种方法是生成索引,将其洗牌,然后将其拆分为70-30个部分

ind = np.arange(df.shape[0])
np.random.shuffle(ind)
X_train = df.iloc[ind[:int(0.7*df.shape[0])],:]
X_test = df.iloc[ind[int(0.7*df.shape[0]):],:]

我建议将
pandas.dataframe
转换为数字矩阵,并使用scikit learn的
train\u test\u split
进行拆分,除非您真的想这样做。

那么您想选择70%作为测试数据,并使用其余30%作为训练数据?一个更简单的方法可能是使用np.random.shuffle来洗牌索引,并使用洗牌索引的前70%作为训练,其余作为测试。是的,这正是我想要的,所以你想选择70%作为测试数据,其余30%作为训练数据?一种更简单的方法可能是使用np.random.shuffle对索引进行随机排列,并使用前70%的随机排列索引作为训练,其余作为测试。是的,这正是我想要的。这并不是随机排列两个集合,因为我假设
ind
的计算方法与原始问题中的计算方法相同
ind\u inversed
表示
ind
中没有的所有其他索引。我得到的错误是
int()参数必须是字符串、类似字节的对象或数字,而不是使用此参数“设置”
technique@jlt199,我已经更新了我的答案。我已经测试过这个解决方案,它确实有效。谢谢,这个也有效。我选择这一个作为批准的答案,只是因为它是我最终使用的答案。其他建议的技术也很有效,毫无疑问将在将来使用。由于我假设
ind
的计算方法与原始问题中的相同,因此它不会将这两个设置随机化
ind\u inversed
表示
ind
中没有的所有其他索引。我得到的错误是
int()参数必须是字符串、类似字节的对象或数字,而不是使用此参数“设置”
technique@jlt199,我已经更新了我的答案。我已经测试过这个解决方案,它确实有效。谢谢,这个也有效。我选择这一个作为批准的答案,只是因为它是我最终使用的答案。其他建议的技术也很有效,毫无疑问将在未来使用。我喜欢这种方法。谢谢我以前使用过
train\u test\u split
(虽然我已经忘记了),但是我发现数据更容易在数据框中检查和可视化。我喜欢这种方法。谢谢我以前使用过
train\u test\u split
(虽然我已经忘记了),但我发现数据更容易在数据帧中检查和可视化。