这里已经有一个关于如何通过train_test_split进行分层训练/测试分裂的描述(分层训练/在scikit-learn中测试分裂)以及如何通过np.split进行随机训练/验证/测试分裂的描述(如何将数据分成3组(训练,验证和测试)?).但是如何进行分层训练/验证/测试分割呢.
对于进行分层(类标签)训练/验证/测试分割而想到的最接近的近似值如下,但我怀疑有一种更好的方法可以在一个函数调用中或以更准确的方式实现:
假设我们想要进行60/20/20列车/验证/测试拆分,那么我目前的方法是首先进行60/40分层拆分,然后在前40个进行50/50 stratifeid拆分,最终得到一个60/20/20分层分裂.
from sklearn.cross_validation import train_test_split
SEED = 2000
x_train, x_validation_and_test, y_train, y_validation_and_test = train_test_split(x, y, test_size=.4, random_state=SEED)
x_validation, x_test, y_validation, y_test = train_test_split(x_validation_and_test, y_validation_and_test, test_size=.5, random_state=SEED)
Run Code Online (Sandbox Code Playgroud)
如果我的方法是正确的和/或你有更好的方法,请回来.
谢谢
有什么方法可以在 python sklearn 上的 train_test_split 上设置种子。我已将参数random_state设置为整数,但仍然无法重现结果。
提前致谢。
我正在使用这篇优秀的文章来学习机器学习。
https://stackabuse.com/python-for-nlp-multi-label-text-classification-with-keras/
作者将 X 和 y 数据拆分后对其进行了标记。
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.20, random_state=42
)
tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(X_train)
X_train = tokenizer.texts_to_sequences(X_train)
X_test = tokenizer.texts_to_sequences(X_test)
vocab_size = len(tokenizer.word_index) + 1
maxlen = 200
X_train = pad_sequences(X_train, padding="post", maxlen=maxlen)
X_test = pad_sequences(X_test, padding="post", maxlen=maxlen)
Run Code Online (Sandbox Code Playgroud)
如果我在使用 train_test_split 类之前标记它,我可以节省几行代码。
tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(X)
X_t = tokenizer.texts_to_sequences(X)
vocab_size = len(tokenizer.word_index) + 1
maxlen = 200
X = pad_sequences(X_t, padding="post", maxlen=maxlen)
Run Code Online (Sandbox Code Playgroud)
我只是想确认我的方法是正确的,我不希望脚本后面有任何惊喜。
我有一个包含以下列的数据文件
'customer', 'calibrat' - 校准样本 = 1; 验证样本 = 0; 'churn', 'churndep', '收入', 'mou',
数据文件包含大约 40000 行,其中 20000 行的校准值为 1。我想将此数据拆分为
X1 = data.loc[:, data.columns != 'churn']
y1 = data.loc[:, data.columns == 'churn']
from imblearn.over_sampling import SMOTE
os = SMOTE(random_state=0)
X1_train, X1_test, y1_train, y1_test = train_test_split(X1, y1, test_size=0.3, random_state=0)
Run Code Online (Sandbox Code Playgroud)
我想要的是,在我的 X1_train 中应该带有 calibrat =1 的校准数据,而在 X1_test 中应该带有用于验证的所有数据 calibrat = 0
python machine-learning logistic-regression train-test-split smote
在sklearn函数stratify中使用参数和函数有什么区别?他们不做同样的事情吗?train_test_splitStratifiedShuffleSplit
我正在使用数据表数据框。如何将数据帧拆分为训练数据集和测试数据集?
\n与 pandas dataframe 类似,我尝试使用train_test_split(dt_df,classes)sklearn.model_selection,但它不起作用并且出现错误。
import datatable as dt\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\n\ndt_df = dt.fread(csv_file_path)\nclasse = dt_df[:, "classe"])\ndel dt_df[:, "classe"])\n\nX_train, X_test, y_train, y_test = train_test_split(dt_df, classe, test_size=test_size)\nRun Code Online (Sandbox Code Playgroud)\n我收到以下错误: TypeError: 列选择器必须是整数或字符串,而不是 <class \'numpy.ndarray\'>
\n我尝试通过将数据帧转换为 numpy 数组来解决方法:
\nclasse = np.ravel(dt_df[:, "classe"])\ndt_df = dt_df.to_numpy()\nRun Code Online (Sandbox Code Playgroud)\n就像这样它可以工作,但是,我不知道是否有一种方法可以train_test_split像 pandas 数据帧一样正常工作。
编辑1: csv文件包含列字符串,并且值是无符号整数。使用print(dt_df)我们得到:
\n | CCC CCG CCU CCA CGC CGG CGU CGA CUC CUG \xe2\x80\xa6 \n---- + --- --- …
在绘制学习曲线以查看模型构建的进展情况时,我意识到验证准确性曲线从一开始就是一条直线。我想这可能只是由于将数据分割为训练集和验证集时出现了一些错误,但是当我迭代 100 次时,我仍然得到或多或少相同的图表。
我该如何解释这一点?这是怎么回事?我计算准确度分数的方式是否存在错误?
另外,准确性一开始就不高,我怀疑我的模型拟合不足,有什么明显的方法可以改进它吗?(我没有办法获得更多数据,那么特征工程就是这样吗?)
我使用下面的代码来计算准确性。
def learning_curve():
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.33)
training_sizes = (np.linspace(0.1, 1.0, 100) * len(X_train)).astype(int)
train_accuracy = []
valid_accuracy = []
clf = LogisticRegression(solver='liblinear')
for size in training_sizes:
clf.fit(X_train.iloc[:size], y_train.iloc[:size])
train_accuracy.append(clf.score(X_train.iloc[:size], y_train.iloc[:size]))
valid_accuracy.append(clf.score(X_valid, y_valid))
return training_sizes, train_accuracy, valid_accuracy
training_scores = []
cross_val_scores = []
for i in range(num_iter):
sizes, train_score, cross_valid_score = learning_curve()
training_scores.append(train_score)
cross_val_scores.append(cross_valid_score)
train_std = np.std(training_scores, axis=0)
train_mean = np.mean(training_scores, axis=0)
cv_std = np.std(cross_val_scores, axis=0)
cv_mean = np.mean(cross_val_scores, axis=0)
plt.plot(sizes, …Run Code Online (Sandbox Code Playgroud) python classification machine-learning cross-validation train-test-split
我正在尝试将数据集拆分为 Julia 中的训练子集和测试子集。到目前为止,我已经尝试使用MLDataUtils.jl包进行此操作,但是结果未达到预期。\n以下是我的发现和问题:
\n代码
\n# the inputs are\n\na = DataFrame(A = [1, 2, 3, 4,5, 6, 7, 8, 9, 10],\n B = [1, 2, 3, 4,5, 6, 7, 8, 9, 10],\n C = [1, 2, 3, 4,5, 6, 7, 8, 9, 10]\n )\nb = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n\nusing MLDataUtils\n(x1, y1), (x2, y2) = stratifiedobs((a,b), p=0.7)\n\n#Output of this operation is: (which is not the expectation)\nprintln("x1 is: $x1")\nx1 is:\n10\xc3\x973 DataFrame\n\xe2\x94\x82 Row \xe2\x94\x82 …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个安装脚本,能够为我设置一个工作区,这样我就不需要手动完成了.我开始在bash中这样做,但很快就意识到这样做不会那么好.
我的下一个想法是使用python来做,但似乎无法以正确的方式做到这一点.我的想法是创建一个列表(列表是一个带有所有数据文件路径的.txt文件),随机播放此列表,然后将每个文件移动到我的火车目录或测试目录,给定比率....
但这是python,并没有更简单的方法来实现它,似乎我正在做一个ullsary解决方法只是为了分割文件.
Bash代码:
# Partition data randomly into train and test.
cd ${PATH_TO_DATASET}
SPLIT=0.5 #train/test split
NUMBER_OF_FILES=$(ls ${PATH_TO_DATASET} | wc -l) ## number of directories in the dataset
even=1
echo ${NUMBER_OF_FILES}
if [ `echo "${NUMBER_OF_FILES} % 2" | bc` -eq 0 ]
then
even=1
echo "Even is true"
else
even=0
echo "Even is false"
fi
echo -e "${BLUE}Seperating files in to train and test set!${NC}"
for ((i=1; i<=${NUMBER_OF_FILES}; i++))
do
ran=$(python -c "import random;print(random.uniform(0.0, 1.0))")
if [[ ${ran} < …Run Code Online (Sandbox Code Playgroud) 我正在做一个数据不平衡的项目。我想使用随机欠采样来平衡数据。我很困惑是否应该在测试列车分割后进行欠采样,或者我应该先进行欠采样,然后再进行列车测试分割?
我的方法:
我的做法正确吗?如果我错了,请纠正我。
train-test-split ×10
python ×5
scikit-learn ×4
bash ×1
dataframe ×1
julia ×1
keras ×1
nlp ×1
pandas ×1
python-3.x ×1
resampling ×1
smote ×1
text-files ×1
tokenize ×1