Select the same number of samples from each class.

조회 수: 1 (최근 30일)
DuckDuck
DuckDuck 2018년 7월 6일
댓글: DuckDuck 2018년 7월 9일
Say I have a table with 2 columns, one column stores the value the other the class of the value. The classes are imbalanced. I want to randomly select the same number of samples from each class. Is there any easy way to do it in Matlab.
  댓글 수: 1
Jan
Jan 2018년 7월 6일
Please post some Matlab code, which creates a small data set. I cannot guess, how you store "values" and "classes". What does "imbalanced" mean here?

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2018년 7월 6일
% create some random data for demo
K = 3 ; % number of groups
N = 5000 ;
x = rand(N,1) ;
y = rand(N,1) ;
% apply kmeans to divide points into 3 groups
idx = kmeans([x,y],K) ;
A = [x y idx] ; % the data with three classes
% Get number of data in each class
ng1 = nnz(idx==1) ; % number of points in group 1
ng2 = nnz(idx==2) ; % number of points in group 1
ng3 = nnz(idx==3) ; % number of points in group 1
% select data for each group
N = min([ng1 ng2 ng3]) ;
iwant = cell(K,1) ;
for i = 1:K
G=A(A(:,3)==i,:) ; % group data
idx = randperm(size(G,1),N) ;
iwant{i} = G(idx) ;
end
  댓글 수: 1
DuckDuck
DuckDuck 2018년 7월 9일
Thanks, actually I was missing randperm part. That's why I asked.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by