or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

datasets.mdfeature-extraction.mdindex.mdmetrics.mdmodel-selection.mdneighbors.mdpipelines.mdpreprocessing.mdsupervised-learning.mdunsupervised-learning.mdutilities.md

metrics.mddocs/

0

# Metrics and Visualization

1

2

This document covers all performance evaluation metrics, scoring functions, and visualization displays in scikit-learn.

3

4

## Classification Metrics

5

6

### Accuracy and Basic Metrics

7

8

#### accuracy_score { .api }

9

```python

10

from sklearn.metrics import accuracy_score

11

12

accuracy_score(

13

y_true: ArrayLike,

14

y_pred: ArrayLike,

15

normalize: bool = True,

16

sample_weight: ArrayLike | None = None

17

) -> float

18

```

19

Accuracy classification score.

20

21

#### balanced_accuracy_score { .api }

22

```python

23

from sklearn.metrics import balanced_accuracy_score

24

25

balanced_accuracy_score(

26

y_true: ArrayLike,

27

y_pred: ArrayLike,

28

sample_weight: ArrayLike | None = None,

29

adjusted: bool = False

30

) -> float

31

```

32

Compute the balanced accuracy.

33

34

#### top_k_accuracy_score { .api }

35

```python

36

from sklearn.metrics import top_k_accuracy_score

37

38

top_k_accuracy_score(

39

y_true: ArrayLike,

40

y_score: ArrayLike,

41

k: int = 2,

42

normalize: bool = True,

43

sample_weight: ArrayLike | None = None,

44

labels: ArrayLike | None = None

45

) -> float

46

```

47

Top-k Accuracy classification score.

48

49

### Precision, Recall, and F-scores

50

51

#### precision_score { .api }

52

```python

53

from sklearn.metrics import precision_score

54

55

precision_score(

56

y_true: ArrayLike,

57

y_pred: ArrayLike,

58

labels: ArrayLike | None = None,

59

pos_label: str | int = 1,

60

average: str | None = "binary",

61

sample_weight: ArrayLike | None = None,

62

zero_division: str | int = "warn"

63

) -> float | ArrayLike

64

```

65

Compute the precision.

66

67

#### recall_score { .api }

68

```python

69

from sklearn.metrics import recall_score

70

71

recall_score(

72

y_true: ArrayLike,

73

y_pred: ArrayLike,

74

labels: ArrayLike | None = None,

75

pos_label: str | int = 1,

76

average: str | None = "binary",

77

sample_weight: ArrayLike | None = None,

78

zero_division: str | int = "warn"

79

) -> float | ArrayLike

80

```

81

Compute the recall.

82

83

#### f1_score { .api }

84

```python

85

from sklearn.metrics import f1_score

86

87

f1_score(

88

y_true: ArrayLike,

89

y_pred: ArrayLike,

90

labels: ArrayLike | None = None,

91

pos_label: str | int = 1,

92

average: str | None = "binary",

93

sample_weight: ArrayLike | None = None,

94

zero_division: str | int = "warn"

95

) -> float | ArrayLike

96

```

97

Compute the F1 score, the harmonic mean of precision and recall.

98

99

#### fbeta_score { .api }

100

```python

101

from sklearn.metrics import fbeta_score

102

103

fbeta_score(

104

y_true: ArrayLike,

105

y_pred: ArrayLike,

106

beta: float,

107

labels: ArrayLike | None = None,

108

pos_label: str | int = 1,

109

average: str | None = "binary",

110

sample_weight: ArrayLike | None = None,

111

zero_division: str | int = "warn"

112

) -> float | ArrayLike

113

```

114

Compute the F-beta score.

115

116

#### precision_recall_fscore_support { .api }

117

```python

118

from sklearn.metrics import precision_recall_fscore_support

119

120

precision_recall_fscore_support(

121

y_true: ArrayLike,

122

y_pred: ArrayLike,

123

beta: float = 1.0,

124

labels: ArrayLike | None = None,

125

pos_label: str | int = 1,

126

average: str | None = None,

127

warn_for: tuple = ("precision", "recall", "f-score"),

128

sample_weight: ArrayLike | None = None,

129

zero_division: str | int = "warn"

130

) -> tuple[ArrayLike, ArrayLike, ArrayLike, ArrayLike]

131

```

132

Compute precision, recall, F-measure and support for each class.

133

134

### Confusion Matrix and Classification Report

135

136

#### confusion_matrix { .api }

137

```python

138

from sklearn.metrics import confusion_matrix

139

140

confusion_matrix(

141

y_true: ArrayLike,

142

y_pred: ArrayLike,

143

labels: ArrayLike | None = None,

144

sample_weight: ArrayLike | None = None,

145

normalize: str | None = None

146

) -> ArrayLike

147

```

148

Compute confusion matrix to evaluate the accuracy of a classification.

149

150

#### classification_report { .api }

151

