Calcularea cuantilelor și histogramelor

Transformarea datelor cu Polars

Liam Brannigan

Data Scientist & Polars Contributor

Cuantile și histograme

reviews = pl.read_csv("restaurants_quantiles.csv")
shape: (15, 4)
| business         | location      | review | price |
| ---              | ---           | ---    | ---   |
| str              | str           | f64    | i64   |
|------------------|---------------|--------|-------|
| 7burgers         | Wakey Wakey   | 4.2    | 15    |
| Bang Bang Burger | Forest Rd.    | 3.8    | 12    |
| ...              | ...           | ...    | ...   |
| The Old Ivy      | Angel         | 4.1    | 28    |
| Franco Manca     | Brixton Rd.   | 4.0    | 22    |
Transformarea datelor cu Polars

Categorizare pe bază de cuantile

Diagramă cu bare afișând prețurile restaurantelor în ordine crescătoare.

Transformarea datelor cu Polars

Categorizare pe bază de cuantile

Diagramă cu bare afișând prețurile restaurantelor în ordine crescătoare, cu percentila 33 ca linie de referință.

Transformarea datelor cu Polars

Categorizare pe bază de cuantile

Diagramă cu bare afișând prețurile restaurantelor în ordine crescătoare, cu percentilele 33 și 67 ca linii de referință.

Transformarea datelor cu Polars

Setul de date cu recenzii

reviews = pl.read_csv("restaurants_quantiles.csv")
shape: (15, 4)
| business         | location      | review | price |
| ---              | ---           | ---    | ---   |
| str              | str           | f64    | i64   |
|------------------|---------------|--------|-------|
| 7burgers         | Wakey Wakey   | 4.2    | 15    |
| Bang Bang Burger | Forest Rd.    | 3.8    | 12    |
| ...              | ...           | ...    | ...   |
| The Old Ivy      | Angel         | 4.1    | 28    |
| Franco Manca     | Brixton Rd.   | 4.0    | 22    |
Transformarea datelor cu Polars

Calcularea cuantilelor

reviews.select(


)
Transformarea datelor cu Polars

Calcularea cuantilelor

reviews.select(
    pl.col("price")

)
Transformarea datelor cu Polars

Calcularea cuantilelor

reviews.select(
    pl.col("price").quantile(0.33)

)
Transformarea datelor cu Polars

Calcularea cuantilelor

reviews.select(
    pl.col("price").quantile(0.33).alias("budget_cutoff"),

)
Transformarea datelor cu Polars

Calcularea cuantilelor

reviews.select(
    pl.col("price").quantile(0.33).alias("budget_cutoff"),
    pl.col("price").quantile(0.67).alias("mid_cutoff")
)
shape: (1, 2)
| budget_cutoff | mid_cutoff |
| ---           | ---        |
| f64           | f64        |
|---------------|------------|
| 14.0          | 20.0       |
Transformarea datelor cu Polars

Crearea categoriilor cu cuantile

reviews.with_columns(
    pl.when(pl.col("price") <= pl.col("price").quantile(0.33)).then(pl.lit("budget"))


)
Transformarea datelor cu Polars

Crearea categoriilor cu cuantile

reviews.with_columns(
    pl.when(pl.col("price") <= pl.col("price").quantile(0.33)).then(pl.lit("budget"))
    .when(pl.col("price") <= pl.col("price").quantile(0.67)).then(pl.lit("mid"))

)
Transformarea datelor cu Polars

Crearea categoriilor cu cuantile

reviews.with_columns(
    pl.when(pl.col("price") <= pl.col("price").quantile(0.33)).then(pl.lit("budget"))
    .when(pl.col("price") <= pl.col("price").quantile(0.67)).then(pl.lit("mid"))
    .otherwise(pl.lit("premium")).alias("price_band")
)
shape: (15, 5)
| business         | location      | review | price | price_band |
| ---              | ---           | ---    | ---   | ---        |
| str              | str           | f64    | i64   | str        |
|------------------|---------------|--------|-------|------------|
| 7burgers         | Wakey Wakey   | 4.2    | 15    | mid        |
| Bang Bang Burger | Forest Rd.    | 3.8    | 12    | budget     |
| ...              | ...           | ...    | ...   | ...        |
Transformarea datelor cu Polars

