Skip to content

Commit

Permalink
Merge pull request #64 from Techtonique/partial-fit
Browse files Browse the repository at this point in the history
Partial fit
  • Loading branch information
thierrymoudiki authored Oct 26, 2024
2 parents c79655a + b0f2448 commit 91538a8
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 88 deletions.
55 changes: 55 additions & 0 deletions examples/custom_partial_fit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import nnetsauce as ns
import numpy as np
from sklearn.datasets import load_diabetes, load_iris, load_wine
from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDRegressor, SGDClassifier, Perceptron
from sklearn.gaussian_process.kernels import Matern
from time import time

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

print(f"\n\n ----- iris ----- \n\n")

data = load_iris()

X = data.data
y= data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state = 123)


regr = ns.CustomClassifier(SGDClassifier(), n_clusters=0)
start = time()
regr.fit(X_train, y_train)
print(f"Elapsed: {time() - start}s")
preds = regr.predict(X_test)
print(regr.score(X_test, y_test))
regr.partial_fit(X_test[0, :], y_test[0])
preds = regr.predict(X_test[1:, :])
print(regr.score(X_test[1:, :], y_test[1:]))
regr.partial_fit(X_test[1, :], y_test[1])
preds = regr.predict(X_test[2:, :])
print(regr.score(X_test[2:, :], y_test[2:]))


print(f"\n\n ----- diabetes ----- \n\n")

data = load_diabetes()
X = data.data
y= data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state = 123)


regr = ns.CustomRegressor(SGDRegressor(), n_clusters=0, n_hidden_features=10)
start = time()
regr.fit(X_train, y_train)
print(f"Elapsed: {time() - start}s")
preds = regr.predict(X_test)
print(np.sqrt(np.mean((preds - y_test)**2)))
regr.partial_fit(X_test[0, :], y_test[0])
preds = regr.predict(X_test[1:, :])
print(np.sqrt(np.mean((preds - y_test[1:])**2)))
regr.partial_fit(X_test[1, :], y_test[1])
preds = regr.predict(X_test[2:, :])
print(np.sqrt(np.mean((preds - y_test[2:])**2)))

4 changes: 2 additions & 2 deletions examples/lazy_custom_deep_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"RandomForestRegressor"])

start = time()
models, predictions = clf.fit(X_train, X_test, y_train, y_test)
models = clf.fit(X_train, X_test, y_train, y_test)
print(f"\nElapsed: {time() - start} seconds\n")

print(models)
Expand All @@ -44,7 +44,7 @@
n_hidden_features=3)

start = time()
models, predictions = clf.fit(X_train, X_test, y_train, y_test)
models = clf.fit(X_train, X_test, y_train, y_test)
print(f"\nElapsed: {time() - start} seconds\n")

print(models)
Expand Down
8 changes: 4 additions & 4 deletions examples/lazy_custom_deep_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
verbose=0, ignore_warnings=True, estimators=["ExtraTreesRegressor",
"RandomForestRegressor",
"LassoLarsIC"])
models, predictions = regr2.fit(X_train, X_test, y_train, y_test)
models = regr2.fit(X_train, X_test, y_train, y_test)
model_dictionary = regr2.provide_models(X_train, X_test, y_train, y_test)
print(models)
print(model_dictionary["DeepCustomRegressor(LassoLarsIC)"])

regr = ns.LazyDeepRegressor(n_layers=3, n_hidden_features=2,
verbose=0, ignore_warnings=True)
models, predictions = regr.fit(X_train, X_test, y_train, y_test)
models = regr.fit(X_train, X_test, y_train, y_test)
model_dictionary = regr.provide_models(X_train, X_test, y_train, y_test)
print(models)
print(model_dictionary["DeepCustomRegressor(LassoLarsIC)"])
Expand All @@ -44,14 +44,14 @@
"RandomForestRegressor",
"LassoLarsIC"],
preprocess=True)
models, predictions = regr2.fit(X_train, X_test, y_train, y_test)
models = regr2.fit(X_train, X_test, y_train, y_test)
model_dictionary = regr2.provide_models(X_train, X_test, y_train, y_test)
print(models)
print(model_dictionary["DeepCustomRegressor(LassoLarsIC)"])