```python

152

from sklearn.metrics import classification_report

153

154

classification_report(

155

y_true: ArrayLike,

156

y_pred: ArrayLike,

157

labels: ArrayLike | None = None,

158

target_names: list[str] | None = None,

159

sample_weight: ArrayLike | None = None,

160

digits: int = 2,

161

output_dict: bool = False,

162

zero_division: str | int = "warn"

163

) -> str | dict

164

```

165

Build a text report showing the main classification metrics.

166

167

### ROC and AUC Metrics

168

169

#### roc_auc_score { .api }

170

```python

171

from sklearn.metrics import roc_auc_score

172

173

roc_auc_score(

174

y_true: ArrayLike,

175

y_score: ArrayLike,

176

average: str | None = "macro",

177

sample_weight: ArrayLike | None = None,

178

max_fpr: float | None = None,

179

multi_class: str = "raise",

180

labels: ArrayLike | None = None

181

) -> float

182

```

183

Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC).

184

185

#### roc_curve { .api }

186

```python

187

from sklearn.metrics import roc_curve

188

189

roc_curve(

190

y_true: ArrayLike,

191

y_score: ArrayLike,

192

pos_label: int | str | None = None,

193

sample_weight: ArrayLike | None = None,

194

drop_intermediate: bool = True

195

) -> tuple[ArrayLike, ArrayLike, ArrayLike]

196

```

197

Compute Receiver operating characteristic (ROC).

198

199

#### auc { .api }

200

```python

201

from sklearn.metrics import auc

202

203

auc(

204

x: ArrayLike,

205

y: ArrayLike

206

) -> float

207

```

208

Compute Area Under the Curve (AUC) using the trapezoidal rule.

209

210

### Precision-Recall Metrics

211

212

#### average_precision_score { .api }

213

```python

214

from sklearn.metrics import average_precision_score

215

216

average_precision_score(

217

y_true: ArrayLike,

218

y_score: ArrayLike,

219

average: str | None = "macro",

220

pos_label: int | str = 1,

221

sample_weight: ArrayLike | None = None

222

) -> float

223

```

224

Compute average precision (AP) from prediction scores.

225

226

#### precision_recall_curve { .api }

227

```python

228

from sklearn.metrics import precision_recall_curve

229

230

precision_recall_curve(

231

y_true: ArrayLike,

232

probas_pred: ArrayLike,

233

pos_label: int | str | None = None,

234

sample_weight: ArrayLike | None = None

235

) -> tuple[ArrayLike, ArrayLike, ArrayLike]

236

```

237

Compute precision-recall pairs for different probability thresholds.

238

239

### Loss Functions

240

241

#### log_loss { .api }

242

```python

243

from sklearn.metrics import log_loss

244

245

log_loss(

246

y_true: ArrayLike,

247

y_pred: ArrayLike,

248

eps: float | str = "auto",

249

normalize: bool = True,

250

sample_weight: ArrayLike | None = None,

251

labels: ArrayLike | None = None

252

) -> float

253

```

254

Log loss, aka logistic loss or cross-entropy loss.

255

256

#### hinge_loss { .api }

257

```python

258

from sklearn.metrics import hinge_loss

259

260

hinge_loss(

261

y_true: ArrayLike,

262

pred_decision: ArrayLike,

263

labels: ArrayLike | None = None,

264

sample_weight: ArrayLike | None = None

265

) -> float

266

```

267

Average hinge loss (non-regularized).

268

269

#### brier_score_loss { .api }

270

```python

271

from sklearn.metrics import brier_score_loss

272

273

brier_score_loss(

274

y_true: ArrayLike,

275

y_prob: ArrayLike,

276

sample_weight: ArrayLike | None = None,

277

pos_label: int | str | None = None

278

) -> float

279

```

280

Compute the Brier score loss.

281

282

#### hamming_loss { .api }

283

```python

284

from sklearn.metrics import hamming_loss

285

286

hamming_loss(

287

y_true: ArrayLike,

288

y_pred: ArrayLike,

289

sample_weight: ArrayLike | None = None

290

) -> float

291

```

292

Compute the average Hamming loss.

293

294

#### jaccard_score { .api }

295

```python

296

from sklearn.metrics import jaccard_score

297

298

jaccard_score(

299

y_true: ArrayLike,

300

y_pred: ArrayLike,

301

labels: ArrayLike | None = None,

302

pos_label: str | int = 1,

303

average: str | None = "binary",

304

sample_weight: ArrayLike | None = None,

305

zero_division: str | int = "warn"

306

) -> float

307

```

308

Jaccard similarity coefficient score.

309

310

#### zero_one_loss { .api }

311

```python

312

from sklearn.metrics import zero_one_loss

313

314

zero_one_loss(

315

y_true: ArrayLike,

316

y_pred: ArrayLike,

317

normalize: bool = True,

318

sample_weight: ArrayLike | None = None

319

) -> float

320

```

321

Zero-one classification loss.

322

323

### Statistical Measures

324

325

#### matthews_corrcoef { .api }

326

```python

327

from sklearn.metrics import matthews_corrcoef

328

329

matthews_corrcoef(

330

y_true: ArrayLike,

331

y_pred: ArrayLike,

332

sample_weight: ArrayLike | None = None

333

) -> float

334

```

