- Notifications
You must be signed in to change notification settings - Fork328
add the california housing example#16
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletionsREADME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletionsexamples/california_housing/run.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
-- This example trains models on the sklean california_housing dataset | ||
-- which is a copy of the test set from the StatLib repository | ||
-- https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html | ||
-- | ||
-- This demonstrates using a table with individual columns as features | ||
-- for regression. | ||
SELECT pgml.load_dataset('california_housing'); | ||
-- view the dataset | ||
SELECT * from pgml.california_housing; | ||
-- train a simple model to classify the data | ||
SELECT pgml.train('California Housing Prediction', 'regression', 'pgml.california_housing', 'target'); | ||
-- check out the predictions | ||
SELECT target, pgml.predict('California Housing Prediction', ARRAY[median_income, house_age, avg_rooms, avg_bedrooms, population, avg_occupants, latitude, longitude]) AS prediction | ||
FROM pgml.california_housing | ||
LIMIT 10; | ||
-- -- train some more models with different algorithms | ||
SELECT pgml.train('California Housing Prediction', 'regression', 'pgml.california_housing', 'target', 'svm'); | ||
SELECT pgml.train('California Housing Prediction', 'regression', 'pgml.california_housing', 'target', 'random_forest'); | ||
SELECT pgml.train('California Housing Prediction', 'regression', 'pgml.california_housing', 'target', 'gradient_boosting_trees'); | ||
-- TODO SELECT pgml.train('California Housing Prediction', 'regression', 'pgml.california_housing', 'target', 'dense_neural_network'); | ||
-- -- check out all that hard work | ||
SELECT * FROM pgml.trained_models; | ||
-- deploy the random_forest model for prediction use | ||
SELECT pgml.deploy('California Housing Prediction', 'random_forest'); | ||
-- check out that throughput | ||
SELECT * FROM pgml.deployed_models; | ||
-- do some hyper param tuning | ||
-- TODO SELECT pgml.hypertune(100, 'California Housing Prediction', 'regression', 'pgml.california_housing', 'target', 'gradient_boosted_trees'); | ||
-- deploy the "best" model for prediction use | ||
SELECT pgml.deploy('California Housing Prediction', 'best_fit'); | ||
-- check out the improved predictions | ||
SELECT target, pgml.predict('California Housing Prediction', ARRAY[median_income, house_age, avg_rooms, avg_bedrooms, population, avg_occupants, latitude, longitude]) AS prediction | ||
FROM pgml.california_housing | ||
LIMIT 10; |
3 changes: 3 additions & 0 deletionsexamples/digits/run.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
28 changes: 26 additions & 2 deletionspgml/pgml/datasets.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,45 @@ | ||
import plpy | ||
import sklearn.datasets | ||
from pgml.sql import q | ||
from pgml.exceptions import PgMLException | ||
def load(source: str): | ||
if source == "digits": | ||
load_digits() | ||
elif source == "california_housing": | ||
load_california_housing() | ||
else: | ||
raise PgMLException(f"Invalid dataset name: {source}. Valid values are ['digits'].") | ||
return "OK" | ||
def load_digits(): | ||
dataset =sklearn.datasets.load_digits() | ||
a = plpy.execute("DROP TABLE IF EXISTS pgml.digits") | ||
a = plpy.execute("CREATE TABLE pgml.digits (image SMALLINT[], target INTEGER)") | ||
a = plpy.execute(f"""COMMENT ON TABLE pgml.digits IS {q(dataset["DESCR"])}""") | ||
for X, y in zip(dataset["data"], dataset["target"]): | ||
X = ",".join("%i" % x for x in list(X)) | ||
plpy.execute(f"""INSERT INTO pgml.digits (image, target) VALUES ('{{{X}}}', {y})""") | ||
def load_california_housing(): | ||
dataset = sklearn.datasets.fetch_california_housing() | ||
a = plpy.execute("DROP TABLE IF EXISTS pgml.california_housing") | ||
a = plpy.execute(""" | ||
CREATE TABLE pgml.california_housing ( | ||
median_income FLOAT4, -- median income in block group | ||
house_age FLOAT4, -- median house age in block group | ||
avg_rooms FLOAT4, -- average number of rooms per household | ||
avg_bedrooms FLOAT4, -- average number of bedrooms per household | ||
population FLOAT4, -- block group population | ||
avg_occupants FLOAT4, -- average number of household members | ||
latitude FLOAT4, -- block group latitude | ||
longitude FLOAT4, -- block group longitudetarget INTEGER | ||
target FLOAT4 | ||
)""") | ||
a = plpy.execute(f"""COMMENT ON TABLE pgml.california_housing IS {q(dataset["DESCR"])}""") | ||
for X, y in zip(dataset["data"], dataset["target"]): | ||
plpy.execute(f""" | ||
INSERT INTO pgml.california_housing (median_income, house_age, avg_rooms, avg_bedrooms, population, avg_occupants, latitude, longitude, target) | ||
VALUES ({q(X[0])}, {q(X[1])}, {q(X[2])}, {q(X[3])}, {q(X[4])}, {q(X[5])}, {q(X[6])}, {q(X[7])}, {q(y)})""") | ||
2 changes: 1 addition & 1 deletionsql/install.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.