# Quickstart

> Install the library, set your API key, and make your first forecast and your first tabular prediction.

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).

   ```sh
   pip install yhatlabs
   ```

2. **Set your API key.** Every call needs one; [create an account](https://yhatlabs.com/models/signup) 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.

   ```sh
   export YHAT_API_KEY="<your-key>"
   ```

   To check it works, see [API keys](https://yhatlabs.com/models/docs/api-keys#check-your-key).

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.

   ```python
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
0  series_0 1961-01-01  430.5   448.5  468.0
1  series_0 1961-02-01  408.9   430.9  452.0
2  series_0 1961-03-01  449.0   486.7  508.2
```

```bash
curl -sO https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv
yhatlabs forecast airline-passengers.csv --h 24 \
--time-col Month --target-col Passengers --level 80 -o forecast.csv
head -4 forecast.csv
```

Output:

```
unique_id,Month,q10,median,q90
series_0,1961-01-01,430.53720868618353,448.541455316843,467.97666760754703
series_0,1961-02-01,408.9291654372314,430.8968252966604,451.962356383921
series_0,1961-03-01,449.0440194902994,486.74538766574983,508.24449670833036
```

```bash
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}}
```

   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.

   ```python
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
```

```bash
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]]
```

   `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.

## Next

- [Forecasting](https://yhatlabs.com/models/docs/time-series/forecasting): uncertainty, many series at once, reading the results.
- [Predicting on tables](https://yhatlabs.com/models/docs/tabular/predicting): fitting once, the options, reading the results.
- [Data checks](https://yhatlabs.com/models/docs/tools/data-checks): find problems in your data before sending it.