335

Compute the Matthews correlation coefficient (MCC).

336

337

#### cohen_kappa_score { .api }

338

```python

339

from sklearn.metrics import cohen_kappa_score

340

341

cohen_kappa_score(

342

y1: ArrayLike,

343

y2: ArrayLike,

344

labels: ArrayLike | None = None,

345

weights: str | None = None,

346

sample_weight: ArrayLike | None = None

347

) -> float

348

```

349

Compute Cohen's kappa: a statistic that measures inter-annotator agreement.

350

351

#### class_likelihood_ratios { .api }

352

```python

353

from sklearn.metrics import class_likelihood_ratios

354

355

class_likelihood_ratios(

356

y_true: ArrayLike,

357

y_pred: ArrayLike,

358

labels: ArrayLike | None = None,

359

sample_weight: ArrayLike | None = None

360

) -> tuple[ArrayLike, ArrayLike]

361

```

362

Compute binary classification positive and negative likelihood ratios.

363

364

### Detection Error Tradeoff

365

366

#### det_curve { .api }

367

```python

368

from sklearn.metrics import det_curve

369

370

det_curve(

371

y_true: ArrayLike,

372

y_score: ArrayLike,

373

pos_label: int | str | None = None,

374

sample_weight: ArrayLike | None = None

375

) -> tuple[ArrayLike, ArrayLike, ArrayLike]

376

```

377

Compute error rates for different probability thresholds.

378

379

## Regression Metrics

380

381

### Basic Regression Metrics

382

383

#### mean_squared_error { .api }

384

```python

385

from sklearn.metrics import mean_squared_error

386

387

mean_squared_error(

388

y_true: ArrayLike,

389

y_pred: ArrayLike,

390

sample_weight: ArrayLike | None = None,

391

multioutput: str | ArrayLike = "uniform_average",

392

squared: bool = True

393

) -> float | ArrayLike

394

```

395

Mean squared error regression loss.

396

397

#### mean_absolute_error { .api }

398

```python

399

from sklearn.metrics import mean_absolute_error

400

401

mean_absolute_error(

402

y_true: ArrayLike,

403

y_pred: ArrayLike,

404

sample_weight: ArrayLike | None = None,

405

multioutput: str | ArrayLike = "uniform_average"

406

) -> float | ArrayLike

407

```

408

Mean absolute error regression loss.

409

410

#### mean_absolute_percentage_error { .api }

411

```python

412

from sklearn.metrics import mean_absolute_percentage_error

413

414

mean_absolute_percentage_error(

415

y_true: ArrayLike,

416

y_pred: ArrayLike,

417

sample_weight: ArrayLike | None = None,

418

multioutput: str | ArrayLike = "uniform_average"

419

) -> float | ArrayLike

420

```

421

Mean absolute percentage error (MAPE) regression loss.

422

423

#### median_absolute_error { .api }

424

```python

425

from sklearn.metrics import median_absolute_error

426

427

median_absolute_error(

428

y_true: ArrayLike,

429

y_pred: ArrayLike,

430

multioutput: str | ArrayLike = "uniform_average",

431

sample_weight: ArrayLike | None = None

432

) -> float | ArrayLike

433

```

434

Median absolute error regression loss.

435

436

#### max_error { .api }

437

```python

438

from sklearn.metrics import max_error

439

440

max_error(

441

y_true: ArrayLike,

442

y_pred: ArrayLike

443

) -> float

444

```

445

Compute the maximum residual error.

446

447

#### mean_squared_log_error { .api }

448

```python

449

from sklearn.metrics import mean_squared_log_error

450

451

mean_squared_log_error(

452

y_true: ArrayLike,

453

y_pred: ArrayLike,

454

sample_weight: ArrayLike | None = None,

455

multioutput: str | ArrayLike = "uniform_average"

456

) -> float | ArrayLike

457

```

458

Mean squared logarithmic error regression loss.

459

460

#### mean_poisson_deviance { .api }

461

```python

462

from sklearn.metrics import mean_poisson_deviance

463

464

mean_poisson_deviance(

465

y_true: ArrayLike,

466

y_pred: ArrayLike,

467

sample_weight: ArrayLike | None = None

468

) -> float

469

```

470

Mean Poisson deviance regression loss.

471

472

#### mean_gamma_deviance { .api }

473

```python

474

from sklearn.metrics import mean_gamma_deviance

475

476

mean_gamma_deviance(

477

y_true: ArrayLike,

478

y_pred: ArrayLike,

479

sample_weight: ArrayLike | None = None

480

) -> float

481

```

482

Mean Gamma deviance regression loss.

483

484

#### mean_tweedie_deviance { .api }

485

```python

486

from sklearn.metrics import mean_tweedie_deviance

487

488

mean_tweedie_deviance(

489

y_true: ArrayLike,

490

y_pred: ArrayLike,

491

sample_weight: ArrayLike | None = None,

492

power: float = 0

493

) -> float

494

```

