2019-01-14 11:45:23 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Joel Therrien.
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-07-02 17:58:53 -07:00
|
|
|
package ca.joeltherrien.randomforest;
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2019-01-09 21:31:27 -08:00
|
|
|
import java.util.Random;
|
2018-07-02 17:58:53 -07:00
|
|
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
public class Bootstrapper<T> {
|
|
|
|
|
|
|
|
final private List<T> originalData;
|
|
|
|
|
2019-01-09 21:31:27 -08:00
|
|
|
public List<T> bootstrap(Random random){
|
2018-07-02 17:58:53 -07:00
|
|
|
final int n = originalData.size();
|
|
|
|
|
|
|
|
final List<T> newList = new ArrayList<>(n);
|
|
|
|
|
|
|
|
for(int i=0; i<n; i++){
|
2019-01-09 21:31:27 -08:00
|
|
|
final int index = random.nextInt(n);
|
2018-07-02 17:58:53 -07:00
|
|
|
|
|
|
|
newList.add(originalData.get(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
return newList;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|