Skip to content

Commit

Permalink
Merge pull request #37 from Techtonique/polyak
Browse files Browse the repository at this point in the history
Polyak
  • Loading branch information
thierrymoudiki authored Oct 8, 2024
2 parents 1f2baa1 + 541f7bb commit cd6552b
Show file tree
Hide file tree
Showing 30 changed files with 387 additions and 20,831 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
mlsauce/.DS_Store
*.html
*.so
*.c

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ for line in sys.stdin:
endef
export PRINT_HELP_PYSCRIPT

BROWSER := python -c "$$BROWSER_PYSCRIPT"
BROWSER := python3 -c "$$BROWSER_PYSCRIPT"

help:
@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)
@python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)

clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts

Expand All @@ -34,7 +34,8 @@ clean-build: ## remove build artifacts
find . -name '*.egg-info' -exec rm -fr {} +
find . -name '*.egg' -exec rm -rf {} +
find . -name '*.so' -exec rm -f {} +
find . -name '*.c' -exec rm -f {} +
find . -name '*.c' -exec rm -f {} +
find . -name '*.html' -exec rm -f {} +

clean-pyc: ## remove Python file artifacts
find . -name '*.pyc' -exec rm -f {} +
Expand Down Expand Up @@ -100,4 +101,4 @@ run-booster: ## run all boosting estimators examples with one command
find examples -maxdepth 2 -name "*boost_*.py" -exec python3 {} \;

run-lazy: ## run all lazy estimators examples with one command
find examples -maxdepth 2 -name "*lazy*.py" -exec python3 {} \;
find examples -maxdepth 2 -name "*lazy*.py" -exec python3 {} \;
17 changes: 17 additions & 0 deletions examples/genboost_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

print(obj.obj['loss'])

print(obj.obj['fit_obj_i'])

obj = ms.GenericBoostingClassifier(clf, tolerance=1e-2, n_clusters=2)
print(obj.get_params())
start = time()
Expand All @@ -61,6 +63,7 @@

print(obj.obj['loss'])

print(obj.obj['fit_obj_i'])

# data 2
print("\n")
Expand All @@ -84,6 +87,8 @@

print(obj.obj['loss'])

print(obj.obj['fit_obj_i'])

obj = ms.GenericBoostingClassifier(clf, n_clusters=3)
print(obj.get_params())
start = time()
Expand All @@ -95,6 +100,8 @@

print(obj.obj['loss'])

print(obj.obj['fit_obj_i'])

# data 3
print("\n")
print("iris data -----")
Expand All @@ -118,6 +125,8 @@

print(obj.obj['loss'])

print(obj.obj['fit_obj_i'])


print("\n")
print("GenericBoosting KRR -----")
Expand All @@ -134,6 +143,8 @@

print(obj.obj['loss'])

print(obj.obj['fit_obj_i'])

obj = ms.GenericBoostingClassifier(clf2, tolerance=1e-2, n_clusters=2)
print(obj.get_params())
start = time()
Expand All @@ -145,6 +156,8 @@

print(obj.obj['loss'])

print(obj.obj['fit_obj_i'])


# data 2
print("\n")
Expand All @@ -168,6 +181,8 @@

print(obj.obj['loss'])

print(obj.obj['fit_obj_i'])

obj = ms.GenericBoostingClassifier(clf2, n_clusters=3)
print(obj.get_params())
start = time()
Expand Down Expand Up @@ -202,3 +217,5 @@

print(obj.obj['loss'])

print(obj.obj['fit_obj_i'])

86 changes: 86 additions & 0 deletions examples/genboost_classifier_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import numpy as np
from sklearn.datasets import load_digits, load_breast_cancer, load_wine, load_iris
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.linear_model import Ridge, LinearRegression, RidgeCV, ElasticNetCV, LassoCV, Lasso
from time import time
from os import chdir
from sklearn import metrics
import os

print(f"\n ----- Running: {os.path.basename(__file__)}... ----- \n")

print(os.path.relpath(os.path.dirname(__file__)))

#wd="/workspace/mlsauce/mlsauce/examples"
#
#chdir(wd)

import mlsauce as ms

eta = 0.9


# data 1
regr1 = Ridge()


print("\n Example 1 --------------------------- \n")
wine = load_wine()
X = wine.data
y = wine.target
# split data into training test and test set
np.random.seed(15029)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2)

obj = ms.GenericBoostingClassifier(regr1)
print(obj.get_params())
start = time()
obj.fit(X_train, y_train)
print(time()-start)
print(f"obj.obj['fit_obj_i'][0]: {obj.obj['fit_obj_i'][0].coef_}")
start = time()
print(f"score init. {obj.score(X_test[3:,],
y_test[3:])}")
print(time()-start)
obj = obj.update(X_test[0,:], y_test[0], eta=eta)
obj = obj.update(X_test[1,:], y_test[1], eta=eta)
obj = obj.update(X_test[2,:], y_test[2], eta=eta)
print(f"score updated: {obj.score(X_test[3:,],
y_test[3:])}")
print(f"obj.obj['fit_obj_i'][0].coef_: {obj.obj['fit_obj_i'][0].coef_}")

print("\n Example 2 --------------------------- \n")

eta = 0.3

regr2 = Ridge()

dataset = load_iris()
X = dataset.data
y = dataset.target
# split data into training test and test set
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,
random_state=13)

obj = ms.GenericBoostingClassifier(regr2)
print(obj.get_params())
start = time()
obj.fit(X_train, y_train)
print(time()-start)
print(f"obj.obj['fit_obj_i'][0]: {obj.obj['fit_obj_i'][0].coef_}")
print(f"obj.obj['Ym']: {obj.obj['Ym']}")
start = time()
print(f"score init. {obj.score(X_test[5:,],
y_test[5:])}")
print(time()-start)
obj = obj.update(X_test[0,:], y_test[0], eta=eta)
obj = obj.update(X_test[1,:], y_test[1], eta=eta)
obj = obj.update(X_test[2,:], y_test[2], eta=eta)
obj = obj.update(X_test[3,:], y_test[3], eta=eta)
obj = obj.update(X_test[4,:], y_test[4], eta=eta)
print(f"score updated: {obj.score(X_test[5:,],
y_test[5:])}")
print(f"obj.obj['fit_obj_i'][0].coef_: {obj.obj['fit_obj_i'][0].coef_}")
print(f"obj.obj['Ym']: {obj.obj['Ym']}")
36 changes: 32 additions & 4 deletions examples/genboost_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import mlsauce as ms
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_diabetes
from sklearn.datasets import load_diabetes, fetch_california_housing
from sklearn.linear_model import Ridge, LinearRegression
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.tree import DecisionTreeRegressor
from time import time
Expand All @@ -21,7 +22,7 @@
print("\n")
print("diabetes data -----")

regr = DecisionTreeRegressor()
regr = LinearRegression()

diabetes = load_diabetes()
X = diabetes.data
Expand All @@ -31,6 +32,8 @@
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2)

print("\n Example 1 --------------------------- \n")

obj = ms.GenericBoostingRegressor(regr, col_sample=0.9, row_sample=0.9)
print(obj.get_params())
start = time()
Expand All @@ -39,9 +42,10 @@
start = time()
print(np.sqrt(np.mean(np.square(obj.predict(X_test) - y_test))))
print(time()-start)

print(obj.obj['loss'])

print("\n Example 2 --------------------------- \n")

obj = ms.GenericBoostingRegressor(regr, col_sample=0.9, row_sample=0.9, n_clusters=2)
print(obj.get_params())
start = time()
Expand All @@ -50,5 +54,29 @@
start = time()
print(np.sqrt(np.mean(np.square(obj.predict(X_test) - y_test))))
print(time()-start)

print(obj.obj['loss'])
print(obj.obj['fit_obj_i'])

print("\n Example 3 --------------------------- \n")

housing = fetch_california_housing()
n_samples = 500
X = housing.data[:n_samples]
y = housing.target[:n_samples]
# split data into training test and test set
np.random.seed(15029)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2)
obj = ms.GenericBoostingRegressor(regr, n_hidden_features=2, n_estimators=10)


obj.fit(X_train, y_train)
print(f"obj.obj['fit_obj_i'][0]: {obj.obj['fit_obj_i'][0].coef_}")
print(f"score: {np.sqrt(np.mean(np.square(obj.predict(X_test[3:10,:]) - y_test[3:10])))}")

obj = obj.update(X_test[0,:], y_test[0])
obj = obj.update(X_test[1,:], y_test[1])
obj = obj.update(X_test[2,:], y_test[2])
print(f"obj.obj['fit_obj_i'][0]: {obj.obj['fit_obj_i'][0].coef_}")
print(f"preds: {obj.predict(X_test[3:10,:])}")
print(f"score: {np.sqrt(np.mean(np.square(obj.predict(X_test[3:10,:]) - y_test[3:10])))}")
4 changes: 2 additions & 2 deletions examples/lazy_booster_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
print(models)

data = fetch_california_housing()
X = data.data
y= data.target
X = data.data[0:1000,:]
y= data.target[0:1000]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state = 123)

regr = ms.LazyBoostingRegressor(verbose=0, ignore_warnings=True,
Expand Down
7 changes: 0 additions & 7 deletions mlsauce-docs/index.html

This file was deleted.

Loading

0 comments on commit cd6552b

Please sign in to comment.