495

Mean Tweedie deviance regression loss.

496

497

### Coefficient of Determination

498

499

#### r2_score { .api }

500

```python

501

from sklearn.metrics import r2_score

502

503

r2_score(

504

y_true: ArrayLike,

505

y_pred: ArrayLike,

506

sample_weight: ArrayLike | None = None,

507

multioutput: str | ArrayLike | None = "uniform_average",

508

force_finite: bool = True

509

) -> float | ArrayLike

510

```

511

R² (coefficient of determination) regression score function.

512

513

#### explained_variance_score { .api }

514

```python

515

from sklearn.metrics import explained_variance_score

516

517

explained_variance_score(

518

y_true: ArrayLike,

519

y_pred: ArrayLike,

520

sample_weight: ArrayLike | None = None,

521

multioutput: str | ArrayLike = "uniform_average",

522

force_finite: bool = True

523

) -> float | ArrayLike

524

```

525

Explained variance regression score function.

526

527

### D² Scores

528

529

#### d2_absolute_error_score { .api }

530

```python

531

from sklearn.metrics import d2_absolute_error_score

532

533

d2_absolute_error_score(

534

y_true: ArrayLike,

535

y_pred: ArrayLike,

536

sample_weight: ArrayLike | None = None,

537

multioutput: str | ArrayLike = "uniform_average"

538

) -> float | ArrayLike

539

```

540

Compute the d2 absolute error score.

541

542

#### d2_log_loss_score { .api }

543

```python

544

from sklearn.metrics import d2_log_loss_score

545

546

d2_log_loss_score(

547

y_true: ArrayLike,

548

y_pred: ArrayLike,

549

sample_weight: ArrayLike | None = None,

550

multioutput: str | ArrayLike = "uniform_average"

551

) -> float | ArrayLike

552

```

553

Compute the d2 log loss score.

554

555

#### d2_pinball_score { .api }

556

```python

557

from sklearn.metrics import d2_pinball_score

558

559

d2_pinball_score(

560

y_true: ArrayLike,

561

y_pred: ArrayLike,

562

sample_weight: ArrayLike | None = None,

563

alpha: float = 0.5,

564

multioutput: str | ArrayLike = "uniform_average"

565

) -> float | ArrayLike

566

```

567

Compute the d2 pinball score.

568

569

#### d2_tweedie_score { .api }

570

```python

571

from sklearn.metrics import d2_tweedie_score

572

573

d2_tweedie_score(

574

y_true: ArrayLike,

575

y_pred: ArrayLike,

576

sample_weight: ArrayLike | None = None,

577

power: float = 0,

578

multioutput: str | ArrayLike = "uniform_average"

579

) -> float | ArrayLike

580

```

581

Compute the d2 Tweedie score.

582

583

### Loss Functions

584

585

#### mean_pinball_loss { .api }

586

```python

587

from sklearn.metrics import mean_pinball_loss

588

589

mean_pinball_loss(

590

y_true: ArrayLike,

591

y_pred: ArrayLike,

592

sample_weight: ArrayLike | None = None,

593

alpha: float = 0.5,

594

multioutput: str | ArrayLike = "uniform_average"

595

) -> float | ArrayLike

596

```

597

Pinball loss for quantile regression.

598

599

## Clustering Metrics

600

601

### Internal Validation

602

603

#### silhouette_score { .api }

604

```python

605

from sklearn.metrics import silhouette_score

606

607

silhouette_score(

608

X: ArrayLike,

609

labels: ArrayLike,

610

metric: str | Callable = "euclidean",

611

sample_size: int | None = None,

612

random_state: int | RandomState | None = None,

613

**kwds

614

) -> float

615

```

616

Compute the mean Silhouette Coefficient of all samples.

617

618

#### silhouette_samples { .api }

619

```python

620

from sklearn.metrics import silhouette_samples

621

622

silhouette_samples(

623

X: ArrayLike,

624

labels: ArrayLike,

625

metric: str | Callable = "euclidean",

626

**kwds

627

) -> ArrayLike

628

```

629

Compute the Silhouette Coefficient for each sample.

630

631

#### calinski_harabasz_score { .api }

632

```python

633

from sklearn.metrics import calinski_harabasz_score

634

635

calinski_harabasz_score(

636

X: ArrayLike,

637

labels: ArrayLike

638

) -> float

639

```

640

Compute the Calinski and Harabasz score.

641

642

#### davies_bouldin_score { .api }

643

```python

644

from sklearn.metrics import davies_bouldin_score

645

646

davies_bouldin_score(

647

X: ArrayLike,

648

labels: ArrayLike

649

) -> float

650

```

651

Compute the Davies-Bouldin score.

652

653

### External Validation

654

655

#### adjusted_rand_score { .api }

656

```python

657

from sklearn.metrics import adjusted_rand_score

658

659

adjusted_rand_score(

660

labels_true: ArrayLike,

661

labels_pred: ArrayLike

662

) -> float

663

```

664

Rand index adjusted for chance.

665

666

