Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

23f basic #120

Open
wants to merge 6 commits into
base: 23f_basic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/mipt-ml-course.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2,160 changes: 1,889 additions & 271 deletions homeworks/hw05_bagging_and_oob/assignment_bagging_and_oob.ipynb

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions homeworks/hw05_bagging_and_oob/bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def _generate_splits(self, data: np.ndarray):
self.indices_list = []
data_length = len(data)
for bag in range(self.num_bags):
# Your Code Here
self.indices_list.append(np.random.choice(len(data), size=data_length))

def fit(self, model_constructor, data, target):
'''
Expand All @@ -32,7 +32,7 @@ def fit(self, model_constructor, data, target):
self.models_list = []
for bag in range(self.num_bags):
model = model_constructor()
data_bag, target_bag = # Your Code Here
data_bag, target_bag = data[self.indices_list[bag]], target[self.indices_list[bag]]
self.models_list.append(model.fit(data_bag, target_bag)) # store fitted models here
if self.oob:
self.data = data
Expand All @@ -42,30 +42,32 @@ def predict(self, data):
'''
Get average prediction for every object from passed dataset
'''
# Your code here
return np.mean([model.predict(data) for model in self.models_list], axis=0)

def _get_oob_predictions_from_every_model(self):
'''
Generates list of lists, where list i contains predictions for self.data[i] object
from all models, which have not seen this object during training phase
'''
list_of_predictions_lists = [[] for _ in range(len(self.data))]
# Your Code Here

for i in range(len(self.data)):
for bag in range(self.num_bags):
if i not in self.indices_list[bag]:
list_of_predictions_lists[i].append(self.models_list[bag].predict([self.data[i]]).item())

self.list_of_predictions_lists = np.array(list_of_predictions_lists, dtype=object)

def _get_averaged_oob_predictions(self):
'''
Compute average prediction for every object from training set.
If object has been used in all bags on training phase, return None instead of prediction
'''
self._get_oob_predictions_from_every_model()
self.oob_predictions = # Your Code Here

self._get_oob_predictions_from_every_model()
self.oob_predictions = [np.mean(preds) for preds in self.list_of_predictions_lists]

def OOB_score(self):
'''
Compute mean square error for all objects, which have at least one prediction
'''
self._get_averaged_oob_predictions()
return # Your Code Here
return np.mean(np.nan_to_num((self.target - self.oob_predictions), 0) ** 2)
Loading