在julia中将数据集拆分为训练和测试

在julia中将数据集拆分为训练和测试,julia,train-test-split,Julia,Train Test Split,我试图在Julia中将数据集拆分为训练和测试子集。到目前为止,我已经尝试使用该软件包进行此操作,但是,结果并没有达到预期。 以下是我的调查结果和问题: 代码 # the inputs are a = DataFrame(A = [1, 2, 3, 4,5, 6, 7, 8, 9, 10], B = [1, 2, 3, 4,5, 6, 7, 8, 9, 10], C = [1, 2, 3, 4,5, 6, 7, 8, 9, 10]

我试图在Julia中将数据集拆分为训练和测试子集。到目前为止,我已经尝试使用该软件包进行此操作,但是,结果并没有达到预期。 以下是我的调查结果和问题:

代码

# the inputs are

a = DataFrame(A = [1, 2, 3, 4,5, 6, 7, 8, 9, 10],
              B = [1, 2, 3, 4,5, 6, 7, 8, 9, 10],
              C = [1, 2, 3, 4,5, 6, 7, 8, 9, 10]
             )
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

using MLDataUtils
(x1, y1), (x2, y2) = stratifiedobs((a,b), p=0.7)

#Output of this operation is: (which is not the expectation)
println("x1 is: $x1")
x1 is:
10×3 DataFrame
│ Row │ A     │ B     │ C     │
│     │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1   │ 1     │ 1     │ 1     │
│ 2   │ 2     │ 2     │ 2     │
│ 3   │ 3     │ 3     │ 3     │
│ 4   │ 4     │ 4     │ 4     │
│ 5   │ 5     │ 5     │ 5     │
│ 6   │ 6     │ 6     │ 6     │
│ 7   │ 7     │ 7     │ 7     │
│ 8   │ 8     │ 8     │ 8     │
│ 9   │ 9     │ 9     │ 9     │
│ 10  │ 10    │ 10    │ 10    │

println("y1 is: $y1")
y1 is:
10-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

# but x2 is printed as 
(0×3 SubDataFrame, Float64[]) 

# while y2 as 
0-element view(::Array{Float64,1}, Int64[]) with eltype Float64)
然而,我希望这个数据集被分成两部分,其中70%的数据在列车中,30%在测试中。 请建议在julia中执行此操作的更好方法。
提前感谢。

也许MLJ.jl开发人员可以向您展示如何使用通用生态系统来实现这一点。以下是仅使用DataFrames.jl的解决方案:

julia> using DataFrames, Random

julia> a = DataFrame(A = [1, 2, 3, 4,5, 6, 7, 8, 9, 10],
                     B = [1, 2, 3, 4,5, 6, 7, 8, 9, 10],
                     C = [1, 2, 3, 4,5, 6, 7, 8, 9, 10]
                    )
10×3 DataFrame
 Row │ A      B      C     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │     1      1      1
   2 │     2      2      2
   3 │     3      3      3
   4 │     4      4      4
   5 │     5      5      5
   6 │     6      6      6
   7 │     7      7      7
   8 │     8      8      8
   9 │     9      9      9
  10 │    10     10     10

julia> function splitdf(df, pct)
           @assert 0 <= pct <= 1
           ids = collect(axes(df, 1))
           shuffle!(ids)
           sel = ids .<= nrow(df) .* pct
           return view(df, sel, :), view(df, .!sel, :)
       end
splitdf (generic function with 1 method)

julia> splitdf(a, 0.7)
(7×3 SubDataFrame
 Row │ A      B      C     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │     3      3      3
   2 │     4      4      4
   3 │     6      6      6
   4 │     7      7      7
   5 │     8      8      8
   6 │     9      9      9
   7 │    10     10     10, 3×3 SubDataFrame
 Row │ A      B      C     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │     1      1      1
   2 │     2      2      2
   3 │     5      5      5)
julia>使用数据帧,随机
julia>a=DataFrame(a=[1,2,3,4,5,6,7,8,9,10],
B=[1,2,3,4,5,6,7,8,9,10],
C=[1,2,3,4,5,6,7,8,9,10]
)
10×3数据帧
一行│ A、B、C
│ Int64 Int64 Int64
─────┼─────────────────────
1.│     1      1      1
2.│     2      2      2
3.│     3      3      3
4.│     4      4      4
5.│     5      5      5
6.│     6      6      6
7.│     7      7      7
8.│     8      8      8
9│     9      9      9
10│    10     10     10
julia>函数拆分df(df,pct)

@assert 0这是我在中为泛型数组实现它的方式:

或者,您可以将其划分为三个或更多部分,并且要划分的数组的数量也是可变的

默认情况下,它们也会被洗牌,但您可以使用Pkg-Pkg使用参数
shuffle
..

。使用车床添加(“车床”)。预处理:TrainTestSplit train,test=TrainTestSplit(df)

还有一个位置参数,在第二个位置,它占拆分的百分比。

我没有安装它,但它似乎详细解释了如何使用MLJ.jl执行拆分。Kaminski,感谢您的建议,它可以工作,令人惊讶的是,它比python中的sklearn版本快得多。干得好!!谢谢你的建议,非常感谢!!!
"""
    partition(data,parts;shuffle=true)
Partition (by rows) one or more matrices according to the shares in `parts`.
# Parameters
* `data`: A matrix/vector or a vector of matrices/vectors
* `parts`: A vector of the required shares (must sum to 1)
* `shufle`: Wheter to randomly shuffle the matrices (preserving the relative order between matrices)
 """
function partition(data::AbstractArray{T,1},parts::AbstractArray{Float64,1};shuffle=true) where T <: AbstractArray
        n = size(data[1],1)
        if !all(size.(data,1) .== n)
            @error "All matrices passed to `partition` must have the same number of rows"
        end
        ridx = shuffle ? Random.shuffle(1:n) : collect(1:n)
        return partition.(data,Ref(parts);shuffle=shuffle, fixedRIdx = ridx)
end

function partition(data::AbstractArray{T,N} where N, parts::AbstractArray{Float64,1};shuffle=true,fixedRIdx=Int64[]) where T
    n = size(data,1)
    nParts = size(parts)
    toReturn = []
    if !(sum(parts) ≈ 1)
        @error "The sum of `parts` in `partition` should total to 1."
    end
    ridx = fixedRIdx
    if (isempty(ridx))
       ridx = shuffle ? Random.shuffle(1:n) : collect(1:n)
    end
    current = 1
    cumPart = 0.0
    for (i,p) in enumerate(parts)
        cumPart += parts[i]
        final = i == nParts ? n : Int64(round(cumPart*n))
        push!(toReturn,data[ridx[current:final],:])
        current = (final +=1)
    end
    return toReturn
end
julia> x = [1:10 11:20]
julia> y = collect(31:40)
julia> ((xtrain,xtest),(ytrain,ytest)) = partition([x,y],[0.7,0.3])