#### rand_score { .api }

667

```python

668

from sklearn.metrics import rand_score

669

670

rand_score(

671

labels_true: ArrayLike,

672

labels_pred: ArrayLike

673

) -> float

674

```

675

Rand index.

676

677

#### adjusted_mutual_info_score { .api }

678

```python

679

from sklearn.metrics import adjusted_mutual_info_score

680

681

adjusted_mutual_info_score(

682

labels_true: ArrayLike,

683

labels_pred: ArrayLike,

684

average_method: str = "arithmetic"

685

) -> float

686

```

687

Adjusted Mutual Information between two clusterings.

688

689

#### normalized_mutual_info_score { .api }

690

```python

691

from sklearn.metrics import normalized_mutual_info_score

692

693

normalized_mutual_info_score(

694

labels_true: ArrayLike,

695

labels_pred: ArrayLike,

696

average_method: str = "arithmetic"

697

) -> float

698

```

699

Normalized Mutual Information between two clusterings.

700

701

#### mutual_info_score { .api }

702

```python

703

from sklearn.metrics import mutual_info_score

704

705

mutual_info_score(

706

labels_true: ArrayLike,

707

labels_pred: ArrayLike,

708

contingency: ArrayLike | None = None

709

) -> float

710

```

711

Mutual Information between two clusterings.

712

713

#### fowlkes_mallows_score { .api }

714

```python

715

from sklearn.metrics import fowlkes_mallows_score

716

717

fowlkes_mallows_score(

718

labels_true: ArrayLike,

719

labels_pred: ArrayLike,

720

sparse: bool = False

721

) -> float

722

```

723

Measure the similarity of two clusterings of a set of points.

724

725

#### homogeneity_score { .api }

726

```python

727

from sklearn.metrics import homogeneity_score

728

729

homogeneity_score(

730

labels_true: ArrayLike,

731

labels_pred: ArrayLike

732

) -> float

733

```

734

Homogeneity metric of a cluster labeling given a ground truth.

735

736

#### completeness_score { .api }

737

```python

738

from sklearn.metrics import completeness_score

739

740

completeness_score(

741

labels_true: ArrayLike,

742

labels_pred: ArrayLike

743

) -> float

744

```

745

Compute completeness metric of a cluster labeling.

746

747

#### v_measure_score { .api }

748

```python

749

from sklearn.metrics import v_measure_score

750

751

v_measure_score(

752

labels_true: ArrayLike,

753

labels_pred: ArrayLike,

754

beta: float = 1.0

755

) -> float

756

```

757

V-measure cluster labeling given a ground truth.

758

759

#### homogeneity_completeness_v_measure { .api }

760

```python

761

from sklearn.metrics import homogeneity_completeness_v_measure

762

763

homogeneity_completeness_v_measure(

764

labels_true: ArrayLike,

765

labels_pred: ArrayLike,

766

beta: float = 1.0

767

) -> tuple[float, float, float]

768

```

769

Compute the homogeneity and completeness and V-Measure scores.

770

771

### Biclustering Metrics

772

773

#### consensus_score { .api }

774

```python

775

from sklearn.metrics import consensus_score

776

777

consensus_score(

778

a: tuple[ArrayLike, ArrayLike],

779

b: tuple[ArrayLike, ArrayLike],

780

similarity: str | Callable = "jaccard"

781

) -> float

782

```

783

The similarity of two sets of biclusters.

784

785

## Multilabel Metrics

786

787

#### coverage_error { .api }

788

```python

789

from sklearn.metrics import coverage_error

790

791

coverage_error(

792

y_true: ArrayLike,

793

y_score: ArrayLike,

794

sample_weight: ArrayLike | None = None

795

) -> float

796

```

797

Coverage error measure.

798

799

#### label_ranking_average_precision_score { .api }

800

```python

801

from sklearn.metrics import label_ranking_average_precision_score

802

803

label_ranking_average_precision_score(

804

y_true: ArrayLike,

805

y_score: ArrayLike,

806

sample_weight: ArrayLike | None = None

807

) -> float

808

```

809

Compute ranking-based average precision.

810

811

#### label_ranking_loss { .api }

812

```python

813

from sklearn.metrics import label_ranking_loss

814

815

label_ranking_loss(

816

y_true: ArrayLike,

817

y_score: ArrayLike,

818

sample_weight: ArrayLike | None = None

819

) -> float

820

```

821

Compute Ranking loss measure.

822

823

## Distance Metrics

824

825

#### pairwise_distances { .api }

826

```python

827

from sklearn.metrics import pairwise_distances

828

829

pairwise_distances(

830

X: ArrayLike,

831

Y: ArrayLike | None = None,

832

metric: str | Callable = "euclidean",

833

n_jobs: int | None = None,

834

force_all_finite: bool | str = True,

835

**kwds

836

) -> ArrayLike

837

```

838

Compute the distance matrix from a vector array X and optional Y.

839

840

#### euclidean_distances { .api }

841

