myfm.MyFMGibbsClassifier¶
- class myfm.MyFMGibbsClassifier(rank: int, init_stdev: float = 0.1, random_seed: int = 42, alpha_0: float = 1.0, beta_0: float = 1.0, gamma_0: float = 1.0, mu_0: float = 0.0, reg_0: float = 1.0, fit_w0: bool = True, fit_linear: bool = True)[source]¶
Bases:
myfm.base.ClassifierMixin
[myfm._myfm.FM
,myfm._myfm.FMHyperParameters
],myfm.gibbs.MyFMGibbsBase
Bayesian Factorization Machines for binary classification tasks.
- __init__(rank: int, init_stdev: float = 0.1, random_seed: int = 42, alpha_0: float = 1.0, beta_0: float = 1.0, gamma_0: float = 1.0, mu_0: float = 0.0, reg_0: float = 1.0, fit_w0: bool = True, fit_linear: bool = True)¶
Setup the configuration.
- Parameters
rank (int) – The number of factors.
init_stdev (float, optional (defalult = 0.1)) – The standard deviation for initialization. The factorization machine weights are randomely sampled from Normal(0, init_stdev ** 2).
random_seed (integer, optional (default = 0.1)) – The random seed used inside the whole learning process.
alpha_0 (float, optional (default = 1.0)) – The half of alpha parameter for the gamma-distribution prior for alpha, lambda_w and lambda_V. Together with beta_0, the priors for these parameters are alpha, lambda_w, lambda_v ~ Gamma(alpha_0 / 2, beta_0 / 2)
beta_0 (float, optional (default = 1.0)) – See the explanation for alpha_0 .
gamma_0 (float, optional (default = 1.0)) – Inverse variance of the prior for mu_w, mu_v. Together with mu_0, the priors for these parameters are mu_w, mu_v ~ Normal(mu_0, 1 / gamma_0)
mu_0 (float, optional (default = 0.0)) – See the explanation for gamma_0.
reg_0 (float, optional (default = 0.0)) – Inverse variance of the prior for w0. w0 ~ Normal(0, 1 / reg_0)
fit_w0 (bool, optional (default = True)) – whether to fit w0, by default True.
fit_linear (bool, optional (default = True)) – whether to fit linear coefficients, by default True.
Methods
__init__
(rank[, init_stdev, random_seed, ...])Setup the configuration.
fit
(X, y[, X_rel, X_test, y_test, ...])Performs Gibbs sampling to fit the data.
get_hyper_trace
()predict
(X[, X_rel, n_workers])Based on the class probability, return binary classified outcome based on threshold = 0.5.
predict_proba
(X[, X_rel, n_workers])Compute the probability that the outcome will be 1 based on posterior predictive mean.
Attributes
Obtain the Gibbs samples for factorized quadratic coefficient V.
Obtain samples for global bias w0.
Obtain the Gibbs samples for linear coefficients w.
- property V_samples: Optional[object]¶
Obtain the Gibbs samples for factorized quadratic coefficient V. Returns None if the model is not fit yet.
- Returns:
Samples for lienar coefficients. The first dimension is for the sample index, the second for the feature index, and the third for the factorized dimension.
- fit(X: Union[numpy.ndarray, scipy.sparse.csr.csr_matrix], y: object, X_rel: List[myfm._myfm.RelationBlock] = [], X_test: Optional[Union[numpy.ndarray, scipy.sparse.csr.csr_matrix]] = None, y_test: Optional[numpy.ndarray] = None, X_rel_test: List[myfm._myfm.RelationBlock] = [], n_iter: int = 100, n_kept_samples: Optional[int] = None, grouping: Optional[List[int]] = None, group_shapes: Optional[List[int]] = None, callback: Optional[Callable[[int, myfm._myfm.FM, myfm._myfm.FMHyperParameters, myfm._myfm.LearningHistory], Tuple[bool, Optional[str]]]] = None, config_builder: Optional[myfm._myfm.ConfigBuilder] = None) myfm.gibbs.MyFMGibbsClassifier [source]¶
Performs Gibbs sampling to fit the data.
- Parameters
X (2D array-like.) – Input variable.
y (1D array-like.) – Target variable.
X_rel (list of RelationBlock, optional (default=[])) – Relation blocks which supplements X.
n_iter (int, optional (default = 100)) – Iterations to perform.
n_kept_samples (int, optional (default = None)) – The number of samples to store. If None, the value is set to n_iter - 5.
grouping (Integer List, optional (default = None)) – If not None, this specifies which column of X belongs to which group. That is, if grouping[i] is g, then, \(w_i\) and \(V_{i, r}\) will be distributed according to \(\mathcal{N}(\mu_w[g], \lambda_w[g])\) and \(\mathcal{N}(\mu_V[g, r], \lambda_V[g,r])\), respectively. If None, all the columns of X are assumed to belong to a single group, 0.
group_shapes (Integer array, optional (default = None)) – If not None, this specifies each variable group’s size. Ignored if grouping is not None. For example, if
group_shapes = [n_1, n_2]
, this is equivalent togrouping = [0] * n_1 + [1] * n_2
callback (function(int, fm, hyper, history) -> (bool, str), optional(default = None)) – Called at the every end of each Gibbs iteration.
- predict(X: Optional[Union[numpy.ndarray, scipy.sparse.csr.csr_matrix]], X_rel: List[myfm._myfm.RelationBlock] = [], n_workers: Optional[int] = None) object [source]¶
Based on the class probability, return binary classified outcome based on threshold = 0.5. If you want class probability instead, use predict_proba method.
- Parameters
X (Optional[ArrayLike]) – When None, treated as a matrix with no column.
X_rel (List[RelationBlock]) – Relations.
n_workers (Optional[int], optional) – The number of threads to compute the posterior predictive mean, by default None
- Returns
One-dimensional array of predicted outcomes.
- Return type
np.ndarray
- predict_proba(X: Optional[Union[numpy.ndarray, scipy.sparse.csr.csr_matrix]], X_rel: List[myfm._myfm.RelationBlock] = [], n_workers: Optional[int] = None) object [source]¶
Compute the probability that the outcome will be 1 based on posterior predictive mean.
- Parameters
X (Optional[ArrayLike]) – When None, treated as a matrix with no column.
X_rel (List[RelationBlock]) – Relations.
n_workers (Optional[int], optional) – The number of threads to compute the posterior predictive mean, by default None
- Returns
One-dimensional array of probabilities.
- Return type
np.ndarray
- property w0_samples: Optional[object]¶
Obtain samples for global bias w0. If the model is not fit yet, return None.
- Returns:
Samples for lienar coefficients.
- property w_samples: Optional[object]¶
Obtain the Gibbs samples for linear coefficients w. Returns None if the model is not fit yet.
- Returns:
Samples for lienar coefficients. The first dimension is for the sample index, and the second for the feature index.