def _check_weights(weights, n_components): """Check the user provided 'weights'. Parameters ---------- weights : array-like, shape (n_components,) The proportions of components of each mixture. n_components : int Number of components. Returns ------- weights : array, shape (n_components,) """ weights = check_array(weights, dtype=[np.float64, np.float32], ensure_2d=False) _check_shape(weights, (n_components, ), 'weights') # check range if (any(np.less(weights, 0.)) or any(np.greater(weights, 1.))): raise ValueError("The parameter 'weights' should be in the range " "[0, 1], but got max value %.5f, min value %.5f" % (np.min(weights), np.max(weights))) # check normalization if not np.allclose(np.abs(1. - np.sum(weights)), 0.): raise ValueError("The parameter 'weights' should be normalized, " "but got sum(weights) = %.5f" % np.sum(weights)) return weights
def _check_precisions(precisions, covariance_type, n_components, n_features): """Validate user provided precisions. Parameters ---------- precisions : array-like 'full' : shape of (n_components, n_features, n_features) 'tied' : shape of (n_features, n_features) 'diag' : shape of (n_components, n_features) 'spherical' : shape of (n_components,) covariance_type : string n_components : int Number of components. n_features : int Number of features. Returns ------- precisions : array """ precisions = check_array(precisions, dtype=[np.float64, np.float32], ensure_2d=False, allow_nd=covariance_type == 'full') precisions_shape = { 'full': (n_components, n_features, n_features), 'tied': (n_features, n_features), 'diag': (n_components, n_features), 'spherical': (n_components, ) } _check_shape(precisions, precisions_shape[covariance_type], '%s precision' % covariance_type) _check_precisions = { 'full': _check_precisions_full, 'tied': _check_precision_matrix, 'diag': _check_precision_positivity, 'spherical': _check_precision_positivity } _check_precisions[covariance_type](precisions, covariance_type) return precisions
def _check_means(means, n_components, n_features): """Validate the provided 'means'. Parameters ---------- means : array-like, shape (n_components, n_features) The centers of the current components. n_components : int Number of components. n_features : int Number of features. Returns ------- means : array, (n_components, n_features) """ means = check_array(means, dtype=[np.float64, np.float32], ensure_2d=False) _check_shape(means, (n_components, n_features), 'means') return means