Skip to content
Log inGet startedHugging FaceGitHub

Get started

Quickstart

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.

  1. Install the library. This installs the yhatlabs Python package and the yhatlabs command (Python 3.9 or later).

    Terminal window
    pip install yhatlabs
  2. 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.

  3. 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 pd
    from yhatlabs import Client
    url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/"
    df = pd.read_csv(url + "airline-passengers.csv") # columns: Month, Passengers
    yh = Client()
    fc = yh.forecast(df, h=24, time_col="Month", target_col="Passengers", level=[80])
    print(fc.df.head(3).round(1))
    Output
    unique_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

    Parameters: Client.forecast

    Each row is one future month: median is the most likely value, and q10 to q90 is the 80% interval. The cURL tab shows the same call made directly against the API, which takes the values as a plain list.

  4. Make a tabular prediction. This learns a diagnosis (0 or 1) 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_cancer
    from sklearn.model_selection import train_test_split
    from yhatlabs import Client
    X, 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()
    Output
    prediction 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

    Parameters: Client.predict

    prediction is the predicted class, and p_0, p_1 are the probabilities of each class. yh.fit stores the fitted model on our side so you can predict from it repeatedly; model.delete() removes it.