# Chalk Python SDK Reference: ML Modeling Functions
source: https://docs.chalk.ai/api-docs#section-ml-modeling-functions

## `sagemaker_predict(body, endpoint, content_type, target_model, target_variant, aws_access_key_id_override, aws_secret_access_key_override, aws_session_token_override, aws_role_arn_override, aws_region_override, aws_profile_name_override, inference_component)`
source: https://docs.chalk.ai/api-docs#sagemaker_predict

Runs a sagemaker prediction on the specified endpoint, passing in the serialized bytes as a feature.

**Parameters**

- `body`: `Underscore | Any`
  Bytes feature to be passed as the serialized input to the sagemaker endpoint.
- `endpoint`: `str`
  The name of the sagemaker endpoint.
- `content_type`: `str | None` = `None`
  The content type of the input data. If not specified, the content type will be inferred from the endpoint.
- `target_model`: `str | None` = `None`
  An optional argument which specifies the target model for the prediction. This should only be used for multimodel sagemaker endpoints.
- `target_variant`: `str | None` = `None`
  An optional argument which specifies the target variant for the prediction. This should only be used for multi variant sagemaker endpoints.
- `aws_access_key_id_override`: `str | None` = `None`
  An optional argument which specifies the AWS access key ID to use for the prediction.
- `aws_secret_access_key_override`: `str | None` = `None`
  An optional argument which specifies the AWS secret access key to use for the prediction.
- `aws_session_token_override`: `str | None` = `None`
  An optional argument which specifies the AWS session token to use for the prediction.
- `aws_role_arn_override`: `str | None` = `None`
  An optional argument which specifies a AWS role ARN that will be assumed to generate credentials for the prediction.
- `aws_region_override`: `str | None` = `None`
  An optional argument which specifies the AWS region to use for the prediction.
- `aws_profile_name_override`: `str | None` = `None`
  An optional argument which specifies the AWS profile name to use for the prediction
- `inference_component`: `str | None` = `None`
  Specify the inference component to use for the prediction.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   encoded_sagemaker_data: bytes
   prediction: float = F.sagemaker_predict(
       _.encoded_sagemaker_data,
       endpoint="prediction-model_1.0.1_2024-09-16",
       target_model="model_v2.tar.gz",
       target_variant="blue"
   )
```

## `vertex_predict(body, endpoint, content_type, gcp_credentials_override, dedicated_endpoint_dns, method, api_host)`
source: https://docs.chalk.ai/api-docs#vertex_predict

Runs a Vertex AI prediction on the specified endpoint, passing serialized bytes as input.

**Parameters**

- `body`: `Underscore | Any`
  Bytes feature to pass as the input to the Vertex AI endpoint.
- `endpoint`: `str`
  Full Vertex AI endpoint resource name. For `method="rawPredict"` (default) this is a deployed endpoint resource: `projects/{project}/locations/{location}/endpoints/{endpoint_id}`. For `method="embedContent"` or `"generateContent"` this is a publisher model resource: `projects/{project}/locations/{location}/publishers/{publisher}/models/{model}`.
- `content_type`: `str | None` = `None`
  Content type of the input (e.g. "application/json"). Optional.
- `gcp_credentials_override`: `str | None` = `None`
  Service account JSON key string. Falls back to `CHALK_FUNCTION_GCP_AUTH_KEY` (base64-encoded SA JSON) or Application Default Credentials if not set.
- `dedicated_endpoint_dns`: `str | None` = `None`
  Dedicated endpoint DNS domain for Vertex AI dedicated endpoints. Retrieve from the `dedicatedEndpointDns` field on your endpoint resource. Example: `"1234567890.us-central1-123456789.prediction.vertexai.goog"` When set, the gRPC connection uses this domain instead of the shared Vertex AI domain. Only valid with `method="rawPredict"`.
- `method`: `'rawPredict' | 'embedContent' | 'generateContent' | None` = `None`
  Which Vertex AI RPC to invoke. `"rawPredict"` (default) calls the gRPC `PredictionService.RawPredict` against a deployed endpoint. `"embedContent"` and `"generateContent"` call the publisher REST endpoints `https://<host>/v1/<endpoint>:embedContent` and `:generateContent` respectively, with a bearer token minted from the configured GCP credentials. Body and response are passed through as opaque JSON bytes.
