def from_data(cls, data, col_variable, row_variable=None): if row_variable is None: row_variable = data.domain.class_var if row_variable is None: raise ValueError("row_variable needs to be specified (data" "has no class)") row_variable = _get_variable(row_variable, data, "row_variable") col_variable = _get_variable(col_variable, data, "col_variable") try: dist, unknowns = data._compute_contingency( [col_variable], row_variable)[0] self = super().__new__(cls, dist.shape) self[...] = dist self.unknowns = unknowns except NotImplementedError: self = np.zeros( (len(row_variable.values), len(col_variable.values))) self.unknowns = 0 rind = data.domain.index(row_variable) cind = data.domain.index(col_variable) for row in data: rval, cval = row[rind], row[cind] if math.isnan(rval): continue w = row.weight if math.isnan(cval): self.unknowns[cval] += w else: self[rval, cval] += w self.row_variable = row_variable self.col_variable = col_variable return self
def from_data(cls, data, col_variable, row_variable=None): if row_variable is None: row_variable = data.domain.class_var if row_variable is None: raise ValueError("row_variable needs to be specified (data " "has no class)") row_variable = _get_variable(row_variable, data, "row_variable") col_variable = _get_variable(col_variable, data, "col_variable") try: dist, unknowns = data._compute_contingency([col_variable], row_variable)[0] self = super().__new__(cls, dist.shape) self[...] = dist self.unknowns = unknowns except NotImplementedError: self = np.zeros( (len(row_variable.values), len(col_variable.values))) self.unknowns = 0 rind = data.domain.index(row_variable) cind = data.domain.index(col_variable) for row in data: rval, cval = row[rind], row[cind] if math.isnan(rval): continue w = row.weight if math.isnan(cval): self.unknowns[cval] += w else: self[rval, cval] += w self.row_variable = row_variable self.col_variable = col_variable return self
def from_data(self, data, col_variable, row_variable=None): if row_variable is None: row_variable = data.domain.class_var if row_variable is None: raise ValueError("row_variable needs to be specified (data" "has no class)") self.row_variable = _get_variable(row_variable, data, "row_variable") self.col_variable = _get_variable(col_variable, data, "col_variable") try: (self.values, self.counts), self.unknowns = data._compute_contingency([col_variable], row_variable)[0] except NotImplementedError: raise NotImplementedError("Fallback method for computation of " "contingencies is not implemented yet")
def from_data(self, data, col_variable, row_variable=None): if row_variable is None: row_variable = data.domain.class_var if row_variable is None: raise ValueError("row_variable needs to be specified (data" "has no class)") self.row_variable = _get_variable(row_variable, data, "row_variable") self.col_variable = _get_variable(col_variable, data, "col_variable") try: self[:], self.unknowns = data._compute_contingency([col_variable], row_variable)[0] except NotImplementedError: raise NotImplementedError("Fallback method for computation of " "contingencies is not implemented yet")
def from_data(cls, data, col_variable, row_variable=None): if row_variable is None: row_variable = data.domain.class_var if row_variable is None: raise ValueError( "row_variable needs to be specified (data has no class)") row_variable = _get_variable(row_variable, data, "row_variable") col_variable = _get_variable(col_variable, data, "col_variable") try: dist, col_unknowns, row_unknowns, unknowns = \ data._compute_contingency([col_variable], row_variable)[0] self = super().__new__(cls, dist.shape) self[...] = dist self.col_unknowns = col_unknowns self.row_unknowns = row_unknowns self.unknowns = unknowns except NotImplementedError: shape = len(row_variable.values), len(col_variable.values) self = super().__new__(cls, shape) self[...] = np.zeros(shape) self.col_unknowns = np.zeros(shape[0]) self.row_unknowns = np.zeros(shape[1]) self.unknowns = 0 rind = data.domain.index(row_variable) cind = data.domain.index(col_variable) for row in data: rval, cval = row[rind], row[cind] w = row.weight if np.isnan(rval) and np.isnan(cval): self.unknowns += w elif np.isnan(rval): self.row_unknowns[int(cval)] += w elif np.isnan(cval): self.col_unknowns[int(rval)] += w else: self[int(rval), int(cval)] += w self.row_variable = row_variable self.col_variable = col_variable return self