# StratifiedKfold

**URL:** https://discourse.julialang.org/t/stratifiedkfold/17957
**Category:** General Usage
**Tags:** question
**Created:** [November 24, 2018, 8:41pm UTC](https://discourse.julialang.org/t/stratifiedkfold/17957 "2018-11-24T20:41:58Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![lvoltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lvoltz/32/6213_2.png) [@lvoltz](https://discourse.julialang.org/u/lvoltz)
#### Post date: [November 24, 2018, 8:41pm UTC](https://discourse.julialang.org/t/stratifiedkfold/17957/1 "2018-11-24T20:41:58Z")

</div>

Hello, I am new in Julia and I am trying to figure out how to use StratifiedKfold. I am using Julia 0.6 and just Kfold now like:

Xdata=readdlm(“data.txt”)  
Ytarget=readdlm(“taget.txt”)  
folds = kfolds((Xdata,Ytarget),k=6)  
(Xtrain1,Ytrain1),(Xtest1,Ytest1)=folds[1] # the first fold and so on…

My data are a lot of numbers (matrix) and differs between 2 patterns -classes, but there are not labels or some number that tell which pattern is. What I know is that from line 1 to 50 is class 1 and from 51 to 90 is class 2.

But I dont understand how to used  
julia; collect(StratifiedKfold([:a, :a, :a, :b, :b, :c, :c, :a, :b, :c], 3))

What are that a, b and c? how I put this in my data? and where I call the Xdata and Ytarget?

Thanks a lot!

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [November 24, 2018, 11:57pm UTC](https://discourse.julialang.org/t/stratifiedkfold/17957/2 "2018-11-24T23:57:40Z")

</div>

I couldn’t really figure it out either, it does give a vector of random permutations though

```julia
using Random

x = length([:a, :a, :a, :b :b, :c]) ##6
rp = randperm(x) ## shuffled collect(1:6) vector

[Xdata[rp[1:4] , :] Ytarget[rp[1:4] ] ## shuffled 4 of 6 data & target

```

add: ah, they would be the target classes / labels, and don’t necessarily need to be symbols

* * *

```julia
folds = collect(StratifiedKfold(Ytarget, 6))
(Xtrain1, Ytrain1) = (Xdata[folds[1], :], Ytarget[folds[1]])
```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 25, 2018, 12:14am UTC](https://discourse.julialang.org/t/stratifiedkfold/17957/3 "2018-11-25T00:14:06Z")

</div>

👋🏼

Is this in MLBase.jl or ScikitLearn.jl?

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [November 25, 2018, 12:25am UTC](https://discourse.julialang.org/t/stratifiedkfold/17957/4 "2018-11-25T00:25:41Z")

</div>

I’m pretty sure it’s MLBase, the docs have the same example

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [November 25, 2018, 12:30am UTC](https://discourse.julialang.org/t/stratifiedkfold/17957/5 "2018-11-25T00:30:10Z")

</div>

> [@lvoltz](#):
>
> My data are a lot of numbers (matrix) and differs between 2 patterns -classes, but there are not labels or some number that tell which pattern is. What I know is that from line 1 to 50 is class 1 and from 51 to 90 is class 2.

A bit unrelated but could I ask why you want to use Stratified KFold here? it seems you have pretty balanced data?

With Sklearn, I think this does the job:

```julia
f(i) = ifelse(i<51, 1, 2)
y = [f(i) for i in 1:90]
using ScikitLearn
folds = ScikitLearn.CrossValidation.StratifiedKFold(y, n_folds=10)

X = randn(90, 20) # say 20 features
fold_1 = X[folds[1][1], :]

```

---

<div class="post-metadata">

### Author: ![lvoltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lvoltz/32/6213_2.png) [@lvoltz](https://discourse.julialang.org/u/lvoltz)
#### Post date: [November 25, 2018, 1:32am UTC](https://discourse.julialang.org/t/stratifiedkfold/17957/6 "2018-11-25T01:32:27Z")

</div>

Hi it is MLBase.jl

---

<div class="post-metadata">

### Author: ![lvoltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lvoltz/32/6213_2.png) [@lvoltz](https://discourse.julialang.org/u/lvoltz)
#### Post date: [November 25, 2018, 1:32am UTC](https://discourse.julialang.org/t/stratifiedkfold/17957/7 "2018-11-25T01:32:29Z")

</div>

Hi!! Yes it is almost balanced, but I have other cases that it is not. But anyway, if I use kfolds it does not gives me balanced data. If i dont shuffle my data kfolds gives me all class1 data, if I shuffle than sometimes gives 30% class 2 and 70% class1 and so on.  
I have a question, I need to take the Xdata and also the Ytarget because Xdata is my dataset and Ytarget is the labels for each samples. Can I include two matrices in StratifiedKfold? As I am using in kfold? thanks!!

---

<div class="post-metadata">

### Author: ![hulu](https://avatars.discourse-cdn.com/v4/letter/h/e36b37/32.png) [@hulu](https://discourse.julialang.org/u/hulu)
#### Post date: [July 6, 2020, 2:50am UTC](https://discourse.julialang.org/t/stratifiedkfold/17957/8 "2020-07-06T02:50:55Z")

</div>

I’ve just started learning Julia，here’s my try , my answer may not be too accurate.  
Symbol (:a ,:b and :c )is equivalent to a placeholder, but the length of symbols must be equal to the length of the data. The proportion of : a or : b has little effect on the result, but the number（:a or :b） must be greater than k. like this:

```julia
#houses is Array
index_row = [i for i = 1:size(houses)[1]]
index_a = [:a for i = 1:size(houses)[1]*0.5]
index_b = [:b for i = 1:size(houses)[1]*0.5]
index = vcat(index_a,index_b)
rows = collect(StratifiedKfold(index, 10))

# pick data
row = rows[1]
houses[row,:]

```