- `api_host`: `str | None` = `None`
  Override the REST API host used when `method` is `"embedContent"` or `"generateContent"`. If unset, the host is derived from the endpoint's location: multi-region locations (`us`, `eu`, `asia`, `global`) use `aiplatform.<loc>.rep.googleapis.com`; specific regions use `<loc>-aiplatform.googleapis.com`.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   encoded_vertex_data: bytes
   prediction: float = F.vertex_predict(
       _.encoded_vertex_data,
       endpoint="projects/my-project/locations/us-central1/endpoints/1234567890",
       content_type="application/json"
   )
```

## `sklearn_decision_tree_classifier(features, model_path, return_probability, threshold)`
source: https://docs.chalk.ai/api-docs#sklearn_decision_tree_classifier

Load scikit-learn decision tree classifier into an expression from a joblib or pickle file.

The model must be trained on the same version of scikit-learn that is deployed in your Chalk environment.

Only binary classification is supported.

**Parameters**

- `features`: `Underscore` = `()`
  A list of features which were used to train the model.
- `model_path`: `str`
  A filepath to the scikit-learn logistic regression model.
- `return_probability`: `bool` = `False`
  Whether to return the prediction probability or the predicted class.
- `threshold`: `float | Underscore` = `0.5`
  The decision threshold for the model (ignored if probability = True).

**Examples**

```python
import chalk.functions as F
@features
class User:
   id: str
   a: float
   b: float
   c: float
   d: float
   e: float
   prediction: bool = F.sklearn_decision_tree_classifier(
      _.a, _.b, _.c, _.d, _.e,
      model_path=os.path.join(os.environ.get("TARGET_ROOT", "."), "models", "./dt_classifier.joblib")
      probabiliy=True, threshold=0.7,
   )
```

## `sklearn_decision_tree_regressor(features, model_path)`
source: https://docs.chalk.ai/api-docs#sklearn_decision_tree_regressor

Load scikit-learn decision tree regressor into an expression from a joblib or pickle file.

The model must be trained on the same version of scikit-learn that is deployed in your Chalk environment.

**Parameters**

- `features`: `Underscore` = `()`
  A list of features which were used to train the model.
- `model_path`: `str`
  A filepath to the scikit-learn logistic regression model.

**Examples**

```python
import chalk.functions as F
@features
class User:
   id: str
   a: float
   b: float
   c: float
   d: float
   e: float
   prediction: bool = F.sklearn_decision_tree_regressor(
      _.a, _.b, _.c, _.d, _.e,
      model_path=os.path.join(os.environ.get("TARGET_ROOT", "."), "models", "./dt_regressor.joblib")
   )
```

## `sklearn_gradient_boosting_regressor(features, model_path)`
source: https://docs.chalk.ai/api-docs#sklearn_gradient_boosting_regressor

Load scikit-learn gradient boosting regressor into an expression from a joblib or pickle file.

The model must be trained on the same version of scikit-learn that is deployed in your Chalk environment.

**Parameters**

- `features`: `Underscore` = `()`
  A list of features which were used to train the model.
- `model_path`: `str`
  A filepath to the scikit-learn logistic regression model.

**Examples**

```python
import chalk.functions as F
@features
class User:
   id: str
   a: float
   b: float
   c: float
   d: float
   e: float
   prediction: float = F.sklearn_gradient_boosting_regressor(
      _.a, _.b, _.c, _.d, _.e,
      model_path=os.path.join(os.environ.get("TARGET_ROOT", "."), "models", "./gb_regressor.joblib"),
   )