```python

842

from sklearn.metrics import euclidean_distances

843

844

euclidean_distances(

845

X: ArrayLike,

846

Y: ArrayLike | None = None,

847

Y_norm_squared: ArrayLike | None = None,

848

squared: bool = False,

849

X_norm_squared: ArrayLike | None = None

850

) -> ArrayLike

851

```

852

Compute the distance matrix between each pair from a vector array X.

853

854

#### manhattan_distances { .api }

855

```python

856

from sklearn.metrics import manhattan_distances

857

858

manhattan_distances(

859

X: ArrayLike,

860

Y: ArrayLike | None = None,

861

sum_over_features: bool = True

862

) -> ArrayLike

863

```

864

Compute the L1 distances between the vectors in X and Y.

865

866

#### cosine_distances { .api }

867

```python

868

from sklearn.metrics import cosine_distances

869

870

cosine_distances(

871

X: ArrayLike,

872

Y: ArrayLike | None = None

873

) -> ArrayLike

874

```

875

Compute cosine distance between samples in X and Y.

876

877

#### haversine_distances { .api }

878

```python

879

from sklearn.metrics import haversine_distances

880

881

haversine_distances(

882

X: ArrayLike,

883

Y: ArrayLike | None = None

884

) -> ArrayLike

885

```

886

Compute the Haversine distance between samples in X and Y.

887

888

### Similarity Metrics

889

890

#### cosine_similarity { .api }

891

```python

892

from sklearn.metrics import cosine_similarity

893

894

cosine_similarity(

895

X: ArrayLike,

896

Y: ArrayLike | None = None,

897

dense_output: bool = True

898

) -> ArrayLike

899

```

900

Compute cosine similarity between samples in X and Y.

901

902

#### linear_kernel { .api }

903

```python

904

from sklearn.metrics import linear_kernel

905

906

linear_kernel(

907

X: ArrayLike,

908

Y: ArrayLike | None = None,

909

dense_output: bool = True

910

) -> ArrayLike

911

```

912

Compute the linear kernel between X and Y.

913

914

#### polynomial_kernel { .api }

915

```python

916

from sklearn.metrics import polynomial_kernel

917

918

polynomial_kernel(

919

X: ArrayLike,

920

Y: ArrayLike | None = None,

921

degree: int = 3,

922

gamma: float | None = None,

923

coef0: float = 1,

924

dense_output: bool = True

925

) -> ArrayLike

926

```

927

Compute the polynomial kernel between X and Y.

928

929

#### rbf_kernel { .api }

930

```python

931

from sklearn.metrics import rbf_kernel

932

933

rbf_kernel(

934

X: ArrayLike,

935

Y: ArrayLike | None = None,

936

gamma: float | None = None,

937

dense_output: bool = True

938

) -> ArrayLike

939

```

940

Compute the rbf (gaussian) kernel between X and Y.

941

942

#### sigmoid_kernel { .api }

943

```python

944

from sklearn.metrics import sigmoid_kernel

945

946

sigmoid_kernel(

947

X: ArrayLike,

948

Y: ArrayLike | None = None,

949

gamma: float | None = None,

950

coef0: float = 1,

951

dense_output: bool = True

952

) -> ArrayLike

953

```

954

Compute the sigmoid kernel between X and Y.

955

956

#### laplacian_kernel { .api }

957

```python

958

from sklearn.metrics import laplacian_kernel

959

960

laplacian_kernel(

961

X: ArrayLike,

962

Y: ArrayLike | None = None,

963

gamma: float | None = None,

964

dense_output: bool = True

965

) -> ArrayLike

966

```

967

Compute the laplacian kernel between X and Y.

968

969

#### chi2_kernel { .api }

970

```python

971

from sklearn.metrics import chi2_kernel

972

973

chi2_kernel(

974

X: ArrayLike,

975

Y: ArrayLike | None = None,

976

gamma: float = 1.0,

977

dense_output: bool = True

978

) -> ArrayLike

979

```

980

Compute the exponential chi-squared kernel between X and Y.

981

982

#### additive_chi2_kernel { .api }

983

```python

984

from sklearn.metrics import additive_chi2_kernel

985

986

additive_chi2_kernel(

987

X: ArrayLike,

988

Y: ArrayLike | None = None,

989

dense_output: bool = True

990

) -> ArrayLike

991

```

992

Compute the additive chi-squared kernel between observations in X and Y.

993

994

### Distance Metric Class

995

996

#### DistanceMetric { .api }

997

```python

998

from sklearn.metrics import DistanceMetric

999

1000

DistanceMetric(

1001

metric: str,

1002

**kwargs

1003

)

1004

```

1005

DistanceMetric class.

1006

1007

## Ranking Metrics

1008

1009

#### dcg_score { .api }

1010

```python

1011

from sklearn.metrics import dcg_score

1012

1013

dcg_score(

1014

y_true: ArrayLike,

1015

y_score: ArrayLike,

1016

k: int | None = None,

1017

log_base: float = 2,

1018

sample_weight: ArrayLike | None = None,

1019

ignore_ties: bool = False

1020

) -> float

1021

```

