> For the complete documentation index, see [llms.txt](https://slmetrics-docs.gitbook.io/v1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://slmetrics-docs.gitbook.io/v1/reference/classification-metrics/logarithmic-loss/logloss.md).

# logloss.md

|         |                 |
| ------- | --------------: |
| logloss | R Documentation |

### Logarithmic Loss

#### Description

A generic S3 function to compute the *logarithmic loss* score for a\
classification model. This function dispatches to S3 methods in`logloss()` and performs no input validation. If you supply NA values or\
vectors of unequal length (e.g. `length(x) != length(y)`), the\
underlying `C++` code may trigger undefined behavior and crash your `R`\
session.

**Defensive measures**

Because `logloss()` operates on raw pointers, pointer-level faults (e.g.\
from NA or mismatched length) occur before any `R`-level error handling.\
Wrapping calls in `try()` or `tryCatch()` will *not* prevent `R`-session\
crashes.

To guard against this, wrap `logloss()` in a "safe" validator that\
checks for NA values and matching length, for example:

{% code overflow="wrap" lineNumbers="true" %}

```r
safe_logloss <- function(x, y, ...) {
  stopifnot(
    !anyNA(x), !anyNA(y),
    length(x) == length(y)
  )
  logloss(x, y, ...)
}
```

{% endcode %}

Apply the same pattern to any custom metric functions to ensure input\
sanity before calling the underlying `C++` code.

#### Usage

```r
## Generic S3 method
## for Logarithmic Loss
logloss(...)

## Generic S3 method
## for weighted Logarithmic Loss
weighted.logloss(...)
```

#### Arguments

| `...` | <p>Arguments passed on to <code>logloss.integer</code>,<code>logloss.factor</code>, <code>weighted.logloss.integer</code>,<code>weighted.logloss.factor</code></p><p><code>actual</code></p><p>A vector length <code>n</code>, and <code>k</code> levels. Can be of integer or factor.</p><p><code>response</code></p><p>A <code>n \times k</code> \<double>-matrix of<br>predicted probabilities. The <code>i</code>-th row should<br>sum to 1 (i.e., a valid probability distribution over the <code>k</code> classes). The first column corresponds to the<br>first factor level in <code>actual</code>, the second column to the<br>second factor level, and so on.</p><p><code>normalize</code></p><p>A \<logical>-value (default: TRUE). If TRUE, the mean<br>cross-entropy across all observations is returned; otherwise, the sum of<br>cross-entropies is returned.</p><p><code>w</code></p><p>A \<double> vector of sample weights.</p> |
| ----- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

#### Value

A \<double>

#### References

MacKay, David JC. Information theory, inference and learning algorithms.\
Cambridge university press, 2003.

Kramer, Oliver, and Oliver Kramer. "Scikit-learn." Machine learning for\
evolution strategies (2016): 45-53.

Virtanen, Pauli, et al. "SciPy 1.0: f'undamental algorithms for\
scientific computing in Python." Nature methods 17.3 (2020): 261-272.

#### Examples

```r
## Classes and
## seed
set.seed(1903)
classes <- c("Kebab", "Falafel")

## Generate actual
## and predicted response
## probabilities
actual_classes <- factor(
x = sample(x = classes, size = 1e3, replace = TRUE),
levels = c("Kebab", "Falafel")
)

response <- runif(n = 1e3)

## Logloss
SLmetrics::logloss(
   actual    = actual_classes, 
   response  = cbind(
 response,
 1 - response
   )
)

## Generate observed
## frequencies 
actual_frequency <- sample(10L:100L, size = 1e3, replace = TRUE)

## Poisson Logloss
SLmetrics::logloss(
   actual    = actual_frequency, 
   response  = response
)




```

```

</div>

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://slmetrics-docs.gitbook.io/v1/reference/classification-metrics/logarithmic-loss/logloss.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
