DSPROGLIB Telegram 6527
🎯 Команда дня: строгая типизация NumPy массивов

Обычно мы пишем так:
def f(x: np.ndarray): ...


🙅‍♂️ Но это не защищает от ошибок — форма и тип данных не проверяются.

✔️ Вместо этого — используем обобщённые типы:
from numpy import ndarray, dtype, uint8, bool_, float64

def process(
x: ndarray[tuple[int], dtype[bool_]],
y: ndarray[tuple[int, int, int], dtype[uint8]],
) -> ndarray[tuple[int], dtype[float64]]:
...


Теперь mypy и pyright ловят ошибки до запуска:
🙅‍♂️ неверная размерность,
🙅‍♂️ неподходящий dtype.

👉 Хотите проверку ещё и в рантайме?

✔️ Используйте static_frame:
import static_frame as sf

@sf.CallGuard.check
def f(x: ndarray[tuple[int], dtype[np.number]]): ...


При передаче неверного массива — будет чёткое сообщение об ошибке.

Полезно для:
— ML / data science
— научных расчётов
— оптимизации с NumPy и Numba

Примеры:
ndarray[tuple[int], dtype[bool_]] — 1D массив булей
ndarray[tuple[int, int], dtype[np.str_]] — 2D массив строк
ndarray[tuple[int], dtype[np.number]] — массив любых чисел

Библиотека дата-сайентиста #буст
Please open Telegram to view this post
VIEW IN TELEGRAM



tgoop.com/dsproglib/6527
Create:
Last Update:

🎯 Команда дня: строгая типизация NumPy массивов

Обычно мы пишем так:

def f(x: np.ndarray): ...


🙅‍♂️ Но это не защищает от ошибок — форма и тип данных не проверяются.

✔️ Вместо этого — используем обобщённые типы:
from numpy import ndarray, dtype, uint8, bool_, float64

def process(
x: ndarray[tuple[int], dtype[bool_]],
y: ndarray[tuple[int, int, int], dtype[uint8]],
) -> ndarray[tuple[int], dtype[float64]]:
...


Теперь mypy и pyright ловят ошибки до запуска:
🙅‍♂️ неверная размерность,
🙅‍♂️ неподходящий dtype.

👉 Хотите проверку ещё и в рантайме?

✔️ Используйте static_frame:
import static_frame as sf

@sf.CallGuard.check
def f(x: ndarray[tuple[int], dtype[np.number]]): ...


При передаче неверного массива — будет чёткое сообщение об ошибке.

Полезно для:
— ML / data science
— научных расчётов
— оптимизации с NumPy и Numba

Примеры:
ndarray[tuple[int], dtype[bool_]] — 1D массив булей
ndarray[tuple[int, int], dtype[np.str_]] — 2D массив строк
ndarray[tuple[int], dtype[np.number]] — массив любых чисел

Библиотека дата-сайентиста #буст

BY Библиотека дата-сайентиста | Data Science, Machine learning, анализ данных, машинное обучение


Share with your friend now:
tgoop.com/dsproglib/6527

View MORE
Open in Telegram


Telegram News

Date: |

4How to customize a Telegram channel? Some Telegram Channels content management tips Step-by-step tutorial on desktop: While some crypto traders move toward screaming as a coping mechanism, many mental health experts have argued that “scream therapy” is pseudoscience. Scientific research or no, it obviously feels good. Find your optimal posting schedule and stick to it. The peak posting times include 8 am, 6 pm, and 8 pm on social media. Try to publish serious stuff in the morning and leave less demanding content later in the day.
from us


Telegram Библиотека дата-сайентиста | Data Science, Machine learning, анализ данных, машинное обучение
FROM American