1022

Compute Discounted Cumulative Gain.

1023

1024

#### ndcg_score { .api }

1025

```python

1026

from sklearn.metrics import ndcg_score

1027

1028

ndcg_score(

1029

y_true: ArrayLike,

1030

y_score: ArrayLike,

1031

k: int | None = None,

1032

sample_weight: ArrayLike | None = None,

1033

ignore_ties: bool = False

1034

) -> float

1035

```

1036

Compute Normalized Discounted Cumulative Gain.

1037

1038

## Scoring and Model Evaluation

1039

1040

### Scoring Functions

1041

1042

#### make_scorer { .api }

1043

```python

1044

from sklearn.metrics import make_scorer

1045

1046

make_scorer(

1047

score_func: Callable,

1048

greater_is_better: bool = True,

1049

needs_proba: bool = False,

1050

needs_threshold: bool = False,

1051

**kwargs

1052

) -> Callable

1053

```

1054

Make a scorer from a performance metric or loss function.

1055

1056

#### get_scorer { .api }

1057

```python

1058

from sklearn.metrics import get_scorer

1059

1060

get_scorer(

1061

scoring: str

1062

) -> Callable

1063

```

1064

Get a scorer from string.

1065

1066

#### get_scorer_names { .api }

1067

```python

1068

from sklearn.metrics import get_scorer_names

1069

1070

get_scorer_names() -> list[str]

1071

```

1072

Get the names of all available scorers.

1073

1074

#### check_scoring { .api }

1075

```python

1076

from sklearn.metrics import check_scoring

1077

1078

check_scoring(

1079

estimator: BaseEstimator,

1080

scoring: str | Callable | None = None,

1081

allow_none: bool = False

1082

) -> Callable | None

1083

```

1084

Determine scorer from user options.

1085

1086

## Visualization and Display Classes

1087

1088

### Classification Displays

1089

1090

#### ConfusionMatrixDisplay { .api }

1091

```python

1092

from sklearn.metrics import ConfusionMatrixDisplay

1093

1094

ConfusionMatrixDisplay(

1095

confusion_matrix: ArrayLike,

1096

display_labels: ArrayLike | None = None

1097

)

1098

```

1099

Confusion Matrix visualization.

1100

1101

#### RocCurveDisplay { .api }

1102

```python

1103

from sklearn.metrics import RocCurveDisplay

1104

1105

RocCurveDisplay(

1106

fpr: ArrayLike,

1107

tpr: ArrayLike,

1108

roc_auc: float | None = None,

1109

estimator_name: str | None = None,

1110

pos_label: str | int | None = None

1111

)

1112

```

1113

ROC Curve visualization.

1114

1115

#### PrecisionRecallDisplay { .api }

1116

```python

1117

from sklearn.metrics import PrecisionRecallDisplay

1118

1119

PrecisionRecallDisplay(

1120

precision: ArrayLike,

1121

recall: ArrayLike,

1122

average_precision: float | None = None,

1123

estimator_name: str | None = None,

1124

pos_label: str | int | None = None

1125

)

1126

```

1127

Precision Recall visualization.

1128

1129

#### DetCurveDisplay { .api }

1130

```python

1131

from sklearn.metrics import DetCurveDisplay

1132

1133

DetCurveDisplay(

1134

fpr: ArrayLike,

1135

fnr: ArrayLike,

1136

estimator_name: str | None = None,

1137

pos_label: str | int | None = None

1138

)

1139

```

1140

DET curve visualization.

1141

1142

### Regression Displays

1143

1144

#### PredictionErrorDisplay { .api }

1145

```python

1146

from sklearn.metrics import PredictionErrorDisplay

1147

1148

PredictionErrorDisplay(

1149

y_true: ArrayLike,

1150

y_pred: ArrayLike,

1151

kind: str = "actual_vs_predicted",

1152

subsample: float | int | None = None,

1153

random_state: int | RandomState | None = None

1154

)

1155

```

1156

Prediction error visualization.

1157

1158

## Examples

1159

1160

### Classification Metrics Example

1161

1162

```python

1163

from sklearn.metrics import (

1164

accuracy_score, precision_score, recall_score, f1_score,

1165

confusion_matrix, classification_report, roc_auc_score

1166

)

1167

from sklearn.datasets import make_classification

1168

from sklearn.ensemble import RandomForestClassifier

1169

from sklearn.model_selection import train_test_split

1170

1171

# Generate sample data

1172

X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)

1173

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

1174

1175

# Train model

1176

clf = RandomForestClassifier(n_estimators=100, random_state=42)

1177

clf.fit(X_train, y_train)

1178

1179

# Make predictions

1180

y_pred = clf.predict(X_test)

1181

y_proba = clf.predict_proba(X_test)[:, 1]

1182

1183

# Calculate metrics

1184

accuracy = accuracy_score(y_test, y_pred)

1185

precision = precision_score(y_test, y_pred)

1186

recall = recall_score(y_test, y_pred)

1187

f1 = f1_score(y_test, y_pred)

1188

roc_auc = roc_auc_score(y_test, y_proba)

1189

1190

print(f"Accuracy: {accuracy:.3f}")

1191

print(f"Precision: {precision:.3f}")

1192

print(f"Recall: {recall:.3f}")

1193

print(f"F1-score: {f1:.3f}")

1194

print(f"ROC-AUC: {roc_auc:.3f}")

1195

1196

# Confusion matrix

1197

cm = confusion_matrix(y_test, y_pred)

1198

print(f"\nConfusion Matrix:\n{cm}")

1199

1200

# Classification report

1201

report = classification_report(y_test, y_pred)

1202

print(f"\nClassification Report:\n{report}")

1203

```

