Skip to content

Commit

Permalink
Merge pull request #3488 from pavlin-policar/feature-statistics-mode-fix
Browse files Browse the repository at this point in the history
[FIX] OWFeatureStatistics: Fix scipy.stats.mode crash on sparse data
  • Loading branch information
janezd authored Dec 19, 2018
2 parents c5528ed + a8f4a11 commit e5de908
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Orange/widgets/data/owfeaturestatistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
or quartile coefficient of dispersion (Q3 - Q1) / (Q3 + Q1)
- Standard deviation for nominal: try out Variation ratio (1 - n_mode/N)
"""

import datetime
import locale
from enum import IntEnum
from typing import Any, Optional, Tuple, List

import numpy as np
import scipy.stats as ss
import scipy.sparse as sp
from AnyQt.QtCore import Qt, QSize, QRectF, QVariant, QModelIndex, pyqtSlot, \
QRegExp, QItemSelection, QItemSelectionRange, QItemSelectionModel
from AnyQt.QtGui import QPainter, QColor
Expand Down Expand Up @@ -236,9 +238,18 @@ def __compute_statistics(self):
continuous_f=lambda x: ut.nanmax(x, axis=0),
time_f=lambda x: ut.nanmax(x, axis=0),
)

# Since scipy apparently can't do mode on sparse matrices, cast it to
# dense. This can be very inefficient for large matrices, and should
# be changed
def __mode(x, *args, **kwargs):
if sp.issparse(x):
x = x.todense(order="C")
return ss.mode(x, *args, **kwargs)[0]

self._center = self.__compute_stat(
matrices,
discrete_f=lambda x: ss.mode(x)[0],
discrete_f=lambda x: __mode(x, axis=0),
continuous_f=lambda x: ut.nanmean(x, axis=0),
time_f=lambda x: ut.nanmean(x, axis=0),
)
Expand Down

0 comments on commit e5de908

Please sign in to comment.