equalisedata.m.svn-base

来自「a function inside machine learning」· SVN-BASE 代码 · 共 34 行

SVN-BASE
34
字号
function [newX, newY] = equaliseData(X, y, params); 
%Make sure that the ratio of positive to negative examples is the same (in
%general) across any seqencial subset of the data

if ~binaryLabels(y)
    newX = X; 
    newY = y; 
    return; 
end 

numExamples = size(X, 1); 
numFeatures = size(X, 2);

positiveIndices = find(y == 1);
negativeIndices = find(y == -1); 
numPositives = length(positiveIndices); 
numNegatives = length(negativeIndices); 

newPositiveIndices = floor((1:numPositives)*(numExamples/numPositives));
newNegativeIndices = setdiff(1:numExamples, newPositiveIndices);

if issparse(X) 
    newX = sparse(numExamples, numFeatures); 
else 
    newX = zeros(numExamples, numFeatures); 
end 

newY = zeros(numExamples, 1); 

newX(newPositiveIndices, :) = X(positiveIndices, :); 
newX(newNegativeIndices, :) = X(negativeIndices, :); 
newY(newPositiveIndices) = y(positiveIndices); 
newY(newNegativeIndices) = y(negativeIndices); 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?