1204

1205

### Regression Metrics Example

1206

1207

```python

1208

from sklearn.metrics import (

1209

mean_squared_error, mean_absolute_error, r2_score,

1210

explained_variance_score, max_error

1211

)

1212

from sklearn.datasets import make_regression

1213

from sklearn.ensemble import RandomForestRegressor

1214

1215

# Generate sample data

1216

X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=42)

1217

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

1218

1219

# Train model

1220

reg = RandomForestRegressor(n_estimators=100, random_state=42)

1221

reg.fit(X_train, y_train)

1222

1223

# Make predictions

1224

y_pred = reg.predict(X_test)

1225

1226

# Calculate metrics

1227

mse = mean_squared_error(y_test, y_pred)

1228

rmse = mean_squared_error(y_test, y_pred, squared=False)

1229

mae = mean_absolute_error(y_test, y_pred)

1230

r2 = r2_score(y_test, y_pred)

1231

ev = explained_variance_score(y_test, y_pred)

1232

max_err = max_error(y_test, y_pred)

1233

1234

print(f"MSE: {mse:.3f}")

1235

print(f"RMSE: {rmse:.3f}")

1236

print(f"MAE: {mae:.3f}")

1237

print(f"R² Score: {r2:.3f}")

1238

print(f"Explained Variance: {ev:.3f}")

1239

print(f"Max Error: {max_err:.3f}")

1240

```

1241

1242

### Clustering Metrics Example

1243

1244

```python

1245

from sklearn.metrics import (

1246

silhouette_score, calinski_harabasz_score, davies_bouldin_score,

1247

adjusted_rand_score, adjusted_mutual_info_score

1248

)

1249

from sklearn.cluster import KMeans

1250

from sklearn.datasets import make_blobs

1251

1252

# Generate sample data

1253

X, y_true = make_blobs(n_samples=300, centers=4, n_features=2,

1254

random_state=42, cluster_std=0.8)

1255

1256

# Perform clustering

1257

kmeans = KMeans(n_clusters=4, random_state=42)

1258

y_pred = kmeans.fit_predict(X)

1259

1260

# Internal validation metrics

1261

silhouette = silhouette_score(X, y_pred)

1262

calinski_harabasz = calinski_harabasz_score(X, y_pred)

1263

davies_bouldin = davies_bouldin_score(X, y_pred)

1264

1265

print(f"Silhouette Score: {silhouette:.3f}")

1266

print(f"Calinski-Harabasz Score: {calinski_harabasz:.3f}")

1267

print(f"Davies-Bouldin Score: {davies_bouldin:.3f}")

1268

1269

# External validation metrics (when true labels are available)

1270

ari = adjusted_rand_score(y_true, y_pred)

1271

ami = adjusted_mutual_info_score(y_true, y_pred)

1272

1273

print(f"Adjusted Rand Index: {ari:.3f}")

1274

print(f"Adjusted Mutual Information: {ami:.3f}")

1275

```

1276

1277

### ROC Curve and Visualization Example

1278

1279

```python

1280

from sklearn.metrics import roc_curve, RocCurveDisplay

1281

import matplotlib.pyplot as plt

1282

1283

# Calculate ROC curve

1284

fpr, tpr, thresholds = roc_curve(y_test, y_proba)

1285

1286

# Create ROC curve display

1287

roc_display = RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=roc_auc)

1288

roc_display.plot()

1289

plt.title('ROC Curve')

1290

plt.show()

1291

1292

# Or create directly from estimator

1293

RocCurveDisplay.from_estimator(clf, X_test, y_test)

1294

plt.show()

1295

```

1296

1297

### Custom Scorer Example

1298

1299

```python

1300

from sklearn.metrics import make_scorer

1301

import numpy as np

1302

1303

# Define custom scoring function

1304

def custom_accuracy(y_true, y_pred):

1305

"""Custom accuracy that weights errors differently."""

1306

return np.mean(y_true == y_pred) * 1.1 # Boost accuracy by 10%

1307

1308

# Create scorer

1309

custom_scorer = make_scorer(custom_accuracy, greater_is_better=True)

1310

1311

# Use in cross-validation or grid search

1312

from sklearn.model_selection import cross_val_score

1313

scores = cross_val_score(clf, X, y, cv=5, scoring=custom_scorer)

1314

print(f"Custom scores: {scores}")

1315

```