```

## `sklearn_logistic_regression(features, model_path, return_probability, threshold)`
source: https://docs.chalk.ai/api-docs#sklearn_logistic_regression

Load scikit-learn logistic regression into an expression from a joblib or pickle file.

The model must be trained on the same version of scikit-learn that is deployed in your Chalk environment.

Only binary classification is supported.

**Parameters**

- `features`: `Underscore` = `()`
  A list of features which were used to train the model.
- `model_path`: `str`
  A filepath to the scikit-learn logistic regression model.
- `return_probability`: `bool` = `False`
  Whether to return the prediction probability or the predicted class.
- `threshold`: `float | Underscore` = `0.5`
  The decision threshold for the model (ignored if probability = True).

**Examples**

```python
import chalk.functions as F
@features
class User:
   id: str
   a: float
   b: float
   c: float
   d: float
   e: float
   prediction: float = F.sklearn_logistic_regression(
      _.a, _.b, _.c, _.d, _.e,
      model_path=os.path.join(os.environ.get("TARGET_ROOT", "."), "models", "./logistic.joblib"),
      probabiliy=True, threshold=0.7,
   )
```

## `sklearn_random_forest_classifier(features, model_path, return_probability, threshold)`
source: https://docs.chalk.ai/api-docs#sklearn_random_forest_classifier

Load scikit-learn random forest classifier into an expression from a joblib or pickle file.

The model must be trained on the same version of scikit-learn that is deployed in your Chalk environment.

Only binary classification is supported.

**Parameters**

- `features`: `Underscore` = `()`
  A list of features which were used to train the model.
- `model_path`: `str`
  A filepath to the scikit-learn logistic regression model.
- `return_probability`: `bool` = `False`
  Whether to return the prediction probability or the predicted class.
- `threshold`: `float` = `0.5`
  The decision threshold for the model (ignored if probability = True).

**Examples**

```python
import chalk.functions as F
@features
class User:
   id: str
   a: float
   b: float
   c: float
   d: float
   e: float
   prediction: float = F.sklearn_random_forest_classifier(
      _.a, _.b, _.c, _.d, _.e,
      model_path=os.path.join(os.environ.get("TARGET_ROOT", "."), "models", "./rf_classifier.joblib"),
      probabiliy=True, threshold=0.7,
   )
```

## `sklearn_random_forest_regressor(features, model_path)`
source: https://docs.chalk.ai/api-docs#sklearn_random_forest_regressor

Load scikit-learn random forest regressor into an expression from a joblib or pickle file.

The model must be trained on the same version of scikit-learn that is deployed in your Chalk environment.

**Parameters**

- `features`: `Underscore` = `()`
  A list of features which were used to train the model.
- `model_path`: `str`
  A filepath to the scikit-learn logistic regression model.

**Examples**

```python
import chalk.functions as F
@features
class User:
   id: str
   a: float
   b: float
   c: float
   d: float
   e: float
   prediction: float = F.sklearn_random_forest_regressor(
      _.a, _.b, _.c, _.d, _.e,
      model_path=os.path.join(os.environ.get("TARGET_ROOT", "."), "models", "./rf_regressor.joblib")
   )
```

## `xgboost_regressor(features, model_path)`
source: https://docs.chalk.ai/api-docs#xgboost_regressor

Load xgboost regressor into an expression from a joblib or pickle file.

The model must be trained on the same version of xgboost that is deployed in your Chalk environment. The joblib dependency also needs to be included in your python requirements.

Currently, only non-null float features are supported.

**Parameters**

- `features`: `Underscore` = `()`
  A list of the features which were used to train the model.
- `model_path`: `str`
  A filepath to the xgboost resgressor model.

**Examples**

```python
import chalk.functions as F
@features
class User:
   id: str
   a: float
   b: float
   c: float
   d: float
   e: float
   prediction: float = F.xgboost_regressor(
      _.a, _.b, _.c, _.d, _.e,
      model_path=os.path.join(os.environ.get("TARGET_ROOT", "."), "models", "./xgb_regressor.joblib"),
   )
```

## `width_bucket(operand, bound1, bound2, bucket_count)`
source: https://docs.chalk.ai/api-docs#width_bucket

Assign a bucket number to a value based on histogram buckets.

**Parameters**

- `operand`: `Underscore | Any`
  The value to assign to a bucket.
- `bound1`: `Underscore | Any`
  The lower bound of the range.
- `bound2`: `Underscore | Any`
  The upper bound of the range.
- `bucket_count`: `Underscore | Any`
  The number of buckets.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class CustomerSegmentation:
   id: str
   transaction_amount: float
   amount_bucket: int = F.width_bucket(_.transaction_amount, 0.0, 1000.0, 10)
```
