В ячейке 43, где они используют LabelEncoder sklearn, есть следующий цикл, который, кажется, проходит через каждую функцию, кроме цены, и кодирует ее:
Код: Выделить всё
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
df_cat_encode= df_cat.copy()
for col in df_cat_encode.select_dtypes(include='O').columns:
df_cat_encode[col]=le.fit_transform(df_cat_encode[col])
Код: Выделить всё
import polars as pl
df = pl.from_repr("""
┌──────────────────┬───────────────┬──────────┬──────────────┬───────────────┬─────────────────────┬───────┐
│ source ┆ destination ┆ cab_type ┆ name ┆ short_summary ┆ icon ┆ price │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str ┆ str ┆ str ┆ str ┆ f64 │
╞══════════════════╪═══════════════╪══════════╪══════════════╪═══════════════╪═════════════════════╪═══════╡
│ Haymarket Square ┆ North Station ┆ Lyft ┆ Shared ┆ Mostly Cloudy ┆ partly-cloudy-night ┆ 5.0 │
│ Haymarket Square ┆ North Station ┆ Lyft ┆ Lux ┆ Rain ┆ rain ┆ 11.0 │
│ Haymarket Square ┆ North Station ┆ Lyft ┆ Lyft ┆ Clear ┆ clear-night ┆ 7.0 │
│ Haymarket Square ┆ North Station ┆ Lyft ┆ Lux Black XL ┆ Clear ┆ clear-night ┆ 26.0 │
└──────────────────┴───────────────┴──────────┴──────────────┴───────────────┴─────────────────────┴───────┘
""")
Код: Выделить всё
expected = pl.from_repr("""
┌────────┬─────────────┬──────────┬──────┬───────────────┬──────┬───────┐
│ source ┆ destination ┆ cab_type ┆ name ┆ short_summary ┆ icon ┆ price │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ f64 │
╞════════╪═════════════╪══════════╪══════╪═══════════════╪══════╪═══════╡
│ 5 ┆ 7 ┆ 0 ┆ 7 ┆ 4 ┆ 5 ┆ 5.0 │
│ 5 ┆ 7 ┆ 0 ┆ 2 ┆ 8 ┆ 6 ┆ 11.0 │
│ 5 ┆ 7 ┆ 0 ┆ 5 ┆ 0 ┆ 1 ┆ 7.0 │
│ 5 ┆ 7 ┆ 0 ┆ 4 ┆ 6 ┆ 1 ┆ 26.0 │
└────────┴─────────────┴──────────┴──────┴───────────────┴──────┴───────┘
""")
Код: Выделить всё
for col in df_cat_encode.select(["source","destination","cab_type","name","short_summary","icon"]).columns:
df_cat_encode.with_columns(le.fit_transform(col))
Код: Выделить всё
Traceback (most recent call last):
File "", line 2, in
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/sklearn/preprocessing/_label.py", line 115, in fit_transform
y = column_or_1d(y, warn=True)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/sklearn/utils/validation.py", line 1038, in column_or_1d
raise ValueError(
ValueError: y should be a 1d array, got an array of shape () instead.
Подробнее здесь: https://stackoverflow.com/questions/746 ... belencoder
Мобильная версия