Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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_Python 3.x_Random_Scikit Learn - Fatal编程技术网

Python 在训练测试中随机定义训练大小

Python 在训练测试中随机定义训练大小,python,python-3.x,random,scikit-learn,Python,Python 3.x,Random,Scikit Learn,我试图将我拥有的数据分为40%训练和60%验证,然后我想重复这个30次,每次随机训练和不同的验证。我该怎么做?(不使用Kfold) 这是我写的,但我每次都得到相同的结果,以确保准确性,我不知道如何通过每次不同的培训和验证来做到这一点。每次迭代我的精度都是一样的,我不知道为什么 for i in range (30): X_train, X_test, y_train, y_test =train_test_split(df,y, train_size=0.4, shuf

我试图将我拥有的数据分为
40%
训练和
60%
验证,然后我想重复这个
30次,每次随机训练和不同的验证。我该怎么做?(不使用
Kfold

这是我写的,但我每次都得到相同的结果,以确保准确性,我不知道如何通过每次不同的培训和验证来做到这一点。每次迭代我的精度都是一样的,我不知道为什么

for i in range (30):
      X_train, X_test, y_train, y_test =train_test_split(df,y, 
      train_size=0.4, shuffle=True)
      metrics.accuracy_score(linsvc.predict(X_train), R_train)

要获得30次迭代中每个迭代的随机训练大小,可以使用随机生成器,然后将其用作训练集大小的一部分


使用以下命令:

from sklearn.model_selection import train_test_split
import random
import numpy as np

X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
y = np.array([1, 2, 1, 2, 1, 2])

for i in range(30):
    # the training size will vary between 0.2 and 0.5 randomly
    random_portion = round(random.uniform(0.2, 0.5) , 3)
    X_train, X_test, y_train, y_test =train_test_split(X,y, train_size= random_portion, shuffle=True)
您可以相应地修改代码


编辑1 您可以根据需要仅使用
numpy
执行相同操作。

from sklearn.model_selection import train_test_split
import numpy as np

X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
y = np.array([1, 2, 1, 2, 1, 2])

for i in range(30):
    random_portion = round(np.random.rand(),3)
    X_train, X_test, y_train, y_test =train_test_split(X,y, train_size= random_portion, shuffle=True)

你能解释一下相同的结果是什么意思吗?你做了什么来检查它们是否相同?是的,我必须用不同的分区重复这个
30次。然而,我把training设置为
.4
并将shuffle设置为true,我认为这可能会将training更改为random,但训练集仍然是
40%
的数据。你能尝试用生成的随机数更改random\u state参数吗?你的意思是:
X\u train,X\u test,y\u train,y\u test=train\u test\u split(df,y,序列大小=0.4,随机播放=False,随机播放状态=42)
?下次它会随机改变训练大小吗?你是在
for
循环中进行模型拟合和评分吗?谢谢@seralouk。我的问题是我不能导入
random
ShuffleSplit
。只需
numpy
训练测试分割
。这是任务还是任务?你为什么不能导入随机?请参阅我的回答中的编辑1谢谢。是的,这是一项作业。我也无法导入
shuffle
。这与最初的问题完全不同。如果您希望在所有迭代中拥有相同部分的训练数据,请指定
train\u size=0.5
shuffle=True
。这样,对于每个迭代迭代你将有50%的训练数据和样本将随机洗牌。