This page takes you from nothing to a forecast and a tabular prediction. Each step says what it does and shows what you should see.
-
Install the library. This installs the
yhatlabsPython package and theyhatlabscommand (Python 3.9 or later).Terminal window pip install yhatlabs -
Set your API key. Every call needs one; create an account and make one on the API keys page if you don’t have one yet. Setting it in your environment lets the library, the command line and agents all find it.
Terminal window export YHAT_API_KEY="<your-key>"To check it works, see API keys.
-
Make a forecast. This loads 12 years of monthly airline passengers and forecasts the next 24 months.
level=[80]asks for an 80% interval around each month: the range the actual value should fall in 8 times out of 10.import pandas as pdfrom yhatlabs import Clienturl = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/"df = pd.read_csv(url + "airline-passengers.csv") # columns: Month, Passengersyh = Client()fc = yh.forecast(df, h=24, time_col="Month", target_col="Passengers", level=[80])print(fc.df.head(3).round(1))Outputunique_id Month q10 median q90 series_0 1961-01-01 430.5 448.5 468.0 series_0 1961-02-01 408.9 430.9 452.0 series_0 1961-03-01 449.0 486.7 508.2 Terminal window curl -sO https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csvyhatlabs forecast airline-passengers.csv --h 24 \--time-col Month --target-col Passengers --level 80 -o forecast.csvhead -4 forecast.csvOutput unique_id,Month,q10,median,q90series_0,1961-01-01,430.53720868618353,448.541455316843,467.97666760754703series_0,1961-02-01,408.9291654372314,430.8968252966604,451.962356383921series_0,1961-03-01,449.0440194902994,486.74538766574983,508.24449670833036Terminal window curl -s https://api.yhatlabs.com/v1/series/forecast \-H "Authorization: Bearer $YHAT_API_KEY" \-H "Content-Type: application/json" \-d '{"freq": "MS", "horizon": 2,"series": [{"id": "passengers","values": [417, 391, 419, 461, 472, 535, 622, 606, 508, 461, 390, 432]}]}'Output {"id":"3a90dd3d-e737-465e-b1f9-ca3c8941d464","model":"chakra-ts","horizon":2,"quantile_levels":[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9],"forecasts":[{"id":"passengers","quantiles":[[383.0753173828125,402.9498291015625,413.9801025390625,422.4678039550781,434.37628173828125,446.0346374511719,460.8013610839844,478.62322998046875,502.9939880371094],[364.9206237792969,390.9168395996094,410.5889892578125,423.1913757324219,436.617431640625,454.6253662109375,474.06512451171875,497.9351806640625,532.4006958007812]]}],"usage":{"series":1}}Parameters:
Client.forecastEach row is one future month:
medianis the most likely value, andq10toq90is the 80% interval. The cURL tab shows the same call made directly against the API, which takes the values as a plain list. -
Make a tabular prediction. This learns a diagnosis (
0or1) from 426 labeled rows of a public medical dataset, then predicts it for 143 rows it has not seen.from sklearn.datasets import load_breast_cancerfrom sklearn.model_selection import train_test_splitfrom yhatlabs import ClientX, y = load_breast_cancer(return_X_y=True, as_frame=True)X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)yh = Client()model = yh.fit(X_train.assign(diagnosis=y_train), target="diagnosis")res = model.predict(X_test)print(res.df.head(3).round(3))print("accuracy:", round((res.predictions == y_test).mean(), 3))model.delete()Outputprediction p_0 p_1 512 0 1.000 0.000 457 1 0.013 0.987 439 1 0.000 1.000 accuracy: 0.979
Terminal window curl -s https://api.yhatlabs.com/v1/table/predict \-H "Authorization: Bearer $YHAT_API_KEY" \-H "Content-Type: application/json" \-d '{"target": "bought","train": [{"visits": 1, "plan": "free", "bought": 0}, {"visits": 9, "plan": "pro", "bought": 1},{"visits": 2, "plan": "free", "bought": 0}, {"visits": 7, "plan": "pro", "bought": 1},{"visits": 3, "plan": "free", "bought": 0}, {"visits": 8, "plan": "pro", "bought": 1},{"visits": 1, "plan": "pro", "bought": 0}, {"visits": 6, "plan": "free", "bought": 1},{"visits": 2, "plan": "pro", "bought": 0}, {"visits": 9, "plan": "free", "bought": 1}],"test": [{"visits": 8, "plan": "pro"}, {"visits": 1, "plan": "free"}]}' \| python3 -c "import sys, json; r = json.load(sys.stdin); print(r['predictions'], r['classes'], r['probabilities'])"Output [1, 0] [0, 1] [[0.000736, 0.999264], [0.999706, 0.000294]]Parameters:
Client.predictpredictionis the predicted class, andp_0,p_1are the probabilities of each class.yh.fitstores the fitted model on our side so you can predict from it repeatedly;model.delete()removes it.
- Forecasting: uncertainty, many series at once, reading the results.
- Predicting on tables: fitting once, the options, reading the results.
- Data checks: find problems in your data before sending it.