Cuantile versus praguri fixe

$$

Praguri bazate pe cuantile

  • Bazate pe datele curente
  • Grupuri echilibrate ca dimensiune
  • Se adaptează când datele se schimbă

$$

Praguri fixe

  • Exemplu: 10 și 20 de lire
  • Dimensiunea grupurilor este necunoscută
  • Ușor de înțeles
Transformarea datelor cu Polars

Categorii cu praguri fixe

reviews.with_columns(


)
Transformarea datelor cu Polars

Categorii cu praguri fixe

reviews.with_columns(
    pl.col("price")

)
Transformarea datelor cu Polars

Categorii cu praguri fixe

reviews.with_columns(
    pl.col("price")
    .cut(
)
Transformarea datelor cu Polars

Categorii cu praguri fixe

reviews.with_columns(
    pl.col("price")
    .cut(breaks=[10, 20] 
)
Transformarea datelor cu Polars

Categorii cu praguri fixe

reviews.with_columns(
    pl.col("price")
    .cut(breaks=[10, 20], labels=["budget", "mid", "premium"]) 
)
Transformarea datelor cu Polars

Categorii cu praguri fixe

reviews.with_columns(
    pl.col("price")
    .cut(breaks=[10, 20], labels=["budget", "mid", "premium"]).alias("price_band")
)
shape: (15, 5)
| business         | location      | review | price | price_band |
| ---              | ---           | ---    | ---   | ---        |
| str              | str           | f64    | i64   | cat        |
|------------------|---------------|--------|-------|------------|
| 7burgers         | Wakey Wakey   | 4.2    | 15    | mid        |
| Costa Coffee     | City Point    | 4.5    | 8     | budget     |
| The Queens Head  | Denman St.    | 4.7    | 25    | premium    |
| ...              | ...           | ...    | ...   | ...        |
Transformarea datelor cu Polars

Construirea unei histograme

reviews["price"]
Transformarea datelor cu Polars

Construirea unei histograme

reviews["price"].hist()
shape: (10, 3)
| breakpoint | category     | count |
| ---        | ---          | ---   |
| f64        | cat          | u32   |
|------------|--------------|-------|
| 8.4        | [6.0, 8.4]   | 3     |
| 10.8       | (8.4, 10.8]  | 1     |
| ...        | ...          | ...   |
| 27.6       | (25.2, 27.6] | 0     |
| 30.0       | (27.6, 30.0] | 2     |
Transformarea datelor cu Polars

Histogramă cu număr definit de intervale

reviews["price"].hist(bin_count=5)
shape: (5, 3)
| breakpoint | category      | count |
| ---        | ---           | ---   |
| f64        | cat           | u32   |
|------------|---------------|-------|
| 10.8       | [6.0, 10.8]   | 4     |
| 15.6       | (10.8, 15.6]  | 3     |
| 20.4       | (15.6, 20.4]  | 3     |
| 25.2       | (20.4, 25.2]  | 3     |
| 30.0       | (25.2, 30.0]  | 2     |
Transformarea datelor cu Polars

Histogramă cu puncte de separare definite

reviews["price"].hist(bins=[0, 10, 20, 100])
shape: (3, 3)
| breakpoint | category       | count |
| ---        | ---            | ---   |
| f64        | cat            | u32   |
|------------|----------------|-------|
| 10.0       | [0.0, 10.0]    | 4     |
| 20.0       | (10.0, 20.0]   | 6     |
| 100.0      | (20.0, 100.0]  | 5     |
Transformarea datelor cu Polars

Reprezentarea histogramei

import plotly.express as px


hist = reviews["price"].hist(bins=[0, 10, 20, 100])
Transformarea datelor cu Polars

Reprezentarea histogramei

import plotly.express as px

hist = reviews["price"].hist(bins=[0, 10, 20, 100])

fig = px.bar(hist, x="category", y="count")

fig.show()
Transformarea datelor cu Polars

Reprezentarea histogramei

Transformarea datelor cu Polars

Hai să exersăm!

Transformarea datelor cu Polars

Preparing Video For Download...