/
array_math.py
34 lines (28 loc) · 1.27 KB
/
array_math.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# ----------------------------------------------------------------------------
# Copyright (C) 2021-2023 Deepchecks (https://www.deepchecks.com)
#
# This file is part of Deepchecks.
# Deepchecks is distributed under the terms of the GNU Affero General
# Public License (version 3 or later).
# You should have received a copy of the GNU Affero General Public License
# along with Deepchecks. If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------------
#
"""Utils module with methods for fast calculations."""
import typing as t
import numpy as np
import pandas as pd
from deepchecks.core.errors import DeepchecksValueError
def fast_sum_by_row(matrix: np.ndarray) -> np.array:
"""Faster alternative to np.sum(matrix, axis=1)."""
return np.matmul(matrix, np.ones(matrix.shape[1]))
def sequence_to_numpy(sequence: t.Sequence):
"""Convert a sequence into a numpy array."""
if isinstance(sequence, np.ndarray):
return sequence.flatten()
elif isinstance(sequence, t.List):
return np.asarray(sequence).flatten()
elif isinstance(sequence, pd.Series):
return sequence.to_numpy().flatten()
else:
raise DeepchecksValueError('Trying to convert a non sequence into a flat list.')