|
1 |
| -## PostgresML |
| 1 | +# PostgresML |
2 | 2 |
|
3 |
| -PostgresML aims to be the easiest way to gain value from machine learning. Anyone with a basic understanding of SQL should be able to build and deploy models to production, while receiving the benefits of a high performance machine learning platform. PostgresML leverages state of the art algorithms with built in best practices, without having to setup additional infrastructure or learn additional programming languages. |
| 3 | +PostgresML aims to be the easiest way to gain value from machine learning. Anyone with a basic understanding of SQL should be able to build and deploy models to production, while receiving the benefits of a high performance machine learning platform. PostgresML leverages state of the art algorithms with built in best practices, without having to setup additional infrastructure or learn additional programming languages. |
4 | 4 |
|
5 |
| -Getting started is as easy as creating a `table` or `view` that holds the training data, and then registering that with PostgresML. |
| 5 | +## Installation |
| 6 | + |
| 7 | +### Docker |
| 8 | + |
| 9 | +The quickest way to try this out is with Docker. If you're on Mac, install [Docker for Mac](https://docs.docker.com/desktop/mac/install/). If you're on Linux (e.g. Ubuntu/Debian), you can follow [these instructions](https://docs.docker.com/engine/install/ubuntu/). For Ubuntu, also install `docker-compose`. Docker and this image also works on Windows/WSL2. |
| 10 | + |
| 11 | +Starting up a local system is then as simple as: |
| 12 | + |
| 13 | +```bash |
| 14 | +$ docker-compose up -d |
| 15 | +``` |
| 16 | + |
| 17 | +PostgresML will run on port 5433, just in case you already have Postgres running. Then to connect, run: |
| 18 | + |
| 19 | +```bash |
| 20 | +$ psql -h 127.0.0.1 -p 5433 -U root |
| 21 | +``` |
| 22 | + |
| 23 | +To validate it works, you can execute this query and you should see this result: |
6 | 24 |
|
7 | 25 | ```sql
|
8 |
| -SELECT pgml.model_regression('Red Wine Quality', training_data_table_or_view_name, label_column_name); |
| 26 | +SELECT pgml.version(); |
| 27 | + |
| 28 | + version |
| 29 | +--------- |
| 30 | + 0.1 |
| 31 | +(1 row) |
9 | 32 | ```
|
10 | 33 |
|
11 |
| -And predict novel datapoints: |
| 34 | +### Mac OS (native) |
| 35 | + |
| 36 | +If you want want to use Docker, a native installation is available. We recommend you use [Postgres.app](https://postgresapp.com/) because it comes with PL/Python, the extension we rely on, built into the installation. Once you have Postgres.app running, you'll need to install the Python framework. Mac OS has multiple distributions of Python, namely one from Brew and one from the Python community (Python.org); |
| 37 | +Postgres.app and PL/Python depend on the community one. The following versions of Python and Postgres.app are compatible: |
| 38 | + |
| 39 | +| **PostgreSQL version** | **Python version** | **Download link** | |
| 40 | +|------------------------|--------------------|-----------------------------------------------------------------------------------------| |
| 41 | +| 14 | 3.9 | [Python 3.9 64-bit](https://www.python.org/ftp/python/3.9.12/python-3.9.12-macos11.pkg) | |
| 42 | +| 13 | 3.8 | [Python 3.8 64-bit](https://www.python.org/ftp/python/3.8.10/python-3.8.10-macos11.pkg) | |
| 43 | + |
| 44 | +All Python.org installers for Mac OS are [available here](https://www.python.org/downloads/macos/). You can also get more details about this in the Postgres.app [documentation](https://postgresapp.com/documentation/plpython.html). |
| 45 | + |
| 46 | +#### Python package |
| 47 | + |
| 48 | +To use our Python package inside Postgres, we need to install it into the global Python package space. Depending on which version of Python you installed in the previous step, |
| 49 | +use its correspoding pip executable. Since Python was installed as a framework, sudo (root) is not required. |
| 50 | + |
| 51 | +For PostgreSQL 14, use Python & Pip 3.9: |
| 52 | + |
| 53 | +```bash |
| 54 | +$ pip3.9 install pgml |
| 55 | +``` |
| 56 | + |
| 57 | +#### PL/Python functions |
| 58 | + |
| 59 | +Finally to interact with the package, install our functions and supporting tables into the database: |
| 60 | + |
| 61 | +```bash |
| 62 | +$ psql -f sql/install.sql |
| 63 | +``` |
| 64 | + |
| 65 | +If everything works, you should be able to run this successfully: |
| 66 | + |
| 67 | +```bash |
| 68 | +$ psql -c 'SELECT pgml.version()' |
| 69 | +``` |
| 70 | + |
| 71 | +### Ubuntu/Debian |
| 72 | + |
| 73 | +Each Ubuntu/Debian distribution comes with its own version of PostgreSQL, the simplest way is to install it from Aptitude: |
| 74 | + |
| 75 | +```bash |
| 76 | +$ sudo apt-get install -y postgresql-plpython3-12 python3 python3-pip postgresql-12 |
| 77 | +``` |
| 78 | + |
| 79 | +Restart PostgreSQL: |
| 80 | + |
| 81 | +```bash |
| 82 | +$ sudo service postgresql restart |
| 83 | +``` |
| 84 | + |
| 85 | +Install our Python package and SQL functions: |
| 86 | + |
| 87 | +```bash |
| 88 | +$ sudo pip3 install pgml |
| 89 | +$ psql -f sql/install.sql |
| 90 | +``` |
| 91 | + |
| 92 | +If everything works, you should be able to run this successfully: |
| 93 | + |
| 94 | +```bash |
| 95 | +$ psql -c 'SELECT pgml.version()' |
| 96 | +``` |
| 97 | + |
| 98 | +## Working with PostgresML |
| 99 | + |
| 100 | +The two most important functions the framework provides are: |
| 101 | + |
| 102 | +1. `pgml.train(project_name TEXT, objective TEXT, relation_name TEXT, y_column_name TEXT)`, |
| 103 | +2. `pgml.predict(project_name TEXT, VARIADIC features DOUBLE PRECISION[])`. |
| 104 | + |
| 105 | +The first function trains a model, given a human-friendly project name, a `regression` or `classification` objective, a table or view name which contains the training and testing datasets, |
| 106 | +and the name of the `y` column containing the target values. The second function predicts novel datapoints, given the project name for an exiting model trained with `pgml.train`, |
| 107 | +and a list of features used to train that model. |
| 108 | + |
| 109 | +We'll be using the [Red Wine Quality](https://www.kaggle.com/datasets/uciml/red-wine-quality-cortez-et-al-2009) dataset from Kaggle for this example. You can find it in the `data` folder in this repository. |
| 110 | +You can import it into PostgresML running in Docker with this: |
| 111 | + |
| 112 | +```bash |
| 113 | +$ psql -f data/winequality-red.sql -p 5433 -U root -h 127.0.0.1 |
| 114 | +``` |
| 115 | + |
| 116 | +### Training a model |
| 117 | + |
| 118 | +Training a model is as easy as creating a table or a view that holds the training data, and then registering that with PostgresML: |
12 | 119 |
|
13 | 120 | ```sql
|
14 |
| -SELECT pgml.predict('Red Wine Quality', red_wines.*) |
15 |
| -FROM pgml.red_wines |
16 |
| -LIMIT 3; |
| 121 | +SELECT * FROM pgml.train('Red Wine Quality', 'regression', 'wine_quality_red', 'quality'); |
| 122 | + |
| 123 | + project_name | objective | status |
| 124 | +---------------------+------------+-------- |
| 125 | + Red Wine Quality | regression | deployed |
| 126 | +``` |
| 127 | + |
| 128 | +The function will snapshot the training data, train the model using multiple algorithms, automatically pick the best one, and make it available for predictions. |
| 129 | + |
| 130 | +### Predictions |
| 131 | + |
| 132 | +Predicting novel datapoints is as simple as: |
| 133 | + |
| 134 | +```sql |
| 135 | +SELECT pgml.predict('Red Wine Quality', 7.4, 0.66, 1.0, 1.8, 0.075, 17.0, 40.0, 0.9978, 3.58, 0.56, 9.4) AS quality; |
17 | 136 |
|
18 | 137 | quality
|
19 | 138 | ---------
|
20 |
| - 0.896432 |
21 |
| - 0.834822 |
22 |
| - 0.954502 |
23 |
| -(3 rows) |
| 139 | + 4.19 |
| 140 | +(1 row) |
24 | 141 | ```
|
25 | 142 |
|
26 | 143 | PostgresML similarly supports classification to predict discrete classes rather than numeric scores for novel data.
|
27 | 144 |
|
28 | 145 | ```sql
|
29 |
| -SELECT pgml.create_classification('Handwritten Digit Classifier', pgml.mnist_training_data, label_column_name); |
| 146 | +SELECT pgml.train('Handwritten Digit Classifier', 'classification', pgml.mnist_training_data, label_column_name); |
30 | 147 | ```
|
31 | 148 |
|
32 | 149 | And predict novel datapoints:
|
|
0 commit comments