regr = ns.LazyDeepRegressor(n_layers=3, n_hidden_features=2,
verbose=0, ignore_warnings=True, preprocess=True)
models, predictions = regr.fit(X_train, X_test, y_train, y_test)
models = regr.fit(X_train, X_test, y_train, y_test)
model_dictionary = regr.provide_models(X_train, X_test, y_train, y_test)
print(models)
print(model_dictionary["DeepCustomRegressor(LassoLarsIC)"])
Expand Down
6 changes: 3 additions & 3 deletions examples/lazy_custom_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
regr = ns.LazyRegressor(verbose=0, ignore_warnings=True, custom_metric=None,
preprocess=True,
estimators=["RandomForestRegressor", "ExtraTreesRegressor"])
models, predictions = regr.fit(X_train, X_test, y_train, y_test)
models = regr.fit(X_train, X_test, y_train, y_test)
model_dictionary = regr.provide_models(X_train, X_test, y_train, y_test)
print(models)
print(model_dictionary["CustomRegressor(RandomForestRegressor)"])

regr = ns.LazyRegressor(verbose=0, ignore_warnings=True, custom_metric=None,
preprocess=True)
models, predictions = regr.fit(X_train, X_test, y_train, y_test)
models = regr.fit(X_train, X_test, y_train, y_test)
model_dictionary = regr.provide_models(X_train, X_test, y_train, y_test)
print(models)
print(model_dictionary["CustomRegressor(RandomForestRegressor)"])

regr2 = ns.LazyRegressor(verbose=0, ignore_warnings=True, custom_metric=None,
preprocess=False)
models, predictions = regr2.fit(X_train, X_test, y_train, y_test)
models = regr2.fit(X_train, X_test, y_train, y_test)
model_dictionary = regr2.provide_models(X_train, X_test, y_train, y_test)
print(models)
print(model_dictionary["CustomRegressor(RandomForestRegressor)"])
10 changes: 5 additions & 5 deletions examples/lazy_deepmts.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
lags = 20, n_hidden_features=7, n_clusters=2,
show_progress=False, preprocess=False,
estimators=["ElasticNetCV", "RidgeCV"])
modelss = regr_mts3.fit(df_train, df_test)
models = regr_mts3.fit(df_train, df_test)
model_dictionary = regr_mts3.provide_models(df_train, df_test)
print(models)
print(model_dictionary["DeepMTS(ElasticNetCV)"])
Expand All @@ -50,7 +50,7 @@
n_layers=3,
lags = 20, n_hidden_features=7, n_clusters=2,
show_progress=False, preprocess=False)
modelss = regr_mts.fit(df_train, df_test)
models = regr_mts.fit(df_train, df_test)
model_dictionary = regr_mts.provide_models(df_train, df_test)
print(models)
print(model_dictionary["DeepMTS(LinearSVR)"])
Expand All @@ -59,7 +59,7 @@
n_layers=3,
lags = 20, n_hidden_features=7, n_clusters=2,
show_progress=False, preprocess=True)
modelss = regr_mts2.fit(df_train, df_test)
models = regr_mts2.fit(df_train, df_test)
model_dictionary = regr_mts.provide_models(df_train, df_test)
print(models)
print(model_dictionary["DeepMTS(LinearSVR)"])
Expand All @@ -69,7 +69,7 @@
lags = 20, n_hidden_features=7, n_clusters=2,
replications=10, kernel="gaussian",
show_progress=False, preprocess=False)
modelss = regr_mts3.fit(df_train, df_test)
models = regr_mts3.fit(df_train, df_test)
model_dictionary = regr_mts3.provide_models(df_train, df_test)
print(models)
print(models["WINKLERSCORE"])
Expand All @@ -81,7 +81,7 @@
replications=100, kernel="gaussian",
type_pi="scp2-kde",
show_progress=False, preprocess=False)
modelss = regr_mts3.fit(df2_train, df2_test)
models = regr_mts3.fit(df2_train, df2_test)
model_dictionary = regr_mts3.provide_models(df2_train, df2_test)
print(models)
print(models["WINKLERSCORE"])
Expand Down
8 changes: 4 additions & 4 deletions examples/lazy_deepmts_horizon.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
show_progress=False, preprocess=False,
estimators=["ElasticNetCV", "RidgeCV"],
h=5, )
models, predictions = regr_mts3.fit(df_train, df_test)
models = regr_mts3.fit(df_train, df_test)
model_dictionary = regr_mts3.provide_models(df_train, df_test)
print(models)
print(model_dictionary["DeepMTS(ElasticNetCV)"])
Expand All @@ -56,7 +56,7 @@
n_layers=3,
show_progress=False, preprocess=False,
h=5, )
models, predictions = regr_mts.fit(df_train, df_test)
models = regr_mts.fit(df_train, df_test)
model_dictionary = regr_mts.provide_models(df_train, df_test)
print(models)
print(model_dictionary["DeepMTS(LinearSVR)"])
Expand All @@ -69,7 +69,7 @@
replications=10, kernel="gaussian",
show_progress=False, preprocess=False,
h=5, )
models, predictions = regr_mts3.fit(df_train, df_test)
models = regr_mts3.fit(df_train, df_test)
model_dictionary = regr_mts3.provide_models(df_train, df_test)
print(models)
print(models[['WINKLERSCORE', 'COVERAGE']])
Expand All @@ -83,7 +83,7 @@
type_pi="scp2-kde",
show_progress=False, preprocess=False,
h=5, )
models, predictions = regr_mts3.fit(df2_train, df2_test)
models = regr_mts3.fit(df2_train, df2_test)
model_dictionary = regr_mts3.provide_models(df2_train, df2_test)
print(models)
print(models[['WINKLERSCORE', 'COVERAGE']])
Expand Down
14 changes: 7 additions & 7 deletions examples/lazy_mts.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
type_pi = "kde",
replications=100, kernel="gaussian",
show_progress=True, preprocess=False)
models, predictions = regr_mts4.fit(df_train, df_test)
models = regr_mts4.fit(df_train, df_test)
model_dictionary = regr_mts4.provide_models(df_train, df_test)
print(models[['WINKLERSCORE', 'COVERAGE']])

Expand All @@ -46,7 +46,7 @@
kernel="gaussian",
replications=100,
show_progress=True, preprocess=False)
models, predictions = regr_mts5.fit(df_train, df_test)
models = regr_mts5.fit(df_train, df_test)
model_dictionary = regr_mts5.provide_models(df_train, df_test)
print(models[['WINKLERSCORE', 'COVERAGE']])

Expand All @@ -56,7 +56,7 @@
kernel="tophat",
replications=100,
show_progress=True, preprocess=False)
models, predictions = regr_mts5.fit(df_train, df_test)
models = regr_mts5.fit(df_train, df_test)
model_dictionary = regr_mts5.provide_models(df_train, df_test)
print(models[['WINKLERSCORE', 'COVERAGE']])

Expand All @@ -66,7 +66,7 @@
kernel="tophat",
replications=100,
show_progress=True, preprocess=False)
models, predictions = regr_mts6.fit(df_train, df_test)
models = regr_mts6.fit(df_train, df_test)
model_dictionary = regr_mts6.provide_models(df_train, df_test)
print(models[['WINKLERSCORE', 'COVERAGE']])

Expand All @@ -90,7 +90,7 @@
replications = 100, kernel="gaussian",
estimators = ["Ridge", "Lasso", "LarsCV", "LassoCV", "LassoLarsCV"],
show_progress=True, preprocess=False)
models, predictions = regr_mts4.fit(df_train, df_test)
models = regr_mts4.fit(df_train, df_test)
model_dictionary = regr_mts4.provide_models(df_train, df_test)
print(models[['WINKLERSCORE', 'COVERAGE']])

Expand All @@ -100,15 +100,15 @@
replications=100, kernel="gaussian",
estimators = ["Ridge", "Lasso", "LarsCV", "LassoCV", "LassoLarsCV"],
show_progress=False, preprocess=False)
models, predictions = regr_mts5.fit(df_train, df_test)
models = regr_mts5.fit(df_train, df_test)
model_dictionary = regr_mts5.provide_models(df_train, df_test)
print(models[['WINKLERSCORE', 'COVERAGE']])

regr_mts5 = ns.LazyDeepMTS(verbose=1, ignore_warnings=True, custom_metric=None,
lags = 20, n_hidden_features=7, n_clusters=2,
type_pi = "gaussian",
show_progress=False, preprocess=False)
models, predictions = regr_mts5.fit(df_train, df_test)
models = regr_mts5.fit(df_train, df_test)
model_dictionary = regr_mts5.provide_models(df_train, df_test)
print(models[['WINKLERSCORE', 'COVERAGE']])

18 changes: 9 additions & 9 deletions examples/lazy_mts_horizon.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
replications=100, kernel="gaussian",
show_progress=True, preprocess=False,
h=5, )
models, predictions = regr_mts4.fit(df_train, df_test)
models = regr_mts4.fit(df_train, df_test)
model_dictionary = regr_mts4.provide_models(df_train, df_test)
print(models)
print(models[['WINKLERSCORE', 'COVERAGE']])
Expand All @@ -50,7 +50,7 @@
replications=100,
show_progress=True, preprocess=False,
h=5, )
models, predictions = regr_mts5.fit(df_train, df_test)
models = regr_mts5.fit(df_train, df_test)
model_dictionary = regr_mts5.provide_models(df_train, df_test)
print(models)
print(models[['WINKLERSCORE', 'COVERAGE']])
Expand All @@ -64,7 +64,7 @@
replications=100,
show_progress=True, preprocess=False,
h=5, )
models, predictions = regr_mts5.fit(df_train, df_test)
models = regr_mts5.fit(df_train, df_test)
model_dictionary = regr_mts5.provide_models(df_train, df_test)
print(models)
print(models[['WINKLERSCORE', 'COVERAGE']])
Expand All @@ -76,7 +76,7 @@
type_pi="scp2-block-bootstrap",
replications=100, show_progress=True,
preprocess=False, h=5 )
models, predictions = regr_mts6.fit(df_train, df_test)
models = regr_mts6.fit(df_train, df_test)
model_dictionary = regr_mts6.provide_models(df_train, df_test)
print(models)
print(models[['WINKLERSCORE', 'COVERAGE']])
Expand All @@ -102,7 +102,7 @@
estimators = ["Ridge", "Lasso", "LarsCV", "LassoCV", "LassoLarsCV"],
show_progress=True, preprocess=False,
h=5, )
models, predictions = regr_mts4.fit(df_train, df_test)
models = regr_mts4.fit(df_train, df_test)
model_dictionary = regr_mts4.provide_models(df_train, df_test)
print(models)
print(models[['WINKLERSCORE', 'COVERAGE']])
Expand All @@ -116,7 +116,7 @@
estimators = ["Ridge", "Lasso", "LarsCV", "LassoCV", "LassoLarsCV"],
show_progress=False, preprocess=False,
h=5, )
models, predictions = regr_mts5.fit(df_train, df_test)
models = regr_mts5.fit(df_train, df_test)
model_dictionary = regr_mts5.provide_models(df_train, df_test)
print(models[['WINKLERSCORE', 'COVERAGE']])

Expand All @@ -127,7 +127,7 @@
#type_pi = "gaussian",
show_progress=False, preprocess=False,
h=5, )
models, predictions = regr_mts5.fit(df_train, df_test)
models = regr_mts5.fit(df_train, df_test)
model_dictionary = regr_mts5.provide_models(df_train, df_test)
print(models)

Expand All @@ -139,7 +139,7 @@
estimators = ["Ridge", "Lasso", "LarsCV", "LassoCV", "LassoLarsCV"],
show_progress=False, preprocess=False,
h=5, )
models, predictions = regr_mts5.fit(df_train, df_test, per_series=True)
models = regr_mts5.fit(df_train, df_test, per_series=True)
model_dictionary = regr_mts5.provide_models(df_train, df_test)
print(models)

Expand All @@ -151,6 +151,6 @@
#type_pi = "gaussian",
show_progress=False, preprocess=False,
h=5, )
models, predictions = regr_mts5.fit(df_train, df_test, per_series=True)
models = regr_mts5.fit(df_train, df_test, per_series=True)
model_dictionary = regr_mts5.provide_models(df_train, df_test)
print(models)
Loading

0 comments on commit 91538a8

Please sign in to comment.