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

Version - 0.0.24 #27

Merged
merged 9 commits into from
Feb 26, 2024
Merged
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
6 changes: 3 additions & 3 deletions IngeoML/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__version__ = '0.0.23'
__version__ = '0.0.24'

from IngeoML.bootstrap import CI, SE, StatisticSamples
from IngeoML.feature_selection import SelectFromModelCV
from IngeoML.optimizer import classifier, regression
from IngeoML.optimizer import classifier, regression
from IngeoML.analysis import feature_importance, predict_shuffle_inputs
51 changes: 51 additions & 0 deletions IngeoML/analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2024 Mario Graff Guerrero

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from sklearn.metrics import f1_score
from joblib import Parallel, delayed
import numpy as np
from IngeoML.utils import progress_bar


def feature_importance(model, X, y, predictions,
score=None, n_jobs: int=1):
"""Estimate the feature importance of the model"""
def compute_score(y, i):
return [score(y, j) for j in i]

if score is None:
score = lambda y, hy: f1_score(y, hy, average='macro')
base = score(y, model.predict(X))
hy = Parallel(n_jobs=n_jobs)(delayed(compute_score)(y, i)
for i in progress_bar(predictions))
hy = np.array(hy)
return base - hy


def predict_shuffle_inputs(model, X, times: int=100, n_jobs: int=1):
"""Predict X by shuffling all the inputs"""
def predict(model, X, rng, i):
inner = []
for _ in range(times):
rng.shuffle(X[:, i])
inner.append(model.predict(X))
return np.vstack(inner)

X_origin = X.copy()
rng = np.random.default_rng()
output = []
output = Parallel(n_jobs=n_jobs,
max_nbytes=None)(delayed(predict)(model, X, rng, i)
for i in progress_bar(range(X.shape[1]),
total=X.shape[1]))
return np.array(output)
279 changes: 0 additions & 279 deletions IngeoML/bootstrap.py

This file was deleted.

11 changes: 8 additions & 3 deletions IngeoML/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,20 @@ def encode(y, validation):
_ = validation[1]
validation[1] = encoder.transform(_.reshape(-1, 1))
return y_enc

def create_batches(batches):
if batches is None:
batches = Batches(size=512 if X.shape[0] >= 2048 else 256,
random_state=0)
batches_ = []
if classifier and class_weight == 'balanced':
if classifier:
splits = batches.split(y=y)
balance = balance_class_weights
if class_weight == 'balanced':
balance = balance_class_weights
elif callable(class_weight):
balance = class_weight
else:
raise NotImplementedError()
else:
splits = batches.split(X)
balance = lambda x: jnp.ones(x.shape[0]) / x.shape[0]
Expand Down
Loading
Loading