Data science with the Python SDK

Use the general matrixone-python-sdk for two complete workflows: SQL and pandas exploration, and training, evaluation and prediction writeback. Both include isolated synthetic inputs, runnable code and expected checks, without external model downloads or business-table changes.

Where work executes

Layer

Work in these tutorials

MatrixOne

Store facts, filter, sort, GROUP BY, exact money aggregation, transactional model/prediction writeback

SDK / SQLAlchemy

Connections, bound parameters, Sessions, fetching and result objects

pandas / NumPy

DataFrame representation, types, missingness checks and training arrays

scikit-learn / Matplotlib

Train, evaluate and plot in the Python process; not database-side training or GPU acceleration

Install and run

Use Python 3.12. Download requirements.txt and these scripts into one directory:

  • sdk_common.py: connection, bound fetching and ownership-aware table cleanup.

  • sdk_eda.py: exact values, SQL aggregation, paging, chart and summary readback.

  • sdk_ml.py: chronological split, Pipeline, model manifest, predictions, rollback and readback.

python -m venv .venv
. .venv/bin/activate
python -m pip install -r requirements.txt

An administrator creates the isolated lab database:

CREATE DATABASE mo_sdk_science_lab;

Set MO_HOST, MO_PORT, MO_USER, MO_PASSWORD and MO_DATABASE=mo_sdk_science_lab. The account needs table creation/removal and read/write privileges only in this lab. Tutorial table names must not exist. Set MO_SSL_CA for synchronous TLS; localhost tests did not check remote certificates.

python sdk_eda.py
python sdk_ml.py

In a Notebook with the same directory/environment, use %run sdk_eda.py or %run sdk_ml.py, or import a module and call main(). The Agg backend saves PNG; load it from the printed path. No async event loop is needed.

Re-running, memory and resources

Scripts clean up only tables they successfully create. Existing names fail rather than being overwritten. Charts/CSV/JSON remain in a newly created local temporary directory printed by the script; lab SQL tables are removed at completion. Persistent deployments need separate result-table/run_id, privilege and retention policies.

Small results are buffered in memory. SDK Client.execute fetches all rows before constructing ResultSet; fetchmany is not server streaming. The example uses keyset pages; first filter/aggregate large data, and define consistency or change boundaries as well. Do not hold a database write transaction open during lengthy model training.

Tested versions and scope

Python 3.12.14, matrixone-python-sdk 0.1.16, SQLAlchemy 2.0.43, PyMySQL 1.1.1, aiomysql 0.3.2, NumPy 2.1.3, pandas 2.2.3, SciPy 1.18.1, scikit-learn 1.8.0, Matplotlib 3.10.8, official MO v4.2.5. Both exact download scripts and all assertions executed on 2026-10-14.

These are small workflow, type and transaction checks, not production capacity, predictive quality, full-database consistency, remote TLS, CDC or distributed-training certifications. See Python connections.