repo_name
stringlengths
7
92
path
stringlengths
5
149
copies
stringlengths
1
3
size
stringlengths
4
6
content
stringlengths
911
693k
license
stringclasses
15 values
plissonf/scikit-learn
sklearn/linear_model/coordinate_descent.py
59
76336
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr> # Fabian Pedregosa <fabian.pedregosa@inria.fr> # Olivier Grisel <olivier.grisel@ensta.org> # Gael Varoquaux <gael.varoquaux@inria.fr> # # License: BSD 3 clause import sys import warnings from abc import ABCMeta, abstractmethod import numpy as np from scipy import sparse from .base import LinearModel, _pre_fit from ..base import RegressorMixin from .base import center_data, sparse_center_data from ..utils import check_array, check_X_y, deprecated from ..utils.validation import check_random_state from ..cross_validation import check_cv from ..externals.joblib import Parallel, delayed from ..externals import six from ..externals.six.moves import xrange from ..utils.extmath import safe_sparse_dot from ..utils.validation import check_is_fitted from ..utils.validation import column_or_1d from ..utils import ConvergenceWarning from . import cd_fast ############################################################################### # Paths functions def _alpha_grid(X, y, Xy=None, l1_ratio=1.0, fit_intercept=True, eps=1e-3, n_alphas=100, normalize=False, copy_X=True): """ Compute the grid of alpha values for elastic net parameter search Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data. Pass directly as Fortran-contiguous data to avoid unnecessary memory duplication y : ndarray, shape (n_samples,) Target values Xy : array-like, optional Xy = np.dot(X.T, y) that can be precomputed. l1_ratio : float The elastic net mixing parameter, with ``0 <= l1_ratio <= 1``. For ``l1_ratio = 0`` the penalty is an L2 penalty. ``For l1_ratio = 1`` it is an L1 penalty. For ``0 < l1_ratio < 1``, the penalty is a combination of L1 and L2. eps : float, optional Length of the path. ``eps=1e-3`` means that ``alpha_min / alpha_max = 1e-3`` n_alphas : int, optional Number of alphas along the regularization path fit_intercept : boolean, default True Whether to fit an intercept or not normalize : boolean, optional, default False If ``True``, the regressors X will be normalized before regression. copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. """ n_samples = len(y) sparse_center = False if Xy is None: X_sparse = sparse.isspmatrix(X) sparse_center = X_sparse and (fit_intercept or normalize) X = check_array(X, 'csc', copy=(copy_X and fit_intercept and not X_sparse)) if not X_sparse: # X can be touched inplace thanks to the above line X, y, _, _, _ = center_data(X, y, fit_intercept, normalize, copy=False) Xy = safe_sparse_dot(X.T, y, dense_output=True) if sparse_center: # Workaround to find alpha_max for sparse matrices. # since we should not destroy the sparsity of such matrices. _, _, X_mean, _, X_std = sparse_center_data(X, y, fit_intercept, normalize) mean_dot = X_mean * np.sum(y) if Xy.ndim == 1: Xy = Xy[:, np.newaxis] if sparse_center: if fit_intercept: Xy -= mean_dot[:, np.newaxis] if normalize: Xy /= X_std[:, np.newaxis] alpha_max = (np.sqrt(np.sum(Xy ** 2, axis=1)).max() / (n_samples * l1_ratio)) if alpha_max <= np.finfo(float).resolution: alphas = np.empty(n_alphas) alphas.fill(np.finfo(float).resolution) return alphas return np.logspace(np.log10(alpha_max * eps), np.log10(alpha_max), num=n_alphas)[::-1] def lasso_path(X, y, eps=1e-3, n_alphas=100, alphas=None, precompute='auto', Xy=None, copy_X=True, coef_init=None, verbose=False, return_n_iter=False, positive=False, **params): """Compute Lasso path with coordinate descent The Lasso optimization function varies for mono and multi-outputs. For mono-output tasks it is:: (1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1 For multi-output tasks it is:: (1 / (2 * n_samples)) * ||Y - XW||^2_Fro + alpha * ||W||_21 Where:: ||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2} i.e. the sum of norm of each row. Read more in the :ref:`User Guide <lasso>`. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data. Pass directly as Fortran-contiguous data to avoid unnecessary memory duplication. If ``y`` is mono-output then ``X`` can be sparse. y : ndarray, shape (n_samples,), or (n_samples, n_outputs) Target values eps : float, optional Length of the path. ``eps=1e-3`` means that ``alpha_min / alpha_max = 1e-3`` n_alphas : int, optional Number of alphas along the regularization path alphas : ndarray, optional List of alphas where to compute the models. If ``None`` alphas are set automatically precompute : True | False | 'auto' | array-like Whether to use a precomputed Gram matrix to speed up calculations. If set to ``'auto'`` let us decide. The Gram matrix can also be passed as argument. Xy : array-like, optional Xy = np.dot(X.T, y) that can be precomputed. It is useful only when the Gram matrix is precomputed. copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. coef_init : array, shape (n_features, ) | None The initial values of the coefficients. verbose : bool or integer Amount of verbosity. params : kwargs keyword arguments passed to the coordinate descent solver. positive : bool, default False If set to True, forces coefficients to be positive. return_n_iter : bool whether to return the number of iterations or not. Returns ------- alphas : array, shape (n_alphas,) The alphas along the path where models are computed. coefs : array, shape (n_features, n_alphas) or \ (n_outputs, n_features, n_alphas) Coefficients along the path. dual_gaps : array, shape (n_alphas,) The dual gaps at the end of the optimization for each alpha. n_iters : array-like, shape (n_alphas,) The number of iterations taken by the coordinate descent optimizer to reach the specified tolerance for each alpha. Notes ----- See examples/linear_model/plot_lasso_coordinate_descent_path.py for an example. To avoid unnecessary memory duplication the X argument of the fit method should be directly passed as a Fortran-contiguous numpy array. Note that in certain cases, the Lars solver may be significantly faster to implement this functionality. In particular, linear interpolation can be used to retrieve model coefficients between the values output by lars_path Examples --------- Comparing lasso_path and lars_path with interpolation: >>> X = np.array([[1, 2, 3.1], [2.3, 5.4, 4.3]]).T >>> y = np.array([1, 2, 3.1]) >>> # Use lasso_path to compute a coefficient path >>> _, coef_path, _ = lasso_path(X, y, alphas=[5., 1., .5]) >>> print(coef_path) [[ 0. 0. 0.46874778] [ 0.2159048 0.4425765 0.23689075]] >>> # Now use lars_path and 1D linear interpolation to compute the >>> # same path >>> from sklearn.linear_model import lars_path >>> alphas, active, coef_path_lars = lars_path(X, y, method='lasso') >>> from scipy import interpolate >>> coef_path_continuous = interpolate.interp1d(alphas[::-1], ... coef_path_lars[:, ::-1]) >>> print(coef_path_continuous([5., 1., .5])) [[ 0. 0. 0.46915237] [ 0.2159048 0.4425765 0.23668876]] See also -------- lars_path Lasso LassoLars LassoCV LassoLarsCV sklearn.decomposition.sparse_encode """ return enet_path(X, y, l1_ratio=1., eps=eps, n_alphas=n_alphas, alphas=alphas, precompute=precompute, Xy=Xy, copy_X=copy_X, coef_init=coef_init, verbose=verbose, positive=positive, **params) def enet_path(X, y, l1_ratio=0.5, eps=1e-3, n_alphas=100, alphas=None, precompute='auto', Xy=None, copy_X=True, coef_init=None, verbose=False, return_n_iter=False, positive=False, **params): """Compute elastic net path with coordinate descent The elastic net optimization function varies for mono and multi-outputs. For mono-output tasks it is:: 1 / (2 * n_samples) * ||y - Xw||^2_2 + + alpha * l1_ratio * ||w||_1 + 0.5 * alpha * (1 - l1_ratio) * ||w||^2_2 For multi-output tasks it is:: (1 / (2 * n_samples)) * ||Y - XW||^Fro_2 + alpha * l1_ratio * ||W||_21 + 0.5 * alpha * (1 - l1_ratio) * ||W||_Fro^2 Where:: ||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2} i.e. the sum of norm of each row. Read more in the :ref:`User Guide <elastic_net>`. Parameters ---------- X : {array-like}, shape (n_samples, n_features) Training data. Pass directly as Fortran-contiguous data to avoid unnecessary memory duplication. If ``y`` is mono-output then ``X`` can be sparse. y : ndarray, shape (n_samples,) or (n_samples, n_outputs) Target values l1_ratio : float, optional float between 0 and 1 passed to elastic net (scaling between l1 and l2 penalties). ``l1_ratio=1`` corresponds to the Lasso eps : float Length of the path. ``eps=1e-3`` means that ``alpha_min / alpha_max = 1e-3`` n_alphas : int, optional Number of alphas along the regularization path alphas : ndarray, optional List of alphas where to compute the models. If None alphas are set automatically precompute : True | False | 'auto' | array-like Whether to use a precomputed Gram matrix to speed up calculations. If set to ``'auto'`` let us decide. The Gram matrix can also be passed as argument. Xy : array-like, optional Xy = np.dot(X.T, y) that can be precomputed. It is useful only when the Gram matrix is precomputed. copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. coef_init : array, shape (n_features, ) | None The initial values of the coefficients. verbose : bool or integer Amount of verbosity. params : kwargs keyword arguments passed to the coordinate descent solver. return_n_iter : bool whether to return the number of iterations or not. positive : bool, default False If set to True, forces coefficients to be positive. Returns ------- alphas : array, shape (n_alphas,) The alphas along the path where models are computed. coefs : array, shape (n_features, n_alphas) or \ (n_outputs, n_features, n_alphas) Coefficients along the path. dual_gaps : array, shape (n_alphas,) The dual gaps at the end of the optimization for each alpha. n_iters : array-like, shape (n_alphas,) The number of iterations taken by the coordinate descent optimizer to reach the specified tolerance for each alpha. (Is returned when ``return_n_iter`` is set to True). Notes ----- See examples/plot_lasso_coordinate_descent_path.py for an example. See also -------- MultiTaskElasticNet MultiTaskElasticNetCV ElasticNet ElasticNetCV """ # We expect X and y to be already float64 Fortran ordered when bypassing # checks check_input = 'check_input' not in params or params['check_input'] pre_fit = 'check_input' not in params or params['pre_fit'] if check_input: X = check_array(X, 'csc', dtype=np.float64, order='F', copy=copy_X) y = check_array(y, 'csc', dtype=np.float64, order='F', copy=False, ensure_2d=False) if Xy is not None: Xy = check_array(Xy, 'csc', dtype=np.float64, order='F', copy=False, ensure_2d=False) n_samples, n_features = X.shape multi_output = False if y.ndim != 1: multi_output = True _, n_outputs = y.shape # MultiTaskElasticNet does not support sparse matrices if not multi_output and sparse.isspmatrix(X): if 'X_mean' in params: # As sparse matrices are not actually centered we need this # to be passed to the CD solver. X_sparse_scaling = params['X_mean'] / params['X_std'] else: X_sparse_scaling = np.zeros(n_features) # X should be normalized and fit already if function is called # from ElasticNet.fit if pre_fit: X, y, X_mean, y_mean, X_std, precompute, Xy = \ _pre_fit(X, y, Xy, precompute, normalize=False, fit_intercept=False, copy=False, Xy_precompute_order='F') if alphas is None: # No need to normalize of fit_intercept: it has been done # above alphas = _alpha_grid(X, y, Xy=Xy, l1_ratio=l1_ratio, fit_intercept=False, eps=eps, n_alphas=n_alphas, normalize=False, copy_X=False) else: alphas = np.sort(alphas)[::-1] # make sure alphas are properly ordered n_alphas = len(alphas) tol = params.get('tol', 1e-4) max_iter = params.get('max_iter', 1000) dual_gaps = np.empty(n_alphas) n_iters = [] rng = check_random_state(params.get('random_state', None)) selection = params.get('selection', 'cyclic') if selection not in ['random', 'cyclic']: raise ValueError("selection should be either random or cyclic.") random = (selection == 'random') if not multi_output: coefs = np.empty((n_features, n_alphas), dtype=np.float64) else: coefs = np.empty((n_outputs, n_features, n_alphas), dtype=np.float64) if coef_init is None: coef_ = np.asfortranarray(np.zeros(coefs.shape[:-1])) else: coef_ = np.asfortranarray(coef_init) for i, alpha in enumerate(alphas): l1_reg = alpha * l1_ratio * n_samples l2_reg = alpha * (1.0 - l1_ratio) * n_samples if not multi_output and sparse.isspmatrix(X): model = cd_fast.sparse_enet_coordinate_descent( coef_, l1_reg, l2_reg, X.data, X.indices, X.indptr, y, X_sparse_scaling, max_iter, tol, rng, random, positive) elif multi_output: model = cd_fast.enet_coordinate_descent_multi_task( coef_, l1_reg, l2_reg, X, y, max_iter, tol, rng, random) elif isinstance(precompute, np.ndarray): # We expect precompute to be already Fortran ordered when bypassing # checks if check_input: precompute = check_array(precompute, 'csc', dtype=np.float64, order='F') model = cd_fast.enet_coordinate_descent_gram( coef_, l1_reg, l2_reg, precompute, Xy, y, max_iter, tol, rng, random, positive) elif precompute is False: model = cd_fast.enet_coordinate_descent( coef_, l1_reg, l2_reg, X, y, max_iter, tol, rng, random, positive) else: raise ValueError("Precompute should be one of True, False, " "'auto' or array-like") coef_, dual_gap_, eps_, n_iter_ = model coefs[..., i] = coef_ dual_gaps[i] = dual_gap_ n_iters.append(n_iter_) if dual_gap_ > eps_: warnings.warn('Objective did not converge.' + ' You might want' + ' to increase the number of iterations', ConvergenceWarning) if verbose: if verbose > 2: print(model) elif verbose > 1: print('Path: %03i out of %03i' % (i, n_alphas)) else: sys.stderr.write('.') if return_n_iter: return alphas, coefs, dual_gaps, n_iters return alphas, coefs, dual_gaps ############################################################################### # ElasticNet model class ElasticNet(LinearModel, RegressorMixin): """Linear regression with combined L1 and L2 priors as regularizer. Minimizes the objective function:: 1 / (2 * n_samples) * ||y - Xw||^2_2 + + alpha * l1_ratio * ||w||_1 + 0.5 * alpha * (1 - l1_ratio) * ||w||^2_2 If you are interested in controlling the L1 and L2 penalty separately, keep in mind that this is equivalent to:: a * L1 + b * L2 where:: alpha = a + b and l1_ratio = a / (a + b) The parameter l1_ratio corresponds to alpha in the glmnet R package while alpha corresponds to the lambda parameter in glmnet. Specifically, l1_ratio = 1 is the lasso penalty. Currently, l1_ratio <= 0.01 is not reliable, unless you supply your own sequence of alpha. Read more in the :ref:`User Guide <elastic_net>`. Parameters ---------- alpha : float Constant that multiplies the penalty terms. Defaults to 1.0 See the notes for the exact mathematical meaning of this parameter. ``alpha = 0`` is equivalent to an ordinary least square, solved by the :class:`LinearRegression` object. For numerical reasons, using ``alpha = 0`` with the Lasso object is not advised and you should prefer the LinearRegression object. l1_ratio : float The ElasticNet mixing parameter, with ``0 <= l1_ratio <= 1``. For ``l1_ratio = 0`` the penalty is an L2 penalty. ``For l1_ratio = 1`` it is an L1 penalty. For ``0 < l1_ratio < 1``, the penalty is a combination of L1 and L2. fit_intercept : bool Whether the intercept should be estimated or not. If ``False``, the data is assumed to be already centered. normalize : boolean, optional, default False If ``True``, the regressors X will be normalized before regression. precompute : True | False | 'auto' | array-like Whether to use a precomputed Gram matrix to speed up calculations. If set to ``'auto'`` let us decide. The Gram matrix can also be passed as argument. For sparse input this option is always ``True`` to preserve sparsity. WARNING : The ``'auto'`` option is deprecated and will be removed in 0.18. max_iter : int, optional The maximum number of iterations copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. tol : float, optional The tolerance for the optimization: if the updates are smaller than ``tol``, the optimization code checks the dual gap for optimality and continues until it is smaller than ``tol``. warm_start : bool, optional When set to ``True``, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. positive : bool, optional When set to ``True``, forces the coefficients to be positive. selection : str, default 'cyclic' If set to 'random', a random coefficient is updated every iteration rather than looping over features sequentially by default. This (setting to 'random') often leads to significantly faster convergence especially when tol is higher than 1e-4. random_state : int, RandomState instance, or None (default) The seed of the pseudo random number generator that selects a random feature to update. Useful only when selection is set to 'random'. Attributes ---------- coef_ : array, shape (n_features,) | (n_targets, n_features) parameter vector (w in the cost function formula) sparse_coef_ : scipy.sparse matrix, shape (n_features, 1) | \ (n_targets, n_features) ``sparse_coef_`` is a readonly property derived from ``coef_`` intercept_ : float | array, shape (n_targets,) independent term in decision function. n_iter_ : array-like, shape (n_targets,) number of iterations run by the coordinate descent solver to reach the specified tolerance. Notes ----- To avoid unnecessary memory duplication the X argument of the fit method should be directly passed as a Fortran-contiguous numpy array. See also -------- SGDRegressor: implements elastic net regression with incremental training. SGDClassifier: implements logistic regression with elastic net penalty (``SGDClassifier(loss="log", penalty="elasticnet")``). """ path = staticmethod(enet_path) def __init__(self, alpha=1.0, l1_ratio=0.5, fit_intercept=True, normalize=False, precompute=False, max_iter=1000, copy_X=True, tol=1e-4, warm_start=False, positive=False, random_state=None, selection='cyclic'): self.alpha = alpha self.l1_ratio = l1_ratio self.coef_ = None self.fit_intercept = fit_intercept self.normalize = normalize self.precompute = precompute self.max_iter = max_iter self.copy_X = copy_X self.tol = tol self.warm_start = warm_start self.positive = positive self.intercept_ = 0.0 self.random_state = random_state self.selection = selection def fit(self, X, y, check_input=True): """Fit model with coordinate descent. Parameters ----------- X : ndarray or scipy.sparse matrix, (n_samples, n_features) Data y : ndarray, shape (n_samples,) or (n_samples, n_targets) Target Notes ----- Coordinate descent is an algorithm that considers each column of data at a time hence it will automatically convert the X input as a Fortran-contiguous numpy array if necessary. To avoid memory re-allocation it is advised to allocate the initial data in memory directly using that format. """ if self.alpha == 0: warnings.warn("With alpha=0, this algorithm does not converge " "well. You are advised to use the LinearRegression " "estimator", stacklevel=2) if self.precompute == 'auto': warnings.warn("Setting precompute to 'auto', was found to be " "slower even when n_samples > n_features. Hence " "it will be removed in 0.18.", DeprecationWarning, stacklevel=2) # We expect X and y to be already float64 Fortran ordered arrays # when bypassing checks if check_input: X, y = check_X_y(X, y, accept_sparse='csc', dtype=np.float64, order='F', copy=self.copy_X and self.fit_intercept, multi_output=True, y_numeric=True) X, y, X_mean, y_mean, X_std, precompute, Xy = \ _pre_fit(X, y, None, self.precompute, self.normalize, self.fit_intercept, copy=False, Xy_precompute_order='F') if y.ndim == 1: y = y[:, np.newaxis] if Xy is not None and Xy.ndim == 1: Xy = Xy[:, np.newaxis] n_samples, n_features = X.shape n_targets = y.shape[1] if self.selection not in ['cyclic', 'random']: raise ValueError("selection should be either random or cyclic.") if not self.warm_start or self.coef_ is None: coef_ = np.zeros((n_targets, n_features), dtype=np.float64, order='F') else: coef_ = self.coef_ if coef_.ndim == 1: coef_ = coef_[np.newaxis, :] dual_gaps_ = np.zeros(n_targets, dtype=np.float64) self.n_iter_ = [] for k in xrange(n_targets): if Xy is not None: this_Xy = Xy[:, k] else: this_Xy = None _, this_coef, this_dual_gap, this_iter = \ self.path(X, y[:, k], l1_ratio=self.l1_ratio, eps=None, n_alphas=None, alphas=[self.alpha], precompute=precompute, Xy=this_Xy, fit_intercept=False, normalize=False, copy_X=True, verbose=False, tol=self.tol, positive=self.positive, X_mean=X_mean, X_std=X_std, return_n_iter=True, coef_init=coef_[k], max_iter=self.max_iter, random_state=self.random_state, selection=self.selection, check_input=False, pre_fit=False) coef_[k] = this_coef[:, 0] dual_gaps_[k] = this_dual_gap[0] self.n_iter_.append(this_iter[0]) if n_targets == 1: self.n_iter_ = self.n_iter_[0] self.coef_, self.dual_gap_ = map(np.squeeze, [coef_, dual_gaps_]) self._set_intercept(X_mean, y_mean, X_std) # return self for chaining fit and predict calls return self @property def sparse_coef_(self): """ sparse representation of the fitted coef """ return sparse.csr_matrix(self.coef_) @deprecated(" and will be removed in 0.19") def decision_function(self, X): """Decision function of the linear model Parameters ---------- X : numpy array or scipy.sparse matrix of shape (n_samples, n_features) Returns ------- T : array, shape (n_samples,) The predicted decision function """ return self._decision_function(X) def _decision_function(self, X): """Decision function of the linear model Parameters ---------- X : numpy array or scipy.sparse matrix of shape (n_samples, n_features) Returns ------- T : array, shape (n_samples,) The predicted decision function """ check_is_fitted(self, 'n_iter_') if sparse.isspmatrix(X): return np.ravel(safe_sparse_dot(self.coef_, X.T, dense_output=True) + self.intercept_) else: return super(ElasticNet, self)._decision_function(X) ############################################################################### # Lasso model class Lasso(ElasticNet): """Linear Model trained with L1 prior as regularizer (aka the Lasso) The optimization objective for Lasso is:: (1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1 Technically the Lasso model is optimizing the same objective function as the Elastic Net with ``l1_ratio=1.0`` (no L2 penalty). Read more in the :ref:`User Guide <lasso>`. Parameters ---------- alpha : float, optional Constant that multiplies the L1 term. Defaults to 1.0. ``alpha = 0`` is equivalent to an ordinary least square, solved by the :class:`LinearRegression` object. For numerical reasons, using ``alpha = 0`` is with the Lasso object is not advised and you should prefer the LinearRegression object. fit_intercept : boolean whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (e.g. data is expected to be already centered). normalize : boolean, optional, default False If ``True``, the regressors X will be normalized before regression. copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. precompute : True | False | 'auto' | array-like Whether to use a precomputed Gram matrix to speed up calculations. If set to ``'auto'`` let us decide. The Gram matrix can also be passed as argument. For sparse input this option is always ``True`` to preserve sparsity. WARNING : The ``'auto'`` option is deprecated and will be removed in 0.18. max_iter : int, optional The maximum number of iterations tol : float, optional The tolerance for the optimization: if the updates are smaller than ``tol``, the optimization code checks the dual gap for optimality and continues until it is smaller than ``tol``. warm_start : bool, optional When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. positive : bool, optional When set to ``True``, forces the coefficients to be positive. selection : str, default 'cyclic' If set to 'random', a random coefficient is updated every iteration rather than looping over features sequentially by default. This (setting to 'random') often leads to significantly faster convergence especially when tol is higher than 1e-4. random_state : int, RandomState instance, or None (default) The seed of the pseudo random number generator that selects a random feature to update. Useful only when selection is set to 'random'. Attributes ---------- coef_ : array, shape (n_features,) | (n_targets, n_features) parameter vector (w in the cost function formula) sparse_coef_ : scipy.sparse matrix, shape (n_features, 1) | \ (n_targets, n_features) ``sparse_coef_`` is a readonly property derived from ``coef_`` intercept_ : float | array, shape (n_targets,) independent term in decision function. n_iter_ : int | array-like, shape (n_targets,) number of iterations run by the coordinate descent solver to reach the specified tolerance. Examples -------- >>> from sklearn import linear_model >>> clf = linear_model.Lasso(alpha=0.1) >>> clf.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2]) Lasso(alpha=0.1, copy_X=True, fit_intercept=True, max_iter=1000, normalize=False, positive=False, precompute=False, random_state=None, selection='cyclic', tol=0.0001, warm_start=False) >>> print(clf.coef_) [ 0.85 0. ] >>> print(clf.intercept_) 0.15 See also -------- lars_path lasso_path LassoLars LassoCV LassoLarsCV sklearn.decomposition.sparse_encode Notes ----- The algorithm used to fit the model is coordinate descent. To avoid unnecessary memory duplication the X argument of the fit method should be directly passed as a Fortran-contiguous numpy array. """ path = staticmethod(enet_path) def __init__(self, alpha=1.0, fit_intercept=True, normalize=False, precompute=False, copy_X=True, max_iter=1000, tol=1e-4, warm_start=False, positive=False, random_state=None, selection='cyclic'): super(Lasso, self).__init__( alpha=alpha, l1_ratio=1.0, fit_intercept=fit_intercept, normalize=normalize, precompute=precompute, copy_X=copy_X, max_iter=max_iter, tol=tol, warm_start=warm_start, positive=positive, random_state=random_state, selection=selection) ############################################################################### # Functions for CV with paths functions def _path_residuals(X, y, train, test, path, path_params, alphas=None, l1_ratio=1, X_order=None, dtype=None): """Returns the MSE for the models computed by 'path' Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data. y : array-like, shape (n_samples,) or (n_samples, n_targets) Target values train : list of indices The indices of the train set test : list of indices The indices of the test set path : callable function returning a list of models on the path. See enet_path for an example of signature path_params : dictionary Parameters passed to the path function alphas : array-like, optional Array of float that is used for cross-validation. If not provided, computed using 'path' l1_ratio : float, optional float between 0 and 1 passed to ElasticNet (scaling between l1 and l2 penalties). For ``l1_ratio = 0`` the penalty is an L2 penalty. For ``l1_ratio = 1`` it is an L1 penalty. For ``0 < l1_ratio < 1``, the penalty is a combination of L1 and L2 X_order : {'F', 'C', or None}, optional The order of the arrays expected by the path function to avoid memory copies dtype : a numpy dtype or None The dtype of the arrays expected by the path function to avoid memory copies """ X_train = X[train] y_train = y[train] X_test = X[test] y_test = y[test] fit_intercept = path_params['fit_intercept'] normalize = path_params['normalize'] if y.ndim == 1: precompute = path_params['precompute'] else: # No Gram variant of multi-task exists right now. # Fall back to default enet_multitask precompute = False X_train, y_train, X_mean, y_mean, X_std, precompute, Xy = \ _pre_fit(X_train, y_train, None, precompute, normalize, fit_intercept, copy=False) path_params = path_params.copy() path_params['Xy'] = Xy path_params['X_mean'] = X_mean path_params['X_std'] = X_std path_params['precompute'] = precompute path_params['copy_X'] = False path_params['alphas'] = alphas if 'l1_ratio' in path_params: path_params['l1_ratio'] = l1_ratio # Do the ordering and type casting here, as if it is done in the path, # X is copied and a reference is kept here X_train = check_array(X_train, 'csc', dtype=dtype, order=X_order) alphas, coefs, _ = path(X_train, y_train, **path_params) del X_train, y_train if y.ndim == 1: # Doing this so that it becomes coherent with multioutput. coefs = coefs[np.newaxis, :, :] y_mean = np.atleast_1d(y_mean) y_test = y_test[:, np.newaxis] if normalize: nonzeros = np.flatnonzero(X_std) coefs[:, nonzeros] /= X_std[nonzeros][:, np.newaxis] intercepts = y_mean[:, np.newaxis] - np.dot(X_mean, coefs) if sparse.issparse(X_test): n_order, n_features, n_alphas = coefs.shape # Work around for sparse matices since coefs is a 3-D numpy array. coefs_feature_major = np.rollaxis(coefs, 1) feature_2d = np.reshape(coefs_feature_major, (n_features, -1)) X_test_coefs = safe_sparse_dot(X_test, feature_2d) X_test_coefs = X_test_coefs.reshape(X_test.shape[0], n_order, -1) else: X_test_coefs = safe_sparse_dot(X_test, coefs) residues = X_test_coefs - y_test[:, :, np.newaxis] residues += intercepts this_mses = ((residues ** 2).mean(axis=0)).mean(axis=0) return this_mses class LinearModelCV(six.with_metaclass(ABCMeta, LinearModel)): """Base class for iterative model fitting along a regularization path""" @abstractmethod def __init__(self, eps=1e-3, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, precompute='auto', max_iter=1000, tol=1e-4, copy_X=True, cv=None, verbose=False, n_jobs=1, positive=False, random_state=None, selection='cyclic'): self.eps = eps self.n_alphas = n_alphas self.alphas = alphas self.fit_intercept = fit_intercept self.normalize = normalize self.precompute = precompute self.max_iter = max_iter self.tol = tol self.copy_X = copy_X self.cv = cv self.verbose = verbose self.n_jobs = n_jobs self.positive = positive self.random_state = random_state self.selection = selection def fit(self, X, y): """Fit linear model with coordinate descent Fit is on grid of alphas and best alpha estimated by cross-validation. Parameters ---------- X : {array-like}, shape (n_samples, n_features) Training data. Pass directly as float64, Fortran-contiguous data to avoid unnecessary memory duplication. If y is mono-output, X can be sparse. y : array-like, shape (n_samples,) or (n_samples, n_targets) Target values """ y = np.asarray(y, dtype=np.float64) if y.shape[0] == 0: raise ValueError("y has 0 samples: %r" % y) if hasattr(self, 'l1_ratio'): model_str = 'ElasticNet' else: model_str = 'Lasso' if isinstance(self, ElasticNetCV) or isinstance(self, LassoCV): if model_str == 'ElasticNet': model = ElasticNet() else: model = Lasso() if y.ndim > 1 and y.shape[1] > 1: raise ValueError("For multi-task outputs, use " "MultiTask%sCV" % (model_str)) y = column_or_1d(y, warn=True) else: if sparse.isspmatrix(X): raise TypeError("X should be dense but a sparse matrix was" "passed") elif y.ndim == 1: raise ValueError("For mono-task outputs, use " "%sCV" % (model_str)) if model_str == 'ElasticNet': model = MultiTaskElasticNet() else: model = MultiTaskLasso() if self.selection not in ["random", "cyclic"]: raise ValueError("selection should be either random or cyclic.") # This makes sure that there is no duplication in memory. # Dealing right with copy_X is important in the following: # Multiple functions touch X and subsamples of X and can induce a # lot of duplication of memory copy_X = self.copy_X and self.fit_intercept if isinstance(X, np.ndarray) or sparse.isspmatrix(X): # Keep a reference to X reference_to_old_X = X # Let us not impose fortran ordering or float64 so far: it is # not useful for the cross-validation loop and will be done # by the model fitting itself X = check_array(X, 'csc', copy=False) if sparse.isspmatrix(X): if (hasattr(reference_to_old_X, "data") and not np.may_share_memory(reference_to_old_X.data, X.data)): # X is a sparse matrix and has been copied copy_X = False elif not np.may_share_memory(reference_to_old_X, X): # X has been copied copy_X = False del reference_to_old_X else: X = check_array(X, 'csc', dtype=np.float64, order='F', copy=copy_X) copy_X = False if X.shape[0] != y.shape[0]: raise ValueError("X and y have inconsistent dimensions (%d != %d)" % (X.shape[0], y.shape[0])) # All LinearModelCV parameters except 'cv' are acceptable path_params = self.get_params() if 'l1_ratio' in path_params: l1_ratios = np.atleast_1d(path_params['l1_ratio']) # For the first path, we need to set l1_ratio path_params['l1_ratio'] = l1_ratios[0] else: l1_ratios = [1, ] path_params.pop('cv', None) path_params.pop('n_jobs', None) alphas = self.alphas n_l1_ratio = len(l1_ratios) if alphas is None: alphas = [] for l1_ratio in l1_ratios: alphas.append(_alpha_grid( X, y, l1_ratio=l1_ratio, fit_intercept=self.fit_intercept, eps=self.eps, n_alphas=self.n_alphas, normalize=self.normalize, copy_X=self.copy_X)) else: # Making sure alphas is properly ordered. alphas = np.tile(np.sort(alphas)[::-1], (n_l1_ratio, 1)) # We want n_alphas to be the number of alphas used for each l1_ratio. n_alphas = len(alphas[0]) path_params.update({'n_alphas': n_alphas}) path_params['copy_X'] = copy_X # We are not computing in parallel, we can modify X # inplace in the folds if not (self.n_jobs == 1 or self.n_jobs is None): path_params['copy_X'] = False # init cross-validation generator cv = check_cv(self.cv, X) # Compute path for all folds and compute MSE to get the best alpha folds = list(cv) best_mse = np.inf # We do a double for loop folded in one, in order to be able to # iterate in parallel on l1_ratio and folds jobs = (delayed(_path_residuals)(X, y, train, test, self.path, path_params, alphas=this_alphas, l1_ratio=this_l1_ratio, X_order='F', dtype=np.float64) for this_l1_ratio, this_alphas in zip(l1_ratios, alphas) for train, test in folds) mse_paths = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, backend="threading")(jobs) mse_paths = np.reshape(mse_paths, (n_l1_ratio, len(folds), -1)) mean_mse = np.mean(mse_paths, axis=1) self.mse_path_ = np.squeeze(np.rollaxis(mse_paths, 2, 1)) for l1_ratio, l1_alphas, mse_alphas in zip(l1_ratios, alphas, mean_mse): i_best_alpha = np.argmin(mse_alphas) this_best_mse = mse_alphas[i_best_alpha] if this_best_mse < best_mse: best_alpha = l1_alphas[i_best_alpha] best_l1_ratio = l1_ratio best_mse = this_best_mse self.l1_ratio_ = best_l1_ratio self.alpha_ = best_alpha if self.alphas is None: self.alphas_ = np.asarray(alphas) if n_l1_ratio == 1: self.alphas_ = self.alphas_[0] # Remove duplicate alphas in case alphas is provided. else: self.alphas_ = np.asarray(alphas[0]) # Refit the model with the parameters selected common_params = dict((name, value) for name, value in self.get_params().items() if name in model.get_params()) model.set_params(**common_params) model.alpha = best_alpha model.l1_ratio = best_l1_ratio model.copy_X = copy_X model.precompute = False model.fit(X, y) if not hasattr(self, 'l1_ratio'): del self.l1_ratio_ self.coef_ = model.coef_ self.intercept_ = model.intercept_ self.dual_gap_ = model.dual_gap_ self.n_iter_ = model.n_iter_ return self class LassoCV(LinearModelCV, RegressorMixin): """Lasso linear model with iterative fitting along a regularization path The best model is selected by cross-validation. The optimization objective for Lasso is:: (1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1 Read more in the :ref:`User Guide <lasso>`. Parameters ---------- eps : float, optional Length of the path. ``eps=1e-3`` means that ``alpha_min / alpha_max = 1e-3``. n_alphas : int, optional Number of alphas along the regularization path alphas : numpy array, optional List of alphas where to compute the models. If ``None`` alphas are set automatically precompute : True | False | 'auto' | array-like Whether to use a precomputed Gram matrix to speed up calculations. If set to ``'auto'`` let us decide. The Gram matrix can also be passed as argument. max_iter : int, optional The maximum number of iterations tol : float, optional The tolerance for the optimization: if the updates are smaller than ``tol``, the optimization code checks the dual gap for optimality and continues until it is smaller than ``tol``. cv : int, cross-validation generator or an iterable, optional Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 3-fold cross-validation, - integer, to specify the number of folds. - An object to be used as a cross-validation generator. - An iterable yielding train/test splits. For integer/None inputs, :class:`KFold` is used. Refer :ref:`User Guide <cross_validation>` for the various cross-validation strategies that can be used here. verbose : bool or integer Amount of verbosity. n_jobs : integer, optional Number of CPUs to use during the cross validation. If ``-1``, use all the CPUs. positive : bool, optional If positive, restrict regression coefficients to be positive selection : str, default 'cyclic' If set to 'random', a random coefficient is updated every iteration rather than looping over features sequentially by default. This (setting to 'random') often leads to significantly faster convergence especially when tol is higher than 1e-4. random_state : int, RandomState instance, or None (default) The seed of the pseudo random number generator that selects a random feature to update. Useful only when selection is set to 'random'. fit_intercept : boolean, default True whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (e.g. data is expected to be already centered). normalize : boolean, optional, default False If ``True``, the regressors X will be normalized before regression. copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. Attributes ---------- alpha_ : float The amount of penalization chosen by cross validation coef_ : array, shape (n_features,) | (n_targets, n_features) parameter vector (w in the cost function formula) intercept_ : float | array, shape (n_targets,) independent term in decision function. mse_path_ : array, shape (n_alphas, n_folds) mean square error for the test set on each fold, varying alpha alphas_ : numpy array, shape (n_alphas,) The grid of alphas used for fitting dual_gap_ : ndarray, shape () The dual gap at the end of the optimization for the optimal alpha (``alpha_``). n_iter_ : int number of iterations run by the coordinate descent solver to reach the specified tolerance for the optimal alpha. Notes ----- See examples/linear_model/lasso_path_with_crossvalidation.py for an example. To avoid unnecessary memory duplication the X argument of the fit method should be directly passed as a Fortran-contiguous numpy array. See also -------- lars_path lasso_path LassoLars Lasso LassoLarsCV """ path = staticmethod(lasso_path) def __init__(self, eps=1e-3, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, precompute='auto', max_iter=1000, tol=1e-4, copy_X=True, cv=None, verbose=False, n_jobs=1, positive=False, random_state=None, selection='cyclic'): super(LassoCV, self).__init__( eps=eps, n_alphas=n_alphas, alphas=alphas, fit_intercept=fit_intercept, normalize=normalize, precompute=precompute, max_iter=max_iter, tol=tol, copy_X=copy_X, cv=cv, verbose=verbose, n_jobs=n_jobs, positive=positive, random_state=random_state, selection=selection) class ElasticNetCV(LinearModelCV, RegressorMixin): """Elastic Net model with iterative fitting along a regularization path The best model is selected by cross-validation. Read more in the :ref:`User Guide <elastic_net>`. Parameters ---------- l1_ratio : float, optional float between 0 and 1 passed to ElasticNet (scaling between l1 and l2 penalties). For ``l1_ratio = 0`` the penalty is an L2 penalty. For ``l1_ratio = 1`` it is an L1 penalty. For ``0 < l1_ratio < 1``, the penalty is a combination of L1 and L2 This parameter can be a list, in which case the different values are tested by cross-validation and the one giving the best prediction score is used. Note that a good choice of list of values for l1_ratio is often to put more values close to 1 (i.e. Lasso) and less close to 0 (i.e. Ridge), as in ``[.1, .5, .7, .9, .95, .99, 1]`` eps : float, optional Length of the path. ``eps=1e-3`` means that ``alpha_min / alpha_max = 1e-3``. n_alphas : int, optional Number of alphas along the regularization path, used for each l1_ratio. alphas : numpy array, optional List of alphas where to compute the models. If None alphas are set automatically precompute : True | False | 'auto' | array-like Whether to use a precomputed Gram matrix to speed up calculations. If set to ``'auto'`` let us decide. The Gram matrix can also be passed as argument. max_iter : int, optional The maximum number of iterations tol : float, optional The tolerance for the optimization: if the updates are smaller than ``tol``, the optimization code checks the dual gap for optimality and continues until it is smaller than ``tol``. cv : int, cross-validation generator or an iterable, optional Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 3-fold cross-validation, - integer, to specify the number of folds. - An object to be used as a cross-validation generator. - An iterable yielding train/test splits. For integer/None inputs, :class:`KFold` is used. Refer :ref:`User Guide <cross_validation>` for the various cross-validation strategies that can be used here. verbose : bool or integer Amount of verbosity. n_jobs : integer, optional Number of CPUs to use during the cross validation. If ``-1``, use all the CPUs. positive : bool, optional When set to ``True``, forces the coefficients to be positive. selection : str, default 'cyclic' If set to 'random', a random coefficient is updated every iteration rather than looping over features sequentially by default. This (setting to 'random') often leads to significantly faster convergence especially when tol is higher than 1e-4. random_state : int, RandomState instance, or None (default) The seed of the pseudo random number generator that selects a random feature to update. Useful only when selection is set to 'random'. fit_intercept : boolean whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (e.g. data is expected to be already centered). normalize : boolean, optional, default False If ``True``, the regressors X will be normalized before regression. copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. Attributes ---------- alpha_ : float The amount of penalization chosen by cross validation l1_ratio_ : float The compromise between l1 and l2 penalization chosen by cross validation coef_ : array, shape (n_features,) | (n_targets, n_features) Parameter vector (w in the cost function formula), intercept_ : float | array, shape (n_targets, n_features) Independent term in the decision function. mse_path_ : array, shape (n_l1_ratio, n_alpha, n_folds) Mean square error for the test set on each fold, varying l1_ratio and alpha. alphas_ : numpy array, shape (n_alphas,) or (n_l1_ratio, n_alphas) The grid of alphas used for fitting, for each l1_ratio. n_iter_ : int number of iterations run by the coordinate descent solver to reach the specified tolerance for the optimal alpha. Notes ----- See examples/linear_model/lasso_path_with_crossvalidation.py for an example. To avoid unnecessary memory duplication the X argument of the fit method should be directly passed as a Fortran-contiguous numpy array. The parameter l1_ratio corresponds to alpha in the glmnet R package while alpha corresponds to the lambda parameter in glmnet. More specifically, the optimization objective is:: 1 / (2 * n_samples) * ||y - Xw||^2_2 + + alpha * l1_ratio * ||w||_1 + 0.5 * alpha * (1 - l1_ratio) * ||w||^2_2 If you are interested in controlling the L1 and L2 penalty separately, keep in mind that this is equivalent to:: a * L1 + b * L2 for:: alpha = a + b and l1_ratio = a / (a + b). See also -------- enet_path ElasticNet """ path = staticmethod(enet_path) def __init__(self, l1_ratio=0.5, eps=1e-3, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, precompute='auto', max_iter=1000, tol=1e-4, cv=None, copy_X=True, verbose=0, n_jobs=1, positive=False, random_state=None, selection='cyclic'): self.l1_ratio = l1_ratio self.eps = eps self.n_alphas = n_alphas self.alphas = alphas self.fit_intercept = fit_intercept self.normalize = normalize self.precompute = precompute self.max_iter = max_iter self.tol = tol self.cv = cv self.copy_X = copy_X self.verbose = verbose self.n_jobs = n_jobs self.positive = positive self.random_state = random_state self.selection = selection ############################################################################### # Multi Task ElasticNet and Lasso models (with joint feature selection) class MultiTaskElasticNet(Lasso): """Multi-task ElasticNet model trained with L1/L2 mixed-norm as regularizer The optimization objective for MultiTaskElasticNet is:: (1 / (2 * n_samples)) * ||Y - XW||^Fro_2 + alpha * l1_ratio * ||W||_21 + 0.5 * alpha * (1 - l1_ratio) * ||W||_Fro^2 Where:: ||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2} i.e. the sum of norm of each row. Read more in the :ref:`User Guide <multi_task_lasso>`. Parameters ---------- alpha : float, optional Constant that multiplies the L1/L2 term. Defaults to 1.0 l1_ratio : float The ElasticNet mixing parameter, with 0 < l1_ratio <= 1. For l1_ratio = 0 the penalty is an L1/L2 penalty. For l1_ratio = 1 it is an L1 penalty. For ``0 < l1_ratio < 1``, the penalty is a combination of L1/L2 and L2. fit_intercept : boolean whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (e.g. data is expected to be already centered). normalize : boolean, optional, default False If ``True``, the regressors X will be normalized before regression. copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. max_iter : int, optional The maximum number of iterations tol : float, optional The tolerance for the optimization: if the updates are smaller than ``tol``, the optimization code checks the dual gap for optimality and continues until it is smaller than ``tol``. warm_start : bool, optional When set to ``True``, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. selection : str, default 'cyclic' If set to 'random', a random coefficient is updated every iteration rather than looping over features sequentially by default. This (setting to 'random') often leads to significantly faster convergence especially when tol is higher than 1e-4. random_state : int, RandomState instance, or None (default) The seed of the pseudo random number generator that selects a random feature to update. Useful only when selection is set to 'random'. Attributes ---------- intercept_ : array, shape (n_tasks,) Independent term in decision function. coef_ : array, shape (n_tasks, n_features) Parameter vector (W in the cost function formula). If a 1D y is \ passed in at fit (non multi-task usage), ``coef_`` is then a 1D array n_iter_ : int number of iterations run by the coordinate descent solver to reach the specified tolerance. Examples -------- >>> from sklearn import linear_model >>> clf = linear_model.MultiTaskElasticNet(alpha=0.1) >>> clf.fit([[0,0], [1, 1], [2, 2]], [[0, 0], [1, 1], [2, 2]]) ... #doctest: +NORMALIZE_WHITESPACE MultiTaskElasticNet(alpha=0.1, copy_X=True, fit_intercept=True, l1_ratio=0.5, max_iter=1000, normalize=False, random_state=None, selection='cyclic', tol=0.0001, warm_start=False) >>> print(clf.coef_) [[ 0.45663524 0.45612256] [ 0.45663524 0.45612256]] >>> print(clf.intercept_) [ 0.0872422 0.0872422] See also -------- ElasticNet, MultiTaskLasso Notes ----- The algorithm used to fit the model is coordinate descent. To avoid unnecessary memory duplication the X argument of the fit method should be directly passed as a Fortran-contiguous numpy array. """ def __init__(self, alpha=1.0, l1_ratio=0.5, fit_intercept=True, normalize=False, copy_X=True, max_iter=1000, tol=1e-4, warm_start=False, random_state=None, selection='cyclic'): self.l1_ratio = l1_ratio self.alpha = alpha self.coef_ = None self.fit_intercept = fit_intercept self.normalize = normalize self.max_iter = max_iter self.copy_X = copy_X self.tol = tol self.warm_start = warm_start self.random_state = random_state self.selection = selection def fit(self, X, y): """Fit MultiTaskLasso model with coordinate descent Parameters ----------- X : ndarray, shape (n_samples, n_features) Data y : ndarray, shape (n_samples, n_tasks) Target Notes ----- Coordinate descent is an algorithm that considers each column of data at a time hence it will automatically convert the X input as a Fortran-contiguous numpy array if necessary. To avoid memory re-allocation it is advised to allocate the initial data in memory directly using that format. """ # X and y must be of type float64 X = check_array(X, dtype=np.float64, order='F', copy=self.copy_X and self.fit_intercept) y = np.asarray(y, dtype=np.float64) if hasattr(self, 'l1_ratio'): model_str = 'ElasticNet' else: model_str = 'Lasso' if y.ndim == 1: raise ValueError("For mono-task outputs, use %s" % model_str) n_samples, n_features = X.shape _, n_tasks = y.shape if n_samples != y.shape[0]: raise ValueError("X and y have inconsistent dimensions (%d != %d)" % (n_samples, y.shape[0])) X, y, X_mean, y_mean, X_std = center_data( X, y, self.fit_intercept, self.normalize, copy=False) if not self.warm_start or self.coef_ is None: self.coef_ = np.zeros((n_tasks, n_features), dtype=np.float64, order='F') l1_reg = self.alpha * self.l1_ratio * n_samples l2_reg = self.alpha * (1.0 - self.l1_ratio) * n_samples self.coef_ = np.asfortranarray(self.coef_) # coef contiguous in memory if self.selection not in ['random', 'cyclic']: raise ValueError("selection should be either random or cyclic.") random = (self.selection == 'random') self.coef_, self.dual_gap_, self.eps_, self.n_iter_ = \ cd_fast.enet_coordinate_descent_multi_task( self.coef_, l1_reg, l2_reg, X, y, self.max_iter, self.tol, check_random_state(self.random_state), random) self._set_intercept(X_mean, y_mean, X_std) if self.dual_gap_ > self.eps_: warnings.warn('Objective did not converge, you might want' ' to increase the number of iterations') # return self for chaining fit and predict calls return self class MultiTaskLasso(MultiTaskElasticNet): """Multi-task Lasso model trained with L1/L2 mixed-norm as regularizer The optimization objective for Lasso is:: (1 / (2 * n_samples)) * ||Y - XW||^2_Fro + alpha * ||W||_21 Where:: ||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2} i.e. the sum of norm of earch row. Read more in the :ref:`User Guide <multi_task_lasso>`. Parameters ---------- alpha : float, optional Constant that multiplies the L1/L2 term. Defaults to 1.0 fit_intercept : boolean whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (e.g. data is expected to be already centered). normalize : boolean, optional, default False If ``True``, the regressors X will be normalized before regression. copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. max_iter : int, optional The maximum number of iterations tol : float, optional The tolerance for the optimization: if the updates are smaller than ``tol``, the optimization code checks the dual gap for optimality and continues until it is smaller than ``tol``. warm_start : bool, optional When set to ``True``, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. selection : str, default 'cyclic' If set to 'random', a random coefficient is updated every iteration rather than looping over features sequentially by default. This (setting to 'random') often leads to significantly faster convergence especially when tol is higher than 1e-4 random_state : int, RandomState instance, or None (default) The seed of the pseudo random number generator that selects a random feature to update. Useful only when selection is set to 'random'. Attributes ---------- coef_ : array, shape (n_tasks, n_features) parameter vector (W in the cost function formula) intercept_ : array, shape (n_tasks,) independent term in decision function. n_iter_ : int number of iterations run by the coordinate descent solver to reach the specified tolerance. Examples -------- >>> from sklearn import linear_model >>> clf = linear_model.MultiTaskLasso(alpha=0.1) >>> clf.fit([[0,0], [1, 1], [2, 2]], [[0, 0], [1, 1], [2, 2]]) MultiTaskLasso(alpha=0.1, copy_X=True, fit_intercept=True, max_iter=1000, normalize=False, random_state=None, selection='cyclic', tol=0.0001, warm_start=False) >>> print(clf.coef_) [[ 0.89393398 0. ] [ 0.89393398 0. ]] >>> print(clf.intercept_) [ 0.10606602 0.10606602] See also -------- Lasso, MultiTaskElasticNet Notes ----- The algorithm used to fit the model is coordinate descent. To avoid unnecessary memory duplication the X argument of the fit method should be directly passed as a Fortran-contiguous numpy array. """ def __init__(self, alpha=1.0, fit_intercept=True, normalize=False, copy_X=True, max_iter=1000, tol=1e-4, warm_start=False, random_state=None, selection='cyclic'): self.alpha = alpha self.coef_ = None self.fit_intercept = fit_intercept self.normalize = normalize self.max_iter = max_iter self.copy_X = copy_X self.tol = tol self.warm_start = warm_start self.l1_ratio = 1.0 self.random_state = random_state self.selection = selection class MultiTaskElasticNetCV(LinearModelCV, RegressorMixin): """Multi-task L1/L2 ElasticNet with built-in cross-validation. The optimization objective for MultiTaskElasticNet is:: (1 / (2 * n_samples)) * ||Y - XW||^Fro_2 + alpha * l1_ratio * ||W||_21 + 0.5 * alpha * (1 - l1_ratio) * ||W||_Fro^2 Where:: ||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2} i.e. the sum of norm of each row. Read more in the :ref:`User Guide <multi_task_lasso>`. Parameters ---------- eps : float, optional Length of the path. ``eps=1e-3`` means that ``alpha_min / alpha_max = 1e-3``. alphas : array-like, optional List of alphas where to compute the models. If not provided, set automatically. n_alphas : int, optional Number of alphas along the regularization path l1_ratio : float or array of floats The ElasticNet mixing parameter, with 0 < l1_ratio <= 1. For l1_ratio = 0 the penalty is an L1/L2 penalty. For l1_ratio = 1 it is an L1 penalty. For ``0 < l1_ratio < 1``, the penalty is a combination of L1/L2 and L2. fit_intercept : boolean whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (e.g. data is expected to be already centered). normalize : boolean, optional, default False If ``True``, the regressors X will be normalized before regression. copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. max_iter : int, optional The maximum number of iterations tol : float, optional The tolerance for the optimization: if the updates are smaller than ``tol``, the optimization code checks the dual gap for optimality and continues until it is smaller than ``tol``. cv : int, cross-validation generator or an iterable, optional Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 3-fold cross-validation, - integer, to specify the number of folds. - An object to be used as a cross-validation generator. - An iterable yielding train/test splits. For integer/None inputs, :class:`KFold` is used. Refer :ref:`User Guide <cross_validation>` for the various cross-validation strategies that can be used here. verbose : bool or integer Amount of verbosity. n_jobs : integer, optional Number of CPUs to use during the cross validation. If ``-1``, use all the CPUs. Note that this is used only if multiple values for l1_ratio are given. selection : str, default 'cyclic' If set to 'random', a random coefficient is updated every iteration rather than looping over features sequentially by default. This (setting to 'random') often leads to significantly faster convergence especially when tol is higher than 1e-4. random_state : int, RandomState instance, or None (default) The seed of the pseudo random number generator that selects a random feature to update. Useful only when selection is set to 'random'. Attributes ---------- intercept_ : array, shape (n_tasks,) Independent term in decision function. coef_ : array, shape (n_tasks, n_features) Parameter vector (W in the cost function formula). alpha_ : float The amount of penalization chosen by cross validation mse_path_ : array, shape (n_alphas, n_folds) or \ (n_l1_ratio, n_alphas, n_folds) mean square error for the test set on each fold, varying alpha alphas_ : numpy array, shape (n_alphas,) or (n_l1_ratio, n_alphas) The grid of alphas used for fitting, for each l1_ratio l1_ratio_ : float best l1_ratio obtained by cross-validation. n_iter_ : int number of iterations run by the coordinate descent solver to reach the specified tolerance for the optimal alpha. Examples -------- >>> from sklearn import linear_model >>> clf = linear_model.MultiTaskElasticNetCV() >>> clf.fit([[0,0], [1, 1], [2, 2]], ... [[0, 0], [1, 1], [2, 2]]) ... #doctest: +NORMALIZE_WHITESPACE MultiTaskElasticNetCV(alphas=None, copy_X=True, cv=None, eps=0.001, fit_intercept=True, l1_ratio=0.5, max_iter=1000, n_alphas=100, n_jobs=1, normalize=False, random_state=None, selection='cyclic', tol=0.0001, verbose=0) >>> print(clf.coef_) [[ 0.52875032 0.46958558] [ 0.52875032 0.46958558]] >>> print(clf.intercept_) [ 0.00166409 0.00166409] See also -------- MultiTaskElasticNet ElasticNetCV MultiTaskLassoCV Notes ----- The algorithm used to fit the model is coordinate descent. To avoid unnecessary memory duplication the X argument of the fit method should be directly passed as a Fortran-contiguous numpy array. """ path = staticmethod(enet_path) def __init__(self, l1_ratio=0.5, eps=1e-3, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, max_iter=1000, tol=1e-4, cv=None, copy_X=True, verbose=0, n_jobs=1, random_state=None, selection='cyclic'): self.l1_ratio = l1_ratio self.eps = eps self.n_alphas = n_alphas self.alphas = alphas self.fit_intercept = fit_intercept self.normalize = normalize self.max_iter = max_iter self.tol = tol self.cv = cv self.copy_X = copy_X self.verbose = verbose self.n_jobs = n_jobs self.random_state = random_state self.selection = selection class MultiTaskLassoCV(LinearModelCV, RegressorMixin): """Multi-task L1/L2 Lasso with built-in cross-validation. The optimization objective for MultiTaskLasso is:: (1 / (2 * n_samples)) * ||Y - XW||^Fro_2 + alpha * ||W||_21 Where:: ||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2} i.e. the sum of norm of each row. Read more in the :ref:`User Guide <multi_task_lasso>`. Parameters ---------- eps : float, optional Length of the path. ``eps=1e-3`` means that ``alpha_min / alpha_max = 1e-3``. alphas : array-like, optional List of alphas where to compute the models. If not provided, set automaticlly. n_alphas : int, optional Number of alphas along the regularization path fit_intercept : boolean whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (e.g. data is expected to be already centered). normalize : boolean, optional, default False If ``True``, the regressors X will be normalized before regression. copy_X : boolean, optional, default True If ``True``, X will be copied; else, it may be overwritten. max_iter : int, optional The maximum number of iterations. tol : float, optional The tolerance for the optimization: if the updates are smaller than ``tol``, the optimization code checks the dual gap for optimality and continues until it is smaller than ``tol``. cv : int, cross-validation generator or an iterable, optional Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 3-fold cross-validation, - integer, to specify the number of folds. - An object to be used as a cross-validation generator. - An iterable yielding train/test splits. For integer/None inputs, :class:`KFold` is used. Refer :ref:`User Guide <cross_validation>` for the various cross-validation strategies that can be used here. verbose : bool or integer Amount of verbosity. n_jobs : integer, optional Number of CPUs to use during the cross validation. If ``-1``, use all the CPUs. Note that this is used only if multiple values for l1_ratio are given. selection : str, default 'cyclic' If set to 'random', a random coefficient is updated every iteration rather than looping over features sequentially by default. This (setting to 'random') often leads to significantly faster convergence especially when tol is higher than 1e-4. random_state : int, RandomState instance, or None (default) The seed of the pseudo random number generator that selects a random feature to update. Useful only when selection is set to 'random'. Attributes ---------- intercept_ : array, shape (n_tasks,) Independent term in decision function. coef_ : array, shape (n_tasks, n_features) Parameter vector (W in the cost function formula). alpha_ : float The amount of penalization chosen by cross validation mse_path_ : array, shape (n_alphas, n_folds) mean square error for the test set on each fold, varying alpha alphas_ : numpy array, shape (n_alphas,) The grid of alphas used for fitting. n_iter_ : int number of iterations run by the coordinate descent solver to reach the specified tolerance for the optimal alpha. See also -------- MultiTaskElasticNet ElasticNetCV MultiTaskElasticNetCV Notes ----- The algorithm used to fit the model is coordinate descent. To avoid unnecessary memory duplication the X argument of the fit method should be directly passed as a Fortran-contiguous numpy array. """ path = staticmethod(lasso_path) def __init__(self, eps=1e-3, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, max_iter=1000, tol=1e-4, copy_X=True, cv=None, verbose=False, n_jobs=1, random_state=None, selection='cyclic'): super(MultiTaskLassoCV, self).__init__( eps=eps, n_alphas=n_alphas, alphas=alphas, fit_intercept=fit_intercept, normalize=normalize, max_iter=max_iter, tol=tol, copy_X=copy_X, cv=cv, verbose=verbose, n_jobs=n_jobs, random_state=random_state, selection=selection)
bsd-3-clause
DonBeo/scikit-learn
sklearn/utils/tests/test_class_weight.py
14
6559
import numpy as np from sklearn.utils.class_weight import compute_class_weight from sklearn.utils.class_weight import compute_sample_weight from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_equal def test_compute_class_weight(): # Test (and demo) compute_class_weight. y = np.asarray([2, 2, 2, 3, 3, 4]) classes = np.unique(y) cw = compute_class_weight("auto", classes, y) assert_almost_equal(cw.sum(), classes.shape) assert_true(cw[0] < cw[1] < cw[2]) def test_compute_class_weight_not_present(): # Raise error when y does not contain all class labels classes = np.arange(4) y = np.asarray([0, 0, 0, 1, 1, 2]) assert_raises(ValueError, compute_class_weight, "auto", classes, y) def test_compute_class_weight_auto_negative(): # Test compute_class_weight when labels are negative # Test with balanced class labels. classes = np.array([-2, -1, 0]) y = np.asarray([-1, -1, 0, 0, -2, -2]) cw = compute_class_weight("auto", classes, y) assert_almost_equal(cw.sum(), classes.shape) assert_equal(len(cw), len(classes)) assert_array_almost_equal(cw, np.array([1., 1., 1.])) # Test with unbalanced class labels. y = np.asarray([-1, 0, 0, -2, -2, -2]) cw = compute_class_weight("auto", classes, y) assert_almost_equal(cw.sum(), classes.shape) assert_equal(len(cw), len(classes)) assert_array_almost_equal(cw, np.array([0.545, 1.636, 0.818]), decimal=3) def test_compute_class_weight_auto_unordered(): # Test compute_class_weight when classes are unordered classes = np.array([1, 0, 3]) y = np.asarray([1, 0, 0, 3, 3, 3]) cw = compute_class_weight("auto", classes, y) assert_almost_equal(cw.sum(), classes.shape) assert_equal(len(cw), len(classes)) assert_array_almost_equal(cw, np.array([1.636, 0.818, 0.545]), decimal=3) def test_compute_sample_weight(): # Test (and demo) compute_sample_weight. # Test with balanced classes y = np.asarray([1, 1, 1, 2, 2, 2]) sample_weight = compute_sample_weight("auto", y) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.]) # Test with user-defined weights sample_weight = compute_sample_weight({1: 2, 2: 1}, y) assert_array_almost_equal(sample_weight, [2., 2., 2., 1., 1., 1.]) # Test with column vector of balanced classes y = np.asarray([[1], [1], [1], [2], [2], [2]]) sample_weight = compute_sample_weight("auto", y) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.]) # Test with unbalanced classes y = np.asarray([1, 1, 1, 2, 2, 2, 3]) sample_weight = compute_sample_weight("auto", y) expected = np.asarray([.6, .6, .6, .6, .6, .6, 1.8]) assert_array_almost_equal(sample_weight, expected) # Test with `None` weights sample_weight = compute_sample_weight(None, y) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 1.]) # Test with multi-output of balanced classes y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]]) sample_weight = compute_sample_weight("auto", y) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.]) # Test with multi-output with user-defined weights y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]]) sample_weight = compute_sample_weight([{1: 2, 2: 1}, {0: 1, 1: 2}], y) assert_array_almost_equal(sample_weight, [2., 2., 2., 2., 2., 2.]) # Test with multi-output of unbalanced classes y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1], [3, -1]]) sample_weight = compute_sample_weight("auto", y) assert_array_almost_equal(sample_weight, expected ** 2) def test_compute_sample_weight_with_subsample(): # Test compute_sample_weight with subsamples specified. # Test with balanced classes and all samples present y = np.asarray([1, 1, 1, 2, 2, 2]) sample_weight = compute_sample_weight("auto", y, range(6)) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.]) # Test with column vector of balanced classes and all samples present y = np.asarray([[1], [1], [1], [2], [2], [2]]) sample_weight = compute_sample_weight("auto", y, range(6)) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1.]) # Test with a subsample y = np.asarray([1, 1, 1, 2, 2, 2]) sample_weight = compute_sample_weight("auto", y, range(4)) assert_array_almost_equal(sample_weight, [.5, .5, .5, 1.5, 1.5, 1.5]) # Test with a bootstrap subsample y = np.asarray([1, 1, 1, 2, 2, 2]) sample_weight = compute_sample_weight("auto", y, [0, 1, 1, 2, 2, 3]) expected = np.asarray([1/3., 1/3., 1/3., 5/3., 5/3., 5/3.]) assert_array_almost_equal(sample_weight, expected) # Test with a bootstrap subsample for multi-output y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]]) sample_weight = compute_sample_weight("auto", y, [0, 1, 1, 2, 2, 3]) assert_array_almost_equal(sample_weight, expected ** 2) # Test with a missing class y = np.asarray([1, 1, 1, 2, 2, 2, 3]) sample_weight = compute_sample_weight("auto", y, range(6)) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 0.]) # Test with a missing class for multi-output y = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1], [2, 2]]) sample_weight = compute_sample_weight("auto", y, range(6)) assert_array_almost_equal(sample_weight, [1., 1., 1., 1., 1., 1., 0.]) def test_compute_sample_weight_errors(): # Test compute_sample_weight raises errors expected. # Invalid preset string y = np.asarray([1, 1, 1, 2, 2, 2]) y_ = np.asarray([[1, 0], [1, 0], [1, 0], [2, 1], [2, 1], [2, 1]]) assert_raises(ValueError, compute_sample_weight, "ni", y) assert_raises(ValueError, compute_sample_weight, "ni", y, range(4)) assert_raises(ValueError, compute_sample_weight, "ni", y_) assert_raises(ValueError, compute_sample_weight, "ni", y_, range(4)) # Not "auto" for subsample assert_raises(ValueError, compute_sample_weight, {1: 2, 2: 1}, y, range(4)) # Not a list or preset for multi-output assert_raises(ValueError, compute_sample_weight, {1: 2, 2: 1}, y_) # Incorrect length list for multi-output assert_raises(ValueError, compute_sample_weight, [{1: 2, 2: 1}], y_)
bsd-3-clause
Phil9l/cosmos
code/artificial_intelligence/src/naive_bayes/gaussian_naive_bayes.py
3
1370
# example using iris dataset # Part of Cosmos by OpenGenus import numpy as np import matplotlib.pyplot as plt import pandas as pd from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB from sklearn.metrics import classification_report, confusion_matrix dataset = pd.read_csv("iris1.csv", header=0) X = dataset.iloc[:, :-1].values y = dataset.iloc[:, 4].values X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, stratify=y) classifier = GaussianNB() classifier.fit(X_train, y_train) y_pred = classifier.predict(X_test) # labeled confusion matrix print( pd.crosstab(y_test, y_pred, rownames=["True"], colnames=["Predicted"], margins=True) ) from sklearn.model_selection import StratifiedKFold from sklearn.metrics import accuracy_score skf = StratifiedKFold(n_splits=10) skf.get_n_splits(X, y) StratifiedKFold(n_splits=10, random_state=None, shuffle=False) a = 0 for train_index, test_index in skf.split(X, y): # print("TRAIN:", train_index, "TEST:", test_index) #These are the mutually exclusive sets from the 10 folds X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] y_pred = classifier.predict(X_test) accuracy = accuracy_score(y_test, y_pred) a += accuracy print("\nK-fold cross validation (10 folds): " + str(a / 10))
gpl-3.0
xyguo/scikit-learn
sklearn/decomposition/nmf.py
6
46993
""" Non-negative matrix factorization """ # Author: Vlad Niculae # Lars Buitinck # Mathieu Blondel <mathieu@mblondel.org> # Tom Dupre la Tour # Author: Chih-Jen Lin, National Taiwan University (original projected gradient # NMF implementation) # Author: Anthony Di Franco (Projected gradient, Python and NumPy port) # License: BSD 3 clause from __future__ import division, print_function from math import sqrt import warnings import numbers import numpy as np import scipy.sparse as sp from ..externals import six from ..base import BaseEstimator, TransformerMixin from ..utils import check_random_state, check_array from ..utils.extmath import randomized_svd, safe_sparse_dot, squared_norm from ..utils.extmath import fast_dot from ..utils.validation import check_is_fitted, check_non_negative from ..utils import deprecated from ..exceptions import ConvergenceWarning from .cdnmf_fast import _update_cdnmf_fast def safe_vstack(Xs): if any(sp.issparse(X) for X in Xs): return sp.vstack(Xs) else: return np.vstack(Xs) def norm(x): """Dot product-based Euclidean norm implementation See: http://fseoane.net/blog/2011/computing-the-vector-norm/ """ return sqrt(squared_norm(x)) def trace_dot(X, Y): """Trace of np.dot(X, Y.T).""" return np.dot(X.ravel(), Y.ravel()) def _sparseness(x): """Hoyer's measure of sparsity for a vector""" sqrt_n = np.sqrt(len(x)) return (sqrt_n - np.linalg.norm(x, 1) / norm(x)) / (sqrt_n - 1) def _check_init(A, shape, whom): A = check_array(A) if np.shape(A) != shape: raise ValueError('Array with wrong shape passed to %s. Expected %s, ' 'but got %s ' % (whom, shape, np.shape(A))) check_non_negative(A, whom) if np.max(A) == 0: raise ValueError('Array passed to %s is full of zeros.' % whom) def _safe_compute_error(X, W, H): """Frobenius norm between X and WH, safe for sparse array""" if not sp.issparse(X): error = norm(X - np.dot(W, H)) else: norm_X = np.dot(X.data, X.data) norm_WH = trace_dot(np.dot(np.dot(W.T, W), H), H) cross_prod = trace_dot((X * H.T), W) error = sqrt(norm_X + norm_WH - 2. * cross_prod) return error def _check_string_param(sparseness, solver): allowed_sparseness = (None, 'data', 'components') if sparseness not in allowed_sparseness: raise ValueError( 'Invalid sparseness parameter: got %r instead of one of %r' % (sparseness, allowed_sparseness)) allowed_solver = ('pg', 'cd') if solver not in allowed_solver: raise ValueError( 'Invalid solver parameter: got %r instead of one of %r' % (solver, allowed_solver)) def _initialize_nmf(X, n_components, init=None, eps=1e-6, random_state=None): """Algorithms for NMF initialization. Computes an initial guess for the non-negative rank k matrix approximation for X: X = WH Parameters ---------- X : array-like, shape (n_samples, n_features) The data matrix to be decomposed. n_components : integer The number of components desired in the approximation. init : None | 'random' | 'nndsvd' | 'nndsvda' | 'nndsvdar' Method used to initialize the procedure. Default: 'nndsvdar' if n_components < n_features, otherwise 'random'. Valid options: - 'random': non-negative random matrices, scaled with: sqrt(X.mean() / n_components) - 'nndsvd': Nonnegative Double Singular Value Decomposition (NNDSVD) initialization (better for sparseness) - 'nndsvda': NNDSVD with zeros filled with the average of X (better when sparsity is not desired) - 'nndsvdar': NNDSVD with zeros filled with small random values (generally faster, less accurate alternative to NNDSVDa for when sparsity is not desired) - 'custom': use custom matrices W and H eps : float Truncate all values less then this in output to zero. random_state : int seed, RandomState instance, or None (default) Random number generator seed control, used in 'nndsvdar' and 'random' modes. Returns ------- W : array-like, shape (n_samples, n_components) Initial guesses for solving X ~= WH H : array-like, shape (n_components, n_features) Initial guesses for solving X ~= WH References ---------- C. Boutsidis, E. Gallopoulos: SVD based initialization: A head start for nonnegative matrix factorization - Pattern Recognition, 2008 http://tinyurl.com/nndsvd """ check_non_negative(X, "NMF initialization") n_samples, n_features = X.shape if init is None: if n_components < n_features: init = 'nndsvd' else: init = 'random' # Random initialization if init == 'random': avg = np.sqrt(X.mean() / n_components) rng = check_random_state(random_state) H = avg * rng.randn(n_components, n_features) W = avg * rng.randn(n_samples, n_components) # we do not write np.abs(H, out=H) to stay compatible with # numpy 1.5 and earlier where the 'out' keyword is not # supported as a kwarg on ufuncs np.abs(H, H) np.abs(W, W) return W, H # NNDSVD initialization U, S, V = randomized_svd(X, n_components, random_state=random_state) W, H = np.zeros(U.shape), np.zeros(V.shape) # The leading singular triplet is non-negative # so it can be used as is for initialization. W[:, 0] = np.sqrt(S[0]) * np.abs(U[:, 0]) H[0, :] = np.sqrt(S[0]) * np.abs(V[0, :]) for j in range(1, n_components): x, y = U[:, j], V[j, :] # extract positive and negative parts of column vectors x_p, y_p = np.maximum(x, 0), np.maximum(y, 0) x_n, y_n = np.abs(np.minimum(x, 0)), np.abs(np.minimum(y, 0)) # and their norms x_p_nrm, y_p_nrm = norm(x_p), norm(y_p) x_n_nrm, y_n_nrm = norm(x_n), norm(y_n) m_p, m_n = x_p_nrm * y_p_nrm, x_n_nrm * y_n_nrm # choose update if m_p > m_n: u = x_p / x_p_nrm v = y_p / y_p_nrm sigma = m_p else: u = x_n / x_n_nrm v = y_n / y_n_nrm sigma = m_n lbd = np.sqrt(S[j] * sigma) W[:, j] = lbd * u H[j, :] = lbd * v W[W < eps] = 0 H[H < eps] = 0 if init == "nndsvd": pass elif init == "nndsvda": avg = X.mean() W[W == 0] = avg H[H == 0] = avg elif init == "nndsvdar": rng = check_random_state(random_state) avg = X.mean() W[W == 0] = abs(avg * rng.randn(len(W[W == 0])) / 100) H[H == 0] = abs(avg * rng.randn(len(H[H == 0])) / 100) else: raise ValueError( 'Invalid init parameter: got %r instead of one of %r' % (init, (None, 'random', 'nndsvd', 'nndsvda', 'nndsvdar'))) return W, H def _nls_subproblem(V, W, H, tol, max_iter, alpha=0., l1_ratio=0., sigma=0.01, beta=0.1): """Non-negative least square solver Solves a non-negative least squares subproblem using the projected gradient descent algorithm. Parameters ---------- V : array-like, shape (n_samples, n_features) Constant matrix. W : array-like, shape (n_samples, n_components) Constant matrix. H : array-like, shape (n_components, n_features) Initial guess for the solution. tol : float Tolerance of the stopping condition. max_iter : int Maximum number of iterations before timing out. alpha : double, default: 0. Constant that multiplies the regularization terms. Set it to zero to have no regularization. l1_ratio : double, default: 0. The regularization mixing parameter, with 0 <= l1_ratio <= 1. For l1_ratio = 0 the penalty is an L2 penalty. For l1_ratio = 1 it is an L1 penalty. For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2. sigma : float Constant used in the sufficient decrease condition checked by the line search. Smaller values lead to a looser sufficient decrease condition, thus reducing the time taken by the line search, but potentially increasing the number of iterations of the projected gradient procedure. 0.01 is a commonly used value in the optimization literature. beta : float Factor by which the step size is decreased (resp. increased) until (resp. as long as) the sufficient decrease condition is satisfied. Larger values allow to find a better step size but lead to longer line search. 0.1 is a commonly used value in the optimization literature. Returns ------- H : array-like, shape (n_components, n_features) Solution to the non-negative least squares problem. grad : array-like, shape (n_components, n_features) The gradient. n_iter : int The number of iterations done by the algorithm. References ---------- C.-J. Lin. Projected gradient methods for non-negative matrix factorization. Neural Computation, 19(2007), 2756-2779. http://www.csie.ntu.edu.tw/~cjlin/nmf/ """ WtV = safe_sparse_dot(W.T, V) WtW = fast_dot(W.T, W) # values justified in the paper (alpha is renamed gamma) gamma = 1 for n_iter in range(1, max_iter + 1): grad = np.dot(WtW, H) - WtV if alpha > 0 and l1_ratio == 1.: grad += alpha elif alpha > 0: grad += alpha * (l1_ratio + (1 - l1_ratio) * H) # The following multiplication with a boolean array is more than twice # as fast as indexing into grad. if norm(grad * np.logical_or(grad < 0, H > 0)) < tol: break Hp = H for inner_iter in range(20): # Gradient step. Hn = H - gamma * grad # Projection step. Hn *= Hn > 0 d = Hn - H gradd = np.dot(grad.ravel(), d.ravel()) dQd = np.dot(np.dot(WtW, d).ravel(), d.ravel()) suff_decr = (1 - sigma) * gradd + 0.5 * dQd < 0 if inner_iter == 0: decr_gamma = not suff_decr if decr_gamma: if suff_decr: H = Hn break else: gamma *= beta elif not suff_decr or (Hp == Hn).all(): H = Hp break else: gamma /= beta Hp = Hn if n_iter == max_iter: warnings.warn("Iteration limit reached in nls subproblem.") return H, grad, n_iter def _update_projected_gradient_w(X, W, H, tolW, nls_max_iter, alpha, l1_ratio, sparseness, beta, eta): """Helper function for _fit_projected_gradient""" n_samples, n_features = X.shape n_components_ = H.shape[0] if sparseness is None: Wt, gradW, iterW = _nls_subproblem(X.T, H.T, W.T, tolW, nls_max_iter, alpha=alpha, l1_ratio=l1_ratio) elif sparseness == 'data': Wt, gradW, iterW = _nls_subproblem( safe_vstack([X.T, np.zeros((1, n_samples))]), safe_vstack([H.T, np.sqrt(beta) * np.ones((1, n_components_))]), W.T, tolW, nls_max_iter, alpha=alpha, l1_ratio=l1_ratio) elif sparseness == 'components': Wt, gradW, iterW = _nls_subproblem( safe_vstack([X.T, np.zeros((n_components_, n_samples))]), safe_vstack([H.T, np.sqrt(eta) * np.eye(n_components_)]), W.T, tolW, nls_max_iter, alpha=alpha, l1_ratio=l1_ratio) return Wt.T, gradW.T, iterW def _update_projected_gradient_h(X, W, H, tolH, nls_max_iter, alpha, l1_ratio, sparseness, beta, eta): """Helper function for _fit_projected_gradient""" n_samples, n_features = X.shape n_components_ = W.shape[1] if sparseness is None: H, gradH, iterH = _nls_subproblem(X, W, H, tolH, nls_max_iter, alpha=alpha, l1_ratio=l1_ratio) elif sparseness == 'data': H, gradH, iterH = _nls_subproblem( safe_vstack([X, np.zeros((n_components_, n_features))]), safe_vstack([W, np.sqrt(eta) * np.eye(n_components_)]), H, tolH, nls_max_iter, alpha=alpha, l1_ratio=l1_ratio) elif sparseness == 'components': H, gradH, iterH = _nls_subproblem( safe_vstack([X, np.zeros((1, n_features))]), safe_vstack([W, np.sqrt(beta) * np.ones((1, n_components_))]), H, tolH, nls_max_iter, alpha=alpha, l1_ratio=l1_ratio) return H, gradH, iterH def _fit_projected_gradient(X, W, H, tol, max_iter, nls_max_iter, alpha, l1_ratio, sparseness, beta, eta): """Compute Non-negative Matrix Factorization (NMF) with Projected Gradient References ---------- C.-J. Lin. Projected gradient methods for non-negative matrix factorization. Neural Computation, 19(2007), 2756-2779. http://www.csie.ntu.edu.tw/~cjlin/nmf/ P. Hoyer. Non-negative Matrix Factorization with Sparseness Constraints. Journal of Machine Learning Research 2004. """ gradW = (np.dot(W, np.dot(H, H.T)) - safe_sparse_dot(X, H.T, dense_output=True)) gradH = (np.dot(np.dot(W.T, W), H) - safe_sparse_dot(W.T, X, dense_output=True)) init_grad = squared_norm(gradW) + squared_norm(gradH.T) # max(0.001, tol) to force alternating minimizations of W and H tolW = max(0.001, tol) * np.sqrt(init_grad) tolH = tolW for n_iter in range(1, max_iter + 1): # stopping condition # as discussed in paper proj_grad_W = squared_norm(gradW * np.logical_or(gradW < 0, W > 0)) proj_grad_H = squared_norm(gradH * np.logical_or(gradH < 0, H > 0)) if (proj_grad_W + proj_grad_H) / init_grad < tol ** 2: break # update W W, gradW, iterW = _update_projected_gradient_w(X, W, H, tolW, nls_max_iter, alpha, l1_ratio, sparseness, beta, eta) if iterW == 1: tolW = 0.1 * tolW # update H H, gradH, iterH = _update_projected_gradient_h(X, W, H, tolH, nls_max_iter, alpha, l1_ratio, sparseness, beta, eta) if iterH == 1: tolH = 0.1 * tolH H[H == 0] = 0 # fix up negative zeros if n_iter == max_iter: W, _, _ = _update_projected_gradient_w(X, W, H, tol, nls_max_iter, alpha, l1_ratio, sparseness, beta, eta) return W, H, n_iter def _update_coordinate_descent(X, W, Ht, l1_reg, l2_reg, shuffle, random_state): """Helper function for _fit_coordinate_descent Update W to minimize the objective function, iterating once over all coordinates. By symmetry, to update H, one can call _update_coordinate_descent(X.T, Ht, W, ...) """ n_components = Ht.shape[1] HHt = fast_dot(Ht.T, Ht) XHt = safe_sparse_dot(X, Ht) # L2 regularization corresponds to increase of the diagonal of HHt if l2_reg != 0.: # adds l2_reg only on the diagonal HHt.flat[::n_components + 1] += l2_reg # L1 regularization corresponds to decrease of each element of XHt if l1_reg != 0.: XHt -= l1_reg if shuffle: permutation = random_state.permutation(n_components) else: permutation = np.arange(n_components) # The following seems to be required on 64-bit Windows w/ Python 3.5. permutation = np.asarray(permutation, dtype=np.intp) return _update_cdnmf_fast(W, HHt, XHt, permutation) def _fit_coordinate_descent(X, W, H, tol=1e-4, max_iter=200, alpha=0.001, l1_ratio=0., regularization=None, update_H=True, verbose=0, shuffle=False, random_state=None): """Compute Non-negative Matrix Factorization (NMF) with Coordinate Descent The objective function is minimized with an alternating minimization of W and H. Each minimization is done with a cyclic (up to a permutation of the features) Coordinate Descent. Parameters ---------- X : array-like, shape (n_samples, n_features) Constant matrix. W : array-like, shape (n_samples, n_components) Initial guess for the solution. H : array-like, shape (n_components, n_features) Initial guess for the solution. tol : float, default: 1e-4 Tolerance of the stopping condition. max_iter : integer, default: 200 Maximum number of iterations before timing out. alpha : double, default: 0. Constant that multiplies the regularization terms. l1_ratio : double, default: 0. The regularization mixing parameter, with 0 <= l1_ratio <= 1. For l1_ratio = 0 the penalty is an L2 penalty. For l1_ratio = 1 it is an L1 penalty. For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2. regularization : 'both' | 'components' | 'transformation' | None Select whether the regularization affects the components (H), the transformation (W), both or none of them. update_H : boolean, default: True Set to True, both W and H will be estimated from initial guesses. Set to False, only W will be estimated. verbose : integer, default: 0 The verbosity level. shuffle : boolean, default: False If true, randomize the order of coordinates in the CD solver. random_state : integer seed, RandomState instance, or None (default) Random number generator seed control. Returns ------- W : array-like, shape (n_samples, n_components) Solution to the non-negative least squares problem. H : array-like, shape (n_components, n_features) Solution to the non-negative least squares problem. n_iter : int The number of iterations done by the algorithm. References ---------- Cichocki, Andrzej, and P. H. A. N. Anh-Huy. "Fast local algorithms for large scale nonnegative matrix and tensor factorizations." IEICE transactions on fundamentals of electronics, communications and computer sciences 92.3: 708-721, 2009. """ # so W and Ht are both in C order in memory Ht = check_array(H.T, order='C') X = check_array(X, accept_sparse='csr') # L1 and L2 regularization l1_H, l2_H, l1_W, l2_W = 0, 0, 0, 0 if regularization in ('both', 'components'): alpha = float(alpha) l1_H = l1_ratio * alpha l2_H = (1. - l1_ratio) * alpha if regularization in ('both', 'transformation'): alpha = float(alpha) l1_W = l1_ratio * alpha l2_W = (1. - l1_ratio) * alpha rng = check_random_state(random_state) for n_iter in range(max_iter): violation = 0. # Update W violation += _update_coordinate_descent(X, W, Ht, l1_W, l2_W, shuffle, rng) # Update H if update_H: violation += _update_coordinate_descent(X.T, Ht, W, l1_H, l2_H, shuffle, rng) if n_iter == 0: violation_init = violation if violation_init == 0: break if verbose: print("violation:", violation / violation_init) if violation / violation_init <= tol: if verbose: print("Converged at iteration", n_iter + 1) break return W, Ht.T, n_iter def non_negative_factorization(X, W=None, H=None, n_components=None, init='random', update_H=True, solver='cd', tol=1e-4, max_iter=200, alpha=0., l1_ratio=0., regularization=None, random_state=None, verbose=0, shuffle=False, nls_max_iter=2000, sparseness=None, beta=1, eta=0.1): """Compute Non-negative Matrix Factorization (NMF) Find two non-negative matrices (W, H) whose product approximates the non- negative matrix X. This factorization can be used for example for dimensionality reduction, source separation or topic extraction. The objective function is:: 0.5 * ||X - WH||_Fro^2 + alpha * l1_ratio * ||vec(W)||_1 + alpha * l1_ratio * ||vec(H)||_1 + 0.5 * alpha * (1 - l1_ratio) * ||W||_Fro^2 + 0.5 * alpha * (1 - l1_ratio) * ||H||_Fro^2 Where:: ||A||_Fro^2 = \sum_{i,j} A_{ij}^2 (Frobenius norm) ||vec(A)||_1 = \sum_{i,j} abs(A_{ij}) (Elementwise L1 norm) The objective function is minimized with an alternating minimization of W and H. If H is given and update_H=False, it solves for W only. Parameters ---------- X : array-like, shape (n_samples, n_features) Constant matrix. W : array-like, shape (n_samples, n_components) If init='custom', it is used as initial guess for the solution. H : array-like, shape (n_components, n_features) If init='custom', it is used as initial guess for the solution. If update_H=False, it is used as a constant, to solve for W only. n_components : integer Number of components, if n_components is not set all features are kept. init : None | 'random' | 'nndsvd' | 'nndsvda' | 'nndsvdar' | 'custom' Method used to initialize the procedure. Default: 'nndsvd' if n_components < n_features, otherwise random. Valid options: - 'random': non-negative random matrices, scaled with: sqrt(X.mean() / n_components) - 'nndsvd': Nonnegative Double Singular Value Decomposition (NNDSVD) initialization (better for sparseness) - 'nndsvda': NNDSVD with zeros filled with the average of X (better when sparsity is not desired) - 'nndsvdar': NNDSVD with zeros filled with small random values (generally faster, less accurate alternative to NNDSVDa for when sparsity is not desired) - 'custom': use custom matrices W and H update_H : boolean, default: True Set to True, both W and H will be estimated from initial guesses. Set to False, only W will be estimated. solver : 'pg' | 'cd' Numerical solver to use: 'pg' is a (deprecated) Projected Gradient solver. 'cd' is a Coordinate Descent solver. tol : float, default: 1e-4 Tolerance of the stopping condition. max_iter : integer, default: 200 Maximum number of iterations before timing out. alpha : double, default: 0. Constant that multiplies the regularization terms. l1_ratio : double, default: 0. The regularization mixing parameter, with 0 <= l1_ratio <= 1. For l1_ratio = 0 the penalty is an elementwise L2 penalty (aka Frobenius Norm). For l1_ratio = 1 it is an elementwise L1 penalty. For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2. regularization : 'both' | 'components' | 'transformation' | None Select whether the regularization affects the components (H), the transformation (W), both or none of them. random_state : integer seed, RandomState instance, or None (default) Random number generator seed control. verbose : integer, default: 0 The verbosity level. shuffle : boolean, default: False If true, randomize the order of coordinates in the CD solver. nls_max_iter : integer, default: 2000 Number of iterations in NLS subproblem. Used only in the deprecated 'pg' solver. sparseness : 'data' | 'components' | None, default: None Where to enforce sparsity in the model. Used only in the deprecated 'pg' solver. beta : double, default: 1 Degree of sparseness, if sparseness is not None. Larger values mean more sparseness. Used only in the deprecated 'pg' solver. eta : double, default: 0.1 Degree of correctness to maintain, if sparsity is not None. Smaller values mean larger error. Used only in the deprecated 'pg' solver. Returns ------- W : array-like, shape (n_samples, n_components) Solution to the non-negative least squares problem. H : array-like, shape (n_components, n_features) Solution to the non-negative least squares problem. n_iter : int Actual number of iterations. References ---------- C.-J. Lin. Projected gradient methods for non-negative matrix factorization. Neural Computation, 19(2007), 2756-2779. http://www.csie.ntu.edu.tw/~cjlin/nmf/ Cichocki, Andrzej, and P. H. A. N. Anh-Huy. "Fast local algorithms for large scale nonnegative matrix and tensor factorizations." IEICE transactions on fundamentals of electronics, communications and computer sciences 92.3: 708-721, 2009. """ X = check_array(X, accept_sparse=('csr', 'csc')) check_non_negative(X, "NMF (input X)") _check_string_param(sparseness, solver) n_samples, n_features = X.shape if n_components is None: n_components = n_features if not isinstance(n_components, six.integer_types) or n_components <= 0: raise ValueError("Number of components must be positive;" " got (n_components=%r)" % n_components) if not isinstance(max_iter, numbers.Number) or max_iter < 0: raise ValueError("Maximum number of iteration must be positive;" " got (max_iter=%r)" % max_iter) if not isinstance(tol, numbers.Number) or tol < 0: raise ValueError("Tolerance for stopping criteria must be " "positive; got (tol=%r)" % tol) # check W and H, or initialize them if init == 'custom' and update_H: _check_init(H, (n_components, n_features), "NMF (input H)") _check_init(W, (n_samples, n_components), "NMF (input W)") elif not update_H: _check_init(H, (n_components, n_features), "NMF (input H)") W = np.zeros((n_samples, n_components)) else: W, H = _initialize_nmf(X, n_components, init=init, random_state=random_state) if solver == 'pg': warnings.warn("'pg' solver will be removed in release 0.19." " Use 'cd' solver instead.", DeprecationWarning) if update_H: # fit_transform W, H, n_iter = _fit_projected_gradient(X, W, H, tol, max_iter, nls_max_iter, alpha, l1_ratio, sparseness, beta, eta) else: # transform W, H, n_iter = _update_projected_gradient_w(X, W, H, tol, nls_max_iter, alpha, l1_ratio, sparseness, beta, eta) elif solver == 'cd': W, H, n_iter = _fit_coordinate_descent(X, W, H, tol, max_iter, alpha, l1_ratio, regularization, update_H=update_H, verbose=verbose, shuffle=shuffle, random_state=random_state) else: raise ValueError("Invalid solver parameter '%s'." % solver) if n_iter == max_iter: warnings.warn("Maximum number of iteration %d reached. Increase it to" " improve convergence." % max_iter, ConvergenceWarning) return W, H, n_iter class NMF(BaseEstimator, TransformerMixin): """Non-Negative Matrix Factorization (NMF) Find two non-negative matrices (W, H) whose product approximates the non- negative matrix X. This factorization can be used for example for dimensionality reduction, source separation or topic extraction. The objective function is:: 0.5 * ||X - WH||_Fro^2 + alpha * l1_ratio * ||vec(W)||_1 + alpha * l1_ratio * ||vec(H)||_1 + 0.5 * alpha * (1 - l1_ratio) * ||W||_Fro^2 + 0.5 * alpha * (1 - l1_ratio) * ||H||_Fro^2 Where:: ||A||_Fro^2 = \sum_{i,j} A_{ij}^2 (Frobenius norm) ||vec(A)||_1 = \sum_{i,j} abs(A_{ij}) (Elementwise L1 norm) The objective function is minimized with an alternating minimization of W and H. Read more in the :ref:`User Guide <NMF>`. Parameters ---------- n_components : int or None Number of components, if n_components is not set all features are kept. init : 'random' | 'nndsvd' | 'nndsvda' | 'nndsvdar' | 'custom' Method used to initialize the procedure. Default: 'nndsvdar' if n_components < n_features, otherwise random. Valid options: - 'random': non-negative random matrices, scaled with: sqrt(X.mean() / n_components) - 'nndsvd': Nonnegative Double Singular Value Decomposition (NNDSVD) initialization (better for sparseness) - 'nndsvda': NNDSVD with zeros filled with the average of X (better when sparsity is not desired) - 'nndsvdar': NNDSVD with zeros filled with small random values (generally faster, less accurate alternative to NNDSVDa for when sparsity is not desired) - 'custom': use custom matrices W and H solver : 'pg' | 'cd' Numerical solver to use: 'pg' is a Projected Gradient solver (deprecated). 'cd' is a Coordinate Descent solver (recommended). .. versionadded:: 0.17 Coordinate Descent solver. .. versionchanged:: 0.17 Deprecated Projected Gradient solver. tol : double, default: 1e-4 Tolerance value used in stopping conditions. max_iter : integer, default: 200 Number of iterations to compute. random_state : integer seed, RandomState instance, or None (default) Random number generator seed control. alpha : double, default: 0. Constant that multiplies the regularization terms. Set it to zero to have no regularization. .. versionadded:: 0.17 *alpha* used in the Coordinate Descent solver. l1_ratio : double, default: 0. The regularization mixing parameter, with 0 <= l1_ratio <= 1. For l1_ratio = 0 the penalty is an elementwise L2 penalty (aka Frobenius Norm). For l1_ratio = 1 it is an elementwise L1 penalty. For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2. .. versionadded:: 0.17 Regularization parameter *l1_ratio* used in the Coordinate Descent solver. shuffle : boolean, default: False If true, randomize the order of coordinates in the CD solver. .. versionadded:: 0.17 *shuffle* parameter used in the Coordinate Descent solver. nls_max_iter : integer, default: 2000 Number of iterations in NLS subproblem. Used only in the deprecated 'pg' solver. .. versionchanged:: 0.17 Deprecated Projected Gradient solver. Use Coordinate Descent solver instead. sparseness : 'data' | 'components' | None, default: None Where to enforce sparsity in the model. Used only in the deprecated 'pg' solver. .. versionchanged:: 0.17 Deprecated Projected Gradient solver. Use Coordinate Descent solver instead. beta : double, default: 1 Degree of sparseness, if sparseness is not None. Larger values mean more sparseness. Used only in the deprecated 'pg' solver. .. versionchanged:: 0.17 Deprecated Projected Gradient solver. Use Coordinate Descent solver instead. eta : double, default: 0.1 Degree of correctness to maintain, if sparsity is not None. Smaller values mean larger error. Used only in the deprecated 'pg' solver. .. versionchanged:: 0.17 Deprecated Projected Gradient solver. Use Coordinate Descent solver instead. Attributes ---------- components_ : array, [n_components, n_features] Non-negative components of the data. reconstruction_err_ : number Frobenius norm of the matrix difference between the training data and the reconstructed data from the fit produced by the model. ``|| X - WH ||_2`` n_iter_ : int Actual number of iterations. Examples -------- >>> import numpy as np >>> X = np.array([[1,1], [2, 1], [3, 1.2], [4, 1], [5, 0.8], [6, 1]]) >>> from sklearn.decomposition import NMF >>> model = NMF(n_components=2, init='random', random_state=0) >>> model.fit(X) #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE NMF(alpha=0.0, beta=1, eta=0.1, init='random', l1_ratio=0.0, max_iter=200, n_components=2, nls_max_iter=2000, random_state=0, shuffle=False, solver='cd', sparseness=None, tol=0.0001, verbose=0) >>> model.components_ array([[ 2.09783018, 0.30560234], [ 2.13443044, 2.13171694]]) >>> model.reconstruction_err_ #doctest: +ELLIPSIS 0.00115993... References ---------- C.-J. Lin. Projected gradient methods for non-negative matrix factorization. Neural Computation, 19(2007), 2756-2779. http://www.csie.ntu.edu.tw/~cjlin/nmf/ Cichocki, Andrzej, and P. H. A. N. Anh-Huy. "Fast local algorithms for large scale nonnegative matrix and tensor factorizations." IEICE transactions on fundamentals of electronics, communications and computer sciences 92.3: 708-721, 2009. """ def __init__(self, n_components=None, init=None, solver='cd', tol=1e-4, max_iter=200, random_state=None, alpha=0., l1_ratio=0., verbose=0, shuffle=False, nls_max_iter=2000, sparseness=None, beta=1, eta=0.1): self.n_components = n_components self.init = init self.solver = solver self.tol = tol self.max_iter = max_iter self.random_state = random_state self.alpha = alpha self.l1_ratio = l1_ratio self.verbose = verbose self.shuffle = shuffle if sparseness is not None: warnings.warn("Controlling regularization through the sparseness," " beta and eta arguments is only available" " for 'pg' solver, which will be removed" " in release 0.19. Use another solver with L1 or L2" " regularization instead.", DeprecationWarning) self.nls_max_iter = nls_max_iter self.sparseness = sparseness self.beta = beta self.eta = eta def fit_transform(self, X, y=None, W=None, H=None): """Learn a NMF model for the data X and returns the transformed data. This is more efficient than calling fit followed by transform. Parameters ---------- X: {array-like, sparse matrix}, shape (n_samples, n_features) Data matrix to be decomposed W : array-like, shape (n_samples, n_components) If init='custom', it is used as initial guess for the solution. H : array-like, shape (n_components, n_features) If init='custom', it is used as initial guess for the solution. Attributes ---------- components_ : array-like, shape (n_components, n_features) Factorization matrix, sometimes called 'dictionary'. n_iter_ : int Actual number of iterations for the transform. Returns ------- W: array, shape (n_samples, n_components) Transformed data. """ X = check_array(X, accept_sparse=('csr', 'csc')) W, H, n_iter_ = non_negative_factorization( X=X, W=W, H=H, n_components=self.n_components, init=self.init, update_H=True, solver=self.solver, tol=self.tol, max_iter=self.max_iter, alpha=self.alpha, l1_ratio=self.l1_ratio, regularization='both', random_state=self.random_state, verbose=self.verbose, shuffle=self.shuffle, nls_max_iter=self.nls_max_iter, sparseness=self.sparseness, beta=self.beta, eta=self.eta) if self.solver == 'pg': self.comp_sparseness_ = _sparseness(H.ravel()) self.data_sparseness_ = _sparseness(W.ravel()) self.reconstruction_err_ = _safe_compute_error(X, W, H) self.n_components_ = H.shape[0] self.components_ = H self.n_iter_ = n_iter_ return W def fit(self, X, y=None, **params): """Learn a NMF model for the data X. Parameters ---------- X: {array-like, sparse matrix}, shape (n_samples, n_features) Data matrix to be decomposed Attributes ---------- components_ : array-like, shape (n_components, n_features) Factorization matrix, sometimes called 'dictionary'. n_iter_ : int Actual number of iterations for the transform. Returns ------- self """ self.fit_transform(X, **params) return self def transform(self, X): """Transform the data X according to the fitted NMF model Parameters ---------- X: {array-like, sparse matrix}, shape (n_samples, n_features) Data matrix to be transformed by the model Attributes ---------- n_iter_ : int Actual number of iterations for the transform. Returns ------- W: array, shape (n_samples, n_components) Transformed data """ check_is_fitted(self, 'n_components_') W, _, n_iter_ = non_negative_factorization( X=X, W=None, H=self.components_, n_components=self.n_components_, init=self.init, update_H=False, solver=self.solver, tol=self.tol, max_iter=self.max_iter, alpha=self.alpha, l1_ratio=self.l1_ratio, regularization='both', random_state=self.random_state, verbose=self.verbose, shuffle=self.shuffle, nls_max_iter=self.nls_max_iter, sparseness=self.sparseness, beta=self.beta, eta=self.eta) self.n_iter_ = n_iter_ return W def inverse_transform(self, W): """ Parameters ---------- W: {array-like, sparse matrix}, shape (n_samples, n_components) Transformed Data matrix Returns ------- X: {array-like, sparse matrix}, shape (n_samples, n_features) Data matrix of original shape .. versionadded:: 0.18 """ check_is_fitted(self, 'n_components_') return np.dot(W, self.components_) @deprecated("It will be removed in release 0.19. Use NMF instead." "'pg' solver is still available until release 0.19.") class ProjectedGradientNMF(NMF): """Non-Negative Matrix Factorization (NMF) Find two non-negative matrices (W, H) whose product approximates the non- negative matrix X. This factorization can be used for example for dimensionality reduction, source separation or topic extraction. The objective function is:: 0.5 * ||X - WH||_Fro^2 + alpha * l1_ratio * ||vec(W)||_1 + alpha * l1_ratio * ||vec(H)||_1 + 0.5 * alpha * (1 - l1_ratio) * ||W||_Fro^2 + 0.5 * alpha * (1 - l1_ratio) * ||H||_Fro^2 Where:: ||A||_Fro^2 = \sum_{i,j} A_{ij}^2 (Frobenius norm) ||vec(A)||_1 = \sum_{i,j} abs(A_{ij}) (Elementwise L1 norm) The objective function is minimized with an alternating minimization of W and H. Read more in the :ref:`User Guide <NMF>`. Parameters ---------- n_components : int or None Number of components, if n_components is not set all features are kept. init : 'random' | 'nndsvd' | 'nndsvda' | 'nndsvdar' | 'custom' Method used to initialize the procedure. Default: 'nndsvdar' if n_components < n_features, otherwise random. Valid options: - 'random': non-negative random matrices, scaled with: sqrt(X.mean() / n_components) - 'nndsvd': Nonnegative Double Singular Value Decomposition (NNDSVD) initialization (better for sparseness) - 'nndsvda': NNDSVD with zeros filled with the average of X (better when sparsity is not desired) - 'nndsvdar': NNDSVD with zeros filled with small random values (generally faster, less accurate alternative to NNDSVDa for when sparsity is not desired) - 'custom': use custom matrices W and H solver : 'pg' | 'cd' Numerical solver to use: 'pg' is a Projected Gradient solver (deprecated). 'cd' is a Coordinate Descent solver (recommended). .. versionadded:: 0.17 Coordinate Descent solver. .. versionchanged:: 0.17 Deprecated Projected Gradient solver. tol : double, default: 1e-4 Tolerance value used in stopping conditions. max_iter : integer, default: 200 Number of iterations to compute. random_state : integer seed, RandomState instance, or None (default) Random number generator seed control. alpha : double, default: 0. Constant that multiplies the regularization terms. Set it to zero to have no regularization. .. versionadded:: 0.17 *alpha* used in the Coordinate Descent solver. l1_ratio : double, default: 0. The regularization mixing parameter, with 0 <= l1_ratio <= 1. For l1_ratio = 0 the penalty is an elementwise L2 penalty (aka Frobenius Norm). For l1_ratio = 1 it is an elementwise L1 penalty. For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2. .. versionadded:: 0.17 Regularization parameter *l1_ratio* used in the Coordinate Descent solver. shuffle : boolean, default: False If true, randomize the order of coordinates in the CD solver. .. versionadded:: 0.17 *shuffle* parameter used in the Coordinate Descent solver. nls_max_iter : integer, default: 2000 Number of iterations in NLS subproblem. Used only in the deprecated 'pg' solver. .. versionchanged:: 0.17 Deprecated Projected Gradient solver. Use Coordinate Descent solver instead. sparseness : 'data' | 'components' | None, default: None Where to enforce sparsity in the model. Used only in the deprecated 'pg' solver. .. versionchanged:: 0.17 Deprecated Projected Gradient solver. Use Coordinate Descent solver instead. beta : double, default: 1 Degree of sparseness, if sparseness is not None. Larger values mean more sparseness. Used only in the deprecated 'pg' solver. .. versionchanged:: 0.17 Deprecated Projected Gradient solver. Use Coordinate Descent solver instead. eta : double, default: 0.1 Degree of correctness to maintain, if sparsity is not None. Smaller values mean larger error. Used only in the deprecated 'pg' solver. .. versionchanged:: 0.17 Deprecated Projected Gradient solver. Use Coordinate Descent solver instead. Attributes ---------- components_ : array, [n_components, n_features] Non-negative components of the data. reconstruction_err_ : number Frobenius norm of the matrix difference between the training data and the reconstructed data from the fit produced by the model. ``|| X - WH ||_2`` n_iter_ : int Actual number of iterations. Examples -------- >>> import numpy as np >>> X = np.array([[1,1], [2, 1], [3, 1.2], [4, 1], [5, 0.8], [6, 1]]) >>> from sklearn.decomposition import NMF >>> model = NMF(n_components=2, init='random', random_state=0) >>> model.fit(X) #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE NMF(alpha=0.0, beta=1, eta=0.1, init='random', l1_ratio=0.0, max_iter=200, n_components=2, nls_max_iter=2000, random_state=0, shuffle=False, solver='cd', sparseness=None, tol=0.0001, verbose=0) >>> model.components_ array([[ 2.09783018, 0.30560234], [ 2.13443044, 2.13171694]]) >>> model.reconstruction_err_ #doctest: +ELLIPSIS 0.00115993... References ---------- C.-J. Lin. Projected gradient methods for non-negative matrix factorization. Neural Computation, 19(2007), 2756-2779. http://www.csie.ntu.edu.tw/~cjlin/nmf/ Cichocki, Andrzej, and P. H. A. N. Anh-Huy. "Fast local algorithms for large scale nonnegative matrix and tensor factorizations." IEICE transactions on fundamentals of electronics, communications and computer sciences 92.3: 708-721, 2009. """ def __init__(self, n_components=None, solver='pg', init=None, tol=1e-4, max_iter=200, random_state=None, alpha=0., l1_ratio=0., verbose=0, nls_max_iter=2000, sparseness=None, beta=1, eta=0.1): super(ProjectedGradientNMF, self).__init__( n_components=n_components, init=init, solver='pg', tol=tol, max_iter=max_iter, random_state=random_state, alpha=alpha, l1_ratio=l1_ratio, verbose=verbose, nls_max_iter=nls_max_iter, sparseness=sparseness, beta=beta, eta=eta)
bsd-3-clause
embray/numpy
numpy/lib/npyio.py
1
66490
from __future__ import division, absolute_import, print_function import sys import os import re import itertools import warnings import weakref from operator import itemgetter import numpy as np from . import format from ._datasource import DataSource from ._compiled_base import packbits, unpackbits from ._iotools import ( LineSplitter, NameValidator, StringConverter, ConverterError, ConverterLockError, ConversionWarning, _is_string_like, has_nested_fields, flatten_dtype, easy_dtype, _bytes_to_name ) from numpy.compat import ( asbytes, asstr, asbytes_nested, bytes, basestring, unicode ) if sys.version_info[0] >= 3: import pickle else: import cPickle as pickle from future_builtins import map loads = pickle.loads __all__ = [ 'savetxt', 'loadtxt', 'genfromtxt', 'ndfromtxt', 'mafromtxt', 'recfromtxt', 'recfromcsv', 'load', 'loads', 'save', 'savez', 'savez_compressed', 'packbits', 'unpackbits', 'fromregex', 'DataSource'] def seek_gzip_factory(f): """Use this factory to produce the class so that we can do a lazy import on gzip. """ import gzip class GzipFile(gzip.GzipFile): def seek(self, offset, whence=0): # figure out new position (we can only seek forwards) if whence == 1: offset = self.offset + offset if whence not in [0, 1]: raise IOError("Illegal argument") if offset < self.offset: # for negative seek, rewind and do positive seek self.rewind() count = offset - self.offset for i in range(count // 1024): self.read(1024) self.read(count % 1024) def tell(self): return self.offset if isinstance(f, str): f = GzipFile(f) elif isinstance(f, gzip.GzipFile): # cast to our GzipFile if its already a gzip.GzipFile try: name = f.name except AttributeError: # Backward compatibility for <= 2.5 name = f.filename mode = f.mode f = GzipFile(fileobj=f.fileobj, filename=name) f.mode = mode return f class BagObj(object): """ BagObj(obj) Convert attribute look-ups to getitems on the object passed in. Parameters ---------- obj : class instance Object on which attribute look-up is performed. Examples -------- >>> from numpy.lib.npyio import BagObj as BO >>> class BagDemo(object): ... def __getitem__(self, key): # An instance of BagObj(BagDemo) ... # will call this method when any ... # attribute look-up is required ... result = "Doesn't matter what you want, " ... return result + "you're gonna get this" ... >>> demo_obj = BagDemo() >>> bagobj = BO(demo_obj) >>> bagobj.hello_there "Doesn't matter what you want, you're gonna get this" >>> bagobj.I_can_be_anything "Doesn't matter what you want, you're gonna get this" """ def __init__(self, obj): # Use weakref to make NpzFile objects collectable by refcount self._obj = weakref.proxy(obj) def __getattribute__(self, key): try: return object.__getattribute__(self, '_obj')[key] except KeyError: raise AttributeError(key) def zipfile_factory(*args, **kwargs): import zipfile kwargs['allowZip64'] = True return zipfile.ZipFile(*args, **kwargs) class NpzFile(object): """ NpzFile(fid) A dictionary-like object with lazy-loading of files in the zipped archive provided on construction. `NpzFile` is used to load files in the NumPy ``.npz`` data archive format. It assumes that files in the archive have a ``.npy`` extension, other files are ignored. The arrays and file strings are lazily loaded on either getitem access using ``obj['key']`` or attribute lookup using ``obj.f.key``. A list of all files (without ``.npy`` extensions) can be obtained with ``obj.files`` and the ZipFile object itself using ``obj.zip``. Attributes ---------- files : list of str List of all files in the archive with a ``.npy`` extension. zip : ZipFile instance The ZipFile object initialized with the zipped archive. f : BagObj instance An object on which attribute can be performed as an alternative to getitem access on the `NpzFile` instance itself. Parameters ---------- fid : file or str The zipped archive to open. This is either a file-like object or a string containing the path to the archive. own_fid : bool, optional Whether NpzFile should close the file handle. Requires that `fid` is a file-like object. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) >>> np.savez(outfile, x=x, y=y) >>> outfile.seek(0) >>> npz = np.load(outfile) >>> isinstance(npz, np.lib.io.NpzFile) True >>> npz.files ['y', 'x'] >>> npz['x'] # getitem access array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> npz.f.x # attribute lookup array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ def __init__(self, fid, own_fid=False): # Import is postponed to here since zipfile depends on gzip, an # optional component of the so-called standard library. _zip = zipfile_factory(fid) self._files = _zip.namelist() self.files = [] for x in self._files: if x.endswith('.npy'): self.files.append(x[:-4]) else: self.files.append(x) self.zip = _zip self.f = BagObj(self) if own_fid: self.fid = fid else: self.fid = None def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() def close(self): """ Close the file. """ if self.zip is not None: self.zip.close() self.zip = None if self.fid is not None: self.fid.close() self.fid = None self.f = None # break reference cycle def __del__(self): self.close() def __getitem__(self, key): # FIXME: This seems like it will copy strings around # more than is strictly necessary. The zipfile # will read the string and then # the format.read_array will copy the string # to another place in memory. # It would be better if the zipfile could read # (or at least uncompress) the data # directly into the array memory. member = 0 if key in self._files: member = 1 elif key in self.files: member = 1 key += '.npy' if member: bytes = self.zip.open(key) magic = bytes.read(len(format.MAGIC_PREFIX)) bytes.close() if magic == format.MAGIC_PREFIX: bytes = self.zip.open(key) return format.read_array(bytes) else: return self.zip.read(key) else: raise KeyError("%s is not a file in the archive" % key) def __iter__(self): return iter(self.files) def items(self): """ Return a list of tuples, with each tuple (filename, array in file). """ return [(f, self[f]) for f in self.files] def iteritems(self): """Generator that returns tuples (filename, array in file).""" for f in self.files: yield (f, self[f]) def keys(self): """Return files in the archive with a ``.npy`` extension.""" return self.files def iterkeys(self): """Return an iterator over the files in the archive.""" return self.__iter__() def __contains__(self, key): return self.files.__contains__(key) def load(file, mmap_mode=None): """ Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files. Parameters ---------- file : file-like object or string The file to read. Compressed files with the filename extension ``.gz`` are acceptable. File-like objects must support the ``seek()`` and ``read()`` methods. Pickled files require that the file-like object support the ``readline()`` method as well. mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional If not None, then memory-map the file, using the given mode (see `numpy.memmap` for a detailed description of the modes). A memory-mapped array is kept on disk. However, it can be accessed and sliced like any ndarray. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory. Returns ------- result : array, tuple, dict, etc. Data stored in the file. For ``.npz`` files, the returned instance of NpzFile class must be closed to avoid leaking file descriptors. Raises ------ IOError If the input file does not exist or cannot be read. See Also -------- save, savez, savez_compressed, loadtxt memmap : Create a memory-map to an array stored in a file on disk. Notes ----- - If the file contains pickle data, then whatever object is stored in the pickle is returned. - If the file is a ``.npy`` file, then a single array is returned. - If the file is a ``.npz`` file, then a dictionary-like object is returned, containing ``{filename: array}`` key-value pairs, one for each file in the archive. - If the file is a ``.npz`` file, the returned value supports the context manager protocol in a similar fashion to the open function:: with load('foo.npz') as data: a = data['a'] The underlying file descriptor is closed when exiting the 'with' block. Examples -------- Store data to disk, and load it again: >>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]])) >>> np.load('/tmp/123.npy') array([[1, 2, 3], [4, 5, 6]]) Store compressed data to disk, and load it again: >>> a=np.array([[1, 2, 3], [4, 5, 6]]) >>> b=np.array([1, 2]) >>> np.savez('/tmp/123.npz', a=a, b=b) >>> data = np.load('/tmp/123.npz') >>> data['a'] array([[1, 2, 3], [4, 5, 6]]) >>> data['b'] array([1, 2]) >>> data.close() Mem-map the stored array, and then access the second row directly from disk: >>> X = np.load('/tmp/123.npy', mmap_mode='r') >>> X[1, :] memmap([4, 5, 6]) """ import gzip own_fid = False if isinstance(file, basestring): fid = open(file, "rb") own_fid = True elif isinstance(file, gzip.GzipFile): fid = seek_gzip_factory(file) else: fid = file try: # Code to distinguish from NumPy binary files and pickles. _ZIP_PREFIX = asbytes('PK\x03\x04') N = len(format.MAGIC_PREFIX) magic = fid.read(N) fid.seek(-N, 1) # back-up if magic.startswith(_ZIP_PREFIX): # zip-file (assume .npz) # Transfer file ownership to NpzFile tmp = own_fid own_fid = False return NpzFile(fid, own_fid=tmp) elif magic == format.MAGIC_PREFIX: # .npy file if mmap_mode: return format.open_memmap(file, mode=mmap_mode) else: return format.read_array(fid) else: # Try a pickle try: return pickle.load(fid) except: raise IOError( "Failed to interpret file %s as a pickle" % repr(file)) finally: if own_fid: fid.close() def save(file, arr): """ Save an array to a binary file in NumPy ``.npy`` format. Parameters ---------- file : file or str File or filename to which the data is saved. If file is a file-object, then the filename is unchanged. If file is a string, a ``.npy`` extension will be appended to the file name if it does not already have one. arr : array_like Array data to be saved. See Also -------- savez : Save several arrays into a ``.npz`` archive savetxt, load Notes ----- For a description of the ``.npy`` format, see `format`. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> np.save(outfile, x) >>> outfile.seek(0) # Only needed here to simulate closing & reopening file >>> np.load(outfile) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ own_fid = False if isinstance(file, basestring): if not file.endswith('.npy'): file = file + '.npy' fid = open(file, "wb") own_fid = True else: fid = file try: arr = np.asanyarray(arr) format.write_array(fid, arr) finally: if own_fid: fid.close() def savez(file, *args, **kwds): """ Save several arrays into a single file in uncompressed ``.npz`` format. If arguments are passed in with no keywords, the corresponding variable names, in the ``.npz`` file, are 'arr_0', 'arr_1', etc. If keyword arguments are given, the corresponding variable names, in the ``.npz`` file will match the keyword names. Parameters ---------- file : str or file Either the file name (string) or an open file (file-like object) where the data will be saved. If file is a string, the ``.npz`` extension will be appended to the file name if it is not already there. args : Arguments, optional Arrays to save to the file. Since it is not possible for Python to know the names of the arrays outside `savez`, the arrays will be saved with names "arr_0", "arr_1", and so on. These arguments can be any expression. kwds : Keyword arguments, optional Arrays to save to the file. Arrays will be saved in the file with the keyword names. Returns ------- None See Also -------- save : Save a single array to a binary file in NumPy format. savetxt : Save an array to a file as plain text. savez_compressed : Save several arrays into a compressed ``.npz`` archive Notes ----- The ``.npz`` file format is a zipped archive of files named after the variables they contain. The archive is not compressed and each file in the archive contains one variable in ``.npy`` format. For a description of the ``.npy`` format, see `format`. When opening the saved ``.npz`` file with `load` a `NpzFile` object is returned. This is a dictionary-like object which can be queried for its list of arrays (with the ``.files`` attribute), and for the arrays themselves. Examples -------- >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x) Using `savez` with \\*args, the arrays are saved with default names. >>> np.savez(outfile, x, y) >>> outfile.seek(0) # Only needed here to simulate closing & reopening file >>> npzfile = np.load(outfile) >>> npzfile.files ['arr_1', 'arr_0'] >>> npzfile['arr_0'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Using `savez` with \\**kwds, the arrays are saved with the keyword names. >>> outfile = TemporaryFile() >>> np.savez(outfile, x=x, y=y) >>> outfile.seek(0) >>> npzfile = np.load(outfile) >>> npzfile.files ['y', 'x'] >>> npzfile['x'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) """ _savez(file, args, kwds, False) def savez_compressed(file, *args, **kwds): """ Save several arrays into a single file in compressed ``.npz`` format. If keyword arguments are given, then filenames are taken from the keywords. If arguments are passed in with no keywords, then stored file names are arr_0, arr_1, etc. Parameters ---------- file : str File name of ``.npz`` file. args : Arguments Function arguments. kwds : Keyword arguments Keywords. See Also -------- numpy.savez : Save several arrays into an uncompressed ``.npz`` file format numpy.load : Load the files created by savez_compressed. """ _savez(file, args, kwds, True) def _savez(file, args, kwds, compress): # Import is postponed to here since zipfile depends on gzip, an optional # component of the so-called standard library. import zipfile # Import deferred for startup time improvement import tempfile if isinstance(file, basestring): if not file.endswith('.npz'): file = file + '.npz' namedict = kwds for i, val in enumerate(args): key = 'arr_%d' % i if key in namedict.keys(): raise ValueError( "Cannot use un-named variables and keyword %s" % key) namedict[key] = val if compress: compression = zipfile.ZIP_DEFLATED else: compression = zipfile.ZIP_STORED zipf = zipfile_factory(file, mode="w", compression=compression) # Stage arrays in a temporary file on disk, before writing to zip. fd, tmpfile = tempfile.mkstemp(suffix='-numpy.npy') os.close(fd) try: for key, val in namedict.items(): fname = key + '.npy' fid = open(tmpfile, 'wb') try: format.write_array(fid, np.asanyarray(val)) fid.close() fid = None zipf.write(tmpfile, arcname=fname) finally: if fid: fid.close() finally: os.remove(tmpfile) zipf.close() def _getconv(dtype): """ Find the correct dtype converter. Adapted from matplotlib """ typ = dtype.type if issubclass(typ, np.bool_): return lambda x: bool(int(x)) if issubclass(typ, np.uint64): return np.uint64 if issubclass(typ, np.int64): return np.int64 if issubclass(typ, np.integer): return lambda x: int(float(x)) elif issubclass(typ, np.floating): return float elif issubclass(typ, np.complex): return complex elif issubclass(typ, np.bytes_): return bytes else: return str def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0): """ Load data from a text file. Each row in the text file must have the same number of values. Parameters ---------- fname : file or str File, filename, or generator to read. If the filename extension is ``.gz`` or ``.bz2``, the file is first decompressed. Note that generators should return byte strings for Python 3k. dtype : data-type, optional Data-type of the resulting array; default: float. If this is a record data-type, the resulting array will be 1-dimensional, and each row will be interpreted as an element of the array. In this case, the number of columns used must match the number of fields in the data-type. comments : str, optional The character used to indicate the start of a comment; default: '#'. delimiter : str, optional The string used to separate values. By default, this is any whitespace. converters : dict, optional A dictionary mapping column number to a function that will convert that column to a float. E.g., if column 0 is a date string: ``converters = {0: datestr2num}``. Converters can also be used to provide a default value for missing data (but see also `genfromtxt`): ``converters = {3: lambda s: float(s.strip() or 0)}``. Default: None. skiprows : int, optional Skip the first `skiprows` lines; default: 0. usecols : sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read. unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)``. When used with a record data-type, arrays are returned for each field. Default is False. ndmin : int, optional The returned array will have at least `ndmin` dimensions. Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2. .. versionadded:: 1.6.0 Returns ------- out : ndarray Data read from the text file. See Also -------- load, fromstring, fromregex genfromtxt : Load data with missing values handled as specified. scipy.io.loadmat : reads MATLAB data files Notes ----- This function aims to be a fast reader for simply formatted files. The `genfromtxt` function provides more sophisticated handling of, e.g., lines with missing values. Examples -------- >>> from StringIO import StringIO # StringIO behaves like a file object >>> c = StringIO("0 1\\n2 3") >>> np.loadtxt(c) array([[ 0., 1.], [ 2., 3.]]) >>> d = StringIO("M 21 72\\nF 35 58") >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'), ... 'formats': ('S1', 'i4', 'f4')}) array([('M', 21, 72.0), ('F', 35, 58.0)], dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')]) >>> c = StringIO("1,0,2\\n3,0,4") >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True) >>> x array([ 1., 3.]) >>> y array([ 2., 4.]) """ # Type conversions for Py3 convenience comments = asbytes(comments) user_converters = converters if delimiter is not None: delimiter = asbytes(delimiter) if usecols is not None: usecols = list(usecols) fown = False try: if _is_string_like(fname): fown = True if fname.endswith('.gz'): fh = iter(seek_gzip_factory(fname)) elif fname.endswith('.bz2'): import bz2 fh = iter(bz2.BZ2File(fname)) elif sys.version_info[0] == 2: fh = iter(open(fname, 'U')) else: fh = iter(open(fname)) else: fh = iter(fname) except TypeError: raise ValueError('fname must be a string, file handle, or generator') X = [] def flatten_dtype(dt): """Unpack a structured data-type, and produce re-packing info.""" if dt.names is None: # If the dtype is flattened, return. # If the dtype has a shape, the dtype occurs # in the list more than once. shape = dt.shape if len(shape) == 0: return ([dt.base], None) else: packing = [(shape[-1], list)] if len(shape) > 1: for dim in dt.shape[-2::-1]: packing = [(dim*packing[0][0], packing*dim)] return ([dt.base] * int(np.prod(dt.shape)), packing) else: types = [] packing = [] for field in dt.names: tp, bytes = dt.fields[field] flat_dt, flat_packing = flatten_dtype(tp) types.extend(flat_dt) # Avoid extra nesting for subarrays if len(tp.shape) > 0: packing.extend(flat_packing) else: packing.append((len(flat_dt), flat_packing)) return (types, packing) def pack_items(items, packing): """Pack items into nested lists based on re-packing info.""" if packing is None: return items[0] elif packing is tuple: return tuple(items) elif packing is list: return list(items) else: start = 0 ret = [] for length, subpacking in packing: ret.append(pack_items(items[start:start+length], subpacking)) start += length return tuple(ret) def split_line(line): """Chop off comments, strip, and split at delimiter.""" line = asbytes(line).split(comments)[0].strip(asbytes('\r\n')) if line: return line.split(delimiter) else: return [] try: # Make sure we're dealing with a proper dtype dtype = np.dtype(dtype) defconv = _getconv(dtype) # Skip the first `skiprows` lines for i in range(skiprows): next(fh) # Read until we find a line with some values, and use # it to estimate the number of columns, N. first_vals = None try: while not first_vals: first_line = next(fh) first_vals = split_line(first_line) except StopIteration: # End of lines reached first_line = '' first_vals = [] warnings.warn('loadtxt: Empty input file: "%s"' % fname) N = len(usecols or first_vals) dtype_types, packing = flatten_dtype(dtype) if len(dtype_types) > 1: # We're dealing with a structured array, each field of # the dtype matches a column converters = [_getconv(dt) for dt in dtype_types] else: # All fields have the same dtype converters = [defconv for i in range(N)] if N > 1: packing = [(N, tuple)] # By preference, use the converters specified by the user for i, conv in (user_converters or {}).items(): if usecols: try: i = usecols.index(i) except ValueError: # Unused converter specified continue converters[i] = conv # Parse each line, including the first for i, line in enumerate(itertools.chain([first_line], fh)): vals = split_line(line) if len(vals) == 0: continue if usecols: vals = [vals[i] for i in usecols] # Convert each value according to its column and store items = [conv(val) for (conv, val) in zip(converters, vals)] # Then pack it according to the dtype's nesting items = pack_items(items, packing) X.append(items) finally: if fown: fh.close() X = np.array(X, dtype) # Multicolumn data are returned with shape (1, N, M), i.e. # (1, 1, M) for a single row - remove the singleton dimension there if X.ndim == 3 and X.shape[:2] == (1, 1): X.shape = (1, -1) # Verify that the array has at least dimensions `ndmin`. # Check correctness of the values of `ndmin` if not ndmin in [0, 1, 2]: raise ValueError('Illegal value of ndmin keyword: %s' % ndmin) # Tweak the size and shape of the arrays - remove extraneous dimensions if X.ndim > ndmin: X = np.squeeze(X) # and ensure we have the minimum number of dimensions asked for # - has to be in this order for the odd case ndmin=1, X.squeeze().ndim=0 if X.ndim < ndmin: if ndmin == 1: X = np.atleast_1d(X) elif ndmin == 2: X = np.atleast_2d(X).T if unpack: if len(dtype_types) > 1: # For structured arrays, return an array for each field. return [X[field] for field in dtype.names] else: return X.T else: return X def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# '): """ Save an array to a text file. Parameters ---------- fname : filename or file handle If the filename ends in ``.gz``, the file is automatically saved in compressed gzip format. `loadtxt` understands gzipped files transparently. X : array_like Data to be saved to a text file. fmt : str or sequence of strs, optional A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. 'Iteration %d -- %10.5f', in which case `delimiter` is ignored. For complex `X`, the legal options for `fmt` are: a) a single specifier, `fmt='%.4e'`, resulting in numbers formatted like `' (%s+%sj)' % (fmt, fmt)` b) a full string specifying every real and imaginary part, e.g. `' %.4e %+.4j %.4e %+.4j %.4e %+.4j'` for 3 columns c) a list of specifiers, one per column - in this case, the real and imaginary part must have separate specifiers, e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns delimiter : str, optional Character separating columns. newline : str, optional .. versionadded:: 1.5.0 header : str, optional String that will be written at the beginning of the file. .. versionadded:: 1.7.0 footer : str, optional String that will be written at the end of the file. .. versionadded:: 1.7.0 comments : str, optional String that will be prepended to the ``header`` and ``footer`` strings, to mark them as comments. Default: '# ', as expected by e.g. ``numpy.loadtxt``. .. versionadded:: 1.7.0 Character separating lines. See Also -------- save : Save an array to a binary file in NumPy ``.npy`` format savez : Save several arrays into an uncompressed ``.npz`` archive savez_compressed : Save several arrays into a compressed ``.npz`` archive Notes ----- Further explanation of the `fmt` parameter (``%[flag]width[.precision]specifier``): flags: ``-`` : left justify ``+`` : Forces to precede result with + or -. ``0`` : Left pad the number with zeros instead of space (see width). width: Minimum number of characters to be printed. The value is not truncated if it has more characters. precision: - For integer specifiers (eg. ``d,i,o,x``), the minimum number of digits. - For ``e, E`` and ``f`` specifiers, the number of digits to print after the decimal point. - For ``g`` and ``G``, the maximum number of significant digits. - For ``s``, the maximum number of characters. specifiers: ``c`` : character ``d`` or ``i`` : signed decimal integer ``e`` or ``E`` : scientific notation with ``e`` or ``E``. ``f`` : decimal floating point ``g,G`` : use the shorter of ``e,E`` or ``f`` ``o`` : signed octal ``s`` : string of characters ``u`` : unsigned decimal integer ``x,X`` : unsigned hexadecimal integer This explanation of ``fmt`` is not complete, for an exhaustive specification see [1]_. References ---------- .. [1] `Format Specification Mini-Language <http://docs.python.org/library/string.html# format-specification-mini-language>`_, Python Documentation. Examples -------- >>> x = y = z = np.arange(0.0,5.0,1.0) >>> np.savetxt('test.out', x, delimiter=',') # X is an array >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation """ # Py3 conversions first if isinstance(fmt, bytes): fmt = asstr(fmt) delimiter = asstr(delimiter) own_fh = False if _is_string_like(fname): own_fh = True if fname.endswith('.gz'): import gzip fh = gzip.open(fname, 'wb') else: if sys.version_info[0] >= 3: fh = open(fname, 'wb') else: fh = open(fname, 'w') elif hasattr(fname, 'write'): fh = fname else: raise ValueError('fname must be a string or file handle') try: X = np.asarray(X) # Handle 1-dimensional arrays if X.ndim == 1: # Common case -- 1d array of numbers if X.dtype.names is None: X = np.atleast_2d(X).T ncol = 1 # Complex dtype -- each field indicates a separate column else: ncol = len(X.dtype.descr) else: ncol = X.shape[1] iscomplex_X = np.iscomplexobj(X) # `fmt` can be a string with multiple insertion points or a # list of formats. E.g. '%10.5f\t%10d' or ('%10.5f', '$10d') if type(fmt) in (list, tuple): if len(fmt) != ncol: raise AttributeError('fmt has wrong shape. %s' % str(fmt)) format = asstr(delimiter).join(map(asstr, fmt)) elif isinstance(fmt, str): n_fmt_chars = fmt.count('%') error = ValueError('fmt has wrong number of %% formats: %s' % fmt) if n_fmt_chars == 1: if iscomplex_X: fmt = [' (%s+%sj)' % (fmt, fmt), ] * ncol else: fmt = [fmt, ] * ncol format = delimiter.join(fmt) elif iscomplex_X and n_fmt_chars != (2 * ncol): raise error elif ((not iscomplex_X) and n_fmt_chars != ncol): raise error else: format = fmt else: raise ValueError('invalid fmt: %r' % (fmt,)) if len(header) > 0: header = header.replace('\n', '\n' + comments) fh.write(asbytes(comments + header + newline)) if iscomplex_X: for row in X: row2 = [] for number in row: row2.append(number.real) row2.append(number.imag) fh.write(asbytes(format % tuple(row2) + newline)) else: for row in X: fh.write(asbytes(format % tuple(row) + newline)) if len(footer) > 0: footer = footer.replace('\n', '\n' + comments) fh.write(asbytes(comments + footer + newline)) finally: if own_fh: fh.close() def fromregex(file, regexp, dtype): """ Construct an array from a text file, using regular expression parsing. The returned array is always a structured array, and is constructed from all matches of the regular expression in the file. Groups in the regular expression are converted to fields of the structured array. Parameters ---------- file : str or file File name or file object to read. regexp : str or regexp Regular expression used to parse the file. Groups in the regular expression correspond to fields in the dtype. dtype : dtype or list of dtypes Dtype for the structured array. Returns ------- output : ndarray The output array, containing the part of the content of `file` that was matched by `regexp`. `output` is always a structured array. Raises ------ TypeError When `dtype` is not a valid dtype for a structured array. See Also -------- fromstring, loadtxt Notes ----- Dtypes for structured arrays can be specified in several forms, but all forms specify at least the data type and field name. For details see `doc.structured_arrays`. Examples -------- >>> f = open('test.dat', 'w') >>> f.write("1312 foo\\n1534 bar\\n444 qux") >>> f.close() >>> regexp = r"(\\d+)\\s+(...)" # match [digits, whitespace, anything] >>> output = np.fromregex('test.dat', regexp, ... [('num', np.int64), ('key', 'S3')]) >>> output array([(1312L, 'foo'), (1534L, 'bar'), (444L, 'qux')], dtype=[('num', '<i8'), ('key', '|S3')]) >>> output['num'] array([1312, 1534, 444], dtype=int64) """ own_fh = False if not hasattr(file, "read"): file = open(file, 'rb') own_fh = True try: if not hasattr(regexp, 'match'): regexp = re.compile(asbytes(regexp)) if not isinstance(dtype, np.dtype): dtype = np.dtype(dtype) seq = regexp.findall(file.read()) if seq and not isinstance(seq[0], tuple): # Only one group is in the regexp. # Create the new array as a single data-type and then # re-interpret as a single-field structured array. newdtype = np.dtype(dtype[dtype.names[0]]) output = np.array(seq, dtype=newdtype) output.dtype = dtype else: output = np.array(seq, dtype=dtype) return output finally: if own_fh: file.close() #####-------------------------------------------------------------------------- #---- --- ASCII functions --- #####-------------------------------------------------------------------------- def genfromtxt(fname, dtype=float, comments='#', delimiter=None, skiprows=0, skip_header=0, skip_footer=0, converters=None, missing='', missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt="f%i", unpack=None, usemask=False, loose=True, invalid_raise=True): """ Load data from a text file, with missing values handled as specified. Each line past the first `skip_header` lines is split at the `delimiter` character, and characters following the `comments` character are discarded. Parameters ---------- fname : file or str File, filename, or generator to read. If the filename extension is `.gz` or `.bz2`, the file is first decompressed. Note that generators must return byte strings in Python 3k. dtype : dtype, optional Data type of the resulting array. If None, the dtypes will be determined by the contents of each column, individually. comments : str, optional The character used to indicate the start of a comment. All the characters occurring on a line after a comment are discarded delimiter : str, int, or sequence, optional The string used to separate values. By default, any consecutive whitespaces act as delimiter. An integer or sequence of integers can also be provided as width(s) of each field. skip_rows : int, optional `skip_rows` was deprecated in numpy 1.5, and will be removed in numpy 2.0. Please use `skip_header` instead. skip_header : int, optional The number of lines to skip at the beginning of the file. skip_footer : int, optional The number of lines to skip at the end of the file. converters : variable, optional The set of functions that convert the data of a column to a value. The converters can also be used to provide a default value for missing data: ``converters = {3: lambda s: float(s or 0)}``. missing : variable, optional `missing` was deprecated in numpy 1.5, and will be removed in numpy 2.0. Please use `missing_values` instead. missing_values : variable, optional The set of strings corresponding to missing data. filling_values : variable, optional The set of values to be used as default when the data are missing. usecols : sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns. names : {None, True, str, sequence}, optional If `names` is True, the field names are read from the first valid line after the first `skip_header` lines. If `names` is a sequence or a single-string of comma-separated names, the names will be used to define the field names in a structured dtype. If `names` is None, the names of the dtype fields will be used, if any. excludelist : sequence, optional A list of names to exclude. This list is appended to the default list ['return','file','print']. Excluded names are appended an underscore: for example, `file` would become `file_`. deletechars : str, optional A string combining invalid characters that must be deleted from the names. defaultfmt : str, optional A format used to define default field names, such as "f%i" or "f_%02i". autostrip : bool, optional Whether to automatically strip white spaces from the variables. replace_space : char, optional Character(s) used in replacement of white spaces in the variables names. By default, use a '_'. case_sensitive : {True, False, 'upper', 'lower'}, optional If True, field names are case sensitive. If False or 'upper', field names are converted to upper case. If 'lower', field names are converted to lower case. unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)`` usemask : bool, optional If True, return a masked array. If False, return a regular array. loose : bool, optional If True, do not raise errors for invalid values. invalid_raise : bool, optional If True, an exception is raised if an inconsistency is detected in the number of columns. If False, a warning is emitted and the offending lines are skipped. Returns ------- out : ndarray Data read from the text file. If `usemask` is True, this is a masked array. See Also -------- numpy.loadtxt : equivalent function when no data is missing. Notes ----- * When spaces are used as delimiters, or when no delimiter has been given as input, there should not be any missing data between two fields. * When the variables are named (either by a flexible dtype or with `names`, there must not be any header in the file (else a ValueError exception is raised). * Individual values are not stripped of spaces by default. When using a custom converter, make sure the function does remove spaces. References ---------- .. [1] Numpy User Guide, section `I/O with Numpy <http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html>`_. Examples --------- >>> from StringIO import StringIO >>> import numpy as np Comma delimited file with mixed dtype >>> s = StringIO("1,1.3,abcde") >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ... ('mystring','S5')], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) Using dtype = None >>> s.seek(0) # needed for StringIO example only >>> data = np.genfromtxt(s, dtype=None, ... names = ['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) Specifying dtype and names >>> s.seek(0) >>> data = np.genfromtxt(s, dtype="i8,f8,S5", ... names=['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, 'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')]) An example with fixed-width columns >>> s = StringIO("11.3abcde") >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'], ... delimiter=[1,3,5]) >>> data array((1, 1.3, 'abcde'), dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')]) """ # Py3 data conversions to bytes, for convenience if comments is not None: comments = asbytes(comments) if isinstance(delimiter, unicode): delimiter = asbytes(delimiter) if isinstance(missing, unicode): missing = asbytes(missing) if isinstance(missing_values, (unicode, list, tuple)): missing_values = asbytes_nested(missing_values) # if usemask: from numpy.ma import MaskedArray, make_mask_descr # Check the input dictionary of converters user_converters = converters or {} if not isinstance(user_converters, dict): raise TypeError( "The input argument 'converter' should be a valid dictionary " "(got '%s' instead)" % type(user_converters)) # Initialize the filehandle, the LineSplitter and the NameValidator own_fhd = False try: if isinstance(fname, basestring): if sys.version_info[0] == 2: fhd = iter(np.lib._datasource.open(fname, 'rbU')) else: fhd = iter(np.lib._datasource.open(fname, 'rb')) own_fhd = True else: fhd = iter(fname) except TypeError: raise TypeError( "fname must be a string, filehandle, or generator. " "(got %s instead)" % type(fname)) split_line = LineSplitter(delimiter=delimiter, comments=comments, autostrip=autostrip)._handyman validate_names = NameValidator(excludelist=excludelist, deletechars=deletechars, case_sensitive=case_sensitive, replace_space=replace_space) # Get the first valid lines after the first skiprows ones .. if skiprows: warnings.warn( "The use of `skiprows` is deprecated, it will be removed in " "numpy 2.0.\nPlease use `skip_header` instead.", DeprecationWarning) skip_header = skiprows # Skip the first `skip_header` rows for i in range(skip_header): next(fhd) # Keep on until we find the first valid values first_values = None try: while not first_values: first_line = next(fhd) if names is True: if comments in first_line: first_line = asbytes('').join(first_line.split(comments)[1:]) first_values = split_line(first_line) except StopIteration: # return an empty array if the datafile is empty first_line = asbytes('') first_values = [] warnings.warn('genfromtxt: Empty input file: "%s"' % fname) # Should we take the first values as names ? if names is True: fval = first_values[0].strip() if fval in comments: del first_values[0] # Check the columns to use: make sure `usecols` is a list if usecols is not None: try: usecols = [_.strip() for _ in usecols.split(",")] except AttributeError: try: usecols = list(usecols) except TypeError: usecols = [usecols, ] nbcols = len(usecols or first_values) # Check the names and overwrite the dtype.names if needed if names is True: names = validate_names([_bytes_to_name(_.strip()) for _ in first_values]) first_line = asbytes('') elif _is_string_like(names): names = validate_names([_.strip() for _ in names.split(',')]) elif names: names = validate_names(names) # Get the dtype if dtype is not None: dtype = easy_dtype(dtype, defaultfmt=defaultfmt, names=names) # Make sure the names is a list (for 2.5) if names is not None: names = list(names) if usecols: for (i, current) in enumerate(usecols): # if usecols is a list of names, convert to a list of indices if _is_string_like(current): usecols[i] = names.index(current) elif current < 0: usecols[i] = current + len(first_values) # If the dtype is not None, make sure we update it if (dtype is not None) and (len(dtype) > nbcols): descr = dtype.descr dtype = np.dtype([descr[_] for _ in usecols]) names = list(dtype.names) # If `names` is not None, update the names elif (names is not None) and (len(names) > nbcols): names = [names[_] for _ in usecols] elif (names is not None) and (dtype is not None): names = list(dtype.names) # Process the missing values ............................... # Rename missing_values for convenience user_missing_values = missing_values or () # Define the list of missing_values (one column: one list) missing_values = [list([asbytes('')]) for _ in range(nbcols)] # We have a dictionary: process it field by field if isinstance(user_missing_values, dict): # Loop on the items for (key, val) in user_missing_values.items(): # Is the key a string ? if _is_string_like(key): try: # Transform it into an integer key = names.index(key) except ValueError: # We couldn't find it: the name must have been dropped continue # Redefine the key as needed if it's a column number if usecols: try: key = usecols.index(key) except ValueError: pass # Transform the value as a list of string if isinstance(val, (list, tuple)): val = [str(_) for _ in val] else: val = [str(val), ] # Add the value(s) to the current list of missing if key is None: # None acts as default for miss in missing_values: miss.extend(val) else: missing_values[key].extend(val) # We have a sequence : each item matches a column elif isinstance(user_missing_values, (list, tuple)): for (value, entry) in zip(user_missing_values, missing_values): value = str(value) if value not in entry: entry.append(value) # We have a string : apply it to all entries elif isinstance(user_missing_values, bytes): user_value = user_missing_values.split(asbytes(",")) for entry in missing_values: entry.extend(user_value) # We have something else: apply it to all entries else: for entry in missing_values: entry.extend([str(user_missing_values)]) # Process the deprecated `missing` if missing != asbytes(''): warnings.warn( "The use of `missing` is deprecated, it will be removed in " "Numpy 2.0.\nPlease use `missing_values` instead.", DeprecationWarning) values = [str(_) for _ in missing.split(asbytes(","))] for entry in missing_values: entry.extend(values) # Process the filling_values ............................... # Rename the input for convenience user_filling_values = filling_values or [] # Define the default filling_values = [None] * nbcols # We have a dictionary : update each entry individually if isinstance(user_filling_values, dict): for (key, val) in user_filling_values.items(): if _is_string_like(key): try: # Transform it into an integer key = names.index(key) except ValueError: # We couldn't find it: the name must have been dropped, continue # Redefine the key if it's a column number and usecols is defined if usecols: try: key = usecols.index(key) except ValueError: pass # Add the value to the list filling_values[key] = val # We have a sequence : update on a one-to-one basis elif isinstance(user_filling_values, (list, tuple)): n = len(user_filling_values) if (n <= nbcols): filling_values[:n] = user_filling_values else: filling_values = user_filling_values[:nbcols] # We have something else : use it for all entries else: filling_values = [user_filling_values] * nbcols # Initialize the converters ................................ if dtype is None: # Note: we can't use a [...]*nbcols, as we would have 3 times the same # ... converter, instead of 3 different converters. converters = [StringConverter(None, missing_values=miss, default=fill) for (miss, fill) in zip(missing_values, filling_values)] else: dtype_flat = flatten_dtype(dtype, flatten_base=True) # Initialize the converters if len(dtype_flat) > 1: # Flexible type : get a converter from each dtype zipit = zip(dtype_flat, missing_values, filling_values) converters = [StringConverter(dt, locked=True, missing_values=miss, default=fill) for (dt, miss, fill) in zipit] else: # Set to a default converter (but w/ different missing values) zipit = zip(missing_values, filling_values) converters = [StringConverter(dtype, locked=True, missing_values=miss, default=fill) for (miss, fill) in zipit] # Update the converters to use the user-defined ones uc_update = [] for (i, conv) in user_converters.items(): # If the converter is specified by column names, use the index instead if _is_string_like(i): try: i = names.index(i) except ValueError: continue elif usecols: try: i = usecols.index(i) except ValueError: # Unused converter specified continue # Find the value to test: if len(first_line): testing_value = first_values[i] else: testing_value = None converters[i].update(conv, locked=True, testing_value=testing_value, default=filling_values[i], missing_values=missing_values[i],) uc_update.append((i, conv)) # Make sure we have the corrected keys in user_converters... user_converters.update(uc_update) miss_chars = [_.missing_values for _ in converters] # Initialize the output lists ... # ... rows rows = [] append_to_rows = rows.append # ... masks if usemask: masks = [] append_to_masks = masks.append # ... invalid invalid = [] append_to_invalid = invalid.append # Parse each line for (i, line) in enumerate(itertools.chain([first_line, ], fhd)): values = split_line(line) nbvalues = len(values) # Skip an empty line if nbvalues == 0: continue # Select only the columns we need if usecols: try: values = [values[_] for _ in usecols] except IndexError: append_to_invalid((i + skip_header + 1, nbvalues)) continue elif nbvalues != nbcols: append_to_invalid((i + skip_header + 1, nbvalues)) continue # Store the values append_to_rows(tuple(values)) if usemask: append_to_masks(tuple([v.strip() in m for (v, m) in zip(values, missing_values)])) if own_fhd: fhd.close() # Upgrade the converters (if needed) if dtype is None: for (i, converter) in enumerate(converters): current_column = [itemgetter(i)(_m) for _m in rows] try: converter.iterupgrade(current_column) except ConverterLockError: errmsg = "Converter #%i is locked and cannot be upgraded: " % i current_column = map(itemgetter(i), rows) for (j, value) in enumerate(current_column): try: converter.upgrade(value) except (ConverterError, ValueError): errmsg += "(occurred line #%i for value '%s')" errmsg %= (j + 1 + skip_header, value) raise ConverterError(errmsg) # Check that we don't have invalid values nbinvalid = len(invalid) if nbinvalid > 0: nbrows = len(rows) + nbinvalid - skip_footer # Construct the error message template = " Line #%%i (got %%i columns instead of %i)" % nbcols if skip_footer > 0: nbinvalid_skipped = len([_ for _ in invalid if _[0] > nbrows + skip_header]) invalid = invalid[:nbinvalid - nbinvalid_skipped] skip_footer -= nbinvalid_skipped # # nbrows -= skip_footer # errmsg = [template % (i, nb) # for (i, nb) in invalid if i < nbrows] # else: errmsg = [template % (i, nb) for (i, nb) in invalid] if len(errmsg): errmsg.insert(0, "Some errors were detected !") errmsg = "\n".join(errmsg) # Raise an exception ? if invalid_raise: raise ValueError(errmsg) # Issue a warning ? else: warnings.warn(errmsg, ConversionWarning) # Strip the last skip_footer data if skip_footer > 0: rows = rows[:-skip_footer] if usemask: masks = masks[:-skip_footer] # Convert each value according to the converter: # We want to modify the list in place to avoid creating a new one... # # if loose: # conversionfuncs = [conv._loose_call for conv in converters] # else: # conversionfuncs = [conv._strict_call for conv in converters] # for (i, vals) in enumerate(rows): # rows[i] = tuple([convert(val) # for (convert, val) in zip(conversionfuncs, vals)]) if loose: rows = list(zip(*[[converter._loose_call(_r) for _r in map(itemgetter(i), rows)] for (i, converter) in enumerate(converters)])) else: rows = list(zip(*[[converter._strict_call(_r) for _r in map(itemgetter(i), rows)] for (i, converter) in enumerate(converters)])) # Reset the dtype data = rows if dtype is None: # Get the dtypes from the types of the converters column_types = [conv.type for conv in converters] # Find the columns with strings... strcolidx = [i for (i, v) in enumerate(column_types) if v in (type('S'), np.string_)] # ... and take the largest number of chars. for i in strcolidx: column_types[i] = "|S%i" % max(len(row[i]) for row in data) # if names is None: # If the dtype is uniform, don't define names, else use '' base = set([c.type for c in converters if c._checked]) if len(base) == 1: (ddtype, mdtype) = (list(base)[0], np.bool) else: ddtype = [(defaultfmt % i, dt) for (i, dt) in enumerate(column_types)] if usemask: mdtype = [(defaultfmt % i, np.bool) for (i, dt) in enumerate(column_types)] else: ddtype = list(zip(names, column_types)) mdtype = list(zip(names, [np.bool] * len(column_types))) output = np.array(data, dtype=ddtype) if usemask: outputmask = np.array(masks, dtype=mdtype) else: # Overwrite the initial dtype names if needed if names and dtype.names: dtype.names = names # Case 1. We have a structured type if len(dtype_flat) > 1: # Nested dtype, eg [('a', int), ('b', [('b0', int), ('b1', 'f4')])] # First, create the array using a flattened dtype: # [('a', int), ('b1', int), ('b2', float)] # Then, view the array using the specified dtype. if 'O' in (_.char for _ in dtype_flat): if has_nested_fields(dtype): raise NotImplementedError( "Nested fields involving objects are not supported...") else: output = np.array(data, dtype=dtype) else: rows = np.array(data, dtype=[('', _) for _ in dtype_flat]) output = rows.view(dtype) # Now, process the rowmasks the same way if usemask: rowmasks = np.array( masks, dtype=np.dtype([('', np.bool) for t in dtype_flat])) # Construct the new dtype mdtype = make_mask_descr(dtype) outputmask = rowmasks.view(mdtype) # Case #2. We have a basic dtype else: # We used some user-defined converters if user_converters: ishomogeneous = True descr = [] for (i, ttype) in enumerate([conv.type for conv in converters]): # Keep the dtype of the current converter if i in user_converters: ishomogeneous &= (ttype == dtype.type) if ttype == np.string_: ttype = "|S%i" % max(len(row[i]) for row in data) descr.append(('', ttype)) else: descr.append(('', dtype)) # So we changed the dtype ? if not ishomogeneous: # We have more than one field if len(descr) > 1: dtype = np.dtype(descr) # We have only one field: drop the name if not needed. else: dtype = np.dtype(ttype) # output = np.array(data, dtype) if usemask: if dtype.names: mdtype = [(_, np.bool) for _ in dtype.names] else: mdtype = np.bool outputmask = np.array(masks, dtype=mdtype) # Try to take care of the missing data we missed names = output.dtype.names if usemask and names: for (name, conv) in zip(names or (), converters): missing_values = [conv(_) for _ in conv.missing_values if _ != asbytes('')] for mval in missing_values: outputmask[name] |= (output[name] == mval) # Construct the final array if usemask: output = output.view(MaskedArray) output._mask = outputmask if unpack: return output.squeeze().T return output.squeeze() def ndfromtxt(fname, **kwargs): """ Load ASCII data stored in a file and return it as a single array. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function. """ kwargs['usemask'] = False return genfromtxt(fname, **kwargs) def mafromtxt(fname, **kwargs): """ Load ASCII data stored in a text file and return a masked array. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function to load ASCII data. """ kwargs['usemask'] = True return genfromtxt(fname, **kwargs) def recfromtxt(fname, **kwargs): """ Load ASCII data from a file and return it in a record array. If ``usemask=False`` a standard `recarray` is returned, if ``usemask=True`` a MaskedRecords array is returned. Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function Notes ----- By default, `dtype` is None, which means that the data-type of the output array will be determined from the data. """ kwargs.update(dtype=kwargs.get('dtype', None)) usemask = kwargs.get('usemask', False) output = genfromtxt(fname, **kwargs) if usemask: from numpy.ma.mrecords import MaskedRecords output = output.view(MaskedRecords) else: output = output.view(np.recarray) return output def recfromcsv(fname, **kwargs): """ Load ASCII data stored in a comma-separated file. The returned array is a record array (if ``usemask=False``, see `recarray`) or a masked record array (if ``usemask=True``, see `ma.mrecords.MaskedRecords`). Parameters ---------- fname, kwargs : For a description of input parameters, see `genfromtxt`. See Also -------- numpy.genfromtxt : generic function to load ASCII data. """ case_sensitive = kwargs.get('case_sensitive', "lower") or "lower" names = kwargs.get('names', True) if names is None: names = True kwargs.update(dtype=kwargs.get('update', None), delimiter=kwargs.get('delimiter', ",") or ",", names=names, case_sensitive=case_sensitive) usemask = kwargs.get("usemask", False) output = genfromtxt(fname, **kwargs) if usemask: from numpy.ma.mrecords import MaskedRecords output = output.view(MaskedRecords) else: output = output.view(np.recarray) return output
bsd-3-clause
Healthcast/RSV
python/all_year_predict/methods.py
2
3879
#!/usr/bin/pyhton import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, neighbors, linear_model from sklearn import svm from sklearn import metrics from sklearn.cross_validation import train_test_split from sklearn.ensemble import RandomForestClassifier def apply_algorithm(paras, X, y): if paras['clf'] == 'svm': clf = svm.SVC(kernel=paras['svm'][1], C=paras['svm'][0], probability=True) elif paras['clf'] == 'knn': clf = neighbors.KNeighborsClassifier(paras['knn'][0],\ weights=paras['knn'][1]) elif paras['clf'] == 'rf': clf = RandomForestClassifier(max_depth=paras['rf'][0], \ n_estimators=paras['rf'][1],\ max_features=paras['rf'][2]) else: print str("unknown classifier") sys.exit(2) return clf def apply_evaluation(paras, X, y, clf, data): X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3, \ random_state=0) clf.fit(X_train, y_train) r = clf.predict(X_test) d = clf.decision_function(X) p = clf.predict_proba(X).T[1]*3 h = data["hospital"].T[data["city"].index(paras["city"])] h1 = h.astype(float) m = max(h1) h1=h1/m*4 plt.figure() # plt.plot(d) plt.plot(y) plt.plot(h1) plt.plot(p) # height = 4 # bottom = -2 # ss = data["season_start"] # date=data["date1"] # c_id = data["city"].index(paras["city"]) # ylabel = data["ylabels"] # for m in ss: # plt.plot([m, m],[bottom, height], 'y--', linewidth=1) # # for m in range(1, len(ss)-1): # a = ss[m] # plt.text(a-5,height, date[a].split('-')[0]) # # #plot the start week # up=1 # for j in range(len(ylabel.T[c_id])-1): # if ylabel.T[c_id,j] == 1 : # plt.plot([j, j],[bottom, height], 'k-', linewidth=2) # if up==1: # plt.text(j-10, height-1, date[j]) # up=0 # else: # plt.text(j-10, height-2, date[j]) # up=1 # plt.show() #plot the results # x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1 # y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1 # # xx, yy = np.meshgrid(np.arange(x_min, x_max, 1), np.arange(y_min, y_max, 1)) # Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Z = Z.reshape(xx.shape) # # plt.figure() # plt.pcolormesh(xx, yy, Z) # plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train) # plt.xlim(xx.min(), xx.max()) # plt.ylim(yy.min(), yy.max()) # plt.title("binary classification classification") # plt.show() # if paras['eva'] == 'accuracy': print "The accuracy:" print metrics.accuracy_score(y_test, r) elif paras['eva'] == 'precision': print "The precision:" print metrics.precision_score(y_test, r) elif paras['eva'] == 'recall': print "The recall:" print metrics.recall_score(y_test, r) elif paras['eva'] == 'confusion': print "The confusion matrix:" print metrics.confusion_matrix(y_test, r) elif paras['eva'] == 'report': print "The report:" print metrics.classification_report(y_test, r) elif paras['eva'] == 'roc' and paras['clf'] == 'svm': scores = clf.decision_function(X_test) print "The auc:" fpr, tpr, thresholds = metrics.roc_curve(y_test, scores) roc_auc = metrics.auc(fpr, tpr) print str(roc_auc) plt.figure() plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc) plt.plot([0, 1], [0, 1], 'k--') plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.show()
gpl-2.0
nmayorov/scikit-learn
examples/plot_multilabel.py
236
4157
# Authors: Vlad Niculae, Mathieu Blondel # License: BSD 3 clause """ ========================= Multilabel classification ========================= This example simulates a multi-label document classification problem. The dataset is generated randomly based on the following process: - pick the number of labels: n ~ Poisson(n_labels) - n times, choose a class c: c ~ Multinomial(theta) - pick the document length: k ~ Poisson(length) - k times, choose a word: w ~ Multinomial(theta_c) In the above process, rejection sampling is used to make sure that n is more than 2, and that the document length is never zero. Likewise, we reject classes which have already been chosen. The documents that are assigned to both classes are plotted surrounded by two colored circles. The classification is performed by projecting to the first two principal components found by PCA and CCA for visualisation purposes, followed by using the :class:`sklearn.multiclass.OneVsRestClassifier` metaclassifier using two SVCs with linear kernels to learn a discriminative model for each class. Note that PCA is used to perform an unsupervised dimensionality reduction, while CCA is used to perform a supervised one. Note: in the plot, "unlabeled samples" does not mean that we don't know the labels (as in semi-supervised learning) but that the samples simply do *not* have a label. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_multilabel_classification from sklearn.multiclass import OneVsRestClassifier from sklearn.svm import SVC from sklearn.preprocessing import LabelBinarizer from sklearn.decomposition import PCA from sklearn.cross_decomposition import CCA def plot_hyperplane(clf, min_x, max_x, linestyle, label): # get the separating hyperplane w = clf.coef_[0] a = -w[0] / w[1] xx = np.linspace(min_x - 5, max_x + 5) # make sure the line is long enough yy = a * xx - (clf.intercept_[0]) / w[1] plt.plot(xx, yy, linestyle, label=label) def plot_subfigure(X, Y, subplot, title, transform): if transform == "pca": X = PCA(n_components=2).fit_transform(X) elif transform == "cca": X = CCA(n_components=2).fit(X, Y).transform(X) else: raise ValueError min_x = np.min(X[:, 0]) max_x = np.max(X[:, 0]) min_y = np.min(X[:, 1]) max_y = np.max(X[:, 1]) classif = OneVsRestClassifier(SVC(kernel='linear')) classif.fit(X, Y) plt.subplot(2, 2, subplot) plt.title(title) zero_class = np.where(Y[:, 0]) one_class = np.where(Y[:, 1]) plt.scatter(X[:, 0], X[:, 1], s=40, c='gray') plt.scatter(X[zero_class, 0], X[zero_class, 1], s=160, edgecolors='b', facecolors='none', linewidths=2, label='Class 1') plt.scatter(X[one_class, 0], X[one_class, 1], s=80, edgecolors='orange', facecolors='none', linewidths=2, label='Class 2') plot_hyperplane(classif.estimators_[0], min_x, max_x, 'k--', 'Boundary\nfor class 1') plot_hyperplane(classif.estimators_[1], min_x, max_x, 'k-.', 'Boundary\nfor class 2') plt.xticks(()) plt.yticks(()) plt.xlim(min_x - .5 * max_x, max_x + .5 * max_x) plt.ylim(min_y - .5 * max_y, max_y + .5 * max_y) if subplot == 2: plt.xlabel('First principal component') plt.ylabel('Second principal component') plt.legend(loc="upper left") plt.figure(figsize=(8, 6)) X, Y = make_multilabel_classification(n_classes=2, n_labels=1, allow_unlabeled=True, random_state=1) plot_subfigure(X, Y, 1, "With unlabeled samples + CCA", "cca") plot_subfigure(X, Y, 2, "With unlabeled samples + PCA", "pca") X, Y = make_multilabel_classification(n_classes=2, n_labels=1, allow_unlabeled=False, random_state=1) plot_subfigure(X, Y, 3, "Without unlabeled samples + CCA", "cca") plot_subfigure(X, Y, 4, "Without unlabeled samples + PCA", "pca") plt.subplots_adjust(.04, .02, .97, .94, .09, .2) plt.show()
bsd-3-clause
rs2/pandas
pandas/tests/io/parser/test_na_values.py
2
15082
""" Tests that NA values are properly handled during parsing for all of the parsers defined in parsers.py """ from io import StringIO import numpy as np import pytest from pandas._libs.parsers import STR_NA_VALUES from pandas import DataFrame, Index, MultiIndex import pandas._testing as tm def test_string_nas(all_parsers): parser = all_parsers data = """A,B,C a,b,c d,,f ,g,h """ result = parser.read_csv(StringIO(data)) expected = DataFrame( [["a", "b", "c"], ["d", np.nan, "f"], [np.nan, "g", "h"]], columns=["A", "B", "C"], ) tm.assert_frame_equal(result, expected) def test_detect_string_na(all_parsers): parser = all_parsers data = """A,B foo,bar NA,baz NaN,nan """ expected = DataFrame( [["foo", "bar"], [np.nan, "baz"], [np.nan, np.nan]], columns=["A", "B"] ) result = parser.read_csv(StringIO(data)) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "na_values", [ ["-999.0", "-999"], [-999, -999.0], [-999.0, -999], ["-999.0"], ["-999"], [-999.0], [-999], ], ) @pytest.mark.parametrize( "data", [ """A,B -999,1.2 2,-999 3,4.5 """, """A,B -999,1.200 2,-999.000 3,4.500 """, ], ) def test_non_string_na_values(all_parsers, data, na_values): # see gh-3611: with an odd float format, we can't match # the string "999.0" exactly but still need float matching parser = all_parsers expected = DataFrame([[np.nan, 1.2], [2.0, np.nan], [3.0, 4.5]], columns=["A", "B"]) result = parser.read_csv(StringIO(data), na_values=na_values) tm.assert_frame_equal(result, expected) def test_default_na_values(all_parsers): _NA_VALUES = { "-1.#IND", "1.#QNAN", "1.#IND", "-1.#QNAN", "#N/A", "N/A", "n/a", "NA", "<NA>", "#NA", "NULL", "null", "NaN", "nan", "-NaN", "-nan", "#N/A N/A", "", } assert _NA_VALUES == STR_NA_VALUES parser = all_parsers nv = len(_NA_VALUES) def f(i, v): if i == 0: buf = "" elif i > 0: buf = "".join([","] * i) buf = f"{buf}{v}" if i < nv - 1: joined = "".join([","] * (nv - i - 1)) buf = f"{buf}{joined}" return buf data = StringIO("\n".join(f(i, v) for i, v in enumerate(_NA_VALUES))) expected = DataFrame(np.nan, columns=range(nv), index=range(nv)) result = parser.read_csv(data, header=None) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("na_values", ["baz", ["baz"]]) def test_custom_na_values(all_parsers, na_values): parser = all_parsers data = """A,B,C ignore,this,row 1,NA,3 -1.#IND,5,baz 7,8,NaN """ expected = DataFrame( [[1.0, np.nan, 3], [np.nan, 5, np.nan], [7, 8, np.nan]], columns=["A", "B", "C"] ) result = parser.read_csv(StringIO(data), na_values=na_values, skiprows=[1]) tm.assert_frame_equal(result, expected) def test_bool_na_values(all_parsers): data = """A,B,C True,False,True NA,True,False False,NA,True""" parser = all_parsers result = parser.read_csv(StringIO(data)) expected = DataFrame( { "A": np.array([True, np.nan, False], dtype=object), "B": np.array([False, True, np.nan], dtype=object), "C": [True, False, True], } ) tm.assert_frame_equal(result, expected) def test_na_value_dict(all_parsers): data = """A,B,C foo,bar,NA bar,foo,foo foo,bar,NA bar,foo,foo""" parser = all_parsers df = parser.read_csv(StringIO(data), na_values={"A": ["foo"], "B": ["bar"]}) expected = DataFrame( { "A": [np.nan, "bar", np.nan, "bar"], "B": [np.nan, "foo", np.nan, "foo"], "C": [np.nan, "foo", np.nan, "foo"], } ) tm.assert_frame_equal(df, expected) @pytest.mark.parametrize( "index_col,expected", [ ( [0], DataFrame({"b": [np.nan], "c": [1], "d": [5]}, index=Index([0], name="a")), ), ( [0, 2], DataFrame( {"b": [np.nan], "d": [5]}, index=MultiIndex.from_tuples([(0, 1)], names=["a", "c"]), ), ), ( ["a", "c"], DataFrame( {"b": [np.nan], "d": [5]}, index=MultiIndex.from_tuples([(0, 1)], names=["a", "c"]), ), ), ], ) def test_na_value_dict_multi_index(all_parsers, index_col, expected): data = """\ a,b,c,d 0,NA,1,5 """ parser = all_parsers result = parser.read_csv(StringIO(data), na_values=set(), index_col=index_col) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "kwargs,expected", [ ( dict(), DataFrame( { "A": ["a", "b", np.nan, "d", "e", np.nan, "g"], "B": [1, 2, 3, 4, 5, 6, 7], "C": ["one", "two", "three", np.nan, "five", np.nan, "seven"], } ), ), ( dict(na_values={"A": [], "C": []}, keep_default_na=False), DataFrame( { "A": ["a", "b", "", "d", "e", "nan", "g"], "B": [1, 2, 3, 4, 5, 6, 7], "C": ["one", "two", "three", "nan", "five", "", "seven"], } ), ), ( dict(na_values=["a"], keep_default_na=False), DataFrame( { "A": [np.nan, "b", "", "d", "e", "nan", "g"], "B": [1, 2, 3, 4, 5, 6, 7], "C": ["one", "two", "three", "nan", "five", "", "seven"], } ), ), ( dict(na_values={"A": [], "C": []}), DataFrame( { "A": ["a", "b", np.nan, "d", "e", np.nan, "g"], "B": [1, 2, 3, 4, 5, 6, 7], "C": ["one", "two", "three", np.nan, "five", np.nan, "seven"], } ), ), ], ) def test_na_values_keep_default(all_parsers, kwargs, expected): data = """\ A,B,C a,1,one b,2,two ,3,three d,4,nan e,5,five nan,6, g,7,seven """ parser = all_parsers result = parser.read_csv(StringIO(data), **kwargs) tm.assert_frame_equal(result, expected) def test_no_na_values_no_keep_default(all_parsers): # see gh-4318: passing na_values=None and # keep_default_na=False yields 'None" as a na_value data = """\ A,B,C a,1,None b,2,two ,3,None d,4,nan e,5,five nan,6, g,7,seven """ parser = all_parsers result = parser.read_csv(StringIO(data), keep_default_na=False) expected = DataFrame( { "A": ["a", "b", "", "d", "e", "nan", "g"], "B": [1, 2, 3, 4, 5, 6, 7], "C": ["None", "two", "None", "nan", "five", "", "seven"], } ) tm.assert_frame_equal(result, expected) def test_no_keep_default_na_dict_na_values(all_parsers): # see gh-19227 data = "a,b\n,2" parser = all_parsers result = parser.read_csv( StringIO(data), na_values={"b": ["2"]}, keep_default_na=False ) expected = DataFrame({"a": [""], "b": [np.nan]}) tm.assert_frame_equal(result, expected) def test_no_keep_default_na_dict_na_scalar_values(all_parsers): # see gh-19227 # # Scalar values shouldn't cause the parsing to crash or fail. data = "a,b\n1,2" parser = all_parsers df = parser.read_csv(StringIO(data), na_values={"b": 2}, keep_default_na=False) expected = DataFrame({"a": [1], "b": [np.nan]}) tm.assert_frame_equal(df, expected) @pytest.mark.parametrize("col_zero_na_values", [113125, "113125"]) def test_no_keep_default_na_dict_na_values_diff_reprs(all_parsers, col_zero_na_values): # see gh-19227 data = """\ 113125,"blah","/blaha",kjsdkj,412.166,225.874,214.008 729639,"qwer","",asdfkj,466.681,,252.373 """ parser = all_parsers expected = DataFrame( { 0: [np.nan, 729639.0], 1: [np.nan, "qwer"], 2: ["/blaha", np.nan], 3: ["kjsdkj", "asdfkj"], 4: [412.166, 466.681], 5: ["225.874", ""], 6: [np.nan, 252.373], } ) result = parser.read_csv( StringIO(data), header=None, keep_default_na=False, na_values={2: "", 6: "214.008", 1: "blah", 0: col_zero_na_values}, ) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "na_filter,row_data", [ (True, [[1, "A"], [np.nan, np.nan], [3, "C"]]), (False, [["1", "A"], ["nan", "B"], ["3", "C"]]), ], ) def test_na_values_na_filter_override(all_parsers, na_filter, row_data): data = """\ A,B 1,A nan,B 3,C """ parser = all_parsers result = parser.read_csv(StringIO(data), na_values=["B"], na_filter=na_filter) expected = DataFrame(row_data, columns=["A", "B"]) tm.assert_frame_equal(result, expected) def test_na_trailing_columns(all_parsers): parser = all_parsers data = """Date,Currency,Symbol,Type,Units,UnitPrice,Cost,Tax 2012-03-14,USD,AAPL,BUY,1000 2012-05-12,USD,SBUX,SELL,500""" # Trailing columns should be all NaN. result = parser.read_csv(StringIO(data)) expected = DataFrame( [ ["2012-03-14", "USD", "AAPL", "BUY", 1000, np.nan, np.nan, np.nan], ["2012-05-12", "USD", "SBUX", "SELL", 500, np.nan, np.nan, np.nan], ], columns=[ "Date", "Currency", "Symbol", "Type", "Units", "UnitPrice", "Cost", "Tax", ], ) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "na_values,row_data", [ (1, [[np.nan, 2.0], [2.0, np.nan]]), ({"a": 2, "b": 1}, [[1.0, 2.0], [np.nan, np.nan]]), ], ) def test_na_values_scalar(all_parsers, na_values, row_data): # see gh-12224 parser = all_parsers names = ["a", "b"] data = "1,2\n2,1" result = parser.read_csv(StringIO(data), names=names, na_values=na_values) expected = DataFrame(row_data, columns=names) tm.assert_frame_equal(result, expected) def test_na_values_dict_aliasing(all_parsers): parser = all_parsers na_values = {"a": 2, "b": 1} na_values_copy = na_values.copy() names = ["a", "b"] data = "1,2\n2,1" expected = DataFrame([[1.0, 2.0], [np.nan, np.nan]], columns=names) result = parser.read_csv(StringIO(data), names=names, na_values=na_values) tm.assert_frame_equal(result, expected) tm.assert_dict_equal(na_values, na_values_copy) def test_na_values_dict_col_index(all_parsers): # see gh-14203 data = "a\nfoo\n1" parser = all_parsers na_values = {0: "foo"} result = parser.read_csv(StringIO(data), na_values=na_values) expected = DataFrame({"a": [np.nan, 1]}) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "data,kwargs,expected", [ ( str(2 ** 63) + "\n" + str(2 ** 63 + 1), dict(na_values=[2 ** 63]), DataFrame([str(2 ** 63), str(2 ** 63 + 1)]), ), (str(2 ** 63) + ",1" + "\n,2", dict(), DataFrame([[str(2 ** 63), 1], ["", 2]])), (str(2 ** 63) + "\n1", dict(na_values=[2 ** 63]), DataFrame([np.nan, 1])), ], ) def test_na_values_uint64(all_parsers, data, kwargs, expected): # see gh-14983 parser = all_parsers result = parser.read_csv(StringIO(data), header=None, **kwargs) tm.assert_frame_equal(result, expected) def test_empty_na_values_no_default_with_index(all_parsers): # see gh-15835 data = "a,1\nb,2" parser = all_parsers expected = DataFrame({"1": [2]}, index=Index(["b"], name="a")) result = parser.read_csv(StringIO(data), index_col=0, keep_default_na=False) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "na_filter,index_data", [(False, ["", "5"]), (True, [np.nan, 5.0])] ) def test_no_na_filter_on_index(all_parsers, na_filter, index_data): # see gh-5239 # # Don't parse NA-values in index unless na_filter=True parser = all_parsers data = "a,b,c\n1,,3\n4,5,6" expected = DataFrame({"a": [1, 4], "c": [3, 6]}, index=Index(index_data, name="b")) result = parser.read_csv(StringIO(data), index_col=[1], na_filter=na_filter) tm.assert_frame_equal(result, expected) def test_inf_na_values_with_int_index(all_parsers): # see gh-17128 parser = all_parsers data = "idx,col1,col2\n1,3,4\n2,inf,-inf" # Don't fail with OverflowError with inf's and integer index column. out = parser.read_csv(StringIO(data), index_col=[0], na_values=["inf", "-inf"]) expected = DataFrame( {"col1": [3, np.nan], "col2": [4, np.nan]}, index=Index([1, 2], name="idx") ) tm.assert_frame_equal(out, expected) @pytest.mark.parametrize("na_filter", [True, False]) def test_na_values_with_dtype_str_and_na_filter(all_parsers, na_filter): # see gh-20377 parser = all_parsers data = "a,b,c\n1,,3\n4,5,6" # na_filter=True --> missing value becomes NaN. # na_filter=False --> missing value remains empty string. empty = np.nan if na_filter else "" expected = DataFrame({"a": ["1", "4"], "b": [empty, "5"], "c": ["3", "6"]}) result = parser.read_csv(StringIO(data), na_filter=na_filter, dtype=str) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "data, na_values", [ ("false,1\n,1\ntrue", None), ("false,1\nnull,1\ntrue", None), ("false,1\nnan,1\ntrue", None), ("false,1\nfoo,1\ntrue", "foo"), ("false,1\nfoo,1\ntrue", ["foo"]), ("false,1\nfoo,1\ntrue", {"a": "foo"}), ], ) def test_cast_NA_to_bool_raises_error(all_parsers, data, na_values): parser = all_parsers msg = ( "(Bool column has NA values in column [0a])|" "(cannot safely convert passed user dtype of " "bool for object dtyped data in column 0)" ) with pytest.raises(ValueError, match=msg): parser.read_csv( StringIO(data), header=None, names=["a", "b"], dtype={"a": "bool"}, na_values=na_values, ) def test_str_nan_dropped(all_parsers): # see gh-21131 parser = all_parsers data = """File: small.csv,, 10010010233,0123,654 foo,,bar 01001000155,4530,898""" result = parser.read_csv( StringIO(data), header=None, names=["col1", "col2", "col3"], dtype={"col1": str, "col2": str, "col3": str}, ).dropna() expected = DataFrame( { "col1": ["10010010233", "01001000155"], "col2": ["0123", "4530"], "col3": ["654", "898"], }, index=[1, 3], ) tm.assert_frame_equal(result, expected)
bsd-3-clause
kazemakase/scikit-learn
sklearn/feature_extraction/text.py
24
50103
# -*- coding: utf-8 -*- # Authors: Olivier Grisel <olivier.grisel@ensta.org> # Mathieu Blondel <mathieu@mblondel.org> # Lars Buitinck <L.J.Buitinck@uva.nl> # Robert Layton <robertlayton@gmail.com> # Jochen Wersdörfer <jochen@wersdoerfer.de> # Roman Sinayev <roman.sinayev@gmail.com> # # License: BSD 3 clause """ The :mod:`sklearn.feature_extraction.text` submodule gathers utilities to build feature vectors from text documents. """ from __future__ import unicode_literals import array from collections import Mapping, defaultdict import numbers from operator import itemgetter import re import unicodedata import numpy as np import scipy.sparse as sp from ..base import BaseEstimator, TransformerMixin from ..externals import six from ..externals.six.moves import xrange from ..preprocessing import normalize from .hashing import FeatureHasher from .stop_words import ENGLISH_STOP_WORDS from ..utils import deprecated from ..utils.fixes import frombuffer_empty, bincount from ..utils.validation import check_is_fitted __all__ = ['CountVectorizer', 'ENGLISH_STOP_WORDS', 'TfidfTransformer', 'TfidfVectorizer', 'strip_accents_ascii', 'strip_accents_unicode', 'strip_tags'] def strip_accents_unicode(s): """Transform accentuated unicode symbols into their simple counterpart Warning: the python-level loop and join operations make this implementation 20 times slower than the strip_accents_ascii basic normalization. See also -------- strip_accents_ascii Remove accentuated char for any unicode symbol that has a direct ASCII equivalent. """ return ''.join([c for c in unicodedata.normalize('NFKD', s) if not unicodedata.combining(c)]) def strip_accents_ascii(s): """Transform accentuated unicode symbols into ascii or nothing Warning: this solution is only suited for languages that have a direct transliteration to ASCII symbols. See also -------- strip_accents_unicode Remove accentuated char for any unicode symbol. """ nkfd_form = unicodedata.normalize('NFKD', s) return nkfd_form.encode('ASCII', 'ignore').decode('ASCII') def strip_tags(s): """Basic regexp based HTML / XML tag stripper function For serious HTML/XML preprocessing you should rather use an external library such as lxml or BeautifulSoup. """ return re.compile(r"<([^>]+)>", flags=re.UNICODE).sub(" ", s) def _check_stop_list(stop): if stop == "english": return ENGLISH_STOP_WORDS elif isinstance(stop, six.string_types): raise ValueError("not a built-in stop list: %s" % stop) else: # assume it's a collection return stop class VectorizerMixin(object): """Provides common code for text vectorizers (tokenization logic).""" _white_spaces = re.compile(r"\s\s+") def decode(self, doc): """Decode the input into a string of unicode symbols The decoding strategy depends on the vectorizer parameters. """ if self.input == 'filename': with open(doc, 'rb') as fh: doc = fh.read() elif self.input == 'file': doc = doc.read() if isinstance(doc, bytes): doc = doc.decode(self.encoding, self.decode_error) if doc is np.nan: raise ValueError("np.nan is an invalid document, expected byte or " "unicode string.") return doc def _word_ngrams(self, tokens, stop_words=None): """Turn tokens into a sequence of n-grams after stop words filtering""" # handle stop words if stop_words is not None: tokens = [w for w in tokens if w not in stop_words] # handle token n-grams min_n, max_n = self.ngram_range if max_n != 1: original_tokens = tokens tokens = [] n_original_tokens = len(original_tokens) for n in xrange(min_n, min(max_n + 1, n_original_tokens + 1)): for i in xrange(n_original_tokens - n + 1): tokens.append(" ".join(original_tokens[i: i + n])) return tokens def _char_ngrams(self, text_document): """Tokenize text_document into a sequence of character n-grams""" # normalize white spaces text_document = self._white_spaces.sub(" ", text_document) text_len = len(text_document) ngrams = [] min_n, max_n = self.ngram_range for n in xrange(min_n, min(max_n + 1, text_len + 1)): for i in xrange(text_len - n + 1): ngrams.append(text_document[i: i + n]) return ngrams def _char_wb_ngrams(self, text_document): """Whitespace sensitive char-n-gram tokenization. Tokenize text_document into a sequence of character n-grams excluding any whitespace (operating only inside word boundaries)""" # normalize white spaces text_document = self._white_spaces.sub(" ", text_document) min_n, max_n = self.ngram_range ngrams = [] for w in text_document.split(): w = ' ' + w + ' ' w_len = len(w) for n in xrange(min_n, max_n + 1): offset = 0 ngrams.append(w[offset:offset + n]) while offset + n < w_len: offset += 1 ngrams.append(w[offset:offset + n]) if offset == 0: # count a short word (w_len < n) only once break return ngrams def build_preprocessor(self): """Return a function to preprocess the text before tokenization""" if self.preprocessor is not None: return self.preprocessor # unfortunately python functools package does not have an efficient # `compose` function that would have allowed us to chain a dynamic # number of functions. However the cost of a lambda call is a few # hundreds of nanoseconds which is negligible when compared to the # cost of tokenizing a string of 1000 chars for instance. noop = lambda x: x # accent stripping if not self.strip_accents: strip_accents = noop elif callable(self.strip_accents): strip_accents = self.strip_accents elif self.strip_accents == 'ascii': strip_accents = strip_accents_ascii elif self.strip_accents == 'unicode': strip_accents = strip_accents_unicode else: raise ValueError('Invalid value for "strip_accents": %s' % self.strip_accents) if self.lowercase: return lambda x: strip_accents(x.lower()) else: return strip_accents def build_tokenizer(self): """Return a function that splits a string into a sequence of tokens""" if self.tokenizer is not None: return self.tokenizer token_pattern = re.compile(self.token_pattern) return lambda doc: token_pattern.findall(doc) def get_stop_words(self): """Build or fetch the effective stop words list""" return _check_stop_list(self.stop_words) def build_analyzer(self): """Return a callable that handles preprocessing and tokenization""" if callable(self.analyzer): return self.analyzer preprocess = self.build_preprocessor() if self.analyzer == 'char': return lambda doc: self._char_ngrams(preprocess(self.decode(doc))) elif self.analyzer == 'char_wb': return lambda doc: self._char_wb_ngrams( preprocess(self.decode(doc))) elif self.analyzer == 'word': stop_words = self.get_stop_words() tokenize = self.build_tokenizer() return lambda doc: self._word_ngrams( tokenize(preprocess(self.decode(doc))), stop_words) else: raise ValueError('%s is not a valid tokenization scheme/analyzer' % self.analyzer) def _validate_vocabulary(self): vocabulary = self.vocabulary if vocabulary is not None: if not isinstance(vocabulary, Mapping): vocab = {} for i, t in enumerate(vocabulary): if vocab.setdefault(t, i) != i: msg = "Duplicate term in vocabulary: %r" % t raise ValueError(msg) vocabulary = vocab else: indices = set(six.itervalues(vocabulary)) if len(indices) != len(vocabulary): raise ValueError("Vocabulary contains repeated indices.") for i in xrange(len(vocabulary)): if i not in indices: msg = ("Vocabulary of size %d doesn't contain index " "%d." % (len(vocabulary), i)) raise ValueError(msg) if not vocabulary: raise ValueError("empty vocabulary passed to fit") self.fixed_vocabulary_ = True self.vocabulary_ = dict(vocabulary) else: self.fixed_vocabulary_ = False def _check_vocabulary(self): """Check if vocabulary is empty or missing (not fit-ed)""" msg = "%(name)s - Vocabulary wasn't fitted." check_is_fitted(self, 'vocabulary_', msg=msg), if len(self.vocabulary_) == 0: raise ValueError("Vocabulary is empty") @property @deprecated("The `fixed_vocabulary` attribute is deprecated and will be " "removed in 0.18. Please use `fixed_vocabulary_` instead.") def fixed_vocabulary(self): return self.fixed_vocabulary_ class HashingVectorizer(BaseEstimator, VectorizerMixin): """Convert a collection of text documents to a matrix of token occurrences It turns a collection of text documents into a scipy.sparse matrix holding token occurrence counts (or binary occurrence information), possibly normalized as token frequencies if norm='l1' or projected on the euclidean unit sphere if norm='l2'. This text vectorizer implementation uses the hashing trick to find the token string name to feature integer index mapping. This strategy has several advantages: - it is very low memory scalable to large datasets as there is no need to store a vocabulary dictionary in memory - it is fast to pickle and un-pickle as it holds no state besides the constructor parameters - it can be used in a streaming (partial fit) or parallel pipeline as there is no state computed during fit. There are also a couple of cons (vs using a CountVectorizer with an in-memory vocabulary): - there is no way to compute the inverse transform (from feature indices to string feature names) which can be a problem when trying to introspect which features are most important to a model. - there can be collisions: distinct tokens can be mapped to the same feature index. However in practice this is rarely an issue if n_features is large enough (e.g. 2 ** 18 for text classification problems). - no IDF weighting as this would render the transformer stateful. The hash function employed is the signed 32-bit version of Murmurhash3. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- input : string {'filename', 'file', 'content'} If 'filename', the sequence passed as an argument to fit is expected to be a list of filenames that need reading to fetch the raw content to analyze. If 'file', the sequence items must have a 'read' method (file-like object) that is called to fetch the bytes in memory. Otherwise the input is expected to be the sequence strings or bytes items are expected to be analyzed directly. encoding : string, default='utf-8' If bytes or files are given to analyze, this encoding is used to decode. decode_error : {'strict', 'ignore', 'replace'} Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given `encoding`. By default, it is 'strict', meaning that a UnicodeDecodeError will be raised. Other values are 'ignore' and 'replace'. strip_accents : {'ascii', 'unicode', None} Remove accents during the preprocessing step. 'ascii' is a fast method that only works on characters that have an direct ASCII mapping. 'unicode' is a slightly slower method that works on any characters. None (default) does nothing. analyzer : string, {'word', 'char', 'char_wb'} or callable Whether the feature should be made of word or character n-grams. Option 'char_wb' creates character n-grams only from text inside word boundaries. If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input. preprocessor : callable or None (default) Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. tokenizer : callable or None (default) Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if ``analyzer == 'word'``. ngram_range : tuple (min_n, max_n), default=(1, 1) The lower and upper boundary of the range of n-values for different n-grams to be extracted. All values of n such that min_n <= n <= max_n will be used. stop_words : string {'english'}, list, or None (default) If 'english', a built-in stop word list for English is used. If a list, that list is assumed to contain stop words, all of which will be removed from the resulting tokens. Only applies if ``analyzer == 'word'``. lowercase : boolean, default=True Convert all characters to lowercase before tokenizing. token_pattern : string Regular expression denoting what constitutes a "token", only used if ``analyzer == 'word'``. The default regexp selects tokens of 2 or more alphanumeric characters (punctuation is completely ignored and always treated as a token separator). n_features : integer, default=(2 ** 20) The number of features (columns) in the output matrices. Small numbers of features are likely to cause hash collisions, but large numbers will cause larger coefficient dimensions in linear learners. norm : 'l1', 'l2' or None, optional Norm used to normalize term vectors. None for no normalization. binary: boolean, default=False. If True, all non zero counts are set to 1. This is useful for discrete probabilistic models that model binary events rather than integer counts. dtype: type, optional Type of the matrix returned by fit_transform() or transform(). non_negative : boolean, default=False Whether output matrices should contain non-negative values only; effectively calls abs on the matrix prior to returning it. When True, output values can be interpreted as frequencies. When False, output values will have expected value zero. See also -------- CountVectorizer, TfidfVectorizer """ def __init__(self, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, stop_words=None, token_pattern=r"(?u)\b\w\w+\b", ngram_range=(1, 1), analyzer='word', n_features=(2 ** 20), binary=False, norm='l2', non_negative=False, dtype=np.float64): self.input = input self.encoding = encoding self.decode_error = decode_error self.strip_accents = strip_accents self.preprocessor = preprocessor self.tokenizer = tokenizer self.analyzer = analyzer self.lowercase = lowercase self.token_pattern = token_pattern self.stop_words = stop_words self.n_features = n_features self.ngram_range = ngram_range self.binary = binary self.norm = norm self.non_negative = non_negative self.dtype = dtype def partial_fit(self, X, y=None): """Does nothing: this transformer is stateless. This method is just there to mark the fact that this transformer can work in a streaming setup. """ return self def fit(self, X, y=None): """Does nothing: this transformer is stateless.""" # triggers a parameter validation self._get_hasher().fit(X, y=y) return self def transform(self, X, y=None): """Transform a sequence of documents to a document-term matrix. Parameters ---------- X : iterable over raw text documents, length = n_samples Samples. Each sample must be a text document (either bytes or unicode strings, file name or file object depending on the constructor argument) which will be tokenized and hashed. y : (ignored) Returns ------- X : scipy.sparse matrix, shape = (n_samples, self.n_features) Document-term matrix. """ analyzer = self.build_analyzer() X = self._get_hasher().transform(analyzer(doc) for doc in X) if self.binary: X.data.fill(1) if self.norm is not None: X = normalize(X, norm=self.norm, copy=False) return X # Alias transform to fit_transform for convenience fit_transform = transform def _get_hasher(self): return FeatureHasher(n_features=self.n_features, input_type='string', dtype=self.dtype, non_negative=self.non_negative) def _document_frequency(X): """Count the number of non-zero values for each feature in sparse X.""" if sp.isspmatrix_csr(X): return bincount(X.indices, minlength=X.shape[1]) else: return np.diff(sp.csc_matrix(X, copy=False).indptr) class CountVectorizer(BaseEstimator, VectorizerMixin): """Convert a collection of text documents to a matrix of token counts This implementation produces a sparse representation of the counts using scipy.sparse.coo_matrix. If you do not provide an a-priori dictionary and you do not use an analyzer that does some kind of feature selection then the number of features will be equal to the vocabulary size found by analyzing the data. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- input : string {'filename', 'file', 'content'} If 'filename', the sequence passed as an argument to fit is expected to be a list of filenames that need reading to fetch the raw content to analyze. If 'file', the sequence items must have a 'read' method (file-like object) that is called to fetch the bytes in memory. Otherwise the input is expected to be the sequence strings or bytes items are expected to be analyzed directly. encoding : string, 'utf-8' by default. If bytes or files are given to analyze, this encoding is used to decode. decode_error : {'strict', 'ignore', 'replace'} Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given `encoding`. By default, it is 'strict', meaning that a UnicodeDecodeError will be raised. Other values are 'ignore' and 'replace'. strip_accents : {'ascii', 'unicode', None} Remove accents during the preprocessing step. 'ascii' is a fast method that only works on characters that have an direct ASCII mapping. 'unicode' is a slightly slower method that works on any characters. None (default) does nothing. analyzer : string, {'word', 'char', 'char_wb'} or callable Whether the feature should be made of word or character n-grams. Option 'char_wb' creates character n-grams only from text inside word boundaries. If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input. Only applies if ``analyzer == 'word'``. preprocessor : callable or None (default) Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. tokenizer : callable or None (default) Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if ``analyzer == 'word'``. ngram_range : tuple (min_n, max_n) The lower and upper boundary of the range of n-values for different n-grams to be extracted. All values of n such that min_n <= n <= max_n will be used. stop_words : string {'english'}, list, or None (default) If 'english', a built-in stop word list for English is used. If a list, that list is assumed to contain stop words, all of which will be removed from the resulting tokens. Only applies if ``analyzer == 'word'``. If None, no stop words will be used. max_df can be set to a value in the range [0.7, 1.0) to automatically detect and filter stop words based on intra corpus document frequency of terms. lowercase : boolean, True by default Convert all characters to lowercase before tokenizing. token_pattern : string Regular expression denoting what constitutes a "token", only used if ``analyzer == 'word'``. The default regexp select tokens of 2 or more alphanumeric characters (punctuation is completely ignored and always treated as a token separator). max_df : float in range [0.0, 1.0] or int, default=1.0 When building the vocabulary ignore terms that have a document frequency strictly higher than the given threshold (corpus-specific stop words). If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. min_df : float in range [0.0, 1.0] or int, default=1 When building the vocabulary ignore terms that have a document frequency strictly lower than the given threshold. This value is also called cut-off in the literature. If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. max_features : int or None, default=None If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus. This parameter is ignored if vocabulary is not None. vocabulary : Mapping or iterable, optional Either a Mapping (e.g., a dict) where keys are terms and values are indices in the feature matrix, or an iterable over terms. If not given, a vocabulary is determined from the input documents. Indices in the mapping should not be repeated and should not have any gap between 0 and the largest index. binary : boolean, default=False If True, all non zero counts are set to 1. This is useful for discrete probabilistic models that model binary events rather than integer counts. dtype : type, optional Type of the matrix returned by fit_transform() or transform(). Attributes ---------- vocabulary_ : dict A mapping of terms to feature indices. stop_words_ : set Terms that were ignored because they either: - occurred in too many documents (`max_df`) - occurred in too few documents (`min_df`) - were cut off by feature selection (`max_features`). This is only available if no vocabulary was given. See also -------- HashingVectorizer, TfidfVectorizer Notes ----- The ``stop_words_`` attribute can get large and increase the model size when pickling. This attribute is provided only for introspection and can be safely removed using delattr or set to None before pickling. """ def __init__(self, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, stop_words=None, token_pattern=r"(?u)\b\w\w+\b", ngram_range=(1, 1), analyzer='word', max_df=1.0, min_df=1, max_features=None, vocabulary=None, binary=False, dtype=np.int64): self.input = input self.encoding = encoding self.decode_error = decode_error self.strip_accents = strip_accents self.preprocessor = preprocessor self.tokenizer = tokenizer self.analyzer = analyzer self.lowercase = lowercase self.token_pattern = token_pattern self.stop_words = stop_words self.max_df = max_df self.min_df = min_df if max_df < 0 or min_df < 0: raise ValueError("negative value for max_df of min_df") self.max_features = max_features if max_features is not None: if (not isinstance(max_features, numbers.Integral) or max_features <= 0): raise ValueError( "max_features=%r, neither a positive integer nor None" % max_features) self.ngram_range = ngram_range self.vocabulary = vocabulary self.binary = binary self.dtype = dtype def _sort_features(self, X, vocabulary): """Sort features by name Returns a reordered matrix and modifies the vocabulary in place """ sorted_features = sorted(six.iteritems(vocabulary)) map_index = np.empty(len(sorted_features), dtype=np.int32) for new_val, (term, old_val) in enumerate(sorted_features): map_index[new_val] = old_val vocabulary[term] = new_val return X[:, map_index] def _limit_features(self, X, vocabulary, high=None, low=None, limit=None): """Remove too rare or too common features. Prune features that are non zero in more samples than high or less documents than low, modifying the vocabulary, and restricting it to at most the limit most frequent. This does not prune samples with zero features. """ if high is None and low is None and limit is None: return X, set() # Calculate a mask based on document frequencies dfs = _document_frequency(X) tfs = np.asarray(X.sum(axis=0)).ravel() mask = np.ones(len(dfs), dtype=bool) if high is not None: mask &= dfs <= high if low is not None: mask &= dfs >= low if limit is not None and mask.sum() > limit: mask_inds = (-tfs[mask]).argsort()[:limit] new_mask = np.zeros(len(dfs), dtype=bool) new_mask[np.where(mask)[0][mask_inds]] = True mask = new_mask new_indices = np.cumsum(mask) - 1 # maps old indices to new removed_terms = set() for term, old_index in list(six.iteritems(vocabulary)): if mask[old_index]: vocabulary[term] = new_indices[old_index] else: del vocabulary[term] removed_terms.add(term) kept_indices = np.where(mask)[0] if len(kept_indices) == 0: raise ValueError("After pruning, no terms remain. Try a lower" " min_df or a higher max_df.") return X[:, kept_indices], removed_terms def _count_vocab(self, raw_documents, fixed_vocab): """Create sparse feature matrix, and vocabulary where fixed_vocab=False """ if fixed_vocab: vocabulary = self.vocabulary_ else: # Add a new value when a new vocabulary item is seen vocabulary = defaultdict() vocabulary.default_factory = vocabulary.__len__ analyze = self.build_analyzer() j_indices = _make_int_array() indptr = _make_int_array() indptr.append(0) for doc in raw_documents: for feature in analyze(doc): try: j_indices.append(vocabulary[feature]) except KeyError: # Ignore out-of-vocabulary items for fixed_vocab=True continue indptr.append(len(j_indices)) if not fixed_vocab: # disable defaultdict behaviour vocabulary = dict(vocabulary) if not vocabulary: raise ValueError("empty vocabulary; perhaps the documents only" " contain stop words") j_indices = frombuffer_empty(j_indices, dtype=np.intc) indptr = np.frombuffer(indptr, dtype=np.intc) values = np.ones(len(j_indices)) X = sp.csr_matrix((values, j_indices, indptr), shape=(len(indptr) - 1, len(vocabulary)), dtype=self.dtype) X.sum_duplicates() return vocabulary, X def fit(self, raw_documents, y=None): """Learn a vocabulary dictionary of all tokens in the raw documents. Parameters ---------- raw_documents : iterable An iterable which yields either str, unicode or file objects. Returns ------- self """ self.fit_transform(raw_documents) return self def fit_transform(self, raw_documents, y=None): """Learn the vocabulary dictionary and return term-document matrix. This is equivalent to fit followed by transform, but more efficiently implemented. Parameters ---------- raw_documents : iterable An iterable which yields either str, unicode or file objects. Returns ------- X : array, [n_samples, n_features] Document-term matrix. """ # We intentionally don't call the transform method to make # fit_transform overridable without unwanted side effects in # TfidfVectorizer. self._validate_vocabulary() max_df = self.max_df min_df = self.min_df max_features = self.max_features vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary_) if self.binary: X.data.fill(1) if not self.fixed_vocabulary_: X = self._sort_features(X, vocabulary) n_doc = X.shape[0] max_doc_count = (max_df if isinstance(max_df, numbers.Integral) else max_df * n_doc) min_doc_count = (min_df if isinstance(min_df, numbers.Integral) else min_df * n_doc) if max_doc_count < min_doc_count: raise ValueError( "max_df corresponds to < documents than min_df") X, self.stop_words_ = self._limit_features(X, vocabulary, max_doc_count, min_doc_count, max_features) self.vocabulary_ = vocabulary return X def transform(self, raw_documents): """Transform documents to document-term matrix. Extract token counts out of raw text documents using the vocabulary fitted with fit or the one provided to the constructor. Parameters ---------- raw_documents : iterable An iterable which yields either str, unicode or file objects. Returns ------- X : sparse matrix, [n_samples, n_features] Document-term matrix. """ if not hasattr(self, 'vocabulary_'): self._validate_vocabulary() self._check_vocabulary() # use the same matrix-building strategy as fit_transform _, X = self._count_vocab(raw_documents, fixed_vocab=True) if self.binary: X.data.fill(1) return X def inverse_transform(self, X): """Return terms per document with nonzero entries in X. Parameters ---------- X : {array, sparse matrix}, shape = [n_samples, n_features] Returns ------- X_inv : list of arrays, len = n_samples List of arrays of terms. """ self._check_vocabulary() if sp.issparse(X): # We need CSR format for fast row manipulations. X = X.tocsr() else: # We need to convert X to a matrix, so that the indexing # returns 2D objects X = np.asmatrix(X) n_samples = X.shape[0] terms = np.array(list(self.vocabulary_.keys())) indices = np.array(list(self.vocabulary_.values())) inverse_vocabulary = terms[np.argsort(indices)] return [inverse_vocabulary[X[i, :].nonzero()[1]].ravel() for i in range(n_samples)] def get_feature_names(self): """Array mapping from feature integer indices to feature name""" self._check_vocabulary() return [t for t, i in sorted(six.iteritems(self.vocabulary_), key=itemgetter(1))] def _make_int_array(): """Construct an array.array of a type suitable for scipy.sparse indices.""" return array.array(str("i")) class TfidfTransformer(BaseEstimator, TransformerMixin): """Transform a count matrix to a normalized tf or tf-idf representation Tf means term-frequency while tf-idf means term-frequency times inverse document-frequency. This is a common term weighting scheme in information retrieval, that has also found good use in document classification. The goal of using tf-idf instead of the raw frequencies of occurrence of a token in a given document is to scale down the impact of tokens that occur very frequently in a given corpus and that are hence empirically less informative than features that occur in a small fraction of the training corpus. The actual formula used for tf-idf is tf * (idf + 1) = tf + tf * idf, instead of tf * idf. The effect of this is that terms with zero idf, i.e. that occur in all documents of a training set, will not be entirely ignored. The formulas used to compute tf and idf depend on parameter settings that correspond to the SMART notation used in IR, as follows: Tf is "n" (natural) by default, "l" (logarithmic) when sublinear_tf=True. Idf is "t" when use_idf is given, "n" (none) otherwise. Normalization is "c" (cosine) when norm='l2', "n" (none) when norm=None. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- norm : 'l1', 'l2' or None, optional Norm used to normalize term vectors. None for no normalization. use_idf : boolean, default=True Enable inverse-document-frequency reweighting. smooth_idf : boolean, default=True Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions. sublinear_tf : boolean, default=False Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf). References ---------- .. [Yates2011] `R. Baeza-Yates and B. Ribeiro-Neto (2011). Modern Information Retrieval. Addison Wesley, pp. 68-74.` .. [MRS2008] `C.D. Manning, P. Raghavan and H. Schuetze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 118-120.` """ def __init__(self, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False): self.norm = norm self.use_idf = use_idf self.smooth_idf = smooth_idf self.sublinear_tf = sublinear_tf def fit(self, X, y=None): """Learn the idf vector (global term weights) Parameters ---------- X : sparse matrix, [n_samples, n_features] a matrix of term/token counts """ if not sp.issparse(X): X = sp.csc_matrix(X) if self.use_idf: n_samples, n_features = X.shape df = _document_frequency(X) # perform idf smoothing if required df += int(self.smooth_idf) n_samples += int(self.smooth_idf) # log+1 instead of log makes sure terms with zero idf don't get # suppressed entirely. idf = np.log(float(n_samples) / df) + 1.0 self._idf_diag = sp.spdiags(idf, diags=0, m=n_features, n=n_features) return self def transform(self, X, copy=True): """Transform a count matrix to a tf or tf-idf representation Parameters ---------- X : sparse matrix, [n_samples, n_features] a matrix of term/token counts copy : boolean, default True Whether to copy X and operate on the copy or perform in-place operations. Returns ------- vectors : sparse matrix, [n_samples, n_features] """ if hasattr(X, 'dtype') and np.issubdtype(X.dtype, np.float): # preserve float family dtype X = sp.csr_matrix(X, copy=copy) else: # convert counts or binary occurrences to floats X = sp.csr_matrix(X, dtype=np.float64, copy=copy) n_samples, n_features = X.shape if self.sublinear_tf: np.log(X.data, X.data) X.data += 1 if self.use_idf: check_is_fitted(self, '_idf_diag', 'idf vector is not fitted') expected_n_features = self._idf_diag.shape[0] if n_features != expected_n_features: raise ValueError("Input has n_features=%d while the model" " has been trained with n_features=%d" % ( n_features, expected_n_features)) # *= doesn't work X = X * self._idf_diag if self.norm: X = normalize(X, norm=self.norm, copy=False) return X @property def idf_(self): if hasattr(self, "_idf_diag"): return np.ravel(self._idf_diag.sum(axis=0)) else: return None class TfidfVectorizer(CountVectorizer): """Convert a collection of raw documents to a matrix of TF-IDF features. Equivalent to CountVectorizer followed by TfidfTransformer. Read more in the :ref:`User Guide <text_feature_extraction>`. Parameters ---------- input : string {'filename', 'file', 'content'} If 'filename', the sequence passed as an argument to fit is expected to be a list of filenames that need reading to fetch the raw content to analyze. If 'file', the sequence items must have a 'read' method (file-like object) that is called to fetch the bytes in memory. Otherwise the input is expected to be the sequence strings or bytes items are expected to be analyzed directly. encoding : string, 'utf-8' by default. If bytes or files are given to analyze, this encoding is used to decode. decode_error : {'strict', 'ignore', 'replace'} Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given `encoding`. By default, it is 'strict', meaning that a UnicodeDecodeError will be raised. Other values are 'ignore' and 'replace'. strip_accents : {'ascii', 'unicode', None} Remove accents during the preprocessing step. 'ascii' is a fast method that only works on characters that have an direct ASCII mapping. 'unicode' is a slightly slower method that works on any characters. None (default) does nothing. analyzer : string, {'word', 'char'} or callable Whether the feature should be made of word or character n-grams. If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input. preprocessor : callable or None (default) Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. tokenizer : callable or None (default) Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if ``analyzer == 'word'``. ngram_range : tuple (min_n, max_n) The lower and upper boundary of the range of n-values for different n-grams to be extracted. All values of n such that min_n <= n <= max_n will be used. stop_words : string {'english'}, list, or None (default) If a string, it is passed to _check_stop_list and the appropriate stop list is returned. 'english' is currently the only supported string value. If a list, that list is assumed to contain stop words, all of which will be removed from the resulting tokens. Only applies if ``analyzer == 'word'``. If None, no stop words will be used. max_df can be set to a value in the range [0.7, 1.0) to automatically detect and filter stop words based on intra corpus document frequency of terms. lowercase : boolean, default True Convert all characters to lowercase before tokenizing. token_pattern : string Regular expression denoting what constitutes a "token", only used if ``analyzer == 'word'``. The default regexp selects tokens of 2 or more alphanumeric characters (punctuation is completely ignored and always treated as a token separator). max_df : float in range [0.0, 1.0] or int, default=1.0 When building the vocabulary ignore terms that have a document frequency strictly higher than the given threshold (corpus-specific stop words). If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. min_df : float in range [0.0, 1.0] or int, default=1 When building the vocabulary ignore terms that have a document frequency strictly lower than the given threshold. This value is also called cut-off in the literature. If float, the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None. max_features : int or None, default=None If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus. This parameter is ignored if vocabulary is not None. vocabulary : Mapping or iterable, optional Either a Mapping (e.g., a dict) where keys are terms and values are indices in the feature matrix, or an iterable over terms. If not given, a vocabulary is determined from the input documents. binary : boolean, default=False If True, all non-zero term counts are set to 1. This does not mean outputs will have only 0/1 values, only that the tf term in tf-idf is binary. (Set idf and normalization to False to get 0/1 outputs.) dtype : type, optional Type of the matrix returned by fit_transform() or transform(). norm : 'l1', 'l2' or None, optional Norm used to normalize term vectors. None for no normalization. use_idf : boolean, default=True Enable inverse-document-frequency reweighting. smooth_idf : boolean, default=True Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions. sublinear_tf : boolean, default=False Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf). Attributes ---------- idf_ : array, shape = [n_features], or None The learned idf vector (global term weights) when ``use_idf`` is set to True, None otherwise. stop_words_ : set Terms that were ignored because they either: - occurred in too many documents (`max_df`) - occurred in too few documents (`min_df`) - were cut off by feature selection (`max_features`). This is only available if no vocabulary was given. See also -------- CountVectorizer Tokenize the documents and count the occurrences of token and return them as a sparse matrix TfidfTransformer Apply Term Frequency Inverse Document Frequency normalization to a sparse matrix of occurrence counts. Notes ----- The ``stop_words_`` attribute can get large and increase the model size when pickling. This attribute is provided only for introspection and can be safely removed using delattr or set to None before pickling. """ def __init__(self, input='content', encoding='utf-8', decode_error='strict', strip_accents=None, lowercase=True, preprocessor=None, tokenizer=None, analyzer='word', stop_words=None, token_pattern=r"(?u)\b\w\w+\b", ngram_range=(1, 1), max_df=1.0, min_df=1, max_features=None, vocabulary=None, binary=False, dtype=np.int64, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False): super(TfidfVectorizer, self).__init__( input=input, encoding=encoding, decode_error=decode_error, strip_accents=strip_accents, lowercase=lowercase, preprocessor=preprocessor, tokenizer=tokenizer, analyzer=analyzer, stop_words=stop_words, token_pattern=token_pattern, ngram_range=ngram_range, max_df=max_df, min_df=min_df, max_features=max_features, vocabulary=vocabulary, binary=binary, dtype=dtype) self._tfidf = TfidfTransformer(norm=norm, use_idf=use_idf, smooth_idf=smooth_idf, sublinear_tf=sublinear_tf) # Broadcast the TF-IDF parameters to the underlying transformer instance # for easy grid search and repr @property def norm(self): return self._tfidf.norm @norm.setter def norm(self, value): self._tfidf.norm = value @property def use_idf(self): return self._tfidf.use_idf @use_idf.setter def use_idf(self, value): self._tfidf.use_idf = value @property def smooth_idf(self): return self._tfidf.smooth_idf @smooth_idf.setter def smooth_idf(self, value): self._tfidf.smooth_idf = value @property def sublinear_tf(self): return self._tfidf.sublinear_tf @sublinear_tf.setter def sublinear_tf(self, value): self._tfidf.sublinear_tf = value @property def idf_(self): return self._tfidf.idf_ def fit(self, raw_documents, y=None): """Learn vocabulary and idf from training set. Parameters ---------- raw_documents : iterable an iterable which yields either str, unicode or file objects Returns ------- self : TfidfVectorizer """ X = super(TfidfVectorizer, self).fit_transform(raw_documents) self._tfidf.fit(X) return self def fit_transform(self, raw_documents, y=None): """Learn vocabulary and idf, return term-document matrix. This is equivalent to fit followed by transform, but more efficiently implemented. Parameters ---------- raw_documents : iterable an iterable which yields either str, unicode or file objects Returns ------- X : sparse matrix, [n_samples, n_features] Tf-idf-weighted document-term matrix. """ X = super(TfidfVectorizer, self).fit_transform(raw_documents) self._tfidf.fit(X) # X is already a transformed view of raw_documents so # we set copy to False return self._tfidf.transform(X, copy=False) def transform(self, raw_documents, copy=True): """Transform documents to document-term matrix. Uses the vocabulary and document frequencies (df) learned by fit (or fit_transform). Parameters ---------- raw_documents : iterable an iterable which yields either str, unicode or file objects copy : boolean, default True Whether to copy X and operate on the copy or perform in-place operations. Returns ------- X : sparse matrix, [n_samples, n_features] Tf-idf-weighted document-term matrix. """ check_is_fitted(self, '_tfidf', 'The tfidf vector is not fitted') X = super(TfidfVectorizer, self).transform(raw_documents) return self._tfidf.transform(X, copy=False)
bsd-3-clause
rjenc29/numerical
course/matplotlib/examples/fill_example.py
1
2229
""" Illustrate different ways of using the various fill functions. """ import numpy as np import matplotlib.pyplot as plt import example_utils def main(): fig, axes = example_utils.setup_axes() fill_example(axes[0]) fill_between_example(axes[1]) stackplot_example(axes[2]) example_utils.title(fig, 'fill/fill_between/stackplot: Filled polygons', y=0.95) fig.savefig('fill_example.png', facecolor='none') plt.show() def fill_example(ax): # Use fill when you want a simple filled polygon between vertices x, y = fill_data() ax.fill(x, y, color='lightblue') ax.margins(0.1) example_utils.label(ax, 'fill') def fill_between_example(ax): # Fill between fills between two curves or a curve and a constant value # It can be used in several ways. We'll illustrate a few below. x, y1, y2 = sin_data() # The most basic (and common) use of fill_between err = np.random.rand(x.size)**2 + 0.1 y = 0.7 * x + 2 ax.fill_between(x, y + err, y - err, color='orange') # Filling between two curves with different colors when they cross in # different directions ax.fill_between(x, y1, y2, where=y1>y2, color='lightblue') ax.fill_between(x, y1, y2, where=y1<y2, color='forestgreen') # Note that this is fillbetween*x*! ax.fill_betweenx(x, -y1, where=y1>0, color='red', alpha=0.5) ax.fill_betweenx(x, -y1, where=y1<0, color='blue', alpha=0.5) ax.margins(0.15) example_utils.label(ax, 'fill_between/x') def stackplot_example(ax): # Stackplot is equivalent to a series of ax.fill_between calls x, y = stackplot_data() ax.stackplot(x, y.cumsum(axis=0), alpha=0.5) example_utils.label(ax, 'stackplot') #-- Data generation ---------------------- def stackplot_data(): x = np.linspace(0, 10, 100) y = np.random.normal(0, 1, (5, 100)) y = y.cumsum(axis=1) y -= y.min(axis=0, keepdims=True) return x, y def sin_data(): x = np.linspace(0, 10, 100) y = np.sin(x) y2 = np.cos(x) return x, y, y2 def fill_data(): t = np.linspace(0, 2*np.pi, 100) r = np.random.normal(0, 1, 100).cumsum() r -= r.min() return r * np.cos(t), r * np.sin(t) main()
mit
tjhunter/phd-thesis-tjhunter
python/kdd/plot_network.py
1
1065
__author__ = 'tjhunter' import build import json import pylab as pl from matplotlib.collections import LineCollection # Draws the network as a pdf and SVG file. def draw_network(ax, fd, link_style): def decode_line(l): #print l dct = json.loads(l) lats = dct['lats'] lons = dct['lons'] return zip(lons, lats) lines = [decode_line(l) for l in fd] #print lines xmin = min([x for l in lines for x,y in l]) xmax = max([x for l in lines for x,y in l]) ymin = min([y for l in lines for x,y in l]) ymax = max([y for l in lines for x,y in l]) lc = LineCollection(lines, **link_style) ax.add_collection(lc, autolim=True) return ((xmin,xmax),(ymin,ymax)) fname = build.data_name('kdd/net_export_6.json') fig = pl.figure("fig1",figsize=(10,10)) ax = fig.gca() ax.set_axis_off() style = {'colors':'k','linewidths':0.5} with open(fname) as f: (xlims, ylims) = draw_network(ax, f, style) ax.set_xlim(*xlims) ax.set_ylim(*ylims) # Saving in pdf is a bit slow build.save_figure(fig, 'figures-kdd/network_export_6',save_svg=True)
apache-2.0
kcompher/thunder
thunder/extraction/source.py
6
31847
from numpy import asarray, mean, sqrt, ndarray, amin, amax, concatenate, sum, zeros, maximum, \ argmin, newaxis, ones, delete, NaN, inf, isnan, clip, logical_or, unique, where, all from thunder.utils.serializable import Serializable from thunder.utils.common import checkParams, aslist from thunder.rdds.images import Images from thunder.rdds.series import Series class Source(Serializable, object): """ A single source, represented as a list of coordinates and other optional specifications. A source also has a set of lazily computed attributes useful for representing and comparing its geometry, such as center, bounding box, and bounding polygon. These properties will be computed lazily and made available as attributes when requested. Parameters ---------- coordinates : array-like List of 2D or 3D coordinates, can be a list of lists or array of shape (n,2) or (n,3) values : list or array-like Value (or weight) associated with each coordiante id : int or string Arbitrary specification per source, typically an index or string label Attributes ---------- center : list or array-like The coordinates of the center of the source polygon : list or array-like The coordinates of a polygon bounding the region (a convex hull) bbox : list or array-like Boundaries of the source (with the lowest values for all axes followed by the highest values) area : scalar The area of the region """ from zope.cachedescriptors import property def __init__(self, coordinates, values=None, id=None): self.coordinates = asarray(coordinates) if self.coordinates.ndim == 1 and len(self.coordinates) > 0: self.coordinates = asarray([self.coordinates]) if values is not None: self.values = asarray(values) if self.values.ndim == 0: self.values = asarray([self.values]) if not (len(self.coordinates) == len(self.values)): raise ValueError("Lengths of coordinates %g and values %g do not match" % (len(self.coordinates), len(self.values))) if id is not None: self.id = id @property.Lazy def center(self): """ Find the region center using a mean. """ # TODO Add option to use weights return mean(self.coordinates, axis=0) @property.Lazy def polygon(self): """ Find the bounding polygon as a convex hull """ # TODO Add option for simplification from scipy.spatial import ConvexHull if len(self.coordinates) >= 4: inds = ConvexHull(self.coordinates).vertices return self.coordinates[inds] else: return self.coordinates @property.Lazy def bbox(self): """ Find the bounding box. """ mn = amin(self.coordinates, axis=0) mx = amax(self.coordinates, axis=0) return concatenate((mn, mx)) @property.Lazy def area(self): """ Find the region area. """ return len(self.coordinates) def restore(self, skip=None): """ Remove all lazy properties, will force recomputation """ if skip is None: skip = [] elif isinstance(skip, str): skip = [skip] for prop in LAZY_ATTRIBUTES: if prop in self.__dict__.keys() and prop not in skip: del self.__dict__[prop] return self def distance(self, other, method='euclidean'): """ Distance between the center of this source and another. Parameters ---------- other : Source, or array-like Either another source, or the center coordinates of another source method : str Specify a distance measure to used for spatial distance between source centers. Current options include Euclidean distance ('euclidean') and L1-norm ('l1'). """ from numpy.linalg import norm checkParams(method, ['euclidean', 'l1']) if method == 'l1': order = 1 else: order = 2 if isinstance(other, Source): return norm(self.center - other.center, ord=order) elif isinstance(other, list) or isinstance(other, ndarray): return norm(self.center - asarray(other), ord=order) def overlap(self, other, method='fraction'): """ Compute the overlap between this source and other. Options are a symmetric measure of overlap based on the fraction of intersecting pixels relative to the union ('fraction'), an assymmetric measure of overlap that expresses detected intersecting pixels (relative to this source) using precision and recall rates ('rates'), or a correlation coefficient of the weights within the intersection (not defined for binary weights) ('correlation') Parameters ---------- other : Source The source to compute overlap with. method : str Which estimate of overlap to compute, options are 'fraction' (symmetric) 'rates' (asymmetric) or 'correlation' """ checkParams(method, ['fraction', 'rates', 'correlation']) coordsSelf = aslist(self.coordinates) coordsOther = aslist(other.coordinates) intersection = [a for a in coordsSelf if a in coordsOther] nhit = float(len(intersection)) ntotal = float(len(set([tuple(x) for x in coordsSelf] + [tuple(x) for x in coordsOther]))) if method == 'rates': recall = nhit / len(coordsSelf) precision = nhit / len(coordsOther) return recall, precision if method == 'fraction': return nhit / float(ntotal) if method == 'correlation': from scipy.stats import spearmanr if not (hasattr(self, 'values') and hasattr(other, 'values')): raise ValueError('Sources must have values to compute correlation') else: valuesSelf = aslist(self.values) valuesOther = aslist(other.values) if len(intersection) > 0: left = [v for v, c in zip(valuesSelf, coordsSelf) if c in coordsOther] right = [v for v, c in zip(valuesOther, coordsOther) if c in coordsSelf] rho, _ = spearmanr(left, right) else: rho = 0.0 return rho def merge(self, other): """ Combine this source with other """ self.coordinates = concatenate((self.coordinates, other.coordinates)) if hasattr(self, 'values'): self.values = concatenate((self.values, other.values)) return self def tolist(self): """ Convert array-like attributes to list """ import copy new = copy.copy(self) for prop in ["coordinates", "values", "center", "bbox", "polygon"]: if prop in self.__dict__.keys(): val = new.__getattribute__(prop) if val is not None and not isinstance(val, list): setattr(new, prop, val.tolist()) return new def toarray(self): """ Convert array-like attributes to ndarray """ import copy new = copy.copy(self) for prop in ["coordinates", "values", "center", "bbox", "polygon"]: if prop in self.__dict__.keys(): val = new.__getattribute__(prop) if val is not None and not isinstance(val, ndarray): setattr(new, prop, asarray(val)) return new def crop(self, minBound, maxBound): """ Crop a source by removing coordinates outside bounds. Follows normal slice indexing conventions. Parameters ---------- minBound : tuple Minimum or starting bounds for each axis maxBound : tuple Maximum or ending bounds for each axis """ coords = self.coordinates newid = self.id if hasattr(self, 'id') else None if hasattr(self, 'values') and self.values is not None: values = self.values inside = [(c, v) for c, v in zip(coords, values) if c not in coords] newcoords, newvalues = zip(*inside) return Source(coordinates=newcoords, values=newvalues, id=newid) else: newcoords = [c for c in coords if all(c >= minBound) and all(c < maxBound)] return Source(coordinates=newcoords, id=newid) def dilate(self, size): """ Dilate a source using morphological operators. Parameters ---------- size : int Size of dilation in pixels """ if size == 0: newcoords = self.coordinates else: size = (size * 2) + 1 if hasattr(self, 'values') and self.values is not None: raise AttributeError('Cannot dilate sources with values') from skimage.morphology import binary_dilation coords = self.coordinates extent = self.bbox[len(self.center):] - self.bbox[0:len(self.center)] + 1 + size * 2 m = zeros(extent) coords = (coords - self.bbox[0:len(self.center)] + size) m[coords.T.tolist()] = 1 m = binary_dilation(m, ones((size, size))) newcoords = asarray(where(m)).T + self.bbox[0:len(self.center)] - size newcoords = [c for c in newcoords if all(c >= 0)] newid = self.id if hasattr(self, 'id') else None return Source(coordinates=newcoords, id=newid) def exclude(self, other): """ Remove coordinates derived from another Source or an array. If other is an array, will remove coordinates of all non-zero elements from this source. If other is a source, will remove any matching coordinates. Parameters ---------- other : ndarray or Source Source to remove """ if isinstance(other, ndarray): coordsOther = asarray(where(other)).T else: coordsOther = aslist(other.coordinates) coordsSelf = aslist(self.coordinates) newid = self.id if hasattr(self, 'id') else None if hasattr(self, 'values') and self.values is not None: valuesSelf = self.values complement = [(c, v) for c, v in zip(coordsSelf, valuesSelf) if c not in coordsOther] newcoords, newvalues = zip(*complement) return Source(coordinates=newcoords, values=newvalues, id=newid) else: complement = [a for a in coordsSelf if a not in coordsOther] return Source(coordinates=complement, id=newid) def outline(self, inner, outer): """ Compute source outline by differencing two dilations Parameters ---------- inner : int Size of inner outline boundary (in pixels) outer : int Size of outer outline boundary (in pixels) """ return self.dilate(outer).exclude(self.dilate(inner)) def transform(self, data, collect=True): """ Extract series from data using a list of sources. Currently only supports averaging over coordinates. Params ------ data : Images or Series object The data from which to extract collect : boolean, optional, default = True Whether to collect to local array or keep as a Series """ if not (isinstance(data, Images) or isinstance(data, Series)): raise Exception("Input must either be Images or Series (or a subclass)") # TODO add support for weighting if isinstance(data, Images): output = data.meanByRegions([self.coordinates]).toSeries() else: output = data.meanOfRegion(self.coordinates) if collect: return output.collectValuesAsArray() else: return output def mask(self, dims=None, binary=True, outline=False, color=None): """ Construct a mask from a source, either locally or within a larger image. Parameters ---------- dims : list or tuple, optional, default = None Dimensions of large image in which to draw mask. If none, will restrict to the bounding box of the region. binary : boolean, optional, deafult = True Whether to incoporate values or only show a binary mask outline : boolean, optional, deafult = False Whether to only show outlines (derived using binary dilation) color : str or array-like RGB triplet (from 0 to 1) or named color (e.g. 'red', 'blue') """ from thunder import Colorize coords = self.coordinates if dims is None: extent = self.bbox[len(self.center):] - self.bbox[0:len(self.center)] + 1 m = zeros(extent) coords = (coords - self.bbox[0:len(self.center)]) else: m = zeros(dims) if hasattr(self, 'values') and self.values is not None and binary is False: m[coords.T.tolist()] = self.values else: m[coords.T.tolist()] = 1 if outline: from skimage.morphology import binary_dilation m = binary_dilation(m, ones((3, 3))) - m if color is not None: m = Colorize(cmap='indexed', colors=[color]).transform([m]) return m def inbounds(self, minBound, maxBound): """ Check what fraction of coordinates are inside given bounds Parameters ---------- minBound : list or tuple Minimum bounds maxBounds : list or tuple Maximum bounds """ minCheck = sum(self.coordinates < minBound, axis=1) > 0 maxCheck = sum(self.coordinates > maxBound, axis=1) > 0 fraction = 1 - sum(logical_or(minCheck, maxCheck)) / float(len(self.coordinates)) return fraction @staticmethod def fromMask(mask, id=None): """ Genearte a source from a mask. Assumes that the mask is an image where all non-zero elements are part of the source. If all non-zero elements are 1, then values will be ignored as the source is assumed to be binary. Parameters ---------- mask : array-like An array (typically 2D or 3D) containing the image mask id : int or string Arbitrary identifier for the source, typically an int or string """ mask = asarray(mask) u = unique(mask) if len(u) == 2 and u[0] == 0 and u[1] == 1: inds = where(mask) return Source(coordinates=asarray(zip(*inds)), id=id) else: inds = where(mask) values = mask[inds] coords = asarray(zip(*inds)) return Source(coordinates=coords, values=values, id=id) @staticmethod def fromCoordinates(coordinates, values=None, id=None): """ Generate a source from a list of coordinates and values. Parameters ---------- coordinates : array-like List coordinates as a list of lists or array of shape (n,2) or (n,3) values : list or array-like Value (or weight) associated with each coordiante id : int or string Arbitrary specification per source, typically an index or string label """ return Source(coordinates, values, id) def __repr__(self): s = self.__class__.__name__ for opt in ["id", "center", "bbox"]: if hasattr(self, opt): o = self.__getattribute__(opt) os = o.tolist() if isinstance(o, ndarray) else o s += '\n%s: %s' % (opt, repr(os)) return s class SourceModel(Serializable, object): """ A source model as a collection of extracted sources. Parameters ---------- sources : list or Sources or a single Source The identified sources See also -------- Source """ def __init__(self, sources): if isinstance(sources, Source): self.sources = [sources] elif isinstance(sources, list) and isinstance(sources[0], Source): self.sources = sources elif isinstance(sources, list): self.sources = [] for ss in sources: self.sources.append(Source(ss)) else: raise Exception("Input type not recognized, must be Source, list of Sources, " "or list of coordinates, got %s" % type(sources)) def __getitem__(self, entry): if not isinstance(entry, int): raise IndexError("Selection not recognized, must be Int, got %s" % type(entry)) return self.sources[entry] def combiner(self, prop, tolist=True): combined = [] for s in self.sources: p = getattr(s, prop) if tolist: p = p.tolist() combined.append(p) return combined @property def coordinates(self): """ List of coordinates combined across sources """ return self.combiner('coordinates') @property def values(self): """ List of coordinates combined across sources """ return self.combiner('values') @property def centers(self): """ Array of centers combined across sources """ return asarray(self.combiner('center')) @property def polygons(self): """ List of polygons combined across sources """ return self.combiner('polygon') @property def areas(self): """ List of areas combined across sources """ return self.combiner('area', tolist=False) @property def count(self): """ Number of sources """ return len(self.sources) def masks(self, dims=None, binary=True, outline=False, base=None, color=None, inds=None): """ Composite masks combined across sources as an image. Parameters ---------- dims : list or tuple, optional, default = None Dimensions of image in which to create masks, must either provide these or provide a base image binary : boolean, optional, deafult = True Whether to incoporate values or only show a binary mask outline : boolean, optional, deafult = False Whether to only show outlines (derived using binary dilation) base : SourceModel or array-like, optional, deafult = None Base background image on which to put masks, or another set of sources (usually for comparisons). color : str, optional, deafult = None Color to assign regions, will assign randomly if 'random' inds : array-like, optional, deafult = None List of indices if only showing a subset """ from thunder import Colorize from matplotlib.cm import get_cmap if inds is None: inds = range(0, self.count) if dims is None and base is None: raise Exception("Must provide image dimensions for composite masks " "or provide a base image.") if base is not None and isinstance(base, SourceModel): outline = True if dims is None and base is not None: dims = asarray(base).shape if isinstance(base, SourceModel): base = base.masks(dims, color='silver') elif isinstance(base, ndarray): base = Colorize(cmap='indexed', colors=['white']).transform([base]) if base is not None and color is None: color = 'deeppink' if color == 'random': combined = zeros(list(dims) + [3]) ncolors = min(self.count, 20) colors = get_cmap('rainbow', ncolors)(range(0, ncolors, 1))[:, 0:3] for i in inds: combined = maximum(self.sources[i].mask(dims, binary, outline, colors[i % len(colors)]), combined) else: combined = zeros(dims) for i in inds: combined = maximum(self.sources[i].mask(dims, binary, outline), combined) if color is not None and color != 'random': combined = Colorize(cmap='indexed', colors=[color]).transform([combined]) if base is not None: combined = maximum(base, combined) return combined def match(self, other, unique=False, minDistance=inf): """ For each source in self, find the index of the closest source in other. Uses euclidean distances between centers to determine distances. Can select nearest matches with or without enforcing uniqueness; if unique is False, will return the closest source in other for each source in self, possibly repeating sources multiple times if unique is True, will only allow each source in other to be matched with a single source in self, as determined by a greedy selection procedure. The minDistance parameter can be used to prevent far-away sources from being chosen during greedy selection. Params ------ other : SourceModel The source model to match sources to unique : boolean, optional, deafult = True Whether to only return unique matches minDistance : scalar, optiona, default = inf Minimum distance to use when selecting matches """ from scipy.spatial.distance import cdist targets = other.centers targetInds = range(0, len(targets)) matches = [] for s in self.sources: update = 1 # skip if no targets left, otherwise update if len(targets) == 0: update = 0 else: dists = cdist(targets, s.center[newaxis]) if dists.min() < minDistance: ind = argmin(dists) else: update = 0 # apply updates, otherwise add a nan if update == 1: matches.append(targetInds[ind]) if unique is True: targets = delete(targets, ind, axis=0) targetInds = delete(targetInds, ind) else: matches.append(NaN) return matches def distance(self, other, minDistance=inf): """ Compute the distance between each source in self and other. First estimates a matching source from other for each source in self, then computes the distance between the two sources. The matches are unique, using a greedy procedure, and minDistance can be used to prevent outliers during matching. Parameters ---------- other : SourceModel The sources to compute distances to minDistance : scalar, optiona, default = inf Minimum distance to use when matching indices """ inds = self.match(other, unique=True, minDistance=minDistance) d = [] for jj, ii in enumerate(inds): if ii is not NaN: d.append(self[jj].distance(other[ii])) else: d.append(NaN) return asarray(d) def overlap(self, other, method='fraction', minDistance=inf): """ Estimate overlap between sources in self and other. Will compute the similarity of sources in self that are found in other, based on either source pixel overlap or correlation. Parameters ---------- other : SourceModel The sources to compare to method : str, optional, default = 'fraction" Method to use when computing overlap between sources ('fraction', 'rates', or 'correlation') minDistance : scalar, optional, default = inf Minimum distance to use when matching indices """ inds = self.match(other, unique=True, minDistance=minDistance) d = [] for jj, ii in enumerate(inds): if ii is not NaN: d.append(self[jj].overlap(other[ii], method=method)) else: if method == 'rates': d.append((NaN, NaN)) else: d.append(NaN) return asarray(d) def similarity(self, other, metric='distance', thresh=5, minDistance=inf): """ Estimate similarity to another set of sources using recall and precision. Will compute the number of sources in self that are also in other, based on a given distance metric and a threshold. The recall rate is the number of matches divided by the number in self, and the precision rate is the number of matches divided by the number in other. Typically self is ground truth and other is an estimate. The F score is defined as 2 * (recall * precision) / (recall + precision) Before computing metrics, all sources in self are matched to other, and a minimum distance can be set to control matching. Parameters ---------- other : SourceModel The sources to compare to. metric : str, optional, default = 'distance' Metric to use when computing distances, options include 'distance' and 'overlap' thresh : scalar, optional, default = 5 The distance below which a source is considered found. minDistance : scalar, optional, default = inf Minimum distance to use when matching indices. """ checkParams(metric, ['distance', 'overlap']) if metric == 'distance': # when evaluating distances, # minimum distance should be the threshold if minDistance == inf: minDistance = thresh vals = self.distance(other, minDistance=minDistance) vals[isnan(vals)] = inf compare = lambda x: x < thresh elif metric == 'overlap': vals = self.overlap(other, method='fraction', minDistance=minDistance) vals[isnan(vals)] = 0 compare = lambda x: x > thresh else: raise Exception("Metric not recognized") recall = sum(map(compare, vals)) / float(self.count) precision = sum(map(compare, vals)) / float(other.count) score = 2 * (recall * precision) / (recall + precision) return recall, precision, score def transform(self, data, collect=True): """ Extract series from data using a list of sources. Currently only supports simple averaging over coordinates. Params ------ data : Images or Series object The data from which to extract signals collect : boolean, optional, default = True Whether to collect to local array or keep as a Series """ if not (isinstance(data, Images) or isinstance(data, Series)): raise Exception("Input must either be Images or Series (or a subclass)") # TODO add support for weighting if isinstance(data, Images): output = data.meanByRegions(self.coordinates).toSeries() else: output = data.meanByRegions(self.coordinates) if collect: return output.collectValuesAsArray() else: return output def clean(self, cleaners=None): """ Apply one or more cleaners to sources, returning filtered sources Parameters ---------- cleaners : Cleaner or list of Cleaners, optional, default = None Which cleaners to apply, if None, will apply BasicCleaner with defaults """ from thunder.extraction.cleaners import Cleaner, BasicCleaner from copy import copy if isinstance(cleaners, list): for c in cleaners: if not isinstance(c, Cleaner): raise Exception("List must only contain Cleaners") elif isinstance(cleaners, Cleaner): cleaners = [cleaners] elif cleaners is None: cleaners = [BasicCleaner()] else: raise Exception("Must provide Cleaner or list of Cleaners, got %s" % type(cleaners)) newmodel = copy(self) for c in cleaners: newmodel = c.clean(newmodel) return newmodel def dilate(self, size): """ Dilate all sources using morphological operators Parameters ---------- size : int Size of dilation in pixels """ return SourceModel([s.dilate(size) for s in self.sources]) def outline(self, inner, outer): """ Outline all sources inner : int Size of inner outline boundary (in pixels) outer : int Size of outer outline boundary (in pixels) """ return SourceModel([s.outline(inner, outer) for s in self.sources]) def crop(self, minBound, maxBound): """ Crop all sources by removing coordinates outside of bounds Parameters ---------- minBound : tuple Minimum or starting bounds for each axis maxBound : tuple Maximum or ending bounds for each axis """ return SourceModel([s.crop(minBound, maxBound) for s in self.sources]) def save(self, f, include=None, overwrite=False, **kwargs): """ Custom save to file with simplified, human-readable output, and selection of lazy attributes. """ import copy output = copy.deepcopy(self) if isinstance(include, str): include = [include] if include is not None: for prop in include: map(lambda s: getattr(s, prop), output.sources) output.sources = map(lambda s: s.restore(include).tolist(), output.sources) simplify = lambda d: d['sources']['py/homogeneousList']['data'] super(SourceModel, output).save(f, simplify=simplify, overwrite=overwrite, **kwargs) @classmethod def load(cls, f, **kwargs): """ Custom load from file to handle simplified, human-readable output """ unsimplify = lambda d: {'sources': { 'py/homogeneousList': {'data': d, 'module': 'thunder.extraction.source', 'type': 'Source'}}} output = super(SourceModel, cls).load(f, unsimplify=unsimplify) output.sources = map(lambda s: s.toarray(), output.sources) return output @classmethod def deserialize(cls, d, **kwargs): """ Custom load from JSON to handle simplified, human-readable output """ unsimplify = lambda d: {'sources': { 'py/homogeneousList': {'data': d, 'module': 'thunder.extraction.source', 'type': 'Source'}}} output = super(SourceModel, cls).deserialize(d, unsimplify=unsimplify) output.sources = map(lambda s: s.toarray(), output.sources) return output def __repr__(self): s = self.__class__.__name__ s += '\n%g sources' % (len(self.sources)) return s LAZY_ATTRIBUTES = ["center", "polygon", "bbox", "area"]
apache-2.0
amanzi/ats-dev
tools/utils/transect_data.py
2
7741
"""Loads and/or plots 2D, topologlically structured data on quadrilaterals using matplotlib. """ import sys,os import numpy as np import h5py import mesh import colors def fullname(varname): fullname = varname if not '.cell.' in fullname: fullname = fullname+'.cell.0' return fullname def transect_data(varnames, keys='all', directory=".", filename="visdump_data.h5", mesh_filename="visdump_mesh.h5", coord_order=None, deformable=False, return_map=False): """Pulls simulation output into structured 2D arrays for transect-based, (i,j) indexing. Input: varnames | A list of variable names to pull, e.g. | ['saturation_liquid', 'saturation_ice'], or a single variable | name, e.g. 'saturation_liquid' keys | Indices of timesteps to pull. Either an int (i.e. 0, -1, etc) | for the kth timestep, or a list of ints, or 'all'. directory | Directory of the run. Defaults to '.' filename | Filename of the run. Defaults to 'visdump_data.h5' mesh_filename | Filename of the mesh. Defaults to 'visdump_mesh.h5' coord_order | Order of the transect coordinates. Defaults to ['x','z']. The | mesh is sorted in this order. deformable | Is the mesh deforming? return_map | See return value below. Output: Output is an array of shape: ( len(varnames+2), len(keys), n_cells_coord_order[0], n_cells_coord_order[1] ) data[0,0,:,:] is the coord_order[0] centroid data[1,0,:,:] is the coord_order[1] centroid data[i+2,k,:,:] is the ith varname data at the kth requested timestep, sorted in the same way as the centroids. Note that the data is re-ordered in INCREASING coordinate, i.e. bottom to top in z. If return_map is True, then returns a tuple, (data, map) where map is a (NX,NZ) array of integers specifying which global id corresponds to the (i,j) cell. This is useful for mapping input data back INTO the unstructured mesh. Example usage: Calculate and plot the thaw depth at step 5. // Pull saturation ice -- TD is where sat ice = 0." data = transect_data(['saturation_ice', 5) // x coordinate for plotting x = data[0,0,:,0] // for each column, find highest z where sat_ice > 0. td_i = np.array([np.where(data[2,0,i,:] > 0.)[0][-1] for i in range(data.shape[2])]) // now that we have an index into the highest cell with ice, determine td as the // mean of the highest cell with ice and the one above that. Note this assumes // all columns have some thawing. td_z = np.array( [ (dat[1,0,i,td_i[i]] + dat[1,0,i,td_i[i+1]]) / 2. for i in range(len(td_i)) ] ) plt.plot(x, td_z) """ if coord_order is None: coord_order = ['x','z'] if type(varnames) is str: varnames = [varnames,] # get centroids xyz = mesh.meshElemCentroids(mesh_filename, directory) # round to avoid issues xyz = np.round(xyz, decimals=5) # get ordering of centroids dtype = [(coord_order[0], float), (coord_order[1], float)] num_order = [] for i in coord_order: if i == 'x': num_order.append(0) elif i == 'y': num_order.append(1) elif i == 'z': num_order.append(2) xyz_sort_order = np.array([tuple([xyz[i,x] for x in num_order]) for i in range(len(xyz))], dtype=dtype) xyz_sorting = xyz_sort_order.argsort(order=coord_order) with h5py.File(os.path.join(directory,filename),'r') as dat: keys_avail = dat[fullname(varnames[0])].keys() keys_avail.sort(lambda a,b: int.__cmp__(int(a),int(b))) if keys == 'all': keys = keys_avail elif type(keys) is str: keys = [keys,] elif type(keys) is int: keys = [keys_avail[keys],] elif type(keys) is slice: keys = keys_avail[keys] elif type(keys) is list: if all(type(k) is int for k in keys): keys = [keys_avail[k] for k in keys] elif all(type(k) is str for k in keys): pass else: raise RuntimeError("Keys requested cannot be processed -- should be 'all', int, or str key, or list of ints or strs.") # get data vals = np.zeros((len(varnames)+2, len(keys), len(xyz)), 'd') for i,key in enumerate(keys): if deformable: xyz = mesh.meshElemCentroids(mesh_filename, directory) vals[0,i,:] = xyz[xyz_sorting,num_order[0]] vals[1,i,:] = xyz[xyz_sorting,num_order[1]] for j,varname in enumerate(varnames): vals[j+2,i,:] = dat[fullname(varname)][key][:,0][xyz_sorting] # reshape the data # determine nx nx = len(set(vals[0,0,:])) nz = vals.shape[2] / nx if (nx * nz != vals.shape[2]): raise RuntimeError("Assumption about first coordinate being cleanly binnable is falling apart -- ask Ethan to rethink this algorithm!") shp = vals.shape if not return_map: return vals.reshape(shp[0], shp[1], nx, nz) else: return vals.reshape(shp[0], shp[1], nx, nz), xyz_sorting.reshape(nx, nz) def plot(dataset, ax, cax=None, vmin=None, vmax=None, cmap="jet", label=None, mesh_filename="visdump_mesh.h5", directory=".", y_coord=0.0, linewidths=1): """Draws a dataset on an ax.""" import matplotlib.collections from matplotlib import pyplot as plt if vmin is None: vmin = dataset.min() if vmax is None: vmax = dataset.max() # get the mesh and collapse to 2D etype, coords, conn = mesh.meshElemXYZ(filename=mesh_filename, directory=directory) if etype is not 'HEX': raise RuntimeError("Only works for Hexs") coords2 = np.array([[coords[i][0::2] for i in c[1:] if abs(coords[i][1] - y_coord) < 1.e-8] for c in conn]) try: assert coords2.shape[2] == 2 assert coords2.shape[1] == 4 except AssertionError: print(coords2.shape) for c in conn: if len(c) != 9: print c raise RuntimeError("what is a conn?") coords3 = np.array([coords[i][:] for i in c[1:] if abs(coords[i][1] - y_coord) < 1.e-8]) if coords3.shape[0] != 4: print coords raise RuntimeError("Unable to squash to 2D") # reorder anti-clockwise for i,c in enumerate(coords2): centroid = c.mean(axis=0) def angle(p1,p2): a1 = np.arctan2((p1[1]-centroid[1]),(p1[0]-centroid[0])) a2 = np.arctan2((p2[1]-centroid[1]),(p2[0]-centroid[0])) if a1 < a2: return -1 elif a2 < a1: return 1 else: return 0 c2 = np.array(sorted(c,angle)) coords2[i] = c2 polygons = matplotlib.collections.PolyCollection(coords2, edgecolor='k', cmap=cmap, linewidths=linewidths) polygons.set_array(dataset) polygons.set_clim(vmin,vmax) ax.add_collection(polygons) xmin = min(c[0] for c in coords.itervalues()) xmax = max(c[0] for c in coords.itervalues()) zmin = min(c[2] for c in coords.itervalues()) zmax = max(c[2] for c in coords.itervalues()) ax.set_xlim(xmin,xmax) ax.set_ylim(zmin,zmax) if cax is not None: cb = plt.colorbar(polygons, cax=cax) if label is not None: cb.set_label(label) return ((xmin,xmax),(zmin,zmax))
bsd-3-clause
lbishal/scikit-learn
examples/gaussian_process/plot_gpc_isoprobability.py
45
3025
#!/usr/bin/python # -*- coding: utf-8 -*- """ ================================================================= Iso-probability lines for Gaussian Processes classification (GPC) ================================================================= A two-dimensional classification example showing iso-probability lines for the predicted probabilities. """ print(__doc__) # Author: Vincent Dubourg <vincent.dubourg@gmail.com> # Adapted to GaussianProcessClassifier: # Jan Hendrik Metzen <jhm@informatik.uni-bremen.de> # Licence: BSD 3 clause import numpy as np from matplotlib import pyplot as pl from matplotlib import cm from sklearn.gaussian_process import GaussianProcessClassifier from sklearn.gaussian_process.kernels import DotProduct, ConstantKernel as C # A few constants lim = 8 def g(x): """The function to predict (classification will then consist in predicting whether g(x) <= 0 or not)""" return 5. - x[:, 1] - .5 * x[:, 0] ** 2. # Design of experiments X = np.array([[-4.61611719, -6.00099547], [4.10469096, 5.32782448], [0.00000000, -0.50000000], [-6.17289014, -4.6984743], [1.3109306, -6.93271427], [-5.03823144, 3.10584743], [-2.87600388, 6.74310541], [5.21301203, 4.26386883]]) # Observations y = np.array(g(X) > 0, dtype=int) # Instanciate and fit Gaussian Process Model kernel = C(0.1, (1e-5, np.inf)) * DotProduct(sigma_0=0.1) ** 2 gp = GaussianProcessClassifier(kernel=kernel) gp.fit(X, y) print("Learned kernel: %s " % gp.kernel_) # Evaluate real function and the predicted probability res = 50 x1, x2 = np.meshgrid(np.linspace(- lim, lim, res), np.linspace(- lim, lim, res)) xx = np.vstack([x1.reshape(x1.size), x2.reshape(x2.size)]).T y_true = g(xx) y_prob = gp.predict_proba(xx)[:, 1] y_true = y_true.reshape((res, res)) y_prob = y_prob.reshape((res, res)) # Plot the probabilistic classification iso-values fig = pl.figure(1) ax = fig.gca() ax.axes.set_aspect('equal') pl.xticks([]) pl.yticks([]) ax.set_xticklabels([]) ax.set_yticklabels([]) pl.xlabel('$x_1$') pl.ylabel('$x_2$') cax = pl.imshow(y_prob, cmap=cm.gray_r, alpha=0.8, extent=(-lim, lim, -lim, lim)) norm = pl.matplotlib.colors.Normalize(vmin=0., vmax=0.9) cb = pl.colorbar(cax, ticks=[0., 0.2, 0.4, 0.6, 0.8, 1.], norm=norm) cb.set_label('${\\rm \mathbb{P}}\left[\widehat{G}(\mathbf{x}) \leq 0\\right]$') pl.clim(0, 1) pl.plot(X[y <= 0, 0], X[y <= 0, 1], 'r.', markersize=12) pl.plot(X[y > 0, 0], X[y > 0, 1], 'b.', markersize=12) cs = pl.contour(x1, x2, y_true, [0.], colors='k', linestyles='dashdot') cs = pl.contour(x1, x2, y_prob, [0.666], colors='b', linestyles='solid') pl.clabel(cs, fontsize=11) cs = pl.contour(x1, x2, y_prob, [0.5], colors='k', linestyles='dashed') pl.clabel(cs, fontsize=11) cs = pl.contour(x1, x2, y_prob, [0.334], colors='r', linestyles='solid') pl.clabel(cs, fontsize=11) pl.show()
bsd-3-clause
montagnero/political-affiliation-prediction
newsreader.py
2
11936
# -*- coding: utf-8 -*- from sklearn.decomposition import KernelPCA from sklearn.metrics.pairwise import pairwise_distances from scipy.stats.mstats import zscore import glob import json import re import datetime import os import cPickle import codecs import itertools from sklearn.feature_extraction.text import TfidfVectorizer from scipy import double,triu,ones,hstack,arange,reshape,zeros,setdiff1d,array,zeros,eye,argmax,percentile def get_news(sources=['spiegel','faz','welt','zeit'], folder='model'): ''' Collects all news articles from political ressort of major German newspapers Articles are transformed to BoW vectors and assigned to a political party For better visualization, articles' BoW vectors are also clustered into topics INPUT folder the model folder containing classifier and BoW transformer sources a list of strings for each newspaper for which a crawl is implemented default ['zeit','sz'] ''' import classifier from bs4 import BeautifulSoup from api import fetch_url import urllib2 news = dict([(source,[]) for source in sources]) # the classifier for prediction of political affiliation clf = classifier.Classifier(folder=folder) for source in sources: if source is 'spiegel': # fetching articles from sueddeutsche.de/politik url = 'http://www.spiegel.de/politik' site = BeautifulSoup(urllib2.urlopen(url).read()) titles = site.findAll("div", { "class" : "teaser" }) urls = ['http://www.spiegel.de'+a.findNext('a')['href'] for a in titles] if source is 'faz': # fetching articles from sueddeutsche.de/politik url = 'http://www.faz.net/aktuell/politik' site = BeautifulSoup(urllib2.urlopen(url).read()) titles = site.findAll("a", { "class" : "TeaserHeadLink" }) urls = ['http://www.faz.net'+a['href'] for a in titles] if source is 'welt': # fetching articles from sueddeutsche.de/politik url = 'http://www.welt.de/politik' site = BeautifulSoup(urllib2.urlopen(url).read()) titles = site.findAll("a", { "class" : "as_teaser-kicker" }) urls = [a['href'] for a in titles] if source is 'sz-without-readability': # fetching articles from sueddeutsche.de/politik url = 'http://www.sueddeutsche.de/politik' site = BeautifulSoup(urllib2.urlopen(url).read()) titles = site.findAll("div", { "class" : "teaser" }) urls = [a.findNext('a')['href'] for a in titles] if source is 'zeit': # fetching articles from zeit.de/politik url = 'http://www.zeit.de/politik' site = BeautifulSoup(urllib2.urlopen(url).read()) titles = site.findAll("span", { "class" : "supertitle" }) urls = [a.parent['href'] for a in titles if a.parent['href'].find('/2015-')>0] print "Found %d articles on %s"%(len(urls),url) # predict party from url for this source print "Predicting %s"%source articles = [] for url in urls: try: title,text = fetch_url(url) prediction = clf.predict(text) prediction['url'] = url articles.append((title,prediction)) except: print('Could not get text from %s'%url) pass news[source] = dict(articles) # save results datestr = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") open(folder+'/news-%s'%(datestr) + '.json', 'wb').write(json.dumps(news,ensure_ascii=False).encode('utf8')) def all_saved_news(folder='model'): import glob from string import digits # get just the most recent news articles file (assuming date label ordering) news = json.load(open(glob.glob(folder+'/news*.json')[-1],"r")) # collect text data from all articles articles, data = [], [] for source in news.keys(): for title, article in news[source].items(): # remove numbers for d in digits: article['text'] = article['text'].replace(d,'') data.append(article['text']) predictions = [prediction['probability'] for prediction in article['prediction']] articles.append({ 'source':source, 'title':title, 'url':article['url'], 'prediction':article['prediction'], 'predictedLabel':article['prediction'][argmax(predictions)]['party'] }) return articles, data def pairwise_dists(data, nneighbors=10, folder='model', dist='l2'): ''' Computes pairwise distances between bag-of-words vectors of articles INPUT folder model folder nneighbors number of closest neighbors to include in distance list ''' stopwords = codecs.open("stopwords.txt", "r", encoding="utf-8", errors='ignore').readlines()[5:] stops = map(lambda x:x.lower().strip(),stopwords) # using now stopwords and filtering out digits bow = TfidfVectorizer(min_df=2,stop_words=stops) X = bow.fit_transform(data) print 'Computing %s pairwise distances'%dist # KPCA transform bow vectors if dist is 'l2_kpca_zscore': K = pairwise_distances(X,metric='l2',n_jobs=1) perc = 50.0 width = percentile(K.flatten(),perc) Xc = zscore(KernelPCA(n_components=50,kernel='rbf',gamma=width).fit_transform(X)) K = pairwise_distances(Xc,metric='l2',n_jobs=1) elif dist is 'l2_kpca': K = pairwise_distances(X,metric='l2',n_jobs=1) perc = 100./len(data) width = percentile(K.flatten(),perc) Xc = KernelPCA(n_components=50,kernel='rbf',gamma=width).fit_transform(X) K = pairwise_distances(Xc,metric='l2',n_jobs=1) elif dist is 'l2': K = pairwise_distances(X,metric='l2',n_jobs=1) elif dist is 'l1': K = pairwise_distances(X,metric='l1',n_jobs=1) # collect closest neighbors distances = [] for urlidx in range(len(data)): idx = (K[urlidx,:]).argsort()[1:nneighbors+1] for sidx in idx: distances.append([urlidx,sidx,(idx==sidx).nonzero()[0][0]]) return distances def load_sentiment(negative='SentiWS_v1.8c/SentiWS_v1.8c_Negative.txt',\ positive='SentiWS_v1.8c/SentiWS_v1.8c_Positive.txt'): words = dict() for line in open(negative).readlines(): parts = line.strip('\n').split('\t') words[parts[0].split('|')[0]] = double(parts[1]) if len(parts)>2: for inflection in parts[2].strip('\n').split(','): words[inflection] = double(parts[1]) for line in open(positive).readlines(): parts = line.strip('\n').split('\t') words[parts[0].split('|')[0]] = double(parts[1]) if len(parts)>2: for inflection in parts[2].strip('\n').split(','): words[inflection] = double(parts[1]) return words def get_sentiments(data): # filtering out some noise words stops = map(lambda x:x.lower().strip(),open('stopwords.txt').readlines()[6:]) # vectorize non-stopwords bow = TfidfVectorizer(min_df=2,stop_words=stops) X = bow.fit_transform(data) # map sentiment vector to bow space words = load_sentiment() sentiment_vec = zeros(X.shape[1]) for key in words.keys(): if bow.vocabulary_.has_key(key): sentiment_vec[bow.vocabulary_[key]] = words[key] # compute sentiments return X.dot(sentiment_vec) def kpca_cluster(data,nclusters=100,ncomponents=40,topwhat=10,zscored=False): ''' Computes clustering of bag-of-words vectors of articles INPUT folder model folder nclusters number of clusters ''' from sklearn.cluster import KMeans # filtering out some noise words stops = map(lambda x:x.lower().strip(),open('stopwords.txt').readlines()[6:]) # vectorize non-stopwords bow = TfidfVectorizer(min_df=2,stop_words=stops) X = bow.fit_transform(data) # creating bow-index-to-word map idx2word = dict(zip(bow.vocabulary_.values(),bow.vocabulary_.keys())) # using now stopwords and filtering out digits print 'Computing pairwise distances' K = pairwise_distances(X,metric='l2',n_jobs=1) perc = 50.0 width = percentile(K.flatten(),perc) # KPCA transform bow vectors Xc = KernelPCA(n_components=ncomponents,kernel='rbf',gamma=width).fit_transform(X) if zscored: Xc = zscore(Xc) # compute clusters km = KMeans(n_clusters=nclusters).fit(Xc) Xc = km.predict(Xc) clusters = [] for icluster in range(nclusters): nmembers = (Xc==icluster).sum() if True:#nmembers < len(data) / 5.0 and nmembers > 1: # only group clusters big enough but not too big members = (Xc==icluster).nonzero()[0] topwordidx = array(X[members,:].sum(axis=0))[0].argsort()[-topwhat:][::-1] topwords = ' '.join([idx2word[wi] for wi in topwordidx]) meanDist = triu(pairwise_distances(X[members,:],metric='l2',n_jobs=1)).sum() meanDist = meanDist / (len(members) + (len(members)**2 - len(members))/2.0) # print u'Cluster %d'%icluster + u' %d members'%nmembers + u' mean Distance %f'%meanDist + u'\n\t'+topwords clusters.append({ 'name':'Cluster-%d'%icluster, 'description': topwords, 'members': list(members), 'meanL2Distances': meanDist }) return clusters def party_cluster(articles): clusters = [] keyf = lambda a: a[1]['predictedLabel'] for k, group in itertools.groupby(sorted(enumerate(articles), key=keyf), keyf): clusters.append({ 'name': k, 'description': k, 'members': [index_article_tuple[0] for index_article_tuple in group] }) return clusters def write_distances_json(folder='model'): articles, data = all_saved_news(folder) dists = ['l2_kpca'] distances_json = { 'articles': articles, 'sentiments': json.dumps(get_sentiments(data).tolist()), 'distances': [ { 'name': dist, 'distances': pairwise_dists(data,dist = dist) } for dist in dists ], 'clusterings': [ { 'name': 'Parteivorhersage', 'clusters': party_cluster(articles) }, { 'name': 'Ähnlichkeit', 'clusters': kpca_cluster(data,nclusters=len(articles)/2,ncomponents=40,zscored=False) }, ] } # save article with party prediction and distances to closest articles datestr = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") open(folder+'/distances-%s'%(datestr)+'.json', 'wb').write(json.dumps(distances_json)) # also save that latest version for the visualization open(folder+'/distances.json', 'wb').write(json.dumps(distances_json)) if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(\ description='Downloads, transforms and clusters news articles') parser.add_argument('-f','--folder',help='Folder to store text files [./model]',\ default='model') parser.add_argument('-d','--download',help='If files should be downloaded',\ action='store_true', default=False) parser.add_argument('-p','--distances',help='If pairwise distances of text should be computed',\ action='store_true', default=False) args = vars(parser.parse_args()) if not os.path.isdir(args['folder']): os.mkdir(args['folder']) if args['download']: get_news(folder=args['folder']) if args['distances']: write_distances_json(folder=args['folder'])
mit
bikong2/scikit-learn
benchmarks/bench_plot_approximate_neighbors.py
244
6011
""" Benchmark for approximate nearest neighbor search using locality sensitive hashing forest. There are two types of benchmarks. First, accuracy of LSHForest queries are measured for various hyper-parameters and index sizes. Second, speed up of LSHForest queries compared to brute force method in exact nearest neighbors is measures for the aforementioned settings. In general, speed up is increasing as the index size grows. """ from __future__ import division import numpy as np from tempfile import gettempdir from time import time from sklearn.neighbors import NearestNeighbors from sklearn.neighbors.approximate import LSHForest from sklearn.datasets import make_blobs from sklearn.externals.joblib import Memory m = Memory(cachedir=gettempdir()) @m.cache() def make_data(n_samples, n_features, n_queries, random_state=0): """Create index and query data.""" print('Generating random blob-ish data') X, _ = make_blobs(n_samples=n_samples + n_queries, n_features=n_features, centers=100, shuffle=True, random_state=random_state) # Keep the last samples as held out query vectors: note since we used # shuffle=True we have ensured that index and query vectors are # samples from the same distribution (a mixture of 100 gaussians in this # case) return X[:n_samples], X[n_samples:] def calc_exact_neighbors(X, queries, n_queries, n_neighbors): """Measures average times for exact neighbor queries.""" print ('Building NearestNeighbors for %d samples in %d dimensions' % (X.shape[0], X.shape[1])) nbrs = NearestNeighbors(algorithm='brute', metric='cosine').fit(X) average_time = 0 t0 = time() neighbors = nbrs.kneighbors(queries, n_neighbors=n_neighbors, return_distance=False) average_time = (time() - t0) / n_queries return neighbors, average_time def calc_accuracy(X, queries, n_queries, n_neighbors, exact_neighbors, average_time_exact, **lshf_params): """Calculates accuracy and the speed up of LSHForest.""" print('Building LSHForest for %d samples in %d dimensions' % (X.shape[0], X.shape[1])) lshf = LSHForest(**lshf_params) t0 = time() lshf.fit(X) lshf_build_time = time() - t0 print('Done in %0.3fs' % lshf_build_time) accuracy = 0 t0 = time() approx_neighbors = lshf.kneighbors(queries, n_neighbors=n_neighbors, return_distance=False) average_time_approx = (time() - t0) / n_queries for i in range(len(queries)): accuracy += np.in1d(approx_neighbors[i], exact_neighbors[i]).mean() accuracy /= n_queries speed_up = average_time_exact / average_time_approx print('Average time for lshf neighbor queries: %0.3fs' % average_time_approx) print ('Average time for exact neighbor queries: %0.3fs' % average_time_exact) print ('Average Accuracy : %0.2f' % accuracy) print ('Speed up: %0.1fx' % speed_up) return speed_up, accuracy if __name__ == '__main__': import matplotlib.pyplot as plt # Initialize index sizes n_samples = [int(1e3), int(1e4), int(1e5), int(1e6)] n_features = int(1e2) n_queries = 100 n_neighbors = 10 X_index, X_query = make_data(np.max(n_samples), n_features, n_queries, random_state=0) params_list = [{'n_estimators': 3, 'n_candidates': 50}, {'n_estimators': 5, 'n_candidates': 70}, {'n_estimators': 10, 'n_candidates': 100}] accuracies = np.zeros((len(n_samples), len(params_list)), dtype=float) speed_ups = np.zeros((len(n_samples), len(params_list)), dtype=float) for i, sample_size in enumerate(n_samples): print ('==========================================================') print ('Sample size: %i' % sample_size) print ('------------------------') exact_neighbors, average_time_exact = calc_exact_neighbors( X_index[:sample_size], X_query, n_queries, n_neighbors) for j, params in enumerate(params_list): print ('LSHF parameters: n_estimators = %i, n_candidates = %i' % (params['n_estimators'], params['n_candidates'])) speed_ups[i, j], accuracies[i, j] = calc_accuracy( X_index[:sample_size], X_query, n_queries, n_neighbors, exact_neighbors, average_time_exact, random_state=0, **params) print ('') print ('==========================================================') # Set labels for LSHForest parameters colors = ['c', 'm', 'y'] legend_rects = [plt.Rectangle((0, 0), 0.1, 0.1, fc=color) for color in colors] legend_labels = ['n_estimators={n_estimators}, ' 'n_candidates={n_candidates}'.format(**p) for p in params_list] # Plot precision plt.figure() plt.legend(legend_rects, legend_labels, loc='upper left') for i in range(len(params_list)): plt.scatter(n_samples, accuracies[:, i], c=colors[i]) plt.plot(n_samples, accuracies[:, i], c=colors[i]) plt.ylim([0, 1.3]) plt.xlim(np.min(n_samples), np.max(n_samples)) plt.semilogx() plt.ylabel("Precision@10") plt.xlabel("Index size") plt.grid(which='both') plt.title("Precision of first 10 neighbors with index size") # Plot speed up plt.figure() plt.legend(legend_rects, legend_labels, loc='upper left') for i in range(len(params_list)): plt.scatter(n_samples, speed_ups[:, i], c=colors[i]) plt.plot(n_samples, speed_ups[:, i], c=colors[i]) plt.ylim(0, np.max(speed_ups)) plt.xlim(np.min(n_samples), np.max(n_samples)) plt.semilogx() plt.ylabel("Speed up") plt.xlabel("Index size") plt.grid(which='both') plt.title("Relationship between Speed up and index size") plt.show()
bsd-3-clause
hsuantien/scikit-learn
doc/tutorial/text_analytics/solutions/exercise_01_language_train_model.py
254
2253
"""Build a language detector model The goal of this exercise is to train a linear classifier on text features that represent sequences of up to 3 consecutive characters so as to be recognize natural languages by using the frequencies of short character sequences as 'fingerprints'. """ # Author: Olivier Grisel <olivier.grisel@ensta.org> # License: Simplified BSD import sys from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.linear_model import Perceptron from sklearn.pipeline import Pipeline from sklearn.datasets import load_files from sklearn.cross_validation import train_test_split from sklearn import metrics # The training data folder must be passed as first argument languages_data_folder = sys.argv[1] dataset = load_files(languages_data_folder) # Split the dataset in training and test set: docs_train, docs_test, y_train, y_test = train_test_split( dataset.data, dataset.target, test_size=0.5) # TASK: Build a an vectorizer that splits strings into sequence of 1 to 3 # characters instead of word tokens vectorizer = TfidfVectorizer(ngram_range=(1, 3), analyzer='char', use_idf=False) # TASK: Build a vectorizer / classifier pipeline using the previous analyzer # the pipeline instance should stored in a variable named clf clf = Pipeline([ ('vec', vectorizer), ('clf', Perceptron()), ]) # TASK: Fit the pipeline on the training set clf.fit(docs_train, y_train) # TASK: Predict the outcome on the testing set in a variable named y_predicted y_predicted = clf.predict(docs_test) # Print the classification report print(metrics.classification_report(y_test, y_predicted, target_names=dataset.target_names)) # Plot the confusion matrix cm = metrics.confusion_matrix(y_test, y_predicted) print(cm) #import pylab as pl #pl.matshow(cm, cmap=pl.cm.jet) #pl.show() # Predict the result on some short new sentences: sentences = [ u'This is a language detection test.', u'Ceci est un test de d\xe9tection de la langue.', u'Dies ist ein Test, um die Sprache zu erkennen.', ] predicted = clf.predict(sentences) for s, p in zip(sentences, predicted): print(u'The language of "%s" is "%s"' % (s, dataset.target_names[p]))
bsd-3-clause
MostafaGazar/tensorflow
tensorflow/contrib/learn/python/learn/dataframe/dataframe.py
85
4704
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """A DataFrame is a container for ingesting and preprocessing data.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import collections from .series import Series from .transform import Transform class DataFrame(object): """A DataFrame is a container for ingesting and preprocessing data.""" def __init__(self): self._columns = {} def columns(self): """Set of the column names.""" return frozenset(self._columns.keys()) def __len__(self): """The number of columns in the DataFrame.""" return len(self._columns) def assign(self, **kwargs): """Adds columns to DataFrame. Args: **kwargs: assignments of the form key=value where key is a string and value is an `inflow.Series`, a `pandas.Series` or a numpy array. Raises: TypeError: keys are not strings. TypeError: values are not `inflow.Series`, `pandas.Series` or `numpy.ndarray`. TODO(jamieas): pandas assign method returns a new DataFrame. Consider switching to this behavior, changing the name or adding in_place as an argument. """ for k, v in kwargs.items(): if not isinstance(k, str): raise TypeError("The only supported type for keys is string; got %s" % type(k)) if v is None: del self._columns[k] elif isinstance(v, Series): self._columns[k] = v elif isinstance(v, Transform) and v.input_valency() == 0: self._columns[k] = v() else: raise TypeError( "Column in assignment must be an inflow.Series, inflow.Transform," " or None; got type '%s'." % type(v).__name__) def select_columns(self, keys): """Returns a new DataFrame with a subset of columns. Args: keys: A list of strings. Each should be the name of a column in the DataFrame. Returns: A new DataFrame containing only the specified columns. """ result = type(self)() for key in keys: result[key] = self._columns[key] return result def exclude_columns(self, exclude_keys): """Returns a new DataFrame with all columns not excluded via exclude_keys. Args: exclude_keys: A list of strings. Each should be the name of a column in the DataFrame. These columns will be excluded from the result. Returns: A new DataFrame containing all columns except those specified. """ result = type(self)() for key, value in self._columns.items(): if key not in exclude_keys: result[key] = value return result def __getitem__(self, key): """Indexing functionality for DataFrames. Args: key: a string or an iterable of strings. Returns: A Series or list of Series corresponding to the given keys. """ if isinstance(key, str): return self._columns[key] elif isinstance(key, collections.Iterable): for i in key: if not isinstance(i, str): raise TypeError("Expected a String; entry %s has type %s." % (i, type(i).__name__)) return [self.__getitem__(i) for i in key] raise TypeError( "Invalid index: %s of type %s. Only strings or lists of strings are " "supported." % (key, type(key))) def __setitem__(self, key, value): if isinstance(key, str): key = [key] if isinstance(value, Series): value = [value] self.assign(**dict(zip(key, value))) def __delitem__(self, key): if isinstance(key, str): key = [key] value = [None for _ in key] self.assign(**dict(zip(key, value))) def build(self, **kwargs): # We do not allow passing a cache here, because that would encourage # working around the rule that DataFrames cannot be expected to be # synced with each other (e.g., they shuffle independently). cache = {} tensors = {name: c.build(cache, **kwargs) for name, c in self._columns.items()} return tensors
apache-2.0
mtconley/turntable
test/lib/python2.7/site-packages/scipy/stats/tests/test_morestats.py
7
38719
# Author: Travis Oliphant, 2002 # # Further enhancements and tests added by numerous SciPy developers. # from __future__ import division, print_function, absolute_import import warnings import numpy as np from numpy.random import RandomState from numpy.testing import (TestCase, run_module_suite, assert_array_equal, assert_almost_equal, assert_array_less, assert_array_almost_equal, assert_raises, assert_, assert_allclose, assert_equal, dec, assert_warns) from scipy import stats # Matplotlib is not a scipy dependency but is optionally used in probplot, so # check if it's available try: import matplotlib.pyplot as plt have_matplotlib = True except: have_matplotlib = False g1 = [1.006, 0.996, 0.998, 1.000, 0.992, 0.993, 1.002, 0.999, 0.994, 1.000] g2 = [0.998, 1.006, 1.000, 1.002, 0.997, 0.998, 0.996, 1.000, 1.006, 0.988] g3 = [0.991, 0.987, 0.997, 0.999, 0.995, 0.994, 1.000, 0.999, 0.996, 0.996] g4 = [1.005, 1.002, 0.994, 1.000, 0.995, 0.994, 0.998, 0.996, 1.002, 0.996] g5 = [0.998, 0.998, 0.982, 0.990, 1.002, 0.984, 0.996, 0.993, 0.980, 0.996] g6 = [1.009, 1.013, 1.009, 0.997, 0.988, 1.002, 0.995, 0.998, 0.981, 0.996] g7 = [0.990, 1.004, 0.996, 1.001, 0.998, 1.000, 1.018, 1.010, 0.996, 1.002] g8 = [0.998, 1.000, 1.006, 1.000, 1.002, 0.996, 0.998, 0.996, 1.002, 1.006] g9 = [1.002, 0.998, 0.996, 0.995, 0.996, 1.004, 1.004, 0.998, 0.999, 0.991] g10 = [0.991, 0.995, 0.984, 0.994, 0.997, 0.997, 0.991, 0.998, 1.004, 0.997] class TestShapiro(TestCase): def test_basic(self): x1 = [0.11,7.87,4.61,10.14,7.95,3.14,0.46, 4.43,0.21,4.75,0.71,1.52,3.24, 0.93,0.42,4.97,9.53,4.55,0.47,6.66] w,pw = stats.shapiro(x1) assert_almost_equal(w,0.90047299861907959,6) assert_almost_equal(pw,0.042089745402336121,6) x2 = [1.36,1.14,2.92,2.55,1.46,1.06,5.27,-1.11, 3.48,1.10,0.88,-0.51,1.46,0.52,6.20,1.69, 0.08,3.67,2.81,3.49] w,pw = stats.shapiro(x2) assert_almost_equal(w,0.9590270,6) assert_almost_equal(pw,0.52460,3) def test_bad_arg(self): # Length of x is less than 3. x = [1] assert_raises(ValueError, stats.shapiro, x) class TestAnderson(TestCase): def test_normal(self): rs = RandomState(1234567890) x1 = rs.standard_exponential(size=50) x2 = rs.standard_normal(size=50) A,crit,sig = stats.anderson(x1) assert_array_less(crit[:-1], A) A,crit,sig = stats.anderson(x2) assert_array_less(A, crit[-2:]) def test_expon(self): rs = RandomState(1234567890) x1 = rs.standard_exponential(size=50) x2 = rs.standard_normal(size=50) A,crit,sig = stats.anderson(x1,'expon') assert_array_less(A, crit[-2:]) olderr = np.seterr(all='ignore') try: A,crit,sig = stats.anderson(x2,'expon') finally: np.seterr(**olderr) assert_(A > crit[-1]) def test_bad_arg(self): assert_raises(ValueError, stats.anderson, [1], dist='plate_of_shrimp') class TestAndersonKSamp(TestCase): def test_example1a(self): # Example data from Scholz & Stephens (1987), originally # published in Lehmann (1995, Nonparametrics, Statistical # Methods Based on Ranks, p. 309) # Pass a mixture of lists and arrays t1 = [38.7, 41.5, 43.8, 44.5, 45.5, 46.0, 47.7, 58.0] t2 = np.array([39.2, 39.3, 39.7, 41.4, 41.8, 42.9, 43.3, 45.8]) t3 = np.array([34.0, 35.0, 39.0, 40.0, 43.0, 43.0, 44.0, 45.0]) t4 = np.array([34.0, 34.8, 34.8, 35.4, 37.2, 37.8, 41.2, 42.8]) assert_warns(UserWarning, stats.anderson_ksamp, (t1, t2, t3, t4), midrank=False) with warnings.catch_warnings(): warnings.filterwarnings('ignore', message='approximate p-value') Tk, tm, p = stats.anderson_ksamp((t1, t2, t3, t4), midrank=False) assert_almost_equal(Tk, 4.449, 3) assert_array_almost_equal([0.4985, 1.3237, 1.9158, 2.4930, 3.2459], tm, 4) assert_almost_equal(p, 0.0021, 4) def test_example1b(self): # Example data from Scholz & Stephens (1987), originally # published in Lehmann (1995, Nonparametrics, Statistical # Methods Based on Ranks, p. 309) # Pass arrays t1 = np.array([38.7, 41.5, 43.8, 44.5, 45.5, 46.0, 47.7, 58.0]) t2 = np.array([39.2, 39.3, 39.7, 41.4, 41.8, 42.9, 43.3, 45.8]) t3 = np.array([34.0, 35.0, 39.0, 40.0, 43.0, 43.0, 44.0, 45.0]) t4 = np.array([34.0, 34.8, 34.8, 35.4, 37.2, 37.8, 41.2, 42.8]) with warnings.catch_warnings(): warnings.filterwarnings('ignore', message='approximate p-value') Tk, tm, p = stats.anderson_ksamp((t1, t2, t3, t4), midrank=True) assert_almost_equal(Tk, 4.480, 3) assert_array_almost_equal([0.4985, 1.3237, 1.9158, 2.4930, 3.2459], tm, 4) assert_almost_equal(p, 0.0020, 4) def test_example2a(self): # Example data taken from an earlier technical report of # Scholz and Stephens # Pass lists instead of arrays t1 = [194, 15, 41, 29, 33, 181] t2 = [413, 14, 58, 37, 100, 65, 9, 169, 447, 184, 36, 201, 118] t3 = [34, 31, 18, 18, 67, 57, 62, 7, 22, 34] t4 = [90, 10, 60, 186, 61, 49, 14, 24, 56, 20, 79, 84, 44, 59, 29, 118, 25, 156, 310, 76, 26, 44, 23, 62] t5 = [130, 208, 70, 101, 208] t6 = [74, 57, 48, 29, 502, 12, 70, 21, 29, 386, 59, 27] t7 = [55, 320, 56, 104, 220, 239, 47, 246, 176, 182, 33] t8 = [23, 261, 87, 7, 120, 14, 62, 47, 225, 71, 246, 21, 42, 20, 5, 12, 120, 11, 3, 14, 71, 11, 14, 11, 16, 90, 1, 16, 52, 95] t9 = [97, 51, 11, 4, 141, 18, 142, 68, 77, 80, 1, 16, 106, 206, 82, 54, 31, 216, 46, 111, 39, 63, 18, 191, 18, 163, 24] t10 = [50, 44, 102, 72, 22, 39, 3, 15, 197, 188, 79, 88, 46, 5, 5, 36, 22, 139, 210, 97, 30, 23, 13, 14] t11 = [359, 9, 12, 270, 603, 3, 104, 2, 438] t12 = [50, 254, 5, 283, 35, 12] t13 = [487, 18, 100, 7, 98, 5, 85, 91, 43, 230, 3, 130] t14 = [102, 209, 14, 57, 54, 32, 67, 59, 134, 152, 27, 14, 230, 66, 61, 34] with warnings.catch_warnings(): warnings.filterwarnings('ignore', message='approximate p-value') Tk, tm, p = stats.anderson_ksamp((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14), midrank=False) assert_almost_equal(Tk, 3.288, 3) assert_array_almost_equal([0.5990, 1.3269, 1.8052, 2.2486, 2.8009], tm, 4) assert_almost_equal(p, 0.0041, 4) def test_example2b(self): # Example data taken from an earlier technical report of # Scholz and Stephens t1 = [194, 15, 41, 29, 33, 181] t2 = [413, 14, 58, 37, 100, 65, 9, 169, 447, 184, 36, 201, 118] t3 = [34, 31, 18, 18, 67, 57, 62, 7, 22, 34] t4 = [90, 10, 60, 186, 61, 49, 14, 24, 56, 20, 79, 84, 44, 59, 29, 118, 25, 156, 310, 76, 26, 44, 23, 62] t5 = [130, 208, 70, 101, 208] t6 = [74, 57, 48, 29, 502, 12, 70, 21, 29, 386, 59, 27] t7 = [55, 320, 56, 104, 220, 239, 47, 246, 176, 182, 33] t8 = [23, 261, 87, 7, 120, 14, 62, 47, 225, 71, 246, 21, 42, 20, 5, 12, 120, 11, 3, 14, 71, 11, 14, 11, 16, 90, 1, 16, 52, 95] t9 = [97, 51, 11, 4, 141, 18, 142, 68, 77, 80, 1, 16, 106, 206, 82, 54, 31, 216, 46, 111, 39, 63, 18, 191, 18, 163, 24] t10 = [50, 44, 102, 72, 22, 39, 3, 15, 197, 188, 79, 88, 46, 5, 5, 36, 22, 139, 210, 97, 30, 23, 13, 14] t11 = [359, 9, 12, 270, 603, 3, 104, 2, 438] t12 = [50, 254, 5, 283, 35, 12] t13 = [487, 18, 100, 7, 98, 5, 85, 91, 43, 230, 3, 130] t14 = [102, 209, 14, 57, 54, 32, 67, 59, 134, 152, 27, 14, 230, 66, 61, 34] with warnings.catch_warnings(): warnings.filterwarnings('ignore', message='approximate p-value') Tk, tm, p = stats.anderson_ksamp((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14), midrank=True) assert_almost_equal(Tk, 3.294, 3) assert_array_almost_equal([0.5990, 1.3269, 1.8052, 2.2486, 2.8009], tm, 4) assert_almost_equal(p, 0.0041, 4) def test_not_enough_samples(self): assert_raises(ValueError, stats.anderson_ksamp, np.ones(5)) def test_no_distinct_observations(self): assert_raises(ValueError, stats.anderson_ksamp, (np.ones(5), np.ones(5))) def test_empty_sample(self): assert_raises(ValueError, stats.anderson_ksamp, (np.ones(5), [])) class TestAnsari(TestCase): def test_small(self): x = [1,2,3,3,4] y = [3,2,6,1,6,1,4,1] W, pval = stats.ansari(x,y) assert_almost_equal(W,23.5,11) assert_almost_equal(pval,0.13499256881897437,11) def test_approx(self): ramsay = np.array((111, 107, 100, 99, 102, 106, 109, 108, 104, 99, 101, 96, 97, 102, 107, 113, 116, 113, 110, 98)) parekh = np.array((107, 108, 106, 98, 105, 103, 110, 105, 104, 100, 96, 108, 103, 104, 114, 114, 113, 108, 106, 99)) with warnings.catch_warnings(): warnings.filterwarnings('ignore', message="Ties preclude use of exact statistic.") W, pval = stats.ansari(ramsay, parekh) assert_almost_equal(W,185.5,11) assert_almost_equal(pval,0.18145819972867083,11) def test_exact(self): W,pval = stats.ansari([1,2,3,4],[15,5,20,8,10,12]) assert_almost_equal(W,10.0,11) assert_almost_equal(pval,0.533333333333333333,7) def test_bad_arg(self): assert_raises(ValueError, stats.ansari, [], [1]) assert_raises(ValueError, stats.ansari, [1], []) class TestBartlett(TestCase): def test_data(self): args = [g1, g2, g3, g4, g5, g6, g7, g8, g9, g10] T, pval = stats.bartlett(*args) assert_almost_equal(T,20.78587342806484,7) assert_almost_equal(pval,0.0136358632781,7) def test_bad_arg(self): # Too few args raises ValueError. assert_raises(ValueError, stats.bartlett, [1]) class TestLevene(TestCase): def test_data(self): args = [g1, g2, g3, g4, g5, g6, g7, g8, g9, g10] W, pval = stats.levene(*args) assert_almost_equal(W,1.7059176930008939,7) assert_almost_equal(pval,0.0990829755522,7) def test_trimmed1(self): # Test that center='trimmed' gives the same result as center='mean' # when proportiontocut=0. W1, pval1 = stats.levene(g1, g2, g3, center='mean') W2, pval2 = stats.levene(g1, g2, g3, center='trimmed', proportiontocut=0.0) assert_almost_equal(W1, W2) assert_almost_equal(pval1, pval2) def test_trimmed2(self): x = [1.2, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 100.0] y = [0.0, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 200.0] np.random.seed(1234) x2 = np.random.permutation(x) # Use center='trimmed' W0, pval0 = stats.levene(x, y, center='trimmed', proportiontocut=0.125) W1, pval1 = stats.levene(x2, y, center='trimmed', proportiontocut=0.125) # Trim the data here, and use center='mean' W2, pval2 = stats.levene(x[1:-1], y[1:-1], center='mean') # Result should be the same. assert_almost_equal(W0, W2) assert_almost_equal(W1, W2) assert_almost_equal(pval1, pval2) def test_equal_mean_median(self): x = np.linspace(-1,1,21) np.random.seed(1234) x2 = np.random.permutation(x) y = x**3 W1, pval1 = stats.levene(x, y, center='mean') W2, pval2 = stats.levene(x2, y, center='median') assert_almost_equal(W1, W2) assert_almost_equal(pval1, pval2) def test_bad_keyword(self): x = np.linspace(-1,1,21) assert_raises(TypeError, stats.levene, x, x, portiontocut=0.1) def test_bad_center_value(self): x = np.linspace(-1,1,21) assert_raises(ValueError, stats.levene, x, x, center='trim') def test_too_few_args(self): assert_raises(ValueError, stats.levene, [1]) class TestBinomP(TestCase): def test_data(self): pval = stats.binom_test(100,250) assert_almost_equal(pval,0.0018833009350757682,11) pval = stats.binom_test(201,405) assert_almost_equal(pval,0.92085205962670713,11) pval = stats.binom_test([682,243],p=3.0/4) assert_almost_equal(pval,0.38249155957481695,11) def test_bad_len_x(self): # Length of x must be 1 or 2. assert_raises(ValueError, stats.binom_test, [1,2,3]) def test_bad_n(self): # len(x) is 1, but n is invalid. # Missing n assert_raises(ValueError, stats.binom_test, [100]) # n less than x[0] assert_raises(ValueError, stats.binom_test, [100], n=50) def test_bad_p(self): assert_raises(ValueError, stats.binom_test, [50, 50], p=2.0) class TestFindRepeats(TestCase): def test_basic(self): a = [1,2,3,4,1,2,3,4,1,2,5] res,nums = stats.find_repeats(a) assert_array_equal(res,[1,2,3,4]) assert_array_equal(nums,[3,3,2,2]) def test_empty_result(self): # Check that empty arrays are returned when there are no repeats. a = [10, 20, 50, 30, 40] repeated, counts = stats.find_repeats(a) assert_array_equal(repeated, []) assert_array_equal(counts, []) class TestFligner(TestCase): def test_data(self): # numbers from R: fligner.test in package stats x1 = np.arange(5) assert_array_almost_equal(stats.fligner(x1,x1**2), (3.2282229927203536, 0.072379187848207877), 11) def test_trimmed1(self): # Test that center='trimmed' gives the same result as center='mean' # when proportiontocut=0. Xsq1, pval1 = stats.fligner(g1, g2, g3, center='mean') Xsq2, pval2 = stats.fligner(g1, g2, g3, center='trimmed', proportiontocut=0.0) assert_almost_equal(Xsq1, Xsq2) assert_almost_equal(pval1, pval2) def test_trimmed2(self): x = [1.2, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 100.0] y = [0.0, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 200.0] # Use center='trimmed' Xsq1, pval1 = stats.fligner(x, y, center='trimmed', proportiontocut=0.125) # Trim the data here, and use center='mean' Xsq2, pval2 = stats.fligner(x[1:-1], y[1:-1], center='mean') # Result should be the same. assert_almost_equal(Xsq1, Xsq2) assert_almost_equal(pval1, pval2) # The following test looks reasonable at first, but fligner() uses the # function stats.rankdata(), and in one of the cases in this test, # there are ties, while in the other (because of normal rounding # errors) there are not. This difference leads to differences in the # third significant digit of W. # #def test_equal_mean_median(self): # x = np.linspace(-1,1,21) # y = x**3 # W1, pval1 = stats.fligner(x, y, center='mean') # W2, pval2 = stats.fligner(x, y, center='median') # assert_almost_equal(W1, W2) # assert_almost_equal(pval1, pval2) def test_bad_keyword(self): x = np.linspace(-1,1,21) assert_raises(TypeError, stats.fligner, x, x, portiontocut=0.1) def test_bad_center_value(self): x = np.linspace(-1,1,21) assert_raises(ValueError, stats.fligner, x, x, center='trim') def test_bad_num_args(self): # Too few args raises ValueError. assert_raises(ValueError, stats.fligner, [1]) class TestMood(TestCase): def test_mood(self): # numbers from R: mood.test in package stats x1 = np.arange(5) assert_array_almost_equal(stats.mood(x1, x1**2), (-1.3830857299399906, 0.16663858066771478), 11) def test_mood_order_of_args(self): # z should change sign when the order of arguments changes, pvalue # should not change np.random.seed(1234) x1 = np.random.randn(10, 1) x2 = np.random.randn(15, 1) z1, p1 = stats.mood(x1, x2) z2, p2 = stats.mood(x2, x1) assert_array_almost_equal([z1, p1], [-z2, p2]) def test_mood_with_axis_none(self): #Test with axis = None, compare with results from R x1 = [-0.626453810742332, 0.183643324222082, -0.835628612410047, 1.59528080213779, 0.329507771815361, -0.820468384118015, 0.487429052428485, 0.738324705129217, 0.575781351653492, -0.305388387156356, 1.51178116845085, 0.389843236411431, -0.621240580541804, -2.2146998871775, 1.12493091814311, -0.0449336090152309, -0.0161902630989461, 0.943836210685299, 0.821221195098089, 0.593901321217509] x2 = [-0.896914546624981, 0.184849184646742, 1.58784533120882, -1.13037567424629, -0.0802517565509893, 0.132420284381094, 0.707954729271733, -0.23969802417184, 1.98447393665293, -0.138787012119665, 0.417650750792556, 0.981752777463662, -0.392695355503813, -1.03966897694891, 1.78222896030858, -2.31106908460517, 0.878604580921265, 0.035806718015226, 1.01282869212708, 0.432265154539617, 2.09081920524915, -1.19992581964387, 1.58963820029007, 1.95465164222325, 0.00493777682814261, -2.45170638784613, 0.477237302613617, -0.596558168631403, 0.792203270299649, 0.289636710177348] x1 = np.array(x1) x2 = np.array(x2) x1.shape = (10, 2) x2.shape = (15, 2) assert_array_almost_equal(stats.mood(x1, x2, axis=None), [-1.31716607555, 0.18778296257]) def test_mood_2d(self): # Test if the results of mood test in 2-D case are consistent with the # R result for the same inputs. Numbers from R mood.test(). ny = 5 np.random.seed(1234) x1 = np.random.randn(10, ny) x2 = np.random.randn(15, ny) z_vectest, pval_vectest = stats.mood(x1, x2) for j in range(ny): assert_array_almost_equal([z_vectest[j], pval_vectest[j]], stats.mood(x1[:, j], x2[:, j])) # inverse order of dimensions x1 = x1.transpose() x2 = x2.transpose() z_vectest, pval_vectest = stats.mood(x1, x2, axis=1) for i in range(ny): # check axis handling is self consistent assert_array_almost_equal([z_vectest[i], pval_vectest[i]], stats.mood(x1[i, :], x2[i, :])) def test_mood_3d(self): shape = (10, 5, 6) np.random.seed(1234) x1 = np.random.randn(*shape) x2 = np.random.randn(*shape) for axis in range(3): z_vectest, pval_vectest = stats.mood(x1, x2, axis=axis) # Tests that result for 3-D arrays is equal to that for the # same calculation on a set of 1-D arrays taken from the # 3-D array axes_idx = ([1, 2], [0, 2], [0, 1]) # the two axes != axis for i in range(shape[axes_idx[axis][0]]): for j in range(shape[axes_idx[axis][1]]): if axis == 0: slice1 = x1[:, i, j] slice2 = x2[:, i, j] elif axis == 1: slice1 = x1[i, :, j] slice2 = x2[i, :, j] else: slice1 = x1[i, j, :] slice2 = x2[i, j, :] assert_array_almost_equal([z_vectest[i, j], pval_vectest[i, j]], stats.mood(slice1, slice2)) def test_mood_bad_arg(self): # Raise ValueError when the sum of the lengths of the args is less than 3 assert_raises(ValueError, stats.mood, [1], []) class TestProbplot(TestCase): def test_basic(self): np.random.seed(12345) x = stats.norm.rvs(size=20) osm, osr = stats.probplot(x, fit=False) osm_expected = [-1.8241636, -1.38768012, -1.11829229, -0.91222575, -0.73908135, -0.5857176, -0.44506467, -0.31273668, -0.18568928, -0.06158146, 0.06158146, 0.18568928, 0.31273668, 0.44506467, 0.5857176, 0.73908135, 0.91222575, 1.11829229, 1.38768012, 1.8241636] assert_allclose(osr, np.sort(x)) assert_allclose(osm, osm_expected) res, res_fit = stats.probplot(x, fit=True) res_fit_expected = [1.05361841, 0.31297795, 0.98741609] assert_allclose(res_fit, res_fit_expected) def test_sparams_keyword(self): np.random.seed(123456) x = stats.norm.rvs(size=100) # Check that None, () and 0 (loc=0, for normal distribution) all work # and give the same results osm1, osr1 = stats.probplot(x, sparams=None, fit=False) osm2, osr2 = stats.probplot(x, sparams=0, fit=False) osm3, osr3 = stats.probplot(x, sparams=(), fit=False) assert_allclose(osm1, osm2) assert_allclose(osm1, osm3) assert_allclose(osr1, osr2) assert_allclose(osr1, osr3) # Check giving (loc, scale) params for normal distribution osm, osr = stats.probplot(x, sparams=(), fit=False) def test_dist_keyword(self): np.random.seed(12345) x = stats.norm.rvs(size=20) osm1, osr1 = stats.probplot(x, fit=False, dist='t', sparams=(3,)) osm2, osr2 = stats.probplot(x, fit=False, dist=stats.t, sparams=(3,)) assert_allclose(osm1, osm2) assert_allclose(osr1, osr2) assert_raises(ValueError, stats.probplot, x, dist='wrong-dist-name') assert_raises(AttributeError, stats.probplot, x, dist=[]) class custom_dist(object): """Some class that looks just enough like a distribution.""" def ppf(self, q): return stats.norm.ppf(q, loc=2) osm1, osr1 = stats.probplot(x, sparams=(2,), fit=False) osm2, osr2 = stats.probplot(x, dist=custom_dist(), fit=False) assert_allclose(osm1, osm2) assert_allclose(osr1, osr2) @dec.skipif(not have_matplotlib) def test_plot_kwarg(self): np.random.seed(7654321) fig = plt.figure() fig.add_subplot(111) x = stats.t.rvs(3, size=100) res1, fitres1 = stats.probplot(x, plot=plt) plt.close() res2, fitres2 = stats.probplot(x, plot=None) res3 = stats.probplot(x, fit=False, plot=plt) plt.close() res4 = stats.probplot(x, fit=False, plot=None) # Check that results are consistent between combinations of `fit` and # `plot` keywords. assert_(len(res1) == len(res2) == len(res3) == len(res4) == 2) assert_allclose(res1, res2) assert_allclose(res1, res3) assert_allclose(res1, res4) assert_allclose(fitres1, fitres2) # Check that a Matplotlib Axes object is accepted fig = plt.figure() ax = fig.add_subplot(111) stats.probplot(x, fit=False, plot=ax) plt.close() def test_probplot_bad_args(self): # Raise ValueError when given an invalid distribution. assert_raises(ValueError, stats.probplot, [1], dist="plate_of_shrimp") def test_wilcoxon_bad_arg(): # Raise ValueError when two args of different lengths are given or # zero_method is unknown. assert_raises(ValueError, stats.wilcoxon, [1], [1,2]) assert_raises(ValueError, stats.wilcoxon, [1,2], [1,2], "dummy") def test_mvsdist_bad_arg(): # Raise ValueError if fewer than two data points are given. data = [1] assert_raises(ValueError, stats.mvsdist, data) def test_kstat_bad_arg(): # Raise ValueError if n > 4 or n > 1. data = [1] n = 10 assert_raises(ValueError, stats.kstat, data, n=n) def test_kstatvar_bad_arg(): # Raise ValueError is n is not 1 or 2. data = [1] n = 10 assert_raises(ValueError, stats.kstatvar, data, n=n) def test_ppcc_max_bad_arg(): # Raise ValueError when given an invalid distribution. data = [1] assert_raises(ValueError, stats.ppcc_max, data, dist="plate_of_shrimp") class TestBoxcox_llf(TestCase): def test_basic(self): np.random.seed(54321) x = stats.norm.rvs(size=10000, loc=10) lmbda = 1 llf = stats.boxcox_llf(lmbda, x) llf_expected = -x.size / 2. * np.log(np.sum(x.std()**2)) assert_allclose(llf, llf_expected) def test_array_like(self): np.random.seed(54321) x = stats.norm.rvs(size=100, loc=10) lmbda = 1 llf = stats.boxcox_llf(lmbda, x) llf2 = stats.boxcox_llf(lmbda, list(x)) assert_allclose(llf, llf2, rtol=1e-12) def test_2d_input(self): # Note: boxcox_llf() was already working with 2-D input (sort of), so # keep it like that. boxcox() doesn't work with 2-D input though, due # to brent() returning a scalar. np.random.seed(54321) x = stats.norm.rvs(size=100, loc=10) lmbda = 1 llf = stats.boxcox_llf(lmbda, x) llf2 = stats.boxcox_llf(lmbda, np.vstack([x, x]).T) assert_allclose([llf, llf], llf2, rtol=1e-12) def test_empty(self): assert_(np.isnan(stats.boxcox_llf(1, []))) class TestBoxcox(TestCase): def test_fixed_lmbda(self): np.random.seed(12345) x = stats.loggamma.rvs(5, size=50) + 5 xt = stats.boxcox(x, lmbda=1) assert_allclose(xt, x - 1) xt = stats.boxcox(x, lmbda=-1) assert_allclose(xt, 1 - 1/x) xt = stats.boxcox(x, lmbda=0) assert_allclose(xt, np.log(x)) # Also test that array_like input works xt = stats.boxcox(list(x), lmbda=0) assert_allclose(xt, np.log(x)) def test_lmbda_None(self): np.random.seed(1234567) # Start from normal rv's, do inverse transform to check that # optimization function gets close to the right answer. np.random.seed(1245) lmbda = 2.5 x = stats.norm.rvs(loc=10, size=50000) x_inv = (x * lmbda + 1)**(-lmbda) xt, maxlog = stats.boxcox(x_inv) assert_almost_equal(maxlog, -1 / lmbda, decimal=2) def test_alpha(self): np.random.seed(1234) x = stats.loggamma.rvs(5, size=50) + 5 # Some regular values for alpha, on a small sample size _, _, interval = stats.boxcox(x, alpha=0.75) assert_allclose(interval, [4.004485780226041, 5.138756355035744]) _, _, interval = stats.boxcox(x, alpha=0.05) assert_allclose(interval, [1.2138178554857557, 8.209033272375663]) # Try some extreme values, see we don't hit the N=500 limit x = stats.loggamma.rvs(7, size=500) + 15 _, _, interval = stats.boxcox(x, alpha=0.001) assert_allclose(interval, [0.3988867, 11.40553131]) _, _, interval = stats.boxcox(x, alpha=0.999) assert_allclose(interval, [5.83316246, 5.83735292]) def test_boxcox_bad_arg(self): # Raise ValueError if any data value is negative. x = np.array([-1]) assert_raises(ValueError, stats.boxcox, x) def test_empty(self): assert_(stats.boxcox([]).shape == (0,)) class TestBoxcoxNormmax(TestCase): def setUp(self): np.random.seed(12345) self.x = stats.loggamma.rvs(5, size=50) + 5 def test_pearsonr(self): maxlog = stats.boxcox_normmax(self.x) assert_allclose(maxlog, 1.804465, rtol=1e-6) def test_mle(self): maxlog = stats.boxcox_normmax(self.x, method='mle') assert_allclose(maxlog, 1.758101, rtol=1e-6) # Check that boxcox() uses 'mle' _, maxlog_boxcox = stats.boxcox(self.x) assert_allclose(maxlog_boxcox, maxlog) def test_all(self): maxlog_all = stats.boxcox_normmax(self.x, method='all') assert_allclose(maxlog_all, [1.804465, 1.758101], rtol=1e-6) class TestBoxcoxNormplot(TestCase): def setUp(self): np.random.seed(7654321) self.x = stats.loggamma.rvs(5, size=500) + 5 def test_basic(self): N = 5 lmbdas, ppcc = stats.boxcox_normplot(self.x, -10, 10, N=N) ppcc_expected = [0.57783375, 0.83610988, 0.97524311, 0.99756057, 0.95843297] assert_allclose(lmbdas, np.linspace(-10, 10, num=N)) assert_allclose(ppcc, ppcc_expected) @dec.skipif(not have_matplotlib) def test_plot_kwarg(self): # Check with the matplotlib.pyplot module fig = plt.figure() fig.add_subplot(111) stats.boxcox_normplot(self.x, -20, 20, plot=plt) plt.close() # Check that a Matplotlib Axes object is accepted fig.add_subplot(111) ax = fig.add_subplot(111) stats.boxcox_normplot(self.x, -20, 20, plot=ax) plt.close() def test_invalid_inputs(self): # `lb` has to be larger than `la` assert_raises(ValueError, stats.boxcox_normplot, self.x, 1, 0) # `x` can not contain negative values assert_raises(ValueError, stats.boxcox_normplot, [-1, 1], 0, 1) def test_empty(self): assert_(stats.boxcox_normplot([], 0, 1).size == 0) class TestCircFuncs(TestCase): def test_circfuncs(self): x = np.array([355,5,2,359,10,350]) M = stats.circmean(x, high=360) Mval = 0.167690146 assert_allclose(M, Mval, rtol=1e-7) V = stats.circvar(x, high=360) Vval = 42.51955609 assert_allclose(V, Vval, rtol=1e-7) S = stats.circstd(x, high=360) Sval = 6.520702116 assert_allclose(S, Sval, rtol=1e-7) def test_circfuncs_small(self): x = np.array([20,21,22,18,19,20.5,19.2]) M1 = x.mean() M2 = stats.circmean(x, high=360) assert_allclose(M2, M1, rtol=1e-5) V1 = x.var() V2 = stats.circvar(x, high=360) assert_allclose(V2, V1, rtol=1e-4) S1 = x.std() S2 = stats.circstd(x, high=360) assert_allclose(S2, S1, rtol=1e-4) def test_circmean_axis(self): x = np.array([[355,5,2,359,10,350], [351,7,4,352,9,349], [357,9,8,358,4,356]]) M1 = stats.circmean(x, high=360) M2 = stats.circmean(x.ravel(), high=360) assert_allclose(M1, M2, rtol=1e-14) M1 = stats.circmean(x, high=360, axis=1) M2 = [stats.circmean(x[i], high=360) for i in range(x.shape[0])] assert_allclose(M1, M2, rtol=1e-14) M1 = stats.circmean(x, high=360, axis=0) M2 = [stats.circmean(x[:,i], high=360) for i in range(x.shape[1])] assert_allclose(M1, M2, rtol=1e-14) def test_circvar_axis(self): x = np.array([[355,5,2,359,10,350], [351,7,4,352,9,349], [357,9,8,358,4,356]]) V1 = stats.circvar(x, high=360) V2 = stats.circvar(x.ravel(), high=360) assert_allclose(V1, V2, rtol=1e-11) V1 = stats.circvar(x, high=360, axis=1) V2 = [stats.circvar(x[i], high=360) for i in range(x.shape[0])] assert_allclose(V1, V2, rtol=1e-11) V1 = stats.circvar(x, high=360, axis=0) V2 = [stats.circvar(x[:,i], high=360) for i in range(x.shape[1])] assert_allclose(V1, V2, rtol=1e-11) def test_circstd_axis(self): x = np.array([[355,5,2,359,10,350], [351,7,4,352,9,349], [357,9,8,358,4,356]]) S1 = stats.circstd(x, high=360) S2 = stats.circstd(x.ravel(), high=360) assert_allclose(S1, S2, rtol=1e-11) S1 = stats.circstd(x, high=360, axis=1) S2 = [stats.circstd(x[i], high=360) for i in range(x.shape[0])] assert_allclose(S1, S2, rtol=1e-11) S1 = stats.circstd(x, high=360, axis=0) S2 = [stats.circstd(x[:,i], high=360) for i in range(x.shape[1])] assert_allclose(S1, S2, rtol=1e-11) def test_circfuncs_array_like(self): x = [355,5,2,359,10,350] assert_allclose(stats.circmean(x, high=360), 0.167690146, rtol=1e-7) assert_allclose(stats.circvar(x, high=360), 42.51955609, rtol=1e-7) assert_allclose(stats.circstd(x, high=360), 6.520702116, rtol=1e-7) def test_empty(self): assert_(np.isnan(stats.circmean([]))) assert_(np.isnan(stats.circstd([]))) assert_(np.isnan(stats.circvar([]))) def test_accuracy_wilcoxon(): freq = [1, 4, 16, 15, 8, 4, 5, 1, 2] nums = range(-4, 5) x = np.concatenate([[u] * v for u, v in zip(nums, freq)]) y = np.zeros(x.size) T, p = stats.wilcoxon(x, y, "pratt") assert_allclose(T, 423) assert_allclose(p, 0.00197547303533107) T, p = stats.wilcoxon(x, y, "zsplit") assert_allclose(T, 441) assert_allclose(p, 0.0032145343172473055) T, p = stats.wilcoxon(x, y, "wilcox") assert_allclose(T, 327) assert_allclose(p, 0.00641346115861) # Test the 'correction' option, using values computed in R with: # > wilcox.test(x, y, paired=TRUE, exact=FALSE, correct={FALSE,TRUE}) x = np.array([120, 114, 181, 188, 180, 146, 121, 191, 132, 113, 127, 112]) y = np.array([133, 143, 119, 189, 112, 199, 198, 113, 115, 121, 142, 187]) T, p = stats.wilcoxon(x, y, correction=False) assert_equal(T, 34) assert_allclose(p, 0.6948866, rtol=1e-6) T, p = stats.wilcoxon(x, y, correction=True) assert_equal(T, 34) assert_allclose(p, 0.7240817, rtol=1e-6) def test_wilcoxon_tie(): # Regression test for gh-2391. # Corresponding R code is: # > result = wilcox.test(rep(0.1, 10), exact=FALSE, correct=FALSE) # > result$p.value # [1] 0.001565402 # > result = wilcox.test(rep(0.1, 10), exact=FALSE, correct=TRUE) # > result$p.value # [1] 0.001904195 stat, p = stats.wilcoxon([0.1] * 10) expected_p = 0.001565402 assert_equal(stat, 0) assert_allclose(p, expected_p, rtol=1e-6) stat, p = stats.wilcoxon([0.1] * 10, correction=True) expected_p = 0.001904195 assert_equal(stat, 0) assert_allclose(p, expected_p, rtol=1e-6) class TestMedianTest(TestCase): def test_bad_n_samples(self): # median_test requires at least two samples. assert_raises(ValueError, stats.median_test, [1, 2, 3]) def test_empty_sample(self): # Each sample must contain at least one value. assert_raises(ValueError, stats.median_test, [], [1, 2, 3]) def test_empty_when_ties_ignored(self): # The grand median is 1, and all values in the first argument are # equal to the grand median. With ties="ignore", those values are # ignored, which results in the first sample being (in effect) empty. # This should raise a ValueError. assert_raises(ValueError, stats.median_test, [1, 1, 1, 1], [2, 0, 1], [2, 0], ties="ignore") def test_empty_contingency_row(self): # The grand median is 1, and with the default ties="below", all the # values in the samples are counted as being below the grand median. # This would result a row of zeros in the contingency table, which is # an error. assert_raises(ValueError, stats.median_test, [1, 1, 1], [1, 1, 1]) # With ties="above", all the values are counted as above the # grand median. assert_raises(ValueError, stats.median_test, [1, 1, 1], [1, 1, 1], ties="above") def test_bad_ties(self): assert_raises(ValueError, stats.median_test, [1, 2, 3], [4, 5], ties="foo") def test_bad_keyword(self): assert_raises(TypeError, stats.median_test, [1, 2, 3], [4, 5], foo="foo") def test_simple(self): x = [1, 2, 3] y = [1, 2, 3] stat, p, med, tbl = stats.median_test(x, y) # The median is floating point, but this equality test should be safe. assert_equal(med, 2.0) assert_array_equal(tbl, [[1, 1], [2, 2]]) # The expected values of the contingency table equal the contingency table, # so the statistic should be 0 and the p-value should be 1. assert_equal(stat, 0) assert_equal(p, 1) def test_ties_options(self): # Test the contingency table calculation. x = [1, 2, 3, 4] y = [5, 6] z = [7, 8, 9] # grand median is 5. # Default 'ties' option is "below". stat, p, m, tbl = stats.median_test(x, y, z) assert_equal(m, 5) assert_equal(tbl, [[0, 1, 3], [4, 1, 0]]) stat, p, m, tbl = stats.median_test(x, y, z, ties="ignore") assert_equal(m, 5) assert_equal(tbl, [[0, 1, 3], [4, 0, 0]]) stat, p, m, tbl = stats.median_test(x, y, z, ties="above") assert_equal(m, 5) assert_equal(tbl, [[0, 2, 3], [4, 0, 0]]) def test_basic(self): # median_test calls chi2_contingency to compute the test statistic # and p-value. Make sure it hasn't screwed up the call... x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8] stat, p, m, tbl = stats.median_test(x, y) assert_equal(m, 4) assert_equal(tbl, [[1, 2], [4, 2]]) exp_stat, exp_p, dof, e = stats.chi2_contingency(tbl) assert_allclose(stat, exp_stat) assert_allclose(p, exp_p) stat, p, m, tbl = stats.median_test(x, y, lambda_=0) assert_equal(m, 4) assert_equal(tbl, [[1, 2], [4, 2]]) exp_stat, exp_p, dof, e = stats.chi2_contingency(tbl, lambda_=0) assert_allclose(stat, exp_stat) assert_allclose(p, exp_p) stat, p, m, tbl = stats.median_test(x, y, correction=False) assert_equal(m, 4) assert_equal(tbl, [[1, 2], [4, 2]]) exp_stat, exp_p, dof, e = stats.chi2_contingency(tbl, correction=False) assert_allclose(stat, exp_stat) assert_allclose(p, exp_p) if __name__ == "__main__": run_module_suite()
mit
mjgrav2001/scikit-learn
sklearn/decomposition/base.py
313
5647
"""Principal Component Analysis Base Classes""" # Author: Alexandre Gramfort <alexandre.gramfort@inria.fr> # Olivier Grisel <olivier.grisel@ensta.org> # Mathieu Blondel <mathieu@mblondel.org> # Denis A. Engemann <d.engemann@fz-juelich.de> # Kyle Kastner <kastnerkyle@gmail.com> # # License: BSD 3 clause import numpy as np from scipy import linalg from ..base import BaseEstimator, TransformerMixin from ..utils import check_array from ..utils.extmath import fast_dot from ..utils.validation import check_is_fitted from ..externals import six from abc import ABCMeta, abstractmethod class _BasePCA(six.with_metaclass(ABCMeta, BaseEstimator, TransformerMixin)): """Base class for PCA methods. Warning: This class should not be used directly. Use derived classes instead. """ def get_covariance(self): """Compute data covariance with the generative model. ``cov = components_.T * S**2 * components_ + sigma2 * eye(n_features)`` where S**2 contains the explained variances, and sigma2 contains the noise variances. Returns ------- cov : array, shape=(n_features, n_features) Estimated covariance of data. """ components_ = self.components_ exp_var = self.explained_variance_ if self.whiten: components_ = components_ * np.sqrt(exp_var[:, np.newaxis]) exp_var_diff = np.maximum(exp_var - self.noise_variance_, 0.) cov = np.dot(components_.T * exp_var_diff, components_) cov.flat[::len(cov) + 1] += self.noise_variance_ # modify diag inplace return cov def get_precision(self): """Compute data precision matrix with the generative model. Equals the inverse of the covariance but computed with the matrix inversion lemma for efficiency. Returns ------- precision : array, shape=(n_features, n_features) Estimated precision of data. """ n_features = self.components_.shape[1] # handle corner cases first if self.n_components_ == 0: return np.eye(n_features) / self.noise_variance_ if self.n_components_ == n_features: return linalg.inv(self.get_covariance()) # Get precision using matrix inversion lemma components_ = self.components_ exp_var = self.explained_variance_ if self.whiten: components_ = components_ * np.sqrt(exp_var[:, np.newaxis]) exp_var_diff = np.maximum(exp_var - self.noise_variance_, 0.) precision = np.dot(components_, components_.T) / self.noise_variance_ precision.flat[::len(precision) + 1] += 1. / exp_var_diff precision = np.dot(components_.T, np.dot(linalg.inv(precision), components_)) precision /= -(self.noise_variance_ ** 2) precision.flat[::len(precision) + 1] += 1. / self.noise_variance_ return precision @abstractmethod def fit(X, y=None): """Placeholder for fit. Subclasses should implement this method! Fit the model with X. Parameters ---------- X : array-like, shape (n_samples, n_features) Training data, where n_samples is the number of samples and n_features is the number of features. Returns ------- self : object Returns the instance itself. """ def transform(self, X, y=None): """Apply dimensionality reduction to X. X is projected on the first principal components previously extracted from a training set. Parameters ---------- X : array-like, shape (n_samples, n_features) New data, where n_samples is the number of samples and n_features is the number of features. Returns ------- X_new : array-like, shape (n_samples, n_components) Examples -------- >>> import numpy as np >>> from sklearn.decomposition import IncrementalPCA >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) >>> ipca = IncrementalPCA(n_components=2, batch_size=3) >>> ipca.fit(X) IncrementalPCA(batch_size=3, copy=True, n_components=2, whiten=False) >>> ipca.transform(X) # doctest: +SKIP """ check_is_fitted(self, ['mean_', 'components_'], all_or_any=all) X = check_array(X) if self.mean_ is not None: X = X - self.mean_ X_transformed = fast_dot(X, self.components_.T) if self.whiten: X_transformed /= np.sqrt(self.explained_variance_) return X_transformed def inverse_transform(self, X, y=None): """Transform data back to its original space. In other words, return an input X_original whose transform would be X. Parameters ---------- X : array-like, shape (n_samples, n_components) New data, where n_samples is the number of samples and n_components is the number of components. Returns ------- X_original array-like, shape (n_samples, n_features) Notes ----- If whitening is enabled, inverse_transform will compute the exact inverse operation, which includes reversing whitening. """ if self.whiten: return fast_dot(X, np.sqrt(self.explained_variance_[:, np.newaxis]) * self.components_) + self.mean_ else: return fast_dot(X, self.components_) + self.mean_
bsd-3-clause
markhamstra/spark
examples/src/main/python/sql/arrow.py
13
3997
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """ A simple example demonstrating Arrow in Spark. Run with: ./bin/spark-submit examples/src/main/python/sql/arrow.py """ from __future__ import print_function from pyspark.sql import SparkSession from pyspark.sql.utils import require_minimum_pandas_version, require_minimum_pyarrow_version require_minimum_pandas_version() require_minimum_pyarrow_version() def dataframe_with_arrow_example(spark): # $example on:dataframe_with_arrow$ import numpy as np import pandas as pd # Enable Arrow-based columnar data transfers spark.conf.set("spark.sql.execution.arrow.enabled", "true") # Generate a Pandas DataFrame pdf = pd.DataFrame(np.random.rand(100, 3)) # Create a Spark DataFrame from a Pandas DataFrame using Arrow df = spark.createDataFrame(pdf) # Convert the Spark DataFrame back to a Pandas DataFrame using Arrow result_pdf = df.select("*").toPandas() # $example off:dataframe_with_arrow$ print("Pandas DataFrame result statistics:\n%s\n" % str(result_pdf.describe())) def scalar_pandas_udf_example(spark): # $example on:scalar_pandas_udf$ import pandas as pd from pyspark.sql.functions import col, pandas_udf from pyspark.sql.types import LongType # Declare the function and create the UDF def multiply_func(a, b): return a * b multiply = pandas_udf(multiply_func, returnType=LongType()) # The function for a pandas_udf should be able to execute with local Pandas data x = pd.Series([1, 2, 3]) print(multiply_func(x, x)) # 0 1 # 1 4 # 2 9 # dtype: int64 # Create a Spark DataFrame, 'spark' is an existing SparkSession df = spark.createDataFrame(pd.DataFrame(x, columns=["x"])) # Execute function as a Spark vectorized UDF df.select(multiply(col("x"), col("x"))).show() # +-------------------+ # |multiply_func(x, x)| # +-------------------+ # | 1| # | 4| # | 9| # +-------------------+ # $example off:scalar_pandas_udf$ def grouped_map_pandas_udf_example(spark): # $example on:grouped_map_pandas_udf$ from pyspark.sql.functions import pandas_udf, PandasUDFType df = spark.createDataFrame( [(1, 1.0), (1, 2.0), (2, 3.0), (2, 5.0), (2, 10.0)], ("id", "v")) @pandas_udf("id long, v double", PandasUDFType.GROUPED_MAP) def substract_mean(pdf): # pdf is a pandas.DataFrame v = pdf.v return pdf.assign(v=v - v.mean()) df.groupby("id").apply(substract_mean).show() # +---+----+ # | id| v| # +---+----+ # | 1|-0.5| # | 1| 0.5| # | 2|-3.0| # | 2|-1.0| # | 2| 4.0| # +---+----+ # $example off:grouped_map_pandas_udf$ if __name__ == "__main__": spark = SparkSession \ .builder \ .appName("Python Arrow-in-Spark example") \ .getOrCreate() print("Running Pandas to/from conversion example") dataframe_with_arrow_example(spark) print("Running pandas_udf scalar example") scalar_pandas_udf_example(spark) print("Running pandas_udf grouped map example") grouped_map_pandas_udf_example(spark) spark.stop()
apache-2.0
elkingtonmcb/scikit-learn
sklearn/setup.py
225
2856
import os from os.path import join import warnings def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration from numpy.distutils.system_info import get_info, BlasNotFoundError import numpy libraries = [] if os.name == 'posix': libraries.append('m') config = Configuration('sklearn', parent_package, top_path) config.add_subpackage('__check_build') config.add_subpackage('svm') config.add_subpackage('datasets') config.add_subpackage('datasets/tests') config.add_subpackage('feature_extraction') config.add_subpackage('feature_extraction/tests') config.add_subpackage('cluster') config.add_subpackage('cluster/tests') config.add_subpackage('covariance') config.add_subpackage('covariance/tests') config.add_subpackage('cross_decomposition') config.add_subpackage('decomposition') config.add_subpackage('decomposition/tests') config.add_subpackage("ensemble") config.add_subpackage("ensemble/tests") config.add_subpackage('feature_selection') config.add_subpackage('feature_selection/tests') config.add_subpackage('utils') config.add_subpackage('utils/tests') config.add_subpackage('externals') config.add_subpackage('mixture') config.add_subpackage('mixture/tests') config.add_subpackage('gaussian_process') config.add_subpackage('gaussian_process/tests') config.add_subpackage('neighbors') config.add_subpackage('neural_network') config.add_subpackage('preprocessing') config.add_subpackage('manifold') config.add_subpackage('metrics') config.add_subpackage('semi_supervised') config.add_subpackage("tree") config.add_subpackage("tree/tests") config.add_subpackage('metrics/tests') config.add_subpackage('metrics/cluster') config.add_subpackage('metrics/cluster/tests') # add cython extension module for isotonic regression config.add_extension( '_isotonic', sources=['_isotonic.c'], include_dirs=[numpy.get_include()], libraries=libraries, ) # some libs needs cblas, fortran-compiled BLAS will not be sufficient blas_info = get_info('blas_opt', 0) if (not blas_info) or ( ('NO_ATLAS_INFO', 1) in blas_info.get('define_macros', [])): config.add_library('cblas', sources=[join('src', 'cblas', '*.c')]) warnings.warn(BlasNotFoundError.__doc__) # the following packages depend on cblas, so they have to be build # after the above. config.add_subpackage('linear_model') config.add_subpackage('utils') # add the test directory config.add_subpackage('tests') return config if __name__ == '__main__': from numpy.distutils.core import setup setup(**configuration(top_path='').todict())
bsd-3-clause
Vimos/scikit-learn
sklearn/utils/testing.py
29
25405
"""Testing utilities.""" # Copyright (c) 2011, 2012 # Authors: Pietro Berkes, # Andreas Muller # Mathieu Blondel # Olivier Grisel # Arnaud Joly # Denis Engemann # Giorgio Patrini # Thierry Guillemot # License: BSD 3 clause import os import inspect import pkgutil import warnings import sys import struct import scipy as sp import scipy.io from functools import wraps from operator import itemgetter try: # Python 2 from urllib2 import urlopen from urllib2 import HTTPError except ImportError: # Python 3+ from urllib.request import urlopen from urllib.error import HTTPError import tempfile import shutil import os.path as op import atexit import unittest # WindowsError only exist on Windows try: WindowsError except NameError: WindowsError = None import sklearn from sklearn.base import BaseEstimator from sklearn.externals import joblib from nose.tools import raises from nose import with_setup from numpy.testing import assert_almost_equal from numpy.testing import assert_array_equal from numpy.testing import assert_array_almost_equal from numpy.testing import assert_array_less from numpy.testing import assert_approx_equal import numpy as np from sklearn.base import (ClassifierMixin, RegressorMixin, TransformerMixin, ClusterMixin) from sklearn.cluster import DBSCAN __all__ = ["assert_equal", "assert_not_equal", "assert_raises", "assert_raises_regexp", "raises", "with_setup", "assert_true", "assert_false", "assert_almost_equal", "assert_array_equal", "assert_array_almost_equal", "assert_array_less", "assert_less", "assert_less_equal", "assert_greater", "assert_greater_equal", "assert_approx_equal", "SkipTest"] _dummy = unittest.TestCase('__init__') assert_equal = _dummy.assertEqual assert_not_equal = _dummy.assertNotEqual assert_true = _dummy.assertTrue assert_false = _dummy.assertFalse assert_raises = _dummy.assertRaises SkipTest = unittest.case.SkipTest assert_dict_equal = _dummy.assertDictEqual assert_in = _dummy.assertIn assert_not_in = _dummy.assertNotIn assert_less = _dummy.assertLess assert_greater = _dummy.assertGreater assert_less_equal = _dummy.assertLessEqual assert_greater_equal = _dummy.assertGreaterEqual try: assert_raises_regex = _dummy.assertRaisesRegex except AttributeError: # Python 2.7 assert_raises_regex = _dummy.assertRaisesRegexp # assert_raises_regexp is deprecated in Python 3.4 in favor of # assert_raises_regex but lets keep the backward compat in scikit-learn with # the old name for now assert_raises_regexp = assert_raises_regex def assert_warns(warning_class, func, *args, **kw): """Test that a certain warning occurs. Parameters ---------- warning_class : the warning class The class to test for, e.g. UserWarning. func : callable Calable object to trigger warnings. *args : the positional arguments to `func`. **kw : the keyword arguments to `func` Returns ------- result : the return value of `func` """ # very important to avoid uncontrolled state propagation clean_warning_registry() with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. result = func(*args, **kw) if hasattr(np, 'VisibleDeprecationWarning'): # Filter out numpy-specific warnings in numpy >= 1.9 w = [e for e in w if e.category is not np.VisibleDeprecationWarning] # Verify some things if not len(w) > 0: raise AssertionError("No warning raised when calling %s" % func.__name__) found = any(warning.category is warning_class for warning in w) if not found: raise AssertionError("%s did not give warning: %s( is %s)" % (func.__name__, warning_class, w)) return result def assert_warns_message(warning_class, message, func, *args, **kw): # very important to avoid uncontrolled state propagation """Test that a certain warning occurs and with a certain message. Parameters ---------- warning_class : the warning class The class to test for, e.g. UserWarning. message : str | callable The entire message or a substring to test for. If callable, it takes a string as argument and will trigger an assertion error if it returns `False`. func : callable Calable object to trigger warnings. *args : the positional arguments to `func`. **kw : the keyword arguments to `func`. Returns ------- result : the return value of `func` """ clean_warning_registry() with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") if hasattr(np, 'VisibleDeprecationWarning'): # Let's not catch the numpy internal DeprecationWarnings warnings.simplefilter('ignore', np.VisibleDeprecationWarning) # Trigger a warning. result = func(*args, **kw) # Verify some things if not len(w) > 0: raise AssertionError("No warning raised when calling %s" % func.__name__) found = [issubclass(warning.category, warning_class) for warning in w] if not any(found): raise AssertionError("No warning raised for %s with class " "%s" % (func.__name__, warning_class)) message_found = False # Checks the message of all warnings belong to warning_class for index in [i for i, x in enumerate(found) if x]: # substring will match, the entire message with typo won't msg = w[index].message # For Python 3 compatibility msg = str(msg.args[0] if hasattr(msg, 'args') else msg) if callable(message): # add support for certain tests check_in_message = message else: check_in_message = lambda msg: message in msg if check_in_message(msg): message_found = True break if not message_found: raise AssertionError("Did not receive the message you expected " "('%s') for <%s>, got: '%s'" % (message, func.__name__, msg)) return result # To remove when we support numpy 1.7 def assert_no_warnings(func, *args, **kw): # very important to avoid uncontrolled state propagation clean_warning_registry() with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') result = func(*args, **kw) if hasattr(np, 'VisibleDeprecationWarning'): # Filter out numpy-specific warnings in numpy >= 1.9 w = [e for e in w if e.category is not np.VisibleDeprecationWarning] if len(w) > 0: raise AssertionError("Got warnings when calling %s: [%s]" % (func.__name__, ', '.join(str(warning) for warning in w))) return result def ignore_warnings(obj=None, category=Warning): """Context manager and decorator to ignore warnings. Note. Using this (in both variants) will clear all warnings from all python modules loaded. In case you need to test cross-module-warning-logging this is not your tool of choice. Parameters ---------- category : warning class, defaults to Warning. The category to filter. If Warning, all categories will be muted. Examples -------- >>> with ignore_warnings(): ... warnings.warn('buhuhuhu') >>> def nasty_warn(): ... warnings.warn('buhuhuhu') ... print(42) >>> ignore_warnings(nasty_warn)() 42 """ if callable(obj): return _IgnoreWarnings(category=category)(obj) else: return _IgnoreWarnings(category=category) class _IgnoreWarnings(object): """Improved and simplified Python warnings context manager and decorator. This class allows to ignore the warnings raise by a function. Copied from Python 2.7.5 and modified as required. Parameters ---------- category : tuple of warning class, defaut to Warning The category to filter. By default, all the categories will be muted. """ def __init__(self, category): self._record = True self._module = sys.modules['warnings'] self._entered = False self.log = [] self.category = category def __call__(self, fn): """Decorator to catch and hide warnings without visual nesting.""" @wraps(fn) def wrapper(*args, **kwargs): # very important to avoid uncontrolled state propagation clean_warning_registry() with warnings.catch_warnings(): warnings.simplefilter("ignore", self.category) return fn(*args, **kwargs) return wrapper def __repr__(self): args = [] if self._record: args.append("record=True") if self._module is not sys.modules['warnings']: args.append("module=%r" % self._module) name = type(self).__name__ return "%s(%s)" % (name, ", ".join(args)) def __enter__(self): clean_warning_registry() # be safe and not propagate state + chaos warnings.simplefilter("ignore", self.category) if self._entered: raise RuntimeError("Cannot enter %r twice" % self) self._entered = True self._filters = self._module.filters self._module.filters = self._filters[:] self._showwarning = self._module.showwarning def __exit__(self, *exc_info): if not self._entered: raise RuntimeError("Cannot exit %r without entering first" % self) self._module.filters = self._filters self._module.showwarning = self._showwarning self.log[:] = [] clean_warning_registry() # be safe and not propagate state + chaos assert_less = _dummy.assertLess assert_greater = _dummy.assertGreater def _assert_allclose(actual, desired, rtol=1e-7, atol=0, err_msg='', verbose=True): actual, desired = np.asanyarray(actual), np.asanyarray(desired) if np.allclose(actual, desired, rtol=rtol, atol=atol): return msg = ('Array not equal to tolerance rtol=%g, atol=%g: ' 'actual %s, desired %s') % (rtol, atol, actual, desired) raise AssertionError(msg) if hasattr(np.testing, 'assert_allclose'): assert_allclose = np.testing.assert_allclose else: assert_allclose = _assert_allclose def assert_raise_message(exceptions, message, function, *args, **kwargs): """Helper function to test error messages in exceptions. Parameters ---------- exceptions : exception or tuple of exception Name of the estimator function : callable Calable object to raise error *args : the positional arguments to `function`. **kw : the keyword arguments to `function` """ try: function(*args, **kwargs) except exceptions as e: error_message = str(e) if message not in error_message: raise AssertionError("Error message does not include the expected" " string: %r. Observed error message: %r" % (message, error_message)) else: # concatenate exception names if isinstance(exceptions, tuple): names = " or ".join(e.__name__ for e in exceptions) else: names = exceptions.__name__ raise AssertionError("%s not raised by %s" % (names, function.__name__)) def fake_mldata(columns_dict, dataname, matfile, ordering=None): """Create a fake mldata data set. Parameters ---------- columns_dict : dict, keys=str, values=ndarray Contains data as columns_dict[column_name] = array of data. dataname : string Name of data set. matfile : string or file object The file name string or the file-like object of the output file. ordering : list, default None List of column_names, determines the ordering in the data set. Notes ----- This function transposes all arrays, while fetch_mldata only transposes 'data', keep that into account in the tests. """ datasets = dict(columns_dict) # transpose all variables for name in datasets: datasets[name] = datasets[name].T if ordering is None: ordering = sorted(list(datasets.keys())) # NOTE: setting up this array is tricky, because of the way Matlab # re-packages 1D arrays datasets['mldata_descr_ordering'] = sp.empty((1, len(ordering)), dtype='object') for i, name in enumerate(ordering): datasets['mldata_descr_ordering'][0, i] = name scipy.io.savemat(matfile, datasets, oned_as='column') class mock_mldata_urlopen(object): def __init__(self, mock_datasets): """Object that mocks the urlopen function to fake requests to mldata. `mock_datasets` is a dictionary of {dataset_name: data_dict}, or {dataset_name: (data_dict, ordering). `data_dict` itself is a dictionary of {column_name: data_array}, and `ordering` is a list of column_names to determine the ordering in the data set (see `fake_mldata` for details). When requesting a dataset with a name that is in mock_datasets, this object creates a fake dataset in a StringIO object and returns it. Otherwise, it raises an HTTPError. """ self.mock_datasets = mock_datasets def __call__(self, urlname): dataset_name = urlname.split('/')[-1] if dataset_name in self.mock_datasets: resource_name = '_' + dataset_name from io import BytesIO matfile = BytesIO() dataset = self.mock_datasets[dataset_name] ordering = None if isinstance(dataset, tuple): dataset, ordering = dataset fake_mldata(dataset, resource_name, matfile, ordering) matfile.seek(0) return matfile else: raise HTTPError(urlname, 404, dataset_name + " is not available", [], None) def install_mldata_mock(mock_datasets): # Lazy import to avoid mutually recursive imports from sklearn import datasets datasets.mldata.urlopen = mock_mldata_urlopen(mock_datasets) def uninstall_mldata_mock(): # Lazy import to avoid mutually recursive imports from sklearn import datasets datasets.mldata.urlopen = urlopen # Meta estimators need another estimator to be instantiated. META_ESTIMATORS = ["OneVsOneClassifier", "MultiOutputEstimator", "MultiOutputRegressor", "MultiOutputClassifier", "OutputCodeClassifier", "OneVsRestClassifier", "RFE", "RFECV", "BaseEnsemble"] # estimators that there is no way to default-construct sensibly OTHER = ["Pipeline", "FeatureUnion", "GridSearchCV", "RandomizedSearchCV", "SelectFromModel"] # some trange ones DONT_TEST = ['SparseCoder', 'EllipticEnvelope', 'DictVectorizer', 'LabelBinarizer', 'LabelEncoder', 'MultiLabelBinarizer', 'TfidfTransformer', 'TfidfVectorizer', 'IsotonicRegression', 'OneHotEncoder', 'RandomTreesEmbedding', 'FeatureHasher', 'DummyClassifier', 'DummyRegressor', 'TruncatedSVD', 'PolynomialFeatures', 'GaussianRandomProjectionHash', 'HashingVectorizer', 'CheckingClassifier', 'PatchExtractor', 'CountVectorizer', # GradientBoosting base estimators, maybe should # exclude them in another way 'ZeroEstimator', 'ScaledLogOddsEstimator', 'QuantileEstimator', 'MeanEstimator', 'LogOddsEstimator', 'PriorProbabilityEstimator', '_SigmoidCalibration', 'VotingClassifier'] def all_estimators(include_meta_estimators=False, include_other=False, type_filter=None, include_dont_test=False): """Get a list of all estimators from sklearn. This function crawls the module and gets all classes that inherit from BaseEstimator. Classes that are defined in test-modules are not included. By default meta_estimators such as GridSearchCV are also not included. Parameters ---------- include_meta_estimators : boolean, default=False Whether to include meta-estimators that can be constructed using an estimator as their first argument. These are currently BaseEnsemble, OneVsOneClassifier, OutputCodeClassifier, OneVsRestClassifier, RFE, RFECV. include_other : boolean, default=False Wether to include meta-estimators that are somehow special and can not be default-constructed sensibly. These are currently Pipeline, FeatureUnion and GridSearchCV include_dont_test : boolean, default=False Whether to include "special" label estimator or test processors. type_filter : string, list of string, or None, default=None Which kind of estimators should be returned. If None, no filter is applied and all estimators are returned. Possible values are 'classifier', 'regressor', 'cluster' and 'transformer' to get estimators only of these specific types, or a list of these to get the estimators that fit at least one of the types. Returns ------- estimators : list of tuples List of (name, class), where ``name`` is the class name as string and ``class`` is the actuall type of the class. """ def is_abstract(c): if not(hasattr(c, '__abstractmethods__')): return False if not len(c.__abstractmethods__): return False return True all_classes = [] # get parent folder path = sklearn.__path__ for importer, modname, ispkg in pkgutil.walk_packages( path=path, prefix='sklearn.', onerror=lambda x: None): if (".tests." in modname): continue module = __import__(modname, fromlist="dummy") classes = inspect.getmembers(module, inspect.isclass) all_classes.extend(classes) all_classes = set(all_classes) estimators = [c for c in all_classes if (issubclass(c[1], BaseEstimator) and c[0] != 'BaseEstimator')] # get rid of abstract base classes estimators = [c for c in estimators if not is_abstract(c[1])] if not include_dont_test: estimators = [c for c in estimators if not c[0] in DONT_TEST] if not include_other: estimators = [c for c in estimators if not c[0] in OTHER] # possibly get rid of meta estimators if not include_meta_estimators: estimators = [c for c in estimators if not c[0] in META_ESTIMATORS] if type_filter is not None: if not isinstance(type_filter, list): type_filter = [type_filter] else: type_filter = list(type_filter) # copy filtered_estimators = [] filters = {'classifier': ClassifierMixin, 'regressor': RegressorMixin, 'transformer': TransformerMixin, 'cluster': ClusterMixin} for name, mixin in filters.items(): if name in type_filter: type_filter.remove(name) filtered_estimators.extend([est for est in estimators if issubclass(est[1], mixin)]) estimators = filtered_estimators if type_filter: raise ValueError("Parameter type_filter must be 'classifier', " "'regressor', 'transformer', 'cluster' or " "None, got" " %s." % repr(type_filter)) # drop duplicates, sort for reproducibility # itemgetter is used to ensure the sort does not extend to the 2nd item of # the tuple return sorted(set(estimators), key=itemgetter(0)) def set_random_state(estimator, random_state=0): """Set random state of an estimator if it has the `random_state` param. Classes for whom random_state is deprecated are ignored. Currently DBSCAN is one such class. """ if isinstance(estimator, DBSCAN): return if "random_state" in estimator.get_params(): estimator.set_params(random_state=random_state) def if_matplotlib(func): """Test decorator that skips test if matplotlib not installed.""" @wraps(func) def run_test(*args, **kwargs): try: import matplotlib matplotlib.use('Agg', warn=False) # this fails if no $DISPLAY specified import matplotlib.pyplot as plt plt.figure() except ImportError: raise SkipTest('Matplotlib not available.') else: return func(*args, **kwargs) return run_test def skip_if_32bit(func): """Test decorator that skips tests on 32bit platforms.""" @wraps(func) def run_test(*args, **kwargs): bits = 8 * struct.calcsize("P") if bits == 32: raise SkipTest('Test skipped on 32bit platforms.') else: return func(*args, **kwargs) return run_test def if_safe_multiprocessing_with_blas(func): """Decorator for tests involving both BLAS calls and multiprocessing. Under POSIX (e.g. Linux or OSX), using multiprocessing in conjunction with some implementation of BLAS (or other libraries that manage an internal posix thread pool) can cause a crash or a freeze of the Python process. In practice all known packaged distributions (from Linux distros or Anaconda) of BLAS under Linux seems to be safe. So we this problem seems to only impact OSX users. This wrapper makes it possible to skip tests that can possibly cause this crash under OS X with. Under Python 3.4+ it is possible to use the `forkserver` start method for multiprocessing to avoid this issue. However it can cause pickling errors on interactively defined functions. It therefore not enabled by default. """ @wraps(func) def run_test(*args, **kwargs): if sys.platform == 'darwin': raise SkipTest( "Possible multi-process bug with some BLAS") return func(*args, **kwargs) return run_test def clean_warning_registry(): """Safe way to reset warnings.""" warnings.resetwarnings() reg = "__warningregistry__" for mod_name, mod in list(sys.modules.items()): if 'six.moves' in mod_name: continue if hasattr(mod, reg): getattr(mod, reg).clear() def check_skip_network(): if int(os.environ.get('SKLEARN_SKIP_NETWORK_TESTS', 0)): raise SkipTest("Text tutorial requires large dataset download") def check_skip_travis(): """Skip test if being run on Travis.""" if os.environ.get('TRAVIS') == "true": raise SkipTest("This test needs to be skipped on Travis") def _delete_folder(folder_path, warn=False): """Utility function to cleanup a temporary folder if still existing. Copy from joblib.pool (for independence). """ try: if os.path.exists(folder_path): # This can fail under windows, # but will succeed when called by atexit shutil.rmtree(folder_path) except WindowsError: if warn: warnings.warn("Could not delete temporary folder %s" % folder_path) class TempMemmap(object): def __init__(self, data, mmap_mode='r'): self.temp_folder = tempfile.mkdtemp(prefix='sklearn_testing_') self.mmap_mode = mmap_mode self.data = data def __enter__(self): fpath = op.join(self.temp_folder, 'data.pkl') joblib.dump(self.data, fpath) data_read_only = joblib.load(fpath, mmap_mode=self.mmap_mode) atexit.register(lambda: _delete_folder(self.temp_folder, warn=True)) return data_read_only def __exit__(self, exc_type, exc_val, exc_tb): _delete_folder(self.temp_folder) with_network = with_setup(check_skip_network) with_travis = with_setup(check_skip_travis) class _named_check(object): """Wraps a check to show a useful description Parameters ---------- check : function Must have ``__name__`` and ``__call__`` arg_text : str A summary of arguments to the check """ # Setting the description on the function itself can give incorrect results # in failing tests def __init__(self, check, arg_text): self.check = check self.description = ("{0[1]}.{0[3]}:{1.__name__}({2})".format( inspect.stack()[1], check, arg_text)) def __call__(self, *args, **kwargs): return self.check(*args, **kwargs)
bsd-3-clause
neale/CS-program
434-MachineLearning/final_project/linearClassifier/sklearn/datasets/mlcomp.py
289
3855
# Copyright (c) 2010 Olivier Grisel <olivier.grisel@ensta.org> # License: BSD 3 clause """Glue code to load http://mlcomp.org data as a scikit.learn dataset""" import os import numbers from sklearn.datasets.base import load_files def _load_document_classification(dataset_path, metadata, set_=None, **kwargs): if set_ is not None: dataset_path = os.path.join(dataset_path, set_) return load_files(dataset_path, metadata.get('description'), **kwargs) LOADERS = { 'DocumentClassification': _load_document_classification, # TODO: implement the remaining domain formats } def load_mlcomp(name_or_id, set_="raw", mlcomp_root=None, **kwargs): """Load a datasets as downloaded from http://mlcomp.org Parameters ---------- name_or_id : the integer id or the string name metadata of the MLComp dataset to load set_ : select the portion to load: 'train', 'test' or 'raw' mlcomp_root : the filesystem path to the root folder where MLComp datasets are stored, if mlcomp_root is None, the MLCOMP_DATASETS_HOME environment variable is looked up instead. **kwargs : domain specific kwargs to be passed to the dataset loader. Read more in the :ref:`User Guide <datasets>`. Returns ------- data : Bunch Dictionary-like object, the interesting attributes are: 'filenames', the files holding the raw to learn, 'target', the classification labels (integer index), 'target_names', the meaning of the labels, and 'DESCR', the full description of the dataset. Note on the lookup process: depending on the type of name_or_id, will choose between integer id lookup or metadata name lookup by looking at the unzipped archives and metadata file. TODO: implement zip dataset loading too """ if mlcomp_root is None: try: mlcomp_root = os.environ['MLCOMP_DATASETS_HOME'] except KeyError: raise ValueError("MLCOMP_DATASETS_HOME env variable is undefined") mlcomp_root = os.path.expanduser(mlcomp_root) mlcomp_root = os.path.abspath(mlcomp_root) mlcomp_root = os.path.normpath(mlcomp_root) if not os.path.exists(mlcomp_root): raise ValueError("Could not find folder: " + mlcomp_root) # dataset lookup if isinstance(name_or_id, numbers.Integral): # id lookup dataset_path = os.path.join(mlcomp_root, str(name_or_id)) else: # assume name based lookup dataset_path = None expected_name_line = "name: " + name_or_id for dataset in os.listdir(mlcomp_root): metadata_file = os.path.join(mlcomp_root, dataset, 'metadata') if not os.path.exists(metadata_file): continue with open(metadata_file) as f: for line in f: if line.strip() == expected_name_line: dataset_path = os.path.join(mlcomp_root, dataset) break if dataset_path is None: raise ValueError("Could not find dataset with metadata line: " + expected_name_line) # loading the dataset metadata metadata = dict() metadata_file = os.path.join(dataset_path, 'metadata') if not os.path.exists(metadata_file): raise ValueError(dataset_path + ' is not a valid MLComp dataset') with open(metadata_file) as f: for line in f: if ":" in line: key, value = line.split(":", 1) metadata[key.strip()] = value.strip() format = metadata.get('format', 'unknow') loader = LOADERS.get(format) if loader is None: raise ValueError("No loader implemented for format: " + format) return loader(dataset_path, metadata, set_=set_, **kwargs)
unlicense
antoinecarme/pyaf
tests/perf/test_ozone_debug_perf.py
1
1566
import pandas as pd import numpy as np # from memory_profiler import profile # from memprof import * import pyaf.ForecastEngine as autof import pyaf.Bench.TS_datasets as tsds #get_ipython().magic('matplotlib inline') # @memprof def test_ozone_debug_perf(): b1 = tsds.load_ozone() df = b1.mPastData # df.tail(10) # df[:-10].tail() # df[:-10:-1] # df.describe() lEngine = autof.cForecastEngine() lEngine H = b1.mHorizon; lEngine.mOptions.mDebugPerformance = True; lEngine.mOptions.mEnableCycles = False; lEngine.mOptions.mEnableTimeBasedTrends = False; lEngine.mOptions.mEnableARModels = False; lEngine.train(df , b1.mTimeVar , b1.mSignalVar, H); lEngine.getModelInfo(); print(lEngine.mSignalDecomposition.mTrPerfDetails.head()); lEngine.mSignalDecomposition.mBestModel.mTimeInfo.mResolution lEngine.standardPlots("outputs/my_ozone"); dfapp_in = df.copy(); dfapp_in.tail() dfapp_out = lEngine.forecast(dfapp_in, H); #dfapp_out.to_csv("outputs/ozone_apply_out.csv") dfapp_out.tail(2 * H) print("Forecast Columns " , dfapp_out.columns); Forecast_DF = dfapp_out[[b1.mTimeVar , b1.mSignalVar, b1.mSignalVar + '_Forecast']] print(Forecast_DF.info()) print("Forecasts\n" , Forecast_DF.tail(H).values); print("\n\n<ModelInfo>") print(lEngine.to_json()); print("</ModelInfo>\n\n") print("\n\n<Forecast>") print(Forecast_DF.tail(2*H).to_json(date_format='iso')) print("</Forecast>\n\n") test_ozone_debug_perf();
bsd-3-clause
nkhuyu/seizure-prediction
ensemble.py
2
9703
#!/usr/bin/env python2.7 from multiprocessing import Pool import sys import numpy as np from sklearn.metrics import roc_auc_score from seizure_prediction.classifiers import make_svm, make_simple_lr, make_lr from seizure_prediction.feature_selection import generate_feature_masks from seizure_prediction.fft_bins import * from seizure_prediction.pipeline import Pipeline, FeatureConcatPipeline, InputSource from seizure_prediction.scores import get_score_summary, print_results from seizure_prediction.tasks import make_csv_for_target_predictions, write_submission_file, \ cross_validation_score, check_training_data_loaded, check_test_data_loaded, make_submission_predictions from seizure_prediction.transforms import Windower, Correlation, FreqCorrelation, FFT, \ Magnitude, PIBSpectralEntropy, Log10, FreqBinning, FlattenChannels, Preprocess, HFD, PFD, Hurst from seizure_prediction.settings import load_settings from main import run_prepare_data_for_cross_validation def run_make_submission(settings, targets_and_pipelines, split_ratio): pool = Pool(settings.N_jobs) for i, (target, pipeline, feature_masks, classifier, classifier_name) in enumerate(targets_and_pipelines): for j, feature_mask in enumerate(feature_masks): progress_str = 'T=%d/%d M=%d/%d' % (i+1, len(targets_and_pipelines), j+1, len(feature_masks)) pool.apply_async(make_submission_predictions, [settings, target, pipeline, classifier, classifier_name], {'feature_mask': feature_mask, 'progress_str': progress_str, 'quiet': True}) pool.close() pool.join() guesses = ['clip,preictal'] num_masks = None classifier_names = [] for target, pipeline, feature_masks, classifier, classifier_name in targets_and_pipelines: classifier_names.append(classifier_name) if num_masks is None: num_masks = len(feature_masks) else: assert num_masks == len(feature_masks) test_predictions = [] for feature_mask in feature_masks: data = make_submission_predictions(settings, target, pipeline, classifier, classifier_name, feature_mask=feature_mask) test_predictions.append(data.mean_predictions) predictions = np.mean(test_predictions, axis=0) guesses += make_csv_for_target_predictions(target, predictions) output = '\n'.join(guesses) write_submission_file(settings, output, 'ensemble n=%d split_ratio=%s' % (num_masks, split_ratio), None, str(classifier_names), targets_and_pipelines) def run_prepare_data(settings, targets, pipelines, train=True, test=False, quiet=False): for pipeline in pipelines: for target in targets: print 'Preparing data for', target if train: check_training_data_loaded(settings, target, pipeline, quiet=quiet) if test: check_test_data_loaded(settings, target, pipeline, quiet=quiet) def run_cross_validation(settings, targets, pipelines, mask_range, split_ratios, classifiers): pool = Pool(settings.N_jobs) for i, pipeline in enumerate(pipelines): for j, (classifier, classifier_name) in enumerate(classifiers): for k, target in enumerate(targets): pool.apply_async(cross_validation_score, [settings, target, pipeline, classifier, classifier_name], {'quiet': True}) for split_num, split_ratio in enumerate(split_ratios): masks = generate_feature_masks(settings, target, pipeline, np.max(mask_range), split_ratio, random_state=0, quiet=True) for mask_num, mask in enumerate(masks): progress_str = 'P=%d/%d C=%d/%d T=%d/%d S=%d/%d M=%d/%d' % (i+1, len(pipelines), j+1, len(classifiers), k+1, len(targets), split_num+1, len(split_ratios), mask_num+1, len(masks)) cross_validation_score(settings, target, pipeline, classifier, classifier_name, feature_mask=mask, quiet=True, return_data=False, pool=pool, progress_str=progress_str) pool.close() pool.join() print 'Finished cross validation mp' summaries = [] for p_num, pipeline in enumerate(pipelines): for classifier, classifier_name in classifiers: scores_full = [] scores_masked = [[[] for y in mask_range] for x in split_ratios] for i, target in enumerate(targets): run_prepare_data_for_cross_validation(settings, [target], [pipeline], quiet=True) data = cross_validation_score(settings, target, pipeline, classifier, classifier_name, pool=None, quiet=True) scores_full.append(data.mean_score) for split_index, split_ratio in enumerate(split_ratios): masks = generate_feature_masks(settings, target, pipeline, np.max(mask_range), split_ratio, random_state=0, quiet=True) for mask_index, num_masks in enumerate(mask_range): predictions = [] y_cvs = None for mask in masks[0:num_masks]: data = cross_validation_score(settings, target, pipeline, classifier, classifier_name, feature_mask=mask, pool=None, quiet=True) predictions.append(data.mean_predictions) if y_cvs is None: y_cvs = data.y_cvs else: for y_cv_1, y_cv_2 in zip(y_cvs, data.y_cvs): assert np.alltrue(y_cv_1 == y_cv_2) predictions = np.mean(predictions, axis=0) scores = [roc_auc_score(y_cv, p) for p, y_cv in zip(predictions, y_cvs)] score = np.mean(scores) scores_masked[split_index][mask_index].append(score) summary = get_score_summary('%s p=%d full' % (classifier_name, p_num), scores_full) summaries.append((summary, np.mean(scores_full))) for split_index, split_ratio in enumerate(split_ratios): for mask_index, num_masks in enumerate(mask_range): scores = scores_masked[split_index][mask_index] summary = get_score_summary('%s p=%d split_ratio=%s masks=%d' % (classifier_name, p_num, split_ratio, num_masks), scores) summaries.append((summary, np.mean(scores))) print summary print_results(summaries) def main(): settings = load_settings() pipelines = [ FeatureConcatPipeline( Pipeline(InputSource(), Preprocess(), Windower(75), Correlation('none')), Pipeline(InputSource(), Preprocess(), Windower(75), FreqCorrelation(1, None, 'none')), Pipeline(InputSource(Preprocess(), Windower(75), FFT(), Magnitude()), FreqBinning(winning_bins, 'mean'), Log10(), FlattenChannels()), Pipeline(InputSource(Preprocess(), Windower(75), FFT(), Magnitude()), PIBSpectralEntropy([0.25, 1, 1.75, 2.5, 3.25, 4, 5, 8.5, 12, 15.5, 19.5, 24])), Pipeline(InputSource(Preprocess(), Windower(75), FFT(), Magnitude()), PIBSpectralEntropy([0.25, 2, 3.5, 6, 15, 24])), Pipeline(InputSource(Preprocess(), Windower(75), FFT(), Magnitude()), PIBSpectralEntropy([0.25, 2, 3.5, 6, 15])), Pipeline(InputSource(Preprocess(), Windower(75), FFT(), Magnitude()), PIBSpectralEntropy([0.25, 2, 3.5])), Pipeline(InputSource(Preprocess(), Windower(75), FFT(), Magnitude()), PIBSpectralEntropy([6, 15, 24])), Pipeline(InputSource(Preprocess(), Windower(75), FFT(), Magnitude()), PIBSpectralEntropy([2, 3.5, 6])), Pipeline(InputSource(Preprocess(), Windower(75), FFT(), Magnitude()), PIBSpectralEntropy([3.5, 6, 15])), Pipeline(InputSource(), Preprocess(), Windower(75), HFD(2)), Pipeline(InputSource(), Preprocess(), Windower(75), PFD()), Pipeline(InputSource(), Preprocess(), Windower(75), Hurst()), ), ] targets = [ 'Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2' ] classifiers = [ make_svm(gamma=0.0079, C=2.7), make_svm(gamma=0.0068, C=2.0), make_svm(gamma=0.003, C=150.0), make_lr(C=0.04), make_simple_lr(), ] make_submission = len(sys.argv) >= 2 and sys.argv[1] == 'submission' do_cv = not make_submission if do_cv: mask_range = [3] split_ratios = [0.4, 0.525, 0.6] run_prepare_data_for_cross_validation(settings, targets, pipelines) run_cross_validation(settings, targets, pipelines, mask_range, split_ratios, classifiers) if make_submission: num_masks = 10 split_ratio = 0.525 classifiers = [ # make_svm(gamma=0.0079, C=2.7), make_svm(gamma=0.0068, C=2.0), # make_svm(gamma=0.003, C=150.0), # make_lr(C=0.04), # make_simple_lr(), ] targets_and_pipelines = [] pipeline = pipelines[0] for classifier, classifier_name in classifiers: for i, target in enumerate(targets): run_prepare_data(settings, [target], [pipeline], test=True) feature_masks = generate_feature_masks(settings, target, pipeline, num_masks, split_ratio, random_state=0, quiet=True) targets_and_pipelines.append((target, pipeline, feature_masks, classifier, classifier_name)) run_make_submission(settings, targets_and_pipelines, split_ratio) if __name__ == "__main__": main()
mit
keir-rex/zipline
zipline/utils/tradingcalendar_tse.py
24
10413
# # Copyright 2014 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import pandas as pd import pytz from datetime import datetime from dateutil import rrule from zipline.utils.tradingcalendar import end, canonicalize_datetime start = pd.Timestamp('1994-01-01', tz='UTC') def get_non_trading_days(start, end): non_trading_rules = [] start = canonicalize_datetime(start) end = canonicalize_datetime(end) weekends = rrule.rrule( rrule.YEARLY, byweekday=(rrule.SA, rrule.SU), cache=True, dtstart=start, until=end ) non_trading_rules.append(weekends) new_years = rrule.rrule( rrule.MONTHLY, byyearday=1, cache=True, dtstart=start, until=end ) non_trading_rules.append(new_years) new_years_sunday = rrule.rrule( rrule.MONTHLY, byyearday=2, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(new_years_sunday) new_years_saturday = rrule.rrule( rrule.MONTHLY, byyearday=3, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(new_years_saturday) # Family day in Ontario, starting in 2008, third monday of February family_day = rrule.rrule( rrule.MONTHLY, bymonth=2, byweekday=(rrule.MO(3)), cache=True, dtstart=datetime(2008, 1, 1, tzinfo=pytz.utc), until=end ) non_trading_rules.append(family_day) good_friday = rrule.rrule( rrule.DAILY, byeaster=-2, cache=True, dtstart=start, until=end ) non_trading_rules.append(good_friday) # Monday prior to May 25th. victoria_day = rrule.rrule( rrule.MONTHLY, bymonth=5, byweekday=rrule.MO, bymonthday=[24, 23, 22, 21, 20, 19, 18], cache=True, dtstart=start, until=end ) non_trading_rules.append(victoria_day) july_1st = rrule.rrule( rrule.MONTHLY, bymonth=7, bymonthday=1, cache=True, dtstart=start, until=end ) non_trading_rules.append(july_1st) july_1st_sunday = rrule.rrule( rrule.MONTHLY, bymonth=7, bymonthday=2, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(july_1st_sunday) july_1st_saturday = rrule.rrule( rrule.MONTHLY, bymonth=7, bymonthday=3, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(july_1st_saturday) civic_holiday = rrule.rrule( rrule.MONTHLY, bymonth=8, byweekday=rrule.MO(1), cache=True, dtstart=start, until=end ) non_trading_rules.append(civic_holiday) labor_day = rrule.rrule( rrule.MONTHLY, bymonth=9, byweekday=(rrule.MO(1)), cache=True, dtstart=start, until=end ) non_trading_rules.append(labor_day) thanksgiving = rrule.rrule( rrule.MONTHLY, bymonth=10, byweekday=(rrule.MO(2)), cache=True, dtstart=start, until=end ) non_trading_rules.append(thanksgiving) christmas = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=25, cache=True, dtstart=start, until=end ) non_trading_rules.append(christmas) # If Christmas is a Sunday then the 26th, a Monday is observed. # (but that would be boxing day), so the 27th is also observed. christmas_sunday = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=27, byweekday=rrule.TU, cache=True, dtstart=start, until=end ) non_trading_rules.append(christmas_sunday) # If Christmas is a Saturday then the 27th, a monday is observed. christmas_saturday = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=27, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(christmas_saturday) boxing_day = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=26, cache=True, dtstart=start, until=end ) non_trading_rules.append(boxing_day) # if boxing day is a sunday, the Christmas was saturday. # Christmas is observed on the 27th, a month and boxing day is observed # on the 28th, a tuesday. boxing_day_sunday = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=28, byweekday=rrule.TU, cache=True, dtstart=start, until=end ) non_trading_rules.append(boxing_day_sunday) # If boxing day is a Saturday then the 28th, a monday is observed. boxing_day_saturday = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=28, byweekday=rrule.MO, cache=True, dtstart=start, until=end ) non_trading_rules.append(boxing_day_saturday) non_trading_ruleset = rrule.rruleset() for rule in non_trading_rules: non_trading_ruleset.rrule(rule) non_trading_days = non_trading_ruleset.between(start, end, inc=True) # Add September 11th closings # The TSX was open for 71 minutes on September 11, 2011. # It was closed on the 12th and reopened on the 13th. # http://www.cbc.ca/news2/interactives/map-tsx/ # # September 2001 # Su Mo Tu We Th Fr Sa # 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 non_trading_days.append( datetime(2001, 9, 12, tzinfo=pytz.utc)) non_trading_days.sort() return pd.DatetimeIndex(non_trading_days) non_trading_days = get_non_trading_days(start, end) trading_day = pd.tseries.offsets.CDay(holidays=non_trading_days) def get_trading_days(start, end, trading_day=trading_day): return pd.date_range(start=start.date(), end=end.date(), freq=trading_day).tz_localize('UTC') trading_days = get_trading_days(start, end) # Days in Environment but not in Calendar (using ^GSPTSE as bm_symbol): # -------------------------------------------------------------------- # Used http://web.tmxmoney.com/pricehistory.php?qm_page=61468&qm_symbol=^TSX # to check whether exchange was open on these days. # 1994-07-01 - July 1st, Yahoo Finance has Volume = 0 # 1996-07-01 - July 1st, Yahoo Finance has Volume = 0 # 1996-08-05 - Civic Holiday, Yahoo Finance has Volume = 0 # 1997-07-01 - July 1st, Yahoo Finance has Volume = 0 # 1997-08-04 - Civic Holiday, Yahoo Finance has Volume = 0 # 2001-05-21 - Victoria day, Yahoo Finance has Volume = 0 # 2004-10-11 - Closed, Thanksgiving - Confirmed closed # 2004-12-28 - Closed, Boxing Day - Confirmed closed # 2012-10-08 - Closed, Thanksgiving - Confirmed closed # Days in Calendar but not in Environment using ^GSPTSE as bm_symbol: # -------------------------------------------------------------------- # Used http://web.tmxmoney.com/pricehistory.php?qm_page=61468&qm_symbol=^TSX # to check whether exchange was open on these days. # 2000-06-28 - No data this far back, can't confirm # 2000-08-28 - No data this far back, can't confirm # 2000-08-29 - No data this far back, can't confirm # 2001-09-11 - TSE Open for 71 min. # 2002-02-01 - Confirm TSE Open # 2002-06-14 - Confirm TSE Open # 2002-07-02 - Confirm TSE Open # 2002-11-11 - TSX website has no data for 2 weeks in 2002 # 2003-07-07 - Confirm TSE Open # 2003-12-16 - Confirm TSE Open def get_early_closes(start, end): # TSX closed at 1:00 PM on december 24th. start = canonicalize_datetime(start) end = canonicalize_datetime(end) start = max(start, datetime(1993, 1, 1, tzinfo=pytz.utc)) end = max(end, datetime(1993, 1, 1, tzinfo=pytz.utc)) # Not included here are early closes prior to 1993 # or unplanned early closes early_close_rules = [] christmas_eve = rrule.rrule( rrule.MONTHLY, bymonth=12, bymonthday=24, byweekday=(rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR), cache=True, dtstart=start, until=end ) early_close_rules.append(christmas_eve) early_close_ruleset = rrule.rruleset() for rule in early_close_rules: early_close_ruleset.rrule(rule) early_closes = early_close_ruleset.between(start, end, inc=True) early_closes.sort() return pd.DatetimeIndex(early_closes) early_closes = get_early_closes(start, end) def get_open_and_closes(trading_days, early_closes, tz='US/Eastern'): open_and_closes = pd.DataFrame(index=trading_days, columns=('market_open', 'market_close')) for day in trading_days: market_open = pd.Timestamp( datetime( year=day.year, month=day.month, day=day.day, hour=9, minute=31), tz='US/Eastern').tz_convert('UTC') # 1 PM if early close, 4 PM otherwise close_hour = 13 if day in early_closes else 16 market_close = pd.Timestamp( datetime( year=day.year, month=day.month, day=day.day, hour=close_hour), tz='US/Eastern').tz_convert('UTC') open_and_closes.loc[day, 'market_open'] = market_open open_and_closes.loc[day, 'market_close'] = market_close return open_and_closes open_and_closes = get_open_and_closes(trading_days, early_closes)
apache-2.0
Sentient07/scikit-learn
sklearn/tests/test_grid_search.py
27
29492
""" Testing for grid search module (sklearn.grid_search) """ from collections import Iterable, Sized from sklearn.externals.six.moves import cStringIO as StringIO from sklearn.externals.six.moves import xrange from itertools import chain, product import pickle import warnings import sys import numpy as np import scipy.sparse as sp from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_not_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_warns from sklearn.utils.testing import assert_raise_message from sklearn.utils.testing import assert_false, assert_true from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_no_warnings from sklearn.utils.testing import ignore_warnings from sklearn.utils.mocking import CheckingClassifier, MockDataFrame from scipy.stats import bernoulli, expon, uniform from sklearn.externals.six.moves import zip from sklearn.base import BaseEstimator from sklearn.datasets import make_classification from sklearn.datasets import make_blobs from sklearn.datasets import make_multilabel_classification from sklearn.svm import LinearSVC, SVC from sklearn.tree import DecisionTreeRegressor from sklearn.tree import DecisionTreeClassifier from sklearn.cluster import KMeans from sklearn.neighbors import KernelDensity from sklearn.metrics import f1_score from sklearn.metrics import make_scorer from sklearn.metrics import roc_auc_score from sklearn.linear_model import Ridge from sklearn.exceptions import ChangedBehaviorWarning from sklearn.exceptions import FitFailedWarning with warnings.catch_warnings(): warnings.simplefilter('ignore') from sklearn.grid_search import (GridSearchCV, RandomizedSearchCV, ParameterGrid, ParameterSampler) from sklearn.cross_validation import KFold, StratifiedKFold from sklearn.preprocessing import Imputer from sklearn.pipeline import Pipeline # Neither of the following two estimators inherit from BaseEstimator, # to test hyperparameter search on user-defined classifiers. class MockClassifier(object): """Dummy classifier to test the cross-validation""" def __init__(self, foo_param=0): self.foo_param = foo_param def fit(self, X, Y): assert_true(len(X) == len(Y)) return self def predict(self, T): return T.shape[0] predict_proba = predict decision_function = predict transform = predict def score(self, X=None, Y=None): if self.foo_param > 1: score = 1. else: score = 0. return score def get_params(self, deep=False): return {'foo_param': self.foo_param} def set_params(self, **params): self.foo_param = params['foo_param'] return self class LinearSVCNoScore(LinearSVC): """An LinearSVC classifier that has no score method.""" @property def score(self): raise AttributeError X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) y = np.array([1, 1, 2, 2]) def assert_grid_iter_equals_getitem(grid): assert_equal(list(grid), [grid[i] for i in range(len(grid))]) def test_parameter_grid(): # Test basic properties of ParameterGrid. params1 = {"foo": [1, 2, 3]} grid1 = ParameterGrid(params1) assert_true(isinstance(grid1, Iterable)) assert_true(isinstance(grid1, Sized)) assert_equal(len(grid1), 3) assert_grid_iter_equals_getitem(grid1) params2 = {"foo": [4, 2], "bar": ["ham", "spam", "eggs"]} grid2 = ParameterGrid(params2) assert_equal(len(grid2), 6) # loop to assert we can iterate over the grid multiple times for i in xrange(2): # tuple + chain transforms {"a": 1, "b": 2} to ("a", 1, "b", 2) points = set(tuple(chain(*(sorted(p.items())))) for p in grid2) assert_equal(points, set(("bar", x, "foo", y) for x, y in product(params2["bar"], params2["foo"]))) assert_grid_iter_equals_getitem(grid2) # Special case: empty grid (useful to get default estimator settings) empty = ParameterGrid({}) assert_equal(len(empty), 1) assert_equal(list(empty), [{}]) assert_grid_iter_equals_getitem(empty) assert_raises(IndexError, lambda: empty[1]) has_empty = ParameterGrid([{'C': [1, 10]}, {}, {'C': [.5]}]) assert_equal(len(has_empty), 4) assert_equal(list(has_empty), [{'C': 1}, {'C': 10}, {}, {'C': .5}]) assert_grid_iter_equals_getitem(has_empty) def test_grid_search(): # Test that the best estimator contains the right value for foo_param clf = MockClassifier() grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, verbose=3) # make sure it selects the smallest parameter in case of ties old_stdout = sys.stdout sys.stdout = StringIO() grid_search.fit(X, y) sys.stdout = old_stdout assert_equal(grid_search.best_estimator_.foo_param, 2) for i, foo_i in enumerate([1, 2, 3]): assert_true(grid_search.grid_scores_[i][0] == {'foo_param': foo_i}) # Smoke test the score etc: grid_search.score(X, y) grid_search.predict_proba(X) grid_search.decision_function(X) grid_search.transform(X) # Test exception handling on scoring grid_search.scoring = 'sklearn' assert_raises(ValueError, grid_search.fit, X, y) @ignore_warnings def test_grid_search_no_score(): # Test grid-search on classifier that has no score function. clf = LinearSVC(random_state=0) X, y = make_blobs(random_state=0, centers=2) Cs = [.1, 1, 10] clf_no_score = LinearSVCNoScore(random_state=0) grid_search = GridSearchCV(clf, {'C': Cs}, scoring='accuracy') grid_search.fit(X, y) grid_search_no_score = GridSearchCV(clf_no_score, {'C': Cs}, scoring='accuracy') # smoketest grid search grid_search_no_score.fit(X, y) # check that best params are equal assert_equal(grid_search_no_score.best_params_, grid_search.best_params_) # check that we can call score and that it gives the correct result assert_equal(grid_search.score(X, y), grid_search_no_score.score(X, y)) # giving no scoring function raises an error grid_search_no_score = GridSearchCV(clf_no_score, {'C': Cs}) assert_raise_message(TypeError, "no scoring", grid_search_no_score.fit, [[1]]) def test_grid_search_score_method(): X, y = make_classification(n_samples=100, n_classes=2, flip_y=.2, random_state=0) clf = LinearSVC(random_state=0) grid = {'C': [.1]} search_no_scoring = GridSearchCV(clf, grid, scoring=None).fit(X, y) search_accuracy = GridSearchCV(clf, grid, scoring='accuracy').fit(X, y) search_no_score_method_auc = GridSearchCV(LinearSVCNoScore(), grid, scoring='roc_auc').fit(X, y) search_auc = GridSearchCV(clf, grid, scoring='roc_auc').fit(X, y) # Check warning only occurs in situation where behavior changed: # estimator requires score method to compete with scoring parameter score_no_scoring = assert_no_warnings(search_no_scoring.score, X, y) score_accuracy = assert_warns(ChangedBehaviorWarning, search_accuracy.score, X, y) score_no_score_auc = assert_no_warnings(search_no_score_method_auc.score, X, y) score_auc = assert_warns(ChangedBehaviorWarning, search_auc.score, X, y) # ensure the test is sane assert_true(score_auc < 1.0) assert_true(score_accuracy < 1.0) assert_not_equal(score_auc, score_accuracy) assert_almost_equal(score_accuracy, score_no_scoring) assert_almost_equal(score_auc, score_no_score_auc) def test_trivial_grid_scores(): # Test search over a "grid" with only one point. # Non-regression test: grid_scores_ wouldn't be set by GridSearchCV. clf = MockClassifier() grid_search = GridSearchCV(clf, {'foo_param': [1]}) grid_search.fit(X, y) assert_true(hasattr(grid_search, "grid_scores_")) random_search = RandomizedSearchCV(clf, {'foo_param': [0]}, n_iter=1) random_search.fit(X, y) assert_true(hasattr(random_search, "grid_scores_")) def test_no_refit(): # Test that grid search can be used for model selection only clf = MockClassifier() grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, refit=False) grid_search.fit(X, y) assert_true(hasattr(grid_search, "best_params_")) def test_grid_search_error(): # Test that grid search will capture errors on data with different # length X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) clf = LinearSVC() cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) assert_raises(ValueError, cv.fit, X_[:180], y_) def test_grid_search_iid(): # test the iid parameter # noise-free simple 2d-data X, y = make_blobs(centers=[[0, 0], [1, 0], [0, 1], [1, 1]], random_state=0, cluster_std=0.1, shuffle=False, n_samples=80) # split dataset into two folds that are not iid # first one contains data of all 4 blobs, second only from two. mask = np.ones(X.shape[0], dtype=np.bool) mask[np.where(y == 1)[0][::2]] = 0 mask[np.where(y == 2)[0][::2]] = 0 # this leads to perfect classification on one fold and a score of 1/3 on # the other svm = SVC(kernel='linear') # create "cv" for splits cv = [[mask, ~mask], [~mask, mask]] # once with iid=True (default) grid_search = GridSearchCV(svm, param_grid={'C': [1, 10]}, cv=cv) grid_search.fit(X, y) first = grid_search.grid_scores_[0] assert_equal(first.parameters['C'], 1) assert_array_almost_equal(first.cv_validation_scores, [1, 1. / 3.]) # for first split, 1/4 of dataset is in test, for second 3/4. # take weighted average assert_almost_equal(first.mean_validation_score, 1 * 1. / 4. + 1. / 3. * 3. / 4.) # once with iid=False grid_search = GridSearchCV(svm, param_grid={'C': [1, 10]}, cv=cv, iid=False) grid_search.fit(X, y) first = grid_search.grid_scores_[0] assert_equal(first.parameters['C'], 1) # scores are the same as above assert_array_almost_equal(first.cv_validation_scores, [1, 1. / 3.]) # averaged score is just mean of scores assert_almost_equal(first.mean_validation_score, np.mean(first.cv_validation_scores)) def test_grid_search_one_grid_point(): X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) param_dict = {"C": [1.0], "kernel": ["rbf"], "gamma": [0.1]} clf = SVC() cv = GridSearchCV(clf, param_dict) cv.fit(X_, y_) clf = SVC(C=1.0, kernel="rbf", gamma=0.1) clf.fit(X_, y_) assert_array_equal(clf.dual_coef_, cv.best_estimator_.dual_coef_) def test_grid_search_bad_param_grid(): param_dict = {"C": 1.0} clf = SVC() assert_raises(ValueError, GridSearchCV, clf, param_dict) param_dict = {"C": []} clf = SVC() assert_raises(ValueError, GridSearchCV, clf, param_dict) param_dict = {"C": np.ones(6).reshape(3, 2)} clf = SVC() assert_raises(ValueError, GridSearchCV, clf, param_dict) def test_grid_search_sparse(): # Test that grid search works with both dense and sparse matrices X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) clf = LinearSVC() cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) cv.fit(X_[:180], y_[:180]) y_pred = cv.predict(X_[180:]) C = cv.best_estimator_.C X_ = sp.csr_matrix(X_) clf = LinearSVC() cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) cv.fit(X_[:180].tocoo(), y_[:180]) y_pred2 = cv.predict(X_[180:]) C2 = cv.best_estimator_.C assert_true(np.mean(y_pred == y_pred2) >= .9) assert_equal(C, C2) def test_grid_search_sparse_scoring(): X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) clf = LinearSVC() cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, scoring="f1") cv.fit(X_[:180], y_[:180]) y_pred = cv.predict(X_[180:]) C = cv.best_estimator_.C X_ = sp.csr_matrix(X_) clf = LinearSVC() cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, scoring="f1") cv.fit(X_[:180], y_[:180]) y_pred2 = cv.predict(X_[180:]) C2 = cv.best_estimator_.C assert_array_equal(y_pred, y_pred2) assert_equal(C, C2) # Smoke test the score # np.testing.assert_allclose(f1_score(cv.predict(X_[:180]), y[:180]), # cv.score(X_[:180], y[:180])) # test loss where greater is worse def f1_loss(y_true_, y_pred_): return -f1_score(y_true_, y_pred_) F1Loss = make_scorer(f1_loss, greater_is_better=False) cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, scoring=F1Loss) cv.fit(X_[:180], y_[:180]) y_pred3 = cv.predict(X_[180:]) C3 = cv.best_estimator_.C assert_equal(C, C3) assert_array_equal(y_pred, y_pred3) def test_grid_search_precomputed_kernel(): # Test that grid search works when the input features are given in the # form of a precomputed kernel matrix X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) # compute the training kernel matrix corresponding to the linear kernel K_train = np.dot(X_[:180], X_[:180].T) y_train = y_[:180] clf = SVC(kernel='precomputed') cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) cv.fit(K_train, y_train) assert_true(cv.best_score_ >= 0) # compute the test kernel matrix K_test = np.dot(X_[180:], X_[:180].T) y_test = y_[180:] y_pred = cv.predict(K_test) assert_true(np.mean(y_pred == y_test) >= 0) # test error is raised when the precomputed kernel is not array-like # or sparse assert_raises(ValueError, cv.fit, K_train.tolist(), y_train) def test_grid_search_precomputed_kernel_error_nonsquare(): # Test that grid search returns an error with a non-square precomputed # training kernel matrix K_train = np.zeros((10, 20)) y_train = np.ones((10, )) clf = SVC(kernel='precomputed') cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) assert_raises(ValueError, cv.fit, K_train, y_train) def test_grid_search_precomputed_kernel_error_kernel_function(): # Test that grid search returns an error when using a kernel_function X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0) kernel_function = lambda x1, x2: np.dot(x1, x2.T) clf = SVC(kernel=kernel_function) cv = GridSearchCV(clf, {'C': [0.1, 1.0]}) assert_raises(ValueError, cv.fit, X_, y_) class BrokenClassifier(BaseEstimator): """Broken classifier that cannot be fit twice""" def __init__(self, parameter=None): self.parameter = parameter def fit(self, X, y): assert_true(not hasattr(self, 'has_been_fit_')) self.has_been_fit_ = True def predict(self, X): return np.zeros(X.shape[0]) @ignore_warnings def test_refit(): # Regression test for bug in refitting # Simulates re-fitting a broken estimator; this used to break with # sparse SVMs. X = np.arange(100).reshape(10, 10) y = np.array([0] * 5 + [1] * 5) clf = GridSearchCV(BrokenClassifier(), [{'parameter': [0, 1]}], scoring="precision", refit=True) clf.fit(X, y) def test_gridsearch_nd(): # Pass X as list in GridSearchCV X_4d = np.arange(10 * 5 * 3 * 2).reshape(10, 5, 3, 2) y_3d = np.arange(10 * 7 * 11).reshape(10, 7, 11) check_X = lambda x: x.shape[1:] == (5, 3, 2) check_y = lambda x: x.shape[1:] == (7, 11) clf = CheckingClassifier(check_X=check_X, check_y=check_y) grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}) grid_search.fit(X_4d, y_3d).score(X, y) assert_true(hasattr(grid_search, "grid_scores_")) def test_X_as_list(): # Pass X as list in GridSearchCV X = np.arange(100).reshape(10, 10) y = np.array([0] * 5 + [1] * 5) clf = CheckingClassifier(check_X=lambda x: isinstance(x, list)) cv = KFold(n=len(X), n_folds=3) grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, cv=cv) grid_search.fit(X.tolist(), y).score(X, y) assert_true(hasattr(grid_search, "grid_scores_")) def test_y_as_list(): # Pass y as list in GridSearchCV X = np.arange(100).reshape(10, 10) y = np.array([0] * 5 + [1] * 5) clf = CheckingClassifier(check_y=lambda x: isinstance(x, list)) cv = KFold(n=len(X), n_folds=3) grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, cv=cv) grid_search.fit(X, y.tolist()).score(X, y) assert_true(hasattr(grid_search, "grid_scores_")) def test_pandas_input(): # check cross_val_score doesn't destroy pandas dataframe types = [(MockDataFrame, MockDataFrame)] try: from pandas import Series, DataFrame types.append((DataFrame, Series)) except ImportError: pass X = np.arange(100).reshape(10, 10) y = np.array([0] * 5 + [1] * 5) for InputFeatureType, TargetType in types: # X dataframe, y series X_df, y_ser = InputFeatureType(X), TargetType(y) check_df = lambda x: isinstance(x, InputFeatureType) check_series = lambda x: isinstance(x, TargetType) clf = CheckingClassifier(check_X=check_df, check_y=check_series) grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}) grid_search.fit(X_df, y_ser).score(X_df, y_ser) grid_search.predict(X_df) assert_true(hasattr(grid_search, "grid_scores_")) def test_unsupervised_grid_search(): # test grid-search with unsupervised estimator X, y = make_blobs(random_state=0) km = KMeans(random_state=0) grid_search = GridSearchCV(km, param_grid=dict(n_clusters=[2, 3, 4]), scoring='adjusted_rand_score') grid_search.fit(X, y) # ARI can find the right number :) assert_equal(grid_search.best_params_["n_clusters"], 3) # Now without a score, and without y grid_search = GridSearchCV(km, param_grid=dict(n_clusters=[2, 3, 4])) grid_search.fit(X) assert_equal(grid_search.best_params_["n_clusters"], 4) def test_gridsearch_no_predict(): # test grid-search with an estimator without predict. # slight duplication of a test from KDE def custom_scoring(estimator, X): return 42 if estimator.bandwidth == .1 else 0 X, _ = make_blobs(cluster_std=.1, random_state=1, centers=[[0, 1], [1, 0], [0, 0]]) search = GridSearchCV(KernelDensity(), param_grid=dict(bandwidth=[.01, .1, 1]), scoring=custom_scoring) search.fit(X) assert_equal(search.best_params_['bandwidth'], .1) assert_equal(search.best_score_, 42) def test_param_sampler(): # test basic properties of param sampler param_distributions = {"kernel": ["rbf", "linear"], "C": uniform(0, 1)} sampler = ParameterSampler(param_distributions=param_distributions, n_iter=10, random_state=0) samples = [x for x in sampler] assert_equal(len(samples), 10) for sample in samples: assert_true(sample["kernel"] in ["rbf", "linear"]) assert_true(0 <= sample["C"] <= 1) def test_randomized_search_grid_scores(): # Make a dataset with a lot of noise to get various kind of prediction # errors across CV folds and parameter settings X, y = make_classification(n_samples=200, n_features=100, n_informative=3, random_state=0) # XXX: as of today (scipy 0.12) it's not possible to set the random seed # of scipy.stats distributions: the assertions in this test should thus # not depend on the randomization params = dict(C=expon(scale=10), gamma=expon(scale=0.1)) n_cv_iter = 3 n_search_iter = 30 search = RandomizedSearchCV(SVC(), n_iter=n_search_iter, cv=n_cv_iter, param_distributions=params, iid=False) search.fit(X, y) assert_equal(len(search.grid_scores_), n_search_iter) # Check consistency of the structure of each cv_score item for cv_score in search.grid_scores_: assert_equal(len(cv_score.cv_validation_scores), n_cv_iter) # Because we set iid to False, the mean_validation score is the # mean of the fold mean scores instead of the aggregate sample-wise # mean score assert_almost_equal(np.mean(cv_score.cv_validation_scores), cv_score.mean_validation_score) assert_equal(list(sorted(cv_score.parameters.keys())), list(sorted(params.keys()))) # Check the consistency with the best_score_ and best_params_ attributes sorted_grid_scores = list(sorted(search.grid_scores_, key=lambda x: x.mean_validation_score)) best_score = sorted_grid_scores[-1].mean_validation_score assert_equal(search.best_score_, best_score) tied_best_params = [s.parameters for s in sorted_grid_scores if s.mean_validation_score == best_score] assert_true(search.best_params_ in tied_best_params, "best_params_={0} is not part of the" " tied best models: {1}".format( search.best_params_, tied_best_params)) def test_grid_search_score_consistency(): # test that correct scores are used clf = LinearSVC(random_state=0) X, y = make_blobs(random_state=0, centers=2) Cs = [.1, 1, 10] for score in ['f1', 'roc_auc']: grid_search = GridSearchCV(clf, {'C': Cs}, scoring=score) grid_search.fit(X, y) cv = StratifiedKFold(n_folds=3, y=y) for C, scores in zip(Cs, grid_search.grid_scores_): clf.set_params(C=C) scores = scores[2] # get the separate runs from grid scores i = 0 for train, test in cv: clf.fit(X[train], y[train]) if score == "f1": correct_score = f1_score(y[test], clf.predict(X[test])) elif score == "roc_auc": dec = clf.decision_function(X[test]) correct_score = roc_auc_score(y[test], dec) assert_almost_equal(correct_score, scores[i]) i += 1 def test_pickle(): # Test that a fit search can be pickled clf = MockClassifier() grid_search = GridSearchCV(clf, {'foo_param': [1, 2, 3]}, refit=True) grid_search.fit(X, y) pickle.dumps(grid_search) # smoke test random_search = RandomizedSearchCV(clf, {'foo_param': [1, 2, 3]}, refit=True, n_iter=3) random_search.fit(X, y) pickle.dumps(random_search) # smoke test def test_grid_search_with_multioutput_data(): # Test search with multi-output estimator X, y = make_multilabel_classification(random_state=0) est_parameters = {"max_depth": [1, 2, 3, 4]} cv = KFold(y.shape[0], random_state=0) estimators = [DecisionTreeRegressor(random_state=0), DecisionTreeClassifier(random_state=0)] # Test with grid search cv for est in estimators: grid_search = GridSearchCV(est, est_parameters, cv=cv) grid_search.fit(X, y) for parameters, _, cv_validation_scores in grid_search.grid_scores_: est.set_params(**parameters) for i, (train, test) in enumerate(cv): est.fit(X[train], y[train]) correct_score = est.score(X[test], y[test]) assert_almost_equal(correct_score, cv_validation_scores[i]) # Test with a randomized search for est in estimators: random_search = RandomizedSearchCV(est, est_parameters, cv=cv, n_iter=3) random_search.fit(X, y) for parameters, _, cv_validation_scores in random_search.grid_scores_: est.set_params(**parameters) for i, (train, test) in enumerate(cv): est.fit(X[train], y[train]) correct_score = est.score(X[test], y[test]) assert_almost_equal(correct_score, cv_validation_scores[i]) def test_predict_proba_disabled(): # Test predict_proba when disabled on estimator. X = np.arange(20).reshape(5, -1) y = [0, 0, 1, 1, 1] clf = SVC(probability=False) gs = GridSearchCV(clf, {}, cv=2).fit(X, y) assert_false(hasattr(gs, "predict_proba")) def test_grid_search_allows_nans(): # Test GridSearchCV with Imputer X = np.arange(20, dtype=np.float64).reshape(5, -1) X[2, :] = np.nan y = [0, 0, 1, 1, 1] p = Pipeline([ ('imputer', Imputer(strategy='mean', missing_values='NaN')), ('classifier', MockClassifier()), ]) GridSearchCV(p, {'classifier__foo_param': [1, 2, 3]}, cv=2).fit(X, y) class FailingClassifier(BaseEstimator): """Classifier that raises a ValueError on fit()""" FAILING_PARAMETER = 2 def __init__(self, parameter=None): self.parameter = parameter def fit(self, X, y=None): if self.parameter == FailingClassifier.FAILING_PARAMETER: raise ValueError("Failing classifier failed as required") def predict(self, X): return np.zeros(X.shape[0]) def test_grid_search_failing_classifier(): # GridSearchCV with on_error != 'raise' # Ensures that a warning is raised and score reset where appropriate. X, y = make_classification(n_samples=20, n_features=10, random_state=0) clf = FailingClassifier() # refit=False because we only want to check that errors caused by fits # to individual folds will be caught and warnings raised instead. If # refit was done, then an exception would be raised on refit and not # caught by grid_search (expected behavior), and this would cause an # error in this test. gs = GridSearchCV(clf, [{'parameter': [0, 1, 2]}], scoring='accuracy', refit=False, error_score=0.0) assert_warns(FitFailedWarning, gs.fit, X, y) # Ensure that grid scores were set to zero as required for those fits # that are expected to fail. assert all(np.all(this_point.cv_validation_scores == 0.0) for this_point in gs.grid_scores_ if this_point.parameters['parameter'] == FailingClassifier.FAILING_PARAMETER) gs = GridSearchCV(clf, [{'parameter': [0, 1, 2]}], scoring='accuracy', refit=False, error_score=float('nan')) assert_warns(FitFailedWarning, gs.fit, X, y) assert all(np.all(np.isnan(this_point.cv_validation_scores)) for this_point in gs.grid_scores_ if this_point.parameters['parameter'] == FailingClassifier.FAILING_PARAMETER) def test_grid_search_failing_classifier_raise(): # GridSearchCV with on_error == 'raise' raises the error X, y = make_classification(n_samples=20, n_features=10, random_state=0) clf = FailingClassifier() # refit=False because we want to test the behaviour of the grid search part gs = GridSearchCV(clf, [{'parameter': [0, 1, 2]}], scoring='accuracy', refit=False, error_score='raise') # FailingClassifier issues a ValueError so this is what we look for. assert_raises(ValueError, gs.fit, X, y) def test_parameters_sampler_replacement(): # raise error if n_iter too large params = {'first': [0, 1], 'second': ['a', 'b', 'c']} sampler = ParameterSampler(params, n_iter=7) assert_raises(ValueError, list, sampler) # degenerates to GridSearchCV if n_iter the same as grid_size sampler = ParameterSampler(params, n_iter=6) samples = list(sampler) assert_equal(len(samples), 6) for values in ParameterGrid(params): assert_true(values in samples) # test sampling without replacement in a large grid params = {'a': range(10), 'b': range(10), 'c': range(10)} sampler = ParameterSampler(params, n_iter=99, random_state=42) samples = list(sampler) assert_equal(len(samples), 99) hashable_samples = ["a%db%dc%d" % (p['a'], p['b'], p['c']) for p in samples] assert_equal(len(set(hashable_samples)), 99) # doesn't go into infinite loops params_distribution = {'first': bernoulli(.5), 'second': ['a', 'b', 'c']} sampler = ParameterSampler(params_distribution, n_iter=7) samples = list(sampler) assert_equal(len(samples), 7) def test_classes__property(): # Test that classes_ property matches best_esimator_.classes_ X = np.arange(100).reshape(10, 10) y = np.array([0] * 5 + [1] * 5) Cs = [.1, 1, 10] grid_search = GridSearchCV(LinearSVC(random_state=0), {'C': Cs}) grid_search.fit(X, y) assert_array_equal(grid_search.best_estimator_.classes_, grid_search.classes_) # Test that regressors do not have a classes_ attribute grid_search = GridSearchCV(Ridge(), {'alpha': [1.0, 2.0]}) grid_search.fit(X, y) assert_false(hasattr(grid_search, 'classes_'))
bsd-3-clause
fdudatamining/framework
tests/draw/test_simple.py
1
1233
import numpy as np import pandas as pd from unittest import TestCase from framework import draw X = np.array([1, 2, 3, 4, 5]) class TestSimplePlots(TestCase): def test_kinds(self): self.assertIsNotNone(draw.draw_kinds) def test_line(self): draw.draw(clear=True, kind='line', x=X, y=X) draw.draw(clear=True, kind='line', y=X) def test_scatter(self): draw.draw(clear=True, kind='scatter', x=X, y=X) draw.draw(clear=True, kind='scatter', y=X) def test_stem(self): draw.draw(clear=True, kind='stem', x=X, y=X) draw.draw(clear=True, kind='stem', y=X) def test_errorbar(self): draw.draw(clear=True, kind='errorbar', x=X, y=X, xerr=X, yerr=X) draw.draw(clear=True, kind='errorbar', y=X, yerr=X) def test_boxplot(self): draw.draw(clear=True, kind='boxplot', x=X) def test_barplot(self): draw.draw(clear=True, kind='barplot', x=X, y=X, width=1) draw.draw(clear=True, kind='barplot', x=X, y=X) draw.draw(clear=True, kind='barplot', y=X) def test_contour(self): draw.draw(clear=True, kind='contour', z=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]) def test_hist(self): draw.draw(clear=True, kind='hist', x=X, bins=2) draw.draw(clear=True, kind='hist', x=X)
gpl-2.0
stevertaylor/NX01
newcmaps.py
28
50518
# New matplotlib colormaps by Nathaniel J. Smith, Stefan van der Walt, # and (in the case of viridis) Eric Firing. # # This file and the colormaps in it are released under the CC0 license / # public domain dedication. We would appreciate credit if you use or # redistribute these colormaps, but do not impose any legal restrictions. # # To the extent possible under law, the persons who associated CC0 with # mpl-colormaps have waived all copyright and related or neighboring rights # to mpl-colormaps. # # You should have received a copy of the CC0 legalcode along with this # work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. __all__ = ['magma', 'inferno', 'plasma', 'viridis'] _magma_data = [[0.001462, 0.000466, 0.013866], [0.002258, 0.001295, 0.018331], [0.003279, 0.002305, 0.023708], [0.004512, 0.003490, 0.029965], [0.005950, 0.004843, 0.037130], [0.007588, 0.006356, 0.044973], [0.009426, 0.008022, 0.052844], [0.011465, 0.009828, 0.060750], [0.013708, 0.011771, 0.068667], [0.016156, 0.013840, 0.076603], [0.018815, 0.016026, 0.084584], [0.021692, 0.018320, 0.092610], [0.024792, 0.020715, 0.100676], [0.028123, 0.023201, 0.108787], [0.031696, 0.025765, 0.116965], [0.035520, 0.028397, 0.125209], [0.039608, 0.031090, 0.133515], [0.043830, 0.033830, 0.141886], [0.048062, 0.036607, 0.150327], [0.052320, 0.039407, 0.158841], [0.056615, 0.042160, 0.167446], [0.060949, 0.044794, 0.176129], [0.065330, 0.047318, 0.184892], [0.069764, 0.049726, 0.193735], [0.074257, 0.052017, 0.202660], [0.078815, 0.054184, 0.211667], [0.083446, 0.056225, 0.220755], [0.088155, 0.058133, 0.229922], [0.092949, 0.059904, 0.239164], [0.097833, 0.061531, 0.248477], [0.102815, 0.063010, 0.257854], [0.107899, 0.064335, 0.267289], [0.113094, 0.065492, 0.276784], [0.118405, 0.066479, 0.286321], [0.123833, 0.067295, 0.295879], [0.129380, 0.067935, 0.305443], [0.135053, 0.068391, 0.315000], [0.140858, 0.068654, 0.324538], [0.146785, 0.068738, 0.334011], [0.152839, 0.068637, 0.343404], [0.159018, 0.068354, 0.352688], [0.165308, 0.067911, 0.361816], [0.171713, 0.067305, 0.370771], [0.178212, 0.066576, 0.379497], [0.184801, 0.065732, 0.387973], [0.191460, 0.064818, 0.396152], [0.198177, 0.063862, 0.404009], [0.204935, 0.062907, 0.411514], [0.211718, 0.061992, 0.418647], [0.218512, 0.061158, 0.425392], [0.225302, 0.060445, 0.431742], [0.232077, 0.059889, 0.437695], [0.238826, 0.059517, 0.443256], [0.245543, 0.059352, 0.448436], [0.252220, 0.059415, 0.453248], [0.258857, 0.059706, 0.457710], [0.265447, 0.060237, 0.461840], [0.271994, 0.060994, 0.465660], [0.278493, 0.061978, 0.469190], [0.284951, 0.063168, 0.472451], [0.291366, 0.064553, 0.475462], [0.297740, 0.066117, 0.478243], [0.304081, 0.067835, 0.480812], [0.310382, 0.069702, 0.483186], [0.316654, 0.071690, 0.485380], [0.322899, 0.073782, 0.487408], [0.329114, 0.075972, 0.489287], [0.335308, 0.078236, 0.491024], [0.341482, 0.080564, 0.492631], [0.347636, 0.082946, 0.494121], [0.353773, 0.085373, 0.495501], [0.359898, 0.087831, 0.496778], [0.366012, 0.090314, 0.497960], [0.372116, 0.092816, 0.499053], [0.378211, 0.095332, 0.500067], [0.384299, 0.097855, 0.501002], [0.390384, 0.100379, 0.501864], [0.396467, 0.102902, 0.502658], [0.402548, 0.105420, 0.503386], [0.408629, 0.107930, 0.504052], [0.414709, 0.110431, 0.504662], [0.420791, 0.112920, 0.505215], [0.426877, 0.115395, 0.505714], [0.432967, 0.117855, 0.506160], [0.439062, 0.120298, 0.506555], [0.445163, 0.122724, 0.506901], [0.451271, 0.125132, 0.507198], [0.457386, 0.127522, 0.507448], [0.463508, 0.129893, 0.507652], [0.469640, 0.132245, 0.507809], [0.475780, 0.134577, 0.507921], [0.481929, 0.136891, 0.507989], [0.488088, 0.139186, 0.508011], [0.494258, 0.141462, 0.507988], [0.500438, 0.143719, 0.507920], [0.506629, 0.145958, 0.507806], [0.512831, 0.148179, 0.507648], [0.519045, 0.150383, 0.507443], [0.525270, 0.152569, 0.507192], [0.531507, 0.154739, 0.506895], [0.537755, 0.156894, 0.506551], [0.544015, 0.159033, 0.506159], [0.550287, 0.161158, 0.505719], [0.556571, 0.163269, 0.505230], [0.562866, 0.165368, 0.504692], [0.569172, 0.167454, 0.504105], [0.575490, 0.169530, 0.503466], [0.581819, 0.171596, 0.502777], [0.588158, 0.173652, 0.502035], [0.594508, 0.175701, 0.501241], [0.600868, 0.177743, 0.500394], [0.607238, 0.179779, 0.499492], [0.613617, 0.181811, 0.498536], [0.620005, 0.183840, 0.497524], [0.626401, 0.185867, 0.496456], [0.632805, 0.187893, 0.495332], [0.639216, 0.189921, 0.494150], [0.645633, 0.191952, 0.492910], [0.652056, 0.193986, 0.491611], [0.658483, 0.196027, 0.490253], [0.664915, 0.198075, 0.488836], [0.671349, 0.200133, 0.487358], [0.677786, 0.202203, 0.485819], [0.684224, 0.204286, 0.484219], [0.690661, 0.206384, 0.482558], [0.697098, 0.208501, 0.480835], [0.703532, 0.210638, 0.479049], [0.709962, 0.212797, 0.477201], [0.716387, 0.214982, 0.475290], [0.722805, 0.217194, 0.473316], [0.729216, 0.219437, 0.471279], [0.735616, 0.221713, 0.469180], [0.742004, 0.224025, 0.467018], [0.748378, 0.226377, 0.464794], [0.754737, 0.228772, 0.462509], [0.761077, 0.231214, 0.460162], [0.767398, 0.233705, 0.457755], [0.773695, 0.236249, 0.455289], [0.779968, 0.238851, 0.452765], [0.786212, 0.241514, 0.450184], [0.792427, 0.244242, 0.447543], [0.798608, 0.247040, 0.444848], [0.804752, 0.249911, 0.442102], [0.810855, 0.252861, 0.439305], [0.816914, 0.255895, 0.436461], [0.822926, 0.259016, 0.433573], [0.828886, 0.262229, 0.430644], [0.834791, 0.265540, 0.427671], [0.840636, 0.268953, 0.424666], [0.846416, 0.272473, 0.421631], [0.852126, 0.276106, 0.418573], [0.857763, 0.279857, 0.415496], [0.863320, 0.283729, 0.412403], [0.868793, 0.287728, 0.409303], [0.874176, 0.291859, 0.406205], [0.879464, 0.296125, 0.403118], [0.884651, 0.300530, 0.400047], [0.889731, 0.305079, 0.397002], [0.894700, 0.309773, 0.393995], [0.899552, 0.314616, 0.391037], [0.904281, 0.319610, 0.388137], [0.908884, 0.324755, 0.385308], [0.913354, 0.330052, 0.382563], [0.917689, 0.335500, 0.379915], [0.921884, 0.341098, 0.377376], [0.925937, 0.346844, 0.374959], [0.929845, 0.352734, 0.372677], [0.933606, 0.358764, 0.370541], [0.937221, 0.364929, 0.368567], [0.940687, 0.371224, 0.366762], [0.944006, 0.377643, 0.365136], [0.947180, 0.384178, 0.363701], [0.950210, 0.390820, 0.362468], [0.953099, 0.397563, 0.361438], [0.955849, 0.404400, 0.360619], [0.958464, 0.411324, 0.360014], [0.960949, 0.418323, 0.359630], [0.963310, 0.425390, 0.359469], [0.965549, 0.432519, 0.359529], [0.967671, 0.439703, 0.359810], [0.969680, 0.446936, 0.360311], [0.971582, 0.454210, 0.361030], [0.973381, 0.461520, 0.361965], [0.975082, 0.468861, 0.363111], [0.976690, 0.476226, 0.364466], [0.978210, 0.483612, 0.366025], [0.979645, 0.491014, 0.367783], [0.981000, 0.498428, 0.369734], [0.982279, 0.505851, 0.371874], [0.983485, 0.513280, 0.374198], [0.984622, 0.520713, 0.376698], [0.985693, 0.528148, 0.379371], [0.986700, 0.535582, 0.382210], [0.987646, 0.543015, 0.385210], [0.988533, 0.550446, 0.388365], [0.989363, 0.557873, 0.391671], [0.990138, 0.565296, 0.395122], [0.990871, 0.572706, 0.398714], [0.991558, 0.580107, 0.402441], [0.992196, 0.587502, 0.406299], [0.992785, 0.594891, 0.410283], [0.993326, 0.602275, 0.414390], [0.993834, 0.609644, 0.418613], [0.994309, 0.616999, 0.422950], [0.994738, 0.624350, 0.427397], [0.995122, 0.631696, 0.431951], [0.995480, 0.639027, 0.436607], [0.995810, 0.646344, 0.441361], [0.996096, 0.653659, 0.446213], [0.996341, 0.660969, 0.451160], [0.996580, 0.668256, 0.456192], [0.996775, 0.675541, 0.461314], [0.996925, 0.682828, 0.466526], [0.997077, 0.690088, 0.471811], [0.997186, 0.697349, 0.477182], [0.997254, 0.704611, 0.482635], [0.997325, 0.711848, 0.488154], [0.997351, 0.719089, 0.493755], [0.997351, 0.726324, 0.499428], [0.997341, 0.733545, 0.505167], [0.997285, 0.740772, 0.510983], [0.997228, 0.747981, 0.516859], [0.997138, 0.755190, 0.522806], [0.997019, 0.762398, 0.528821], [0.996898, 0.769591, 0.534892], [0.996727, 0.776795, 0.541039], [0.996571, 0.783977, 0.547233], [0.996369, 0.791167, 0.553499], [0.996162, 0.798348, 0.559820], [0.995932, 0.805527, 0.566202], [0.995680, 0.812706, 0.572645], [0.995424, 0.819875, 0.579140], [0.995131, 0.827052, 0.585701], [0.994851, 0.834213, 0.592307], [0.994524, 0.841387, 0.598983], [0.994222, 0.848540, 0.605696], [0.993866, 0.855711, 0.612482], [0.993545, 0.862859, 0.619299], [0.993170, 0.870024, 0.626189], [0.992831, 0.877168, 0.633109], [0.992440, 0.884330, 0.640099], [0.992089, 0.891470, 0.647116], [0.991688, 0.898627, 0.654202], [0.991332, 0.905763, 0.661309], [0.990930, 0.912915, 0.668481], [0.990570, 0.920049, 0.675675], [0.990175, 0.927196, 0.682926], [0.989815, 0.934329, 0.690198], [0.989434, 0.941470, 0.697519], [0.989077, 0.948604, 0.704863], [0.988717, 0.955742, 0.712242], [0.988367, 0.962878, 0.719649], [0.988033, 0.970012, 0.727077], [0.987691, 0.977154, 0.734536], [0.987387, 0.984288, 0.742002], [0.987053, 0.991438, 0.749504]] _inferno_data = [[0.001462, 0.000466, 0.013866], [0.002267, 0.001270, 0.018570], [0.003299, 0.002249, 0.024239], [0.004547, 0.003392, 0.030909], [0.006006, 0.004692, 0.038558], [0.007676, 0.006136, 0.046836], [0.009561, 0.007713, 0.055143], [0.011663, 0.009417, 0.063460], [0.013995, 0.011225, 0.071862], [0.016561, 0.013136, 0.080282], [0.019373, 0.015133, 0.088767], [0.022447, 0.017199, 0.097327], [0.025793, 0.019331, 0.105930], [0.029432, 0.021503, 0.114621], [0.033385, 0.023702, 0.123397], [0.037668, 0.025921, 0.132232], [0.042253, 0.028139, 0.141141], [0.046915, 0.030324, 0.150164], [0.051644, 0.032474, 0.159254], [0.056449, 0.034569, 0.168414], [0.061340, 0.036590, 0.177642], [0.066331, 0.038504, 0.186962], [0.071429, 0.040294, 0.196354], [0.076637, 0.041905, 0.205799], [0.081962, 0.043328, 0.215289], [0.087411, 0.044556, 0.224813], [0.092990, 0.045583, 0.234358], [0.098702, 0.046402, 0.243904], [0.104551, 0.047008, 0.253430], [0.110536, 0.047399, 0.262912], [0.116656, 0.047574, 0.272321], [0.122908, 0.047536, 0.281624], [0.129285, 0.047293, 0.290788], [0.135778, 0.046856, 0.299776], [0.142378, 0.046242, 0.308553], [0.149073, 0.045468, 0.317085], [0.155850, 0.044559, 0.325338], [0.162689, 0.043554, 0.333277], [0.169575, 0.042489, 0.340874], [0.176493, 0.041402, 0.348111], [0.183429, 0.040329, 0.354971], [0.190367, 0.039309, 0.361447], [0.197297, 0.038400, 0.367535], [0.204209, 0.037632, 0.373238], [0.211095, 0.037030, 0.378563], [0.217949, 0.036615, 0.383522], [0.224763, 0.036405, 0.388129], [0.231538, 0.036405, 0.392400], [0.238273, 0.036621, 0.396353], [0.244967, 0.037055, 0.400007], [0.251620, 0.037705, 0.403378], [0.258234, 0.038571, 0.406485], [0.264810, 0.039647, 0.409345], [0.271347, 0.040922, 0.411976], [0.277850, 0.042353, 0.414392], [0.284321, 0.043933, 0.416608], [0.290763, 0.045644, 0.418637], [0.297178, 0.047470, 0.420491], [0.303568, 0.049396, 0.422182], [0.309935, 0.051407, 0.423721], [0.316282, 0.053490, 0.425116], [0.322610, 0.055634, 0.426377], [0.328921, 0.057827, 0.427511], [0.335217, 0.060060, 0.428524], [0.341500, 0.062325, 0.429425], [0.347771, 0.064616, 0.430217], [0.354032, 0.066925, 0.430906], [0.360284, 0.069247, 0.431497], [0.366529, 0.071579, 0.431994], [0.372768, 0.073915, 0.432400], [0.379001, 0.076253, 0.432719], [0.385228, 0.078591, 0.432955], [0.391453, 0.080927, 0.433109], [0.397674, 0.083257, 0.433183], [0.403894, 0.085580, 0.433179], [0.410113, 0.087896, 0.433098], [0.416331, 0.090203, 0.432943], [0.422549, 0.092501, 0.432714], [0.428768, 0.094790, 0.432412], [0.434987, 0.097069, 0.432039], [0.441207, 0.099338, 0.431594], [0.447428, 0.101597, 0.431080], [0.453651, 0.103848, 0.430498], [0.459875, 0.106089, 0.429846], [0.466100, 0.108322, 0.429125], [0.472328, 0.110547, 0.428334], [0.478558, 0.112764, 0.427475], [0.484789, 0.114974, 0.426548], [0.491022, 0.117179, 0.425552], [0.497257, 0.119379, 0.424488], [0.503493, 0.121575, 0.423356], [0.509730, 0.123769, 0.422156], [0.515967, 0.125960, 0.420887], [0.522206, 0.128150, 0.419549], [0.528444, 0.130341, 0.418142], [0.534683, 0.132534, 0.416667], [0.540920, 0.134729, 0.415123], [0.547157, 0.136929, 0.413511], [0.553392, 0.139134, 0.411829], [0.559624, 0.141346, 0.410078], [0.565854, 0.143567, 0.408258], [0.572081, 0.145797, 0.406369], [0.578304, 0.148039, 0.404411], [0.584521, 0.150294, 0.402385], [0.590734, 0.152563, 0.400290], [0.596940, 0.154848, 0.398125], [0.603139, 0.157151, 0.395891], [0.609330, 0.159474, 0.393589], [0.615513, 0.161817, 0.391219], [0.621685, 0.164184, 0.388781], [0.627847, 0.166575, 0.386276], [0.633998, 0.168992, 0.383704], [0.640135, 0.171438, 0.381065], [0.646260, 0.173914, 0.378359], [0.652369, 0.176421, 0.375586], [0.658463, 0.178962, 0.372748], [0.664540, 0.181539, 0.369846], [0.670599, 0.184153, 0.366879], [0.676638, 0.186807, 0.363849], [0.682656, 0.189501, 0.360757], [0.688653, 0.192239, 0.357603], [0.694627, 0.195021, 0.354388], [0.700576, 0.197851, 0.351113], [0.706500, 0.200728, 0.347777], [0.712396, 0.203656, 0.344383], [0.718264, 0.206636, 0.340931], [0.724103, 0.209670, 0.337424], [0.729909, 0.212759, 0.333861], [0.735683, 0.215906, 0.330245], [0.741423, 0.219112, 0.326576], [0.747127, 0.222378, 0.322856], [0.752794, 0.225706, 0.319085], [0.758422, 0.229097, 0.315266], [0.764010, 0.232554, 0.311399], [0.769556, 0.236077, 0.307485], [0.775059, 0.239667, 0.303526], [0.780517, 0.243327, 0.299523], [0.785929, 0.247056, 0.295477], [0.791293, 0.250856, 0.291390], [0.796607, 0.254728, 0.287264], [0.801871, 0.258674, 0.283099], [0.807082, 0.262692, 0.278898], [0.812239, 0.266786, 0.274661], [0.817341, 0.270954, 0.270390], [0.822386, 0.275197, 0.266085], [0.827372, 0.279517, 0.261750], [0.832299, 0.283913, 0.257383], [0.837165, 0.288385, 0.252988], [0.841969, 0.292933, 0.248564], [0.846709, 0.297559, 0.244113], [0.851384, 0.302260, 0.239636], [0.855992, 0.307038, 0.235133], [0.860533, 0.311892, 0.230606], [0.865006, 0.316822, 0.226055], [0.869409, 0.321827, 0.221482], [0.873741, 0.326906, 0.216886], [0.878001, 0.332060, 0.212268], [0.882188, 0.337287, 0.207628], [0.886302, 0.342586, 0.202968], [0.890341, 0.347957, 0.198286], [0.894305, 0.353399, 0.193584], [0.898192, 0.358911, 0.188860], [0.902003, 0.364492, 0.184116], [0.905735, 0.370140, 0.179350], [0.909390, 0.375856, 0.174563], [0.912966, 0.381636, 0.169755], [0.916462, 0.387481, 0.164924], [0.919879, 0.393389, 0.160070], [0.923215, 0.399359, 0.155193], [0.926470, 0.405389, 0.150292], [0.929644, 0.411479, 0.145367], [0.932737, 0.417627, 0.140417], [0.935747, 0.423831, 0.135440], [0.938675, 0.430091, 0.130438], [0.941521, 0.436405, 0.125409], [0.944285, 0.442772, 0.120354], [0.946965, 0.449191, 0.115272], [0.949562, 0.455660, 0.110164], [0.952075, 0.462178, 0.105031], [0.954506, 0.468744, 0.099874], [0.956852, 0.475356, 0.094695], [0.959114, 0.482014, 0.089499], [0.961293, 0.488716, 0.084289], [0.963387, 0.495462, 0.079073], [0.965397, 0.502249, 0.073859], [0.967322, 0.509078, 0.068659], [0.969163, 0.515946, 0.063488], [0.970919, 0.522853, 0.058367], [0.972590, 0.529798, 0.053324], [0.974176, 0.536780, 0.048392], [0.975677, 0.543798, 0.043618], [0.977092, 0.550850, 0.039050], [0.978422, 0.557937, 0.034931], [0.979666, 0.565057, 0.031409], [0.980824, 0.572209, 0.028508], [0.981895, 0.579392, 0.026250], [0.982881, 0.586606, 0.024661], [0.983779, 0.593849, 0.023770], [0.984591, 0.601122, 0.023606], [0.985315, 0.608422, 0.024202], [0.985952, 0.615750, 0.025592], [0.986502, 0.623105, 0.027814], [0.986964, 0.630485, 0.030908], [0.987337, 0.637890, 0.034916], [0.987622, 0.645320, 0.039886], [0.987819, 0.652773, 0.045581], [0.987926, 0.660250, 0.051750], [0.987945, 0.667748, 0.058329], [0.987874, 0.675267, 0.065257], [0.987714, 0.682807, 0.072489], [0.987464, 0.690366, 0.079990], [0.987124, 0.697944, 0.087731], [0.986694, 0.705540, 0.095694], [0.986175, 0.713153, 0.103863], [0.985566, 0.720782, 0.112229], [0.984865, 0.728427, 0.120785], [0.984075, 0.736087, 0.129527], [0.983196, 0.743758, 0.138453], [0.982228, 0.751442, 0.147565], [0.981173, 0.759135, 0.156863], [0.980032, 0.766837, 0.166353], [0.978806, 0.774545, 0.176037], [0.977497, 0.782258, 0.185923], [0.976108, 0.789974, 0.196018], [0.974638, 0.797692, 0.206332], [0.973088, 0.805409, 0.216877], [0.971468, 0.813122, 0.227658], [0.969783, 0.820825, 0.238686], [0.968041, 0.828515, 0.249972], [0.966243, 0.836191, 0.261534], [0.964394, 0.843848, 0.273391], [0.962517, 0.851476, 0.285546], [0.960626, 0.859069, 0.298010], [0.958720, 0.866624, 0.310820], [0.956834, 0.874129, 0.323974], [0.954997, 0.881569, 0.337475], [0.953215, 0.888942, 0.351369], [0.951546, 0.896226, 0.365627], [0.950018, 0.903409, 0.380271], [0.948683, 0.910473, 0.395289], [0.947594, 0.917399, 0.410665], [0.946809, 0.924168, 0.426373], [0.946392, 0.930761, 0.442367], [0.946403, 0.937159, 0.458592], [0.946903, 0.943348, 0.474970], [0.947937, 0.949318, 0.491426], [0.949545, 0.955063, 0.507860], [0.951740, 0.960587, 0.524203], [0.954529, 0.965896, 0.540361], [0.957896, 0.971003, 0.556275], [0.961812, 0.975924, 0.571925], [0.966249, 0.980678, 0.587206], [0.971162, 0.985282, 0.602154], [0.976511, 0.989753, 0.616760], [0.982257, 0.994109, 0.631017], [0.988362, 0.998364, 0.644924]] _plasma_data = [[0.050383, 0.029803, 0.527975], [0.063536, 0.028426, 0.533124], [0.075353, 0.027206, 0.538007], [0.086222, 0.026125, 0.542658], [0.096379, 0.025165, 0.547103], [0.105980, 0.024309, 0.551368], [0.115124, 0.023556, 0.555468], [0.123903, 0.022878, 0.559423], [0.132381, 0.022258, 0.563250], [0.140603, 0.021687, 0.566959], [0.148607, 0.021154, 0.570562], [0.156421, 0.020651, 0.574065], [0.164070, 0.020171, 0.577478], [0.171574, 0.019706, 0.580806], [0.178950, 0.019252, 0.584054], [0.186213, 0.018803, 0.587228], [0.193374, 0.018354, 0.590330], [0.200445, 0.017902, 0.593364], [0.207435, 0.017442, 0.596333], [0.214350, 0.016973, 0.599239], [0.221197, 0.016497, 0.602083], [0.227983, 0.016007, 0.604867], [0.234715, 0.015502, 0.607592], [0.241396, 0.014979, 0.610259], [0.248032, 0.014439, 0.612868], [0.254627, 0.013882, 0.615419], [0.261183, 0.013308, 0.617911], [0.267703, 0.012716, 0.620346], [0.274191, 0.012109, 0.622722], [0.280648, 0.011488, 0.625038], [0.287076, 0.010855, 0.627295], [0.293478, 0.010213, 0.629490], [0.299855, 0.009561, 0.631624], [0.306210, 0.008902, 0.633694], [0.312543, 0.008239, 0.635700], [0.318856, 0.007576, 0.637640], [0.325150, 0.006915, 0.639512], [0.331426, 0.006261, 0.641316], [0.337683, 0.005618, 0.643049], [0.343925, 0.004991, 0.644710], [0.350150, 0.004382, 0.646298], [0.356359, 0.003798, 0.647810], [0.362553, 0.003243, 0.649245], [0.368733, 0.002724, 0.650601], [0.374897, 0.002245, 0.651876], [0.381047, 0.001814, 0.653068], [0.387183, 0.001434, 0.654177], [0.393304, 0.001114, 0.655199], [0.399411, 0.000859, 0.656133], [0.405503, 0.000678, 0.656977], [0.411580, 0.000577, 0.657730], [0.417642, 0.000564, 0.658390], [0.423689, 0.000646, 0.658956], [0.429719, 0.000831, 0.659425], [0.435734, 0.001127, 0.659797], [0.441732, 0.001540, 0.660069], [0.447714, 0.002080, 0.660240], [0.453677, 0.002755, 0.660310], [0.459623, 0.003574, 0.660277], [0.465550, 0.004545, 0.660139], [0.471457, 0.005678, 0.659897], [0.477344, 0.006980, 0.659549], [0.483210, 0.008460, 0.659095], [0.489055, 0.010127, 0.658534], [0.494877, 0.011990, 0.657865], [0.500678, 0.014055, 0.657088], [0.506454, 0.016333, 0.656202], [0.512206, 0.018833, 0.655209], [0.517933, 0.021563, 0.654109], [0.523633, 0.024532, 0.652901], [0.529306, 0.027747, 0.651586], [0.534952, 0.031217, 0.650165], [0.540570, 0.034950, 0.648640], [0.546157, 0.038954, 0.647010], [0.551715, 0.043136, 0.645277], [0.557243, 0.047331, 0.643443], [0.562738, 0.051545, 0.641509], [0.568201, 0.055778, 0.639477], [0.573632, 0.060028, 0.637349], [0.579029, 0.064296, 0.635126], [0.584391, 0.068579, 0.632812], [0.589719, 0.072878, 0.630408], [0.595011, 0.077190, 0.627917], [0.600266, 0.081516, 0.625342], [0.605485, 0.085854, 0.622686], [0.610667, 0.090204, 0.619951], [0.615812, 0.094564, 0.617140], [0.620919, 0.098934, 0.614257], [0.625987, 0.103312, 0.611305], [0.631017, 0.107699, 0.608287], [0.636008, 0.112092, 0.605205], [0.640959, 0.116492, 0.602065], [0.645872, 0.120898, 0.598867], [0.650746, 0.125309, 0.595617], [0.655580, 0.129725, 0.592317], [0.660374, 0.134144, 0.588971], [0.665129, 0.138566, 0.585582], [0.669845, 0.142992, 0.582154], [0.674522, 0.147419, 0.578688], [0.679160, 0.151848, 0.575189], [0.683758, 0.156278, 0.571660], [0.688318, 0.160709, 0.568103], [0.692840, 0.165141, 0.564522], [0.697324, 0.169573, 0.560919], [0.701769, 0.174005, 0.557296], [0.706178, 0.178437, 0.553657], [0.710549, 0.182868, 0.550004], [0.714883, 0.187299, 0.546338], [0.719181, 0.191729, 0.542663], [0.723444, 0.196158, 0.538981], [0.727670, 0.200586, 0.535293], [0.731862, 0.205013, 0.531601], [0.736019, 0.209439, 0.527908], [0.740143, 0.213864, 0.524216], [0.744232, 0.218288, 0.520524], [0.748289, 0.222711, 0.516834], [0.752312, 0.227133, 0.513149], [0.756304, 0.231555, 0.509468], [0.760264, 0.235976, 0.505794], [0.764193, 0.240396, 0.502126], [0.768090, 0.244817, 0.498465], [0.771958, 0.249237, 0.494813], [0.775796, 0.253658, 0.491171], [0.779604, 0.258078, 0.487539], [0.783383, 0.262500, 0.483918], [0.787133, 0.266922, 0.480307], [0.790855, 0.271345, 0.476706], [0.794549, 0.275770, 0.473117], [0.798216, 0.280197, 0.469538], [0.801855, 0.284626, 0.465971], [0.805467, 0.289057, 0.462415], [0.809052, 0.293491, 0.458870], [0.812612, 0.297928, 0.455338], [0.816144, 0.302368, 0.451816], [0.819651, 0.306812, 0.448306], [0.823132, 0.311261, 0.444806], [0.826588, 0.315714, 0.441316], [0.830018, 0.320172, 0.437836], [0.833422, 0.324635, 0.434366], [0.836801, 0.329105, 0.430905], [0.840155, 0.333580, 0.427455], [0.843484, 0.338062, 0.424013], [0.846788, 0.342551, 0.420579], [0.850066, 0.347048, 0.417153], [0.853319, 0.351553, 0.413734], [0.856547, 0.356066, 0.410322], [0.859750, 0.360588, 0.406917], [0.862927, 0.365119, 0.403519], [0.866078, 0.369660, 0.400126], [0.869203, 0.374212, 0.396738], [0.872303, 0.378774, 0.393355], [0.875376, 0.383347, 0.389976], [0.878423, 0.387932, 0.386600], [0.881443, 0.392529, 0.383229], [0.884436, 0.397139, 0.379860], [0.887402, 0.401762, 0.376494], [0.890340, 0.406398, 0.373130], [0.893250, 0.411048, 0.369768], [0.896131, 0.415712, 0.366407], [0.898984, 0.420392, 0.363047], [0.901807, 0.425087, 0.359688], [0.904601, 0.429797, 0.356329], [0.907365, 0.434524, 0.352970], [0.910098, 0.439268, 0.349610], [0.912800, 0.444029, 0.346251], [0.915471, 0.448807, 0.342890], [0.918109, 0.453603, 0.339529], [0.920714, 0.458417, 0.336166], [0.923287, 0.463251, 0.332801], [0.925825, 0.468103, 0.329435], [0.928329, 0.472975, 0.326067], [0.930798, 0.477867, 0.322697], [0.933232, 0.482780, 0.319325], [0.935630, 0.487712, 0.315952], [0.937990, 0.492667, 0.312575], [0.940313, 0.497642, 0.309197], [0.942598, 0.502639, 0.305816], [0.944844, 0.507658, 0.302433], [0.947051, 0.512699, 0.299049], [0.949217, 0.517763, 0.295662], [0.951344, 0.522850, 0.292275], [0.953428, 0.527960, 0.288883], [0.955470, 0.533093, 0.285490], [0.957469, 0.538250, 0.282096], [0.959424, 0.543431, 0.278701], [0.961336, 0.548636, 0.275305], [0.963203, 0.553865, 0.271909], [0.965024, 0.559118, 0.268513], [0.966798, 0.564396, 0.265118], [0.968526, 0.569700, 0.261721], [0.970205, 0.575028, 0.258325], [0.971835, 0.580382, 0.254931], [0.973416, 0.585761, 0.251540], [0.974947, 0.591165, 0.248151], [0.976428, 0.596595, 0.244767], [0.977856, 0.602051, 0.241387], [0.979233, 0.607532, 0.238013], [0.980556, 0.613039, 0.234646], [0.981826, 0.618572, 0.231287], [0.983041, 0.624131, 0.227937], [0.984199, 0.629718, 0.224595], [0.985301, 0.635330, 0.221265], [0.986345, 0.640969, 0.217948], [0.987332, 0.646633, 0.214648], [0.988260, 0.652325, 0.211364], [0.989128, 0.658043, 0.208100], [0.989935, 0.663787, 0.204859], [0.990681, 0.669558, 0.201642], [0.991365, 0.675355, 0.198453], [0.991985, 0.681179, 0.195295], [0.992541, 0.687030, 0.192170], [0.993032, 0.692907, 0.189084], [0.993456, 0.698810, 0.186041], [0.993814, 0.704741, 0.183043], [0.994103, 0.710698, 0.180097], [0.994324, 0.716681, 0.177208], [0.994474, 0.722691, 0.174381], [0.994553, 0.728728, 0.171622], [0.994561, 0.734791, 0.168938], [0.994495, 0.740880, 0.166335], [0.994355, 0.746995, 0.163821], [0.994141, 0.753137, 0.161404], [0.993851, 0.759304, 0.159092], [0.993482, 0.765499, 0.156891], [0.993033, 0.771720, 0.154808], [0.992505, 0.777967, 0.152855], [0.991897, 0.784239, 0.151042], [0.991209, 0.790537, 0.149377], [0.990439, 0.796859, 0.147870], [0.989587, 0.803205, 0.146529], [0.988648, 0.809579, 0.145357], [0.987621, 0.815978, 0.144363], [0.986509, 0.822401, 0.143557], [0.985314, 0.828846, 0.142945], [0.984031, 0.835315, 0.142528], [0.982653, 0.841812, 0.142303], [0.981190, 0.848329, 0.142279], [0.979644, 0.854866, 0.142453], [0.977995, 0.861432, 0.142808], [0.976265, 0.868016, 0.143351], [0.974443, 0.874622, 0.144061], [0.972530, 0.881250, 0.144923], [0.970533, 0.887896, 0.145919], [0.968443, 0.894564, 0.147014], [0.966271, 0.901249, 0.148180], [0.964021, 0.907950, 0.149370], [0.961681, 0.914672, 0.150520], [0.959276, 0.921407, 0.151566], [0.956808, 0.928152, 0.152409], [0.954287, 0.934908, 0.152921], [0.951726, 0.941671, 0.152925], [0.949151, 0.948435, 0.152178], [0.946602, 0.955190, 0.150328], [0.944152, 0.961916, 0.146861], [0.941896, 0.968590, 0.140956], [0.940015, 0.975158, 0.131326]] _viridis_data = [[0.267004, 0.004874, 0.329415], [0.268510, 0.009605, 0.335427], [0.269944, 0.014625, 0.341379], [0.271305, 0.019942, 0.347269], [0.272594, 0.025563, 0.353093], [0.273809, 0.031497, 0.358853], [0.274952, 0.037752, 0.364543], [0.276022, 0.044167, 0.370164], [0.277018, 0.050344, 0.375715], [0.277941, 0.056324, 0.381191], [0.278791, 0.062145, 0.386592], [0.279566, 0.067836, 0.391917], [0.280267, 0.073417, 0.397163], [0.280894, 0.078907, 0.402329], [0.281446, 0.084320, 0.407414], [0.281924, 0.089666, 0.412415], [0.282327, 0.094955, 0.417331], [0.282656, 0.100196, 0.422160], [0.282910, 0.105393, 0.426902], [0.283091, 0.110553, 0.431554], [0.283197, 0.115680, 0.436115], [0.283229, 0.120777, 0.440584], [0.283187, 0.125848, 0.444960], [0.283072, 0.130895, 0.449241], [0.282884, 0.135920, 0.453427], [0.282623, 0.140926, 0.457517], [0.282290, 0.145912, 0.461510], [0.281887, 0.150881, 0.465405], [0.281412, 0.155834, 0.469201], [0.280868, 0.160771, 0.472899], [0.280255, 0.165693, 0.476498], [0.279574, 0.170599, 0.479997], [0.278826, 0.175490, 0.483397], [0.278012, 0.180367, 0.486697], [0.277134, 0.185228, 0.489898], [0.276194, 0.190074, 0.493001], [0.275191, 0.194905, 0.496005], [0.274128, 0.199721, 0.498911], [0.273006, 0.204520, 0.501721], [0.271828, 0.209303, 0.504434], [0.270595, 0.214069, 0.507052], [0.269308, 0.218818, 0.509577], [0.267968, 0.223549, 0.512008], [0.266580, 0.228262, 0.514349], [0.265145, 0.232956, 0.516599], [0.263663, 0.237631, 0.518762], [0.262138, 0.242286, 0.520837], [0.260571, 0.246922, 0.522828], [0.258965, 0.251537, 0.524736], [0.257322, 0.256130, 0.526563], [0.255645, 0.260703, 0.528312], [0.253935, 0.265254, 0.529983], [0.252194, 0.269783, 0.531579], [0.250425, 0.274290, 0.533103], [0.248629, 0.278775, 0.534556], [0.246811, 0.283237, 0.535941], [0.244972, 0.287675, 0.537260], [0.243113, 0.292092, 0.538516], [0.241237, 0.296485, 0.539709], [0.239346, 0.300855, 0.540844], [0.237441, 0.305202, 0.541921], [0.235526, 0.309527, 0.542944], [0.233603, 0.313828, 0.543914], [0.231674, 0.318106, 0.544834], [0.229739, 0.322361, 0.545706], [0.227802, 0.326594, 0.546532], [0.225863, 0.330805, 0.547314], [0.223925, 0.334994, 0.548053], [0.221989, 0.339161, 0.548752], [0.220057, 0.343307, 0.549413], [0.218130, 0.347432, 0.550038], [0.216210, 0.351535, 0.550627], [0.214298, 0.355619, 0.551184], [0.212395, 0.359683, 0.551710], [0.210503, 0.363727, 0.552206], [0.208623, 0.367752, 0.552675], [0.206756, 0.371758, 0.553117], [0.204903, 0.375746, 0.553533], [0.203063, 0.379716, 0.553925], [0.201239, 0.383670, 0.554294], [0.199430, 0.387607, 0.554642], [0.197636, 0.391528, 0.554969], [0.195860, 0.395433, 0.555276], [0.194100, 0.399323, 0.555565], [0.192357, 0.403199, 0.555836], [0.190631, 0.407061, 0.556089], [0.188923, 0.410910, 0.556326], [0.187231, 0.414746, 0.556547], [0.185556, 0.418570, 0.556753], [0.183898, 0.422383, 0.556944], [0.182256, 0.426184, 0.557120], [0.180629, 0.429975, 0.557282], [0.179019, 0.433756, 0.557430], [0.177423, 0.437527, 0.557565], [0.175841, 0.441290, 0.557685], [0.174274, 0.445044, 0.557792], [0.172719, 0.448791, 0.557885], [0.171176, 0.452530, 0.557965], [0.169646, 0.456262, 0.558030], [0.168126, 0.459988, 0.558082], [0.166617, 0.463708, 0.558119], [0.165117, 0.467423, 0.558141], [0.163625, 0.471133, 0.558148], [0.162142, 0.474838, 0.558140], [0.160665, 0.478540, 0.558115], [0.159194, 0.482237, 0.558073], [0.157729, 0.485932, 0.558013], [0.156270, 0.489624, 0.557936], [0.154815, 0.493313, 0.557840], [0.153364, 0.497000, 0.557724], [0.151918, 0.500685, 0.557587], [0.150476, 0.504369, 0.557430], [0.149039, 0.508051, 0.557250], [0.147607, 0.511733, 0.557049], [0.146180, 0.515413, 0.556823], [0.144759, 0.519093, 0.556572], [0.143343, 0.522773, 0.556295], [0.141935, 0.526453, 0.555991], [0.140536, 0.530132, 0.555659], [0.139147, 0.533812, 0.555298], [0.137770, 0.537492, 0.554906], [0.136408, 0.541173, 0.554483], [0.135066, 0.544853, 0.554029], [0.133743, 0.548535, 0.553541], [0.132444, 0.552216, 0.553018], [0.131172, 0.555899, 0.552459], [0.129933, 0.559582, 0.551864], [0.128729, 0.563265, 0.551229], [0.127568, 0.566949, 0.550556], [0.126453, 0.570633, 0.549841], [0.125394, 0.574318, 0.549086], [0.124395, 0.578002, 0.548287], [0.123463, 0.581687, 0.547445], [0.122606, 0.585371, 0.546557], [0.121831, 0.589055, 0.545623], [0.121148, 0.592739, 0.544641], [0.120565, 0.596422, 0.543611], [0.120092, 0.600104, 0.542530], [0.119738, 0.603785, 0.541400], [0.119512, 0.607464, 0.540218], [0.119423, 0.611141, 0.538982], [0.119483, 0.614817, 0.537692], [0.119699, 0.618490, 0.536347], [0.120081, 0.622161, 0.534946], [0.120638, 0.625828, 0.533488], [0.121380, 0.629492, 0.531973], [0.122312, 0.633153, 0.530398], [0.123444, 0.636809, 0.528763], [0.124780, 0.640461, 0.527068], [0.126326, 0.644107, 0.525311], [0.128087, 0.647749, 0.523491], [0.130067, 0.651384, 0.521608], [0.132268, 0.655014, 0.519661], [0.134692, 0.658636, 0.517649], [0.137339, 0.662252, 0.515571], [0.140210, 0.665859, 0.513427], [0.143303, 0.669459, 0.511215], [0.146616, 0.673050, 0.508936], [0.150148, 0.676631, 0.506589], [0.153894, 0.680203, 0.504172], [0.157851, 0.683765, 0.501686], [0.162016, 0.687316, 0.499129], [0.166383, 0.690856, 0.496502], [0.170948, 0.694384, 0.493803], [0.175707, 0.697900, 0.491033], [0.180653, 0.701402, 0.488189], [0.185783, 0.704891, 0.485273], [0.191090, 0.708366, 0.482284], [0.196571, 0.711827, 0.479221], [0.202219, 0.715272, 0.476084], [0.208030, 0.718701, 0.472873], [0.214000, 0.722114, 0.469588], [0.220124, 0.725509, 0.466226], [0.226397, 0.728888, 0.462789], [0.232815, 0.732247, 0.459277], [0.239374, 0.735588, 0.455688], [0.246070, 0.738910, 0.452024], [0.252899, 0.742211, 0.448284], [0.259857, 0.745492, 0.444467], [0.266941, 0.748751, 0.440573], [0.274149, 0.751988, 0.436601], [0.281477, 0.755203, 0.432552], [0.288921, 0.758394, 0.428426], [0.296479, 0.761561, 0.424223], [0.304148, 0.764704, 0.419943], [0.311925, 0.767822, 0.415586], [0.319809, 0.770914, 0.411152], [0.327796, 0.773980, 0.406640], [0.335885, 0.777018, 0.402049], [0.344074, 0.780029, 0.397381], [0.352360, 0.783011, 0.392636], [0.360741, 0.785964, 0.387814], [0.369214, 0.788888, 0.382914], [0.377779, 0.791781, 0.377939], [0.386433, 0.794644, 0.372886], [0.395174, 0.797475, 0.367757], [0.404001, 0.800275, 0.362552], [0.412913, 0.803041, 0.357269], [0.421908, 0.805774, 0.351910], [0.430983, 0.808473, 0.346476], [0.440137, 0.811138, 0.340967], [0.449368, 0.813768, 0.335384], [0.458674, 0.816363, 0.329727], [0.468053, 0.818921, 0.323998], [0.477504, 0.821444, 0.318195], [0.487026, 0.823929, 0.312321], [0.496615, 0.826376, 0.306377], [0.506271, 0.828786, 0.300362], [0.515992, 0.831158, 0.294279], [0.525776, 0.833491, 0.288127], [0.535621, 0.835785, 0.281908], [0.545524, 0.838039, 0.275626], [0.555484, 0.840254, 0.269281], [0.565498, 0.842430, 0.262877], [0.575563, 0.844566, 0.256415], [0.585678, 0.846661, 0.249897], [0.595839, 0.848717, 0.243329], [0.606045, 0.850733, 0.236712], [0.616293, 0.852709, 0.230052], [0.626579, 0.854645, 0.223353], [0.636902, 0.856542, 0.216620], [0.647257, 0.858400, 0.209861], [0.657642, 0.860219, 0.203082], [0.668054, 0.861999, 0.196293], [0.678489, 0.863742, 0.189503], [0.688944, 0.865448, 0.182725], [0.699415, 0.867117, 0.175971], [0.709898, 0.868751, 0.169257], [0.720391, 0.870350, 0.162603], [0.730889, 0.871916, 0.156029], [0.741388, 0.873449, 0.149561], [0.751884, 0.874951, 0.143228], [0.762373, 0.876424, 0.137064], [0.772852, 0.877868, 0.131109], [0.783315, 0.879285, 0.125405], [0.793760, 0.880678, 0.120005], [0.804182, 0.882046, 0.114965], [0.814576, 0.883393, 0.110347], [0.824940, 0.884720, 0.106217], [0.835270, 0.886029, 0.102646], [0.845561, 0.887322, 0.099702], [0.855810, 0.888601, 0.097452], [0.866013, 0.889868, 0.095953], [0.876168, 0.891125, 0.095250], [0.886271, 0.892374, 0.095374], [0.896320, 0.893616, 0.096335], [0.906311, 0.894855, 0.098125], [0.916242, 0.896091, 0.100717], [0.926106, 0.897330, 0.104071], [0.935904, 0.898570, 0.108131], [0.945636, 0.899815, 0.112838], [0.955300, 0.901065, 0.118128], [0.964894, 0.902323, 0.123941], [0.974417, 0.903590, 0.130215], [0.983868, 0.904867, 0.136897], [0.993248, 0.906157, 0.143936]] from matplotlib.colors import ListedColormap cmaps = {} for (name, data) in (('magma', _magma_data), ('inferno', _inferno_data), ('plasma', _plasma_data), ('viridis', _viridis_data)): cmaps[name] = ListedColormap(data, name=name) magma = cmaps['magma'] inferno = cmaps['inferno'] plasma = cmaps['plasma'] viridis = cmaps['viridis']
mit
anntzer/scikit-learn
sklearn/ensemble/__init__.py
12
1655
""" The :mod:`sklearn.ensemble` module includes ensemble-based methods for classification, regression and anomaly detection. """ import typing from ._base import BaseEnsemble from ._forest import RandomForestClassifier from ._forest import RandomForestRegressor from ._forest import RandomTreesEmbedding from ._forest import ExtraTreesClassifier from ._forest import ExtraTreesRegressor from ._bagging import BaggingClassifier from ._bagging import BaggingRegressor from ._iforest import IsolationForest from ._weight_boosting import AdaBoostClassifier from ._weight_boosting import AdaBoostRegressor from ._gb import GradientBoostingClassifier from ._gb import GradientBoostingRegressor from ._voting import VotingClassifier from ._voting import VotingRegressor from ._stacking import StackingClassifier from ._stacking import StackingRegressor if typing.TYPE_CHECKING: # Avoid errors in type checkers (e.g. mypy) for experimental estimators. # TODO: remove this check once the estimator is no longer experimental. from ._hist_gradient_boosting.gradient_boosting import ( # noqa HistGradientBoostingRegressor, HistGradientBoostingClassifier ) __all__ = ["BaseEnsemble", "RandomForestClassifier", "RandomForestRegressor", "RandomTreesEmbedding", "ExtraTreesClassifier", "ExtraTreesRegressor", "BaggingClassifier", "BaggingRegressor", "IsolationForest", "GradientBoostingClassifier", "GradientBoostingRegressor", "AdaBoostClassifier", "AdaBoostRegressor", "VotingClassifier", "VotingRegressor", "StackingClassifier", "StackingRegressor", ]
bsd-3-clause
natj/bender
paper/figs/fig9.py
1
4141
import numpy as np import math from pylab import * from palettable.wesanderson import Zissou_5 as wsZ import matplotlib.ticker as mtick from scipy.interpolate import interp1d from scipy.interpolate import griddata from scipy.signal import savgol_filter def smooth(xx, yy): yy = savgol_filter(yy, 7, 2) np.clip(yy, 0.0, 1000.0, out=yy) yy[0] = 0.0 yy[-1] = 0.0 return xx, yy #Read JN files def read_lineprof(fname): da = np.genfromtxt(fname, delimiter=",") des = np.diff(da[:,0])[2] norm = np.sum(des*da[:,1]) return da[:,0],da[:,1]/norm #Read JN files def read_csv(fname): da = np.genfromtxt(fname, delimiter=",") des = np.diff(da[:,0])[2] norm = np.sum(des*da[:,1]) return da[:,0],da[:,1] #/norm ## Plot fig = figure(figsize=(5,3), dpi=80) rc('font', family='serif') rc('xtick', labelsize='xx-small') rc('ytick', labelsize='xx-small') gs = GridSpec(1, 1) #gs.update(wspace = 0.34) #gs.update(hspace = 0.4) lsize = 10.0 xmin = 0.69 xmax = 0.82 #error window limits eymin = -0.5 eymax = 0.5 #path to files #path_JN = "../../out3/lines/" path_JN = "../../out/lines2/" #labels size tsize = 10.0 nu = '700' #fig.text(0.5, 0.92, '$\\theta_s = 18^{\\circ}$', ha='center', va='center', size=tsize) #fig.text(0.5, 0.72, '$\\theta_s = 45^{\\circ}$', ha='center', va='center', size=tsize) #fig.text(0.5, 0.52, '$\\theta_s = 90^{\\circ}$', ha='center', va='center', size=tsize) #fig.text(0.5, 0.32, 'Hopf $\\theta_s = 45^{\circ}$', ha='center', va='center', size=tsize) #fig.text(0.5, 0.12, 'Phase',ha='center', va='center', size=lsize) ax1 = subplot(gs[0,0]) ax1.minorticks_on() ax1.set_xlim(xmin, xmax) ax1.set_ylim(0.0, 30) ax1.set_ylabel('Normalized flux',size=lsize) ax1.set_xlabel('Energy $E/E\'$',size=lsize) #xx1, yy1 = read_lineprof(path_JN+'lineprof_f700pbbr10m1.4i20.csv') #ax1.plot(xx1, yy1, "k--") #xx2, yy2 = read_lineprof(path_JN+'lineprof_obl_HTq0_f700pbbr10m1.4i20.csv') #ax1.plot(xx2, yy2, "k-") #lineprof_obl_HTq3_f700pbbr10m1.4i20.csv #lineprof_obl_HTq5_f700pbbr10m1.4i20.csv #lineprof_obl_HTq2_f700pbbr10m1.4i20.csv files_JN = [ "lineprof_f700pbbr10m1.4i20.csv", "lineprof_obl_f700pbbr10m1.4i20.csv", #"lineprof_sph2_HTqfix_f700pbbr10m1.4i20.csv"] #"lineprof_obl_HTq0_f700pbbr10m1.4i20.csv", "lineprof_obl_HTq1_f700pbbr10m1.4i20.csv"] #"lineprof_obl_HTq4_f700pbbr10m1.4i20.csv"] files_JN = ['sch/lineprofile_f700_bb_r10_m1.4_i20.csv', 'obl/lineprofile_f700_bb_r10_m1.4_i20.csv', 'q/lineprofile_f700_bb_r10_m1.4_i20.csv'] cols = ["black", "blue", "red", "magenta"] i = 0 for file_name in files_JN: xx, yy = read_lineprof(path_JN+file_name) xx, yy = smooth(xx, yy) ax1.plot(xx, yy, color=cols[i], linestyle="solid") i += 1 #path_JN = "../../out3/lines/" xx, yy = read_lineprof("../../out3/lines/lineprof_obl_HTq4_f700pbbr10m1.4i20.csv") ax1.plot(xx, yy, color="red", linestyle="dashed") #files_Bau = [ #"sch+dopp.csv", #"sch+dopp+obl.csv", #"HT.csv", #"HT_obl.csv"] files_Bau = ['sch.csv', 'obl.csv', 'ht.csv'] i = 0 for file_name in files_Bau: xx, yy = read_csv(path_JN+file_name) #rescale xx for correct scaling #xx = (xx-0.72)/(0.89-0.72)*(0.8-0.72) + 0.72 #ax1.plot(xx, yy, color=cols[i], linestyle="dashed") i += 1 ############ q's #xx3, yy3 = read_lineprof(path_JN+'lineprof_obl_HTq1_f700pbbr10m1.4i20.csv') #ax1.plot(xx3, yy3, "k-", label="$q = -0.268$") # #xx4, yy4 = read_lineprof(path_JN+'lineprof_obl_HTq2_f700pbbr10m1.4i20.csv') #ax1.plot(xx4, yy4, "r-", label="$q \\times 2$") # #xx5, yy5 = read_lineprof(path_JN+'lineprof_obl_HTq3_f700pbbr10m1.4i20.csv') #ax1.plot(xx5, yy5, "g-", label="$q \\times 3$") # #xx6, yy6 = read_lineprof(path_JN+'lineprof_obl_HTq4_f700pbbr10m1.4i20.csv') #ax1.plot(xx6, yy6, "b-", label="$q \\times 4$") # #xx7, yy7 = read_lineprof(path_JN+'lineprof_obl_HTq5_f700pbbr10m1.4i20.csv') #ax1.plot(xx7, yy7, "m-", label="$q \\times 5$") # #legend = ax1.legend(loc='upper left', shadow=False, labelspacing=0.1) #for label in legend.get_texts(): # label.set_fontsize('x-small') savefig('fig9_testi.pdf', bbox_inches='tight')
mit
pianomania/scikit-learn
sklearn/linear_model/stochastic_gradient.py
16
50617
# Authors: Peter Prettenhofer <peter.prettenhofer@gmail.com> (main author) # Mathieu Blondel (partial_fit support) # # License: BSD 3 clause """Classification and regression using Stochastic Gradient Descent (SGD).""" import numpy as np from abc import ABCMeta, abstractmethod from ..externals.joblib import Parallel, delayed from .base import LinearClassifierMixin, SparseCoefMixin from .base import make_dataset from ..base import BaseEstimator, RegressorMixin from ..utils import check_array, check_random_state, check_X_y from ..utils.extmath import safe_sparse_dot from ..utils.multiclass import _check_partial_fit_first_call from ..utils.validation import check_is_fitted from ..externals import six from .sgd_fast import plain_sgd, average_sgd from ..utils.fixes import astype from ..utils import compute_class_weight from ..utils import deprecated from .sgd_fast import Hinge from .sgd_fast import SquaredHinge from .sgd_fast import Log from .sgd_fast import ModifiedHuber from .sgd_fast import SquaredLoss from .sgd_fast import Huber from .sgd_fast import EpsilonInsensitive from .sgd_fast import SquaredEpsilonInsensitive LEARNING_RATE_TYPES = {"constant": 1, "optimal": 2, "invscaling": 3, "pa1": 4, "pa2": 5} PENALTY_TYPES = {"none": 0, "l2": 2, "l1": 1, "elasticnet": 3} DEFAULT_EPSILON = 0.1 # Default value of ``epsilon`` parameter. class BaseSGD(six.with_metaclass(ABCMeta, BaseEstimator, SparseCoefMixin)): """Base class for SGD classification and regression.""" def __init__(self, loss, penalty='l2', alpha=0.0001, C=1.0, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=0.1, random_state=None, learning_rate="optimal", eta0=0.0, power_t=0.5, warm_start=False, average=False): self.loss = loss self.penalty = penalty self.learning_rate = learning_rate self.epsilon = epsilon self.alpha = alpha self.C = C self.l1_ratio = l1_ratio self.fit_intercept = fit_intercept self.n_iter = n_iter self.shuffle = shuffle self.random_state = random_state self.verbose = verbose self.eta0 = eta0 self.power_t = power_t self.warm_start = warm_start self.average = average self._validate_params() def set_params(self, *args, **kwargs): super(BaseSGD, self).set_params(*args, **kwargs) self._validate_params() return self @abstractmethod def fit(self, X, y): """Fit model.""" def _validate_params(self): """Validate input params. """ if not isinstance(self.shuffle, bool): raise ValueError("shuffle must be either True or False") if self.n_iter <= 0: raise ValueError("n_iter must be > zero") if not (0.0 <= self.l1_ratio <= 1.0): raise ValueError("l1_ratio must be in [0, 1]") if self.alpha < 0.0: raise ValueError("alpha must be >= 0") if self.learning_rate in ("constant", "invscaling"): if self.eta0 <= 0.0: raise ValueError("eta0 must be > 0") if self.learning_rate == "optimal" and self.alpha == 0: raise ValueError("alpha must be > 0 since " "learning_rate is 'optimal'. alpha is used " "to compute the optimal learning rate.") # raises ValueError if not registered self._get_penalty_type(self.penalty) self._get_learning_rate_type(self.learning_rate) if self.loss not in self.loss_functions: raise ValueError("The loss %s is not supported. " % self.loss) def _get_loss_function(self, loss): """Get concrete ``LossFunction`` object for str ``loss``. """ try: loss_ = self.loss_functions[loss] loss_class, args = loss_[0], loss_[1:] if loss in ('huber', 'epsilon_insensitive', 'squared_epsilon_insensitive'): args = (self.epsilon, ) return loss_class(*args) except KeyError: raise ValueError("The loss %s is not supported. " % loss) def _get_learning_rate_type(self, learning_rate): try: return LEARNING_RATE_TYPES[learning_rate] except KeyError: raise ValueError("learning rate %s " "is not supported. " % learning_rate) def _get_penalty_type(self, penalty): penalty = str(penalty).lower() try: return PENALTY_TYPES[penalty] except KeyError: raise ValueError("Penalty %s is not supported. " % penalty) def _validate_sample_weight(self, sample_weight, n_samples): """Set the sample weight array.""" if sample_weight is None: # uniform sample weights sample_weight = np.ones(n_samples, dtype=np.float64, order='C') else: # user-provided array sample_weight = np.asarray(sample_weight, dtype=np.float64, order="C") if sample_weight.shape[0] != n_samples: raise ValueError("Shapes of X and sample_weight do not match.") return sample_weight def _allocate_parameter_mem(self, n_classes, n_features, coef_init=None, intercept_init=None): """Allocate mem for parameters; initialize if provided.""" if n_classes > 2: # allocate coef_ for multi-class if coef_init is not None: coef_init = np.asarray(coef_init, order="C") if coef_init.shape != (n_classes, n_features): raise ValueError("Provided ``coef_`` does not match " "dataset. ") self.coef_ = coef_init else: self.coef_ = np.zeros((n_classes, n_features), dtype=np.float64, order="C") # allocate intercept_ for multi-class if intercept_init is not None: intercept_init = np.asarray(intercept_init, order="C") if intercept_init.shape != (n_classes, ): raise ValueError("Provided intercept_init " "does not match dataset.") self.intercept_ = intercept_init else: self.intercept_ = np.zeros(n_classes, dtype=np.float64, order="C") else: # allocate coef_ for binary problem if coef_init is not None: coef_init = np.asarray(coef_init, dtype=np.float64, order="C") coef_init = coef_init.ravel() if coef_init.shape != (n_features,): raise ValueError("Provided coef_init does not " "match dataset.") self.coef_ = coef_init else: self.coef_ = np.zeros(n_features, dtype=np.float64, order="C") # allocate intercept_ for binary problem if intercept_init is not None: intercept_init = np.asarray(intercept_init, dtype=np.float64) if intercept_init.shape != (1,) and intercept_init.shape != (): raise ValueError("Provided intercept_init " "does not match dataset.") self.intercept_ = intercept_init.reshape(1,) else: self.intercept_ = np.zeros(1, dtype=np.float64, order="C") # initialize average parameters if self.average > 0: self.standard_coef_ = self.coef_ self.standard_intercept_ = self.intercept_ self.average_coef_ = np.zeros(self.coef_.shape, dtype=np.float64, order="C") self.average_intercept_ = np.zeros(self.standard_intercept_.shape, dtype=np.float64, order="C") def _prepare_fit_binary(est, y, i): """Initialization for fit_binary. Returns y, coef, intercept. """ y_i = np.ones(y.shape, dtype=np.float64, order="C") y_i[y != est.classes_[i]] = -1.0 average_intercept = 0 average_coef = None if len(est.classes_) == 2: if not est.average: coef = est.coef_.ravel() intercept = est.intercept_[0] else: coef = est.standard_coef_.ravel() intercept = est.standard_intercept_[0] average_coef = est.average_coef_.ravel() average_intercept = est.average_intercept_[0] else: if not est.average: coef = est.coef_[i] intercept = est.intercept_[i] else: coef = est.standard_coef_[i] intercept = est.standard_intercept_[i] average_coef = est.average_coef_[i] average_intercept = est.average_intercept_[i] return y_i, coef, intercept, average_coef, average_intercept def fit_binary(est, i, X, y, alpha, C, learning_rate, n_iter, pos_weight, neg_weight, sample_weight): """Fit a single binary classifier. The i'th class is considered the "positive" class. """ # if average is not true, average_coef, and average_intercept will be # unused y_i, coef, intercept, average_coef, average_intercept = \ _prepare_fit_binary(est, y, i) assert y_i.shape[0] == y.shape[0] == sample_weight.shape[0] dataset, intercept_decay = make_dataset(X, y_i, sample_weight) penalty_type = est._get_penalty_type(est.penalty) learning_rate_type = est._get_learning_rate_type(learning_rate) # XXX should have random_state_! random_state = check_random_state(est.random_state) # numpy mtrand expects a C long which is a signed 32 bit integer under # Windows seed = random_state.randint(0, np.iinfo(np.int32).max) if not est.average: return plain_sgd(coef, intercept, est.loss_function_, penalty_type, alpha, C, est.l1_ratio, dataset, n_iter, int(est.fit_intercept), int(est.verbose), int(est.shuffle), seed, pos_weight, neg_weight, learning_rate_type, est.eta0, est.power_t, est.t_, intercept_decay) else: standard_coef, standard_intercept, average_coef, \ average_intercept = average_sgd(coef, intercept, average_coef, average_intercept, est.loss_function_, penalty_type, alpha, C, est.l1_ratio, dataset, n_iter, int(est.fit_intercept), int(est.verbose), int(est.shuffle), seed, pos_weight, neg_weight, learning_rate_type, est.eta0, est.power_t, est.t_, intercept_decay, est.average) if len(est.classes_) == 2: est.average_intercept_[0] = average_intercept else: est.average_intercept_[i] = average_intercept return standard_coef, standard_intercept class BaseSGDClassifier(six.with_metaclass(ABCMeta, BaseSGD, LinearClassifierMixin)): loss_functions = { "hinge": (Hinge, 1.0), "squared_hinge": (SquaredHinge, 1.0), "perceptron": (Hinge, 0.0), "log": (Log, ), "modified_huber": (ModifiedHuber, ), "squared_loss": (SquaredLoss, ), "huber": (Huber, DEFAULT_EPSILON), "epsilon_insensitive": (EpsilonInsensitive, DEFAULT_EPSILON), "squared_epsilon_insensitive": (SquaredEpsilonInsensitive, DEFAULT_EPSILON), } @abstractmethod def __init__(self, loss="hinge", penalty='l2', alpha=0.0001, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=DEFAULT_EPSILON, n_jobs=1, random_state=None, learning_rate="optimal", eta0=0.0, power_t=0.5, class_weight=None, warm_start=False, average=False): super(BaseSGDClassifier, self).__init__(loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio, fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle, verbose=verbose, epsilon=epsilon, random_state=random_state, learning_rate=learning_rate, eta0=eta0, power_t=power_t, warm_start=warm_start, average=average) self.class_weight = class_weight self.n_jobs = int(n_jobs) @property @deprecated("Attribute loss_function was deprecated in version 0.19 and " "will be removed in 0.21. Use 'loss_function_' instead") def loss_function(self): return self.loss_function_ def _partial_fit(self, X, y, alpha, C, loss, learning_rate, n_iter, classes, sample_weight, coef_init, intercept_init): X, y = check_X_y(X, y, 'csr', dtype=np.float64, order="C") n_samples, n_features = X.shape self._validate_params() _check_partial_fit_first_call(self, classes) n_classes = self.classes_.shape[0] # Allocate datastructures from input arguments self._expanded_class_weight = compute_class_weight(self.class_weight, self.classes_, y) sample_weight = self._validate_sample_weight(sample_weight, n_samples) if getattr(self, "coef_", None) is None or coef_init is not None: self._allocate_parameter_mem(n_classes, n_features, coef_init, intercept_init) elif n_features != self.coef_.shape[-1]: raise ValueError("Number of features %d does not match previous " "data %d." % (n_features, self.coef_.shape[-1])) self.loss_function_ = self._get_loss_function(loss) if not hasattr(self, "t_"): self.t_ = 1.0 # delegate to concrete training procedure if n_classes > 2: self._fit_multiclass(X, y, alpha=alpha, C=C, learning_rate=learning_rate, sample_weight=sample_weight, n_iter=n_iter) elif n_classes == 2: self._fit_binary(X, y, alpha=alpha, C=C, learning_rate=learning_rate, sample_weight=sample_weight, n_iter=n_iter) else: raise ValueError("The number of class labels must be " "greater than one.") return self def _fit(self, X, y, alpha, C, loss, learning_rate, coef_init=None, intercept_init=None, sample_weight=None): if hasattr(self, "classes_"): self.classes_ = None X, y = check_X_y(X, y, 'csr', dtype=np.float64, order="C") n_samples, n_features = X.shape # labels can be encoded as float, int, or string literals # np.unique sorts in asc order; largest class id is positive class classes = np.unique(y) if self.warm_start and hasattr(self, "coef_"): if coef_init is None: coef_init = self.coef_ if intercept_init is None: intercept_init = self.intercept_ else: self.coef_ = None self.intercept_ = None if self.average > 0: self.standard_coef_ = self.coef_ self.standard_intercept_ = self.intercept_ self.average_coef_ = None self.average_intercept_ = None # Clear iteration count for multiple call to fit. self.t_ = 1.0 self._partial_fit(X, y, alpha, C, loss, learning_rate, self.n_iter, classes, sample_weight, coef_init, intercept_init) return self def _fit_binary(self, X, y, alpha, C, sample_weight, learning_rate, n_iter): """Fit a binary classifier on X and y. """ coef, intercept = fit_binary(self, 1, X, y, alpha, C, learning_rate, n_iter, self._expanded_class_weight[1], self._expanded_class_weight[0], sample_weight) self.t_ += n_iter * X.shape[0] # need to be 2d if self.average > 0: if self.average <= self.t_ - 1: self.coef_ = self.average_coef_.reshape(1, -1) self.intercept_ = self.average_intercept_ else: self.coef_ = self.standard_coef_.reshape(1, -1) self.standard_intercept_ = np.atleast_1d(intercept) self.intercept_ = self.standard_intercept_ else: self.coef_ = coef.reshape(1, -1) # intercept is a float, need to convert it to an array of length 1 self.intercept_ = np.atleast_1d(intercept) def _fit_multiclass(self, X, y, alpha, C, learning_rate, sample_weight, n_iter): """Fit a multi-class classifier by combining binary classifiers Each binary classifier predicts one class versus all others. This strategy is called OVA: One Versus All. """ # Use joblib to fit OvA in parallel. result = Parallel(n_jobs=self.n_jobs, backend="threading", verbose=self.verbose)( delayed(fit_binary)(self, i, X, y, alpha, C, learning_rate, n_iter, self._expanded_class_weight[i], 1., sample_weight) for i in range(len(self.classes_))) for i, (_, intercept) in enumerate(result): self.intercept_[i] = intercept self.t_ += n_iter * X.shape[0] if self.average > 0: if self.average <= self.t_ - 1.0: self.coef_ = self.average_coef_ self.intercept_ = self.average_intercept_ else: self.coef_ = self.standard_coef_ self.standard_intercept_ = np.atleast_1d(self.intercept_) self.intercept_ = self.standard_intercept_ def partial_fit(self, X, y, classes=None, sample_weight=None): """Fit linear model with Stochastic Gradient Descent. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Subset of the training data y : numpy array, shape (n_samples,) Subset of the target values classes : array, shape (n_classes,) Classes across all calls to partial_fit. Can be obtained by via `np.unique(y_all)`, where y_all is the target vector of the entire dataset. This argument is required for the first call to partial_fit and can be omitted in the subsequent calls. Note that y doesn't need to contain all labels in `classes`. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples. If not provided, uniform weights are assumed. Returns ------- self : returns an instance of self. """ if self.class_weight in ['balanced']: raise ValueError("class_weight '{0}' is not supported for " "partial_fit. In order to use 'balanced' weights," " use compute_class_weight('{0}', classes, y). " "In place of y you can us a large enough sample " "of the full training set target to properly " "estimate the class frequency distributions. " "Pass the resulting weights as the class_weight " "parameter.".format(self.class_weight)) return self._partial_fit(X, y, alpha=self.alpha, C=1.0, loss=self.loss, learning_rate=self.learning_rate, n_iter=1, classes=classes, sample_weight=sample_weight, coef_init=None, intercept_init=None) def fit(self, X, y, coef_init=None, intercept_init=None, sample_weight=None): """Fit linear model with Stochastic Gradient Descent. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data y : numpy array, shape (n_samples,) Target values coef_init : array, shape (n_classes, n_features) The initial coefficients to warm-start the optimization. intercept_init : array, shape (n_classes,) The initial intercept to warm-start the optimization. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples. If not provided, uniform weights are assumed. These weights will be multiplied with class_weight (passed through the constructor) if class_weight is specified Returns ------- self : returns an instance of self. """ return self._fit(X, y, alpha=self.alpha, C=1.0, loss=self.loss, learning_rate=self.learning_rate, coef_init=coef_init, intercept_init=intercept_init, sample_weight=sample_weight) class SGDClassifier(BaseSGDClassifier): """Linear classifiers (SVM, logistic regression, a.o.) with SGD training. This estimator implements regularized linear models with stochastic gradient descent (SGD) learning: the gradient of the loss is estimated each sample at a time and the model is updated along the way with a decreasing strength schedule (aka learning rate). SGD allows minibatch (online/out-of-core) learning, see the partial_fit method. For best results using the default learning rate schedule, the data should have zero mean and unit variance. This implementation works with data represented as dense or sparse arrays of floating point values for the features. The model it fits can be controlled with the loss parameter; by default, it fits a linear support vector machine (SVM). The regularizer is a penalty added to the loss function that shrinks model parameters towards the zero vector using either the squared euclidean norm L2 or the absolute norm L1 or a combination of both (Elastic Net). If the parameter update crosses the 0.0 value because of the regularizer, the update is truncated to 0.0 to allow for learning sparse models and achieve online feature selection. Read more in the :ref:`User Guide <sgd>`. Parameters ---------- loss : str, 'hinge', 'log', 'modified_huber', 'squared_hinge',\ 'perceptron', or a regression loss: 'squared_loss', 'huber',\ 'epsilon_insensitive', or 'squared_epsilon_insensitive' The loss function to be used. Defaults to 'hinge', which gives a linear SVM. The 'log' loss gives logistic regression, a probabilistic classifier. 'modified_huber' is another smooth loss that brings tolerance to outliers as well as probability estimates. 'squared_hinge' is like hinge but is quadratically penalized. 'perceptron' is the linear loss used by the perceptron algorithm. The other losses are designed for regression but can be useful in classification as well; see SGDRegressor for a description. penalty : str, 'none', 'l2', 'l1', or 'elasticnet' The penalty (aka regularization term) to be used. Defaults to 'l2' which is the standard regularizer for linear SVM models. 'l1' and 'elasticnet' might bring sparsity to the model (feature selection) not achievable with 'l2'. alpha : float Constant that multiplies the regularization term. Defaults to 0.0001 Also used to compute learning_rate when set to 'optimal'. l1_ratio : float The Elastic Net mixing parameter, with 0 <= l1_ratio <= 1. l1_ratio=0 corresponds to L2 penalty, l1_ratio=1 to L1. Defaults to 0.15. fit_intercept : bool Whether the intercept should be estimated or not. If False, the data is assumed to be already centered. Defaults to True. n_iter : int, optional The number of passes over the training data (aka epochs). The number of iterations is set to 1 if using partial_fit. Defaults to 5. shuffle : bool, optional Whether or not the training data should be shuffled after each epoch. Defaults to True. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data. verbose : integer, optional The verbosity level epsilon : float Epsilon in the epsilon-insensitive loss functions; only if `loss` is 'huber', 'epsilon_insensitive', or 'squared_epsilon_insensitive'. For 'huber', determines the threshold at which it becomes less important to get the prediction exactly right. For epsilon-insensitive, any differences between the current prediction and the correct label are ignored if they are less than this threshold. n_jobs : integer, optional The number of CPUs to use to do the OVA (One Versus All, for multi-class problems) computation. -1 means 'all CPUs'. Defaults to 1. learning_rate : string, optional The learning rate schedule: - 'constant': eta = eta0 - 'optimal': eta = 1.0 / (alpha * (t + t0)) [default] - 'invscaling': eta = eta0 / pow(t, power_t) where t0 is chosen by a heuristic proposed by Leon Bottou. eta0 : double The initial learning rate for the 'constant' or 'invscaling' schedules. The default value is 0.0 as eta0 is not used by the default schedule 'optimal'. power_t : double The exponent for inverse scaling learning rate [default 0.5]. class_weight : dict, {class_label: weight} or "balanced" or None, optional Preset for the class_weight fit parameter. Weights associated with classes. If not given, all classes are supposed to have weight one. The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` warm_start : bool, optional When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. average : bool or int, optional When set to True, computes the averaged SGD weights and stores the result in the ``coef_`` attribute. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches average. So ``average=10`` will begin averaging after seeing 10 samples. Attributes ---------- coef_ : array, shape (1, n_features) if n_classes == 2 else (n_classes,\ n_features) Weights assigned to the features. intercept_ : array, shape (1,) if n_classes == 2 else (n_classes,) Constants in decision function. loss_function_ : concrete ``LossFunction`` Examples -------- >>> import numpy as np >>> from sklearn import linear_model >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) >>> Y = np.array([1, 1, 2, 2]) >>> clf = linear_model.SGDClassifier() >>> clf.fit(X, Y) ... #doctest: +NORMALIZE_WHITESPACE SGDClassifier(alpha=0.0001, average=False, class_weight=None, epsilon=0.1, eta0=0.0, fit_intercept=True, l1_ratio=0.15, learning_rate='optimal', loss='hinge', n_iter=5, n_jobs=1, penalty='l2', power_t=0.5, random_state=None, shuffle=True, verbose=0, warm_start=False) >>> print(clf.predict([[-0.8, -1]])) [1] See also -------- LinearSVC, LogisticRegression, Perceptron """ def __init__(self, loss="hinge", penalty='l2', alpha=0.0001, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=DEFAULT_EPSILON, n_jobs=1, random_state=None, learning_rate="optimal", eta0=0.0, power_t=0.5, class_weight=None, warm_start=False, average=False): super(SGDClassifier, self).__init__( loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio, fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle, verbose=verbose, epsilon=epsilon, n_jobs=n_jobs, random_state=random_state, learning_rate=learning_rate, eta0=eta0, power_t=power_t, class_weight=class_weight, warm_start=warm_start, average=average) def _check_proba(self): check_is_fitted(self, "t_") if self.loss not in ("log", "modified_huber"): raise AttributeError("probability estimates are not available for" " loss=%r" % self.loss) @property def predict_proba(self): """Probability estimates. This method is only available for log loss and modified Huber loss. Multiclass probability estimates are derived from binary (one-vs.-rest) estimates by simple normalization, as recommended by Zadrozny and Elkan. Binary probability estimates for loss="modified_huber" are given by (clip(decision_function(X), -1, 1) + 1) / 2. For other loss functions it is necessary to perform proper probability calibration by wrapping the classifier with :class:`sklearn.calibration.CalibratedClassifierCV` instead. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Returns ------- array, shape (n_samples, n_classes) Returns the probability of the sample for each class in the model, where classes are ordered as they are in `self.classes_`. References ---------- Zadrozny and Elkan, "Transforming classifier scores into multiclass probability estimates", SIGKDD'02, http://www.research.ibm.com/people/z/zadrozny/kdd2002-Transf.pdf The justification for the formula in the loss="modified_huber" case is in the appendix B in: http://jmlr.csail.mit.edu/papers/volume2/zhang02c/zhang02c.pdf """ self._check_proba() return self._predict_proba def _predict_proba(self, X): if self.loss == "log": return self._predict_proba_lr(X) elif self.loss == "modified_huber": binary = (len(self.classes_) == 2) scores = self.decision_function(X) if binary: prob2 = np.ones((scores.shape[0], 2)) prob = prob2[:, 1] else: prob = scores np.clip(scores, -1, 1, prob) prob += 1. prob /= 2. if binary: prob2[:, 0] -= prob prob = prob2 else: # the above might assign zero to all classes, which doesn't # normalize neatly; work around this to produce uniform # probabilities prob_sum = prob.sum(axis=1) all_zero = (prob_sum == 0) if np.any(all_zero): prob[all_zero, :] = 1 prob_sum[all_zero] = len(self.classes_) # normalize prob /= prob_sum.reshape((prob.shape[0], -1)) return prob else: raise NotImplementedError("predict_(log_)proba only supported when" " loss='log' or loss='modified_huber' " "(%r given)" % self.loss) @property def predict_log_proba(self): """Log of probability estimates. This method is only available for log loss and modified Huber loss. When loss="modified_huber", probability estimates may be hard zeros and ones, so taking the logarithm is not possible. See ``predict_proba`` for details. Parameters ---------- X : array-like, shape (n_samples, n_features) Returns ------- T : array-like, shape (n_samples, n_classes) Returns the log-probability of the sample for each class in the model, where classes are ordered as they are in `self.classes_`. """ self._check_proba() return self._predict_log_proba def _predict_log_proba(self, X): return np.log(self.predict_proba(X)) class BaseSGDRegressor(BaseSGD, RegressorMixin): loss_functions = { "squared_loss": (SquaredLoss, ), "huber": (Huber, DEFAULT_EPSILON), "epsilon_insensitive": (EpsilonInsensitive, DEFAULT_EPSILON), "squared_epsilon_insensitive": (SquaredEpsilonInsensitive, DEFAULT_EPSILON), } @abstractmethod def __init__(self, loss="squared_loss", penalty="l2", alpha=0.0001, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=DEFAULT_EPSILON, random_state=None, learning_rate="invscaling", eta0=0.01, power_t=0.25, warm_start=False, average=False): super(BaseSGDRegressor, self).__init__(loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio, fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle, verbose=verbose, epsilon=epsilon, random_state=random_state, learning_rate=learning_rate, eta0=eta0, power_t=power_t, warm_start=warm_start, average=average) def _partial_fit(self, X, y, alpha, C, loss, learning_rate, n_iter, sample_weight, coef_init, intercept_init): X, y = check_X_y(X, y, "csr", copy=False, order='C', dtype=np.float64) y = astype(y, np.float64, copy=False) n_samples, n_features = X.shape self._validate_params() # Allocate datastructures from input arguments sample_weight = self._validate_sample_weight(sample_weight, n_samples) if getattr(self, "coef_", None) is None: self._allocate_parameter_mem(1, n_features, coef_init, intercept_init) elif n_features != self.coef_.shape[-1]: raise ValueError("Number of features %d does not match previous " "data %d." % (n_features, self.coef_.shape[-1])) if self.average > 0 and getattr(self, "average_coef_", None) is None: self.average_coef_ = np.zeros(n_features, dtype=np.float64, order="C") self.average_intercept_ = np.zeros(1, dtype=np.float64, order="C") self._fit_regressor(X, y, alpha, C, loss, learning_rate, sample_weight, n_iter) return self def partial_fit(self, X, y, sample_weight=None): """Fit linear model with Stochastic Gradient Descent. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Subset of training data y : numpy array of shape (n_samples,) Subset of target values sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples. If not provided, uniform weights are assumed. Returns ------- self : returns an instance of self. """ return self._partial_fit(X, y, self.alpha, C=1.0, loss=self.loss, learning_rate=self.learning_rate, n_iter=1, sample_weight=sample_weight, coef_init=None, intercept_init=None) def _fit(self, X, y, alpha, C, loss, learning_rate, coef_init=None, intercept_init=None, sample_weight=None): if self.warm_start and getattr(self, "coef_", None) is not None: if coef_init is None: coef_init = self.coef_ if intercept_init is None: intercept_init = self.intercept_ else: self.coef_ = None self.intercept_ = None if self.average > 0: self.standard_intercept_ = self.intercept_ self.standard_coef_ = self.coef_ self.average_coef_ = None self.average_intercept_ = None # Clear iteration count for multiple call to fit. self.t_ = 1.0 return self._partial_fit(X, y, alpha, C, loss, learning_rate, self.n_iter, sample_weight, coef_init, intercept_init) def fit(self, X, y, coef_init=None, intercept_init=None, sample_weight=None): """Fit linear model with Stochastic Gradient Descent. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Training data y : numpy array, shape (n_samples,) Target values coef_init : array, shape (n_features,) The initial coefficients to warm-start the optimization. intercept_init : array, shape (1,) The initial intercept to warm-start the optimization. sample_weight : array-like, shape (n_samples,), optional Weights applied to individual samples (1. for unweighted). Returns ------- self : returns an instance of self. """ return self._fit(X, y, alpha=self.alpha, C=1.0, loss=self.loss, learning_rate=self.learning_rate, coef_init=coef_init, intercept_init=intercept_init, sample_weight=sample_weight) def _decision_function(self, X): """Predict using the linear model Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Returns ------- array, shape (n_samples,) Predicted target values per element in X. """ check_is_fitted(self, ["t_", "coef_", "intercept_"], all_or_any=all) X = check_array(X, accept_sparse='csr') scores = safe_sparse_dot(X, self.coef_.T, dense_output=True) + self.intercept_ return scores.ravel() def predict(self, X): """Predict using the linear model Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Returns ------- array, shape (n_samples,) Predicted target values per element in X. """ return self._decision_function(X) def _fit_regressor(self, X, y, alpha, C, loss, learning_rate, sample_weight, n_iter): dataset, intercept_decay = make_dataset(X, y, sample_weight) loss_function = self._get_loss_function(loss) penalty_type = self._get_penalty_type(self.penalty) learning_rate_type = self._get_learning_rate_type(learning_rate) if not hasattr(self, "t_"): self.t_ = 1.0 random_state = check_random_state(self.random_state) # numpy mtrand expects a C long which is a signed 32 bit integer under # Windows seed = random_state.randint(0, np.iinfo(np.int32).max) if self.average > 0: self.standard_coef_, self.standard_intercept_, \ self.average_coef_, self.average_intercept_ =\ average_sgd(self.standard_coef_, self.standard_intercept_[0], self.average_coef_, self.average_intercept_[0], loss_function, penalty_type, alpha, C, self.l1_ratio, dataset, n_iter, int(self.fit_intercept), int(self.verbose), int(self.shuffle), seed, 1.0, 1.0, learning_rate_type, self.eta0, self.power_t, self.t_, intercept_decay, self.average) self.average_intercept_ = np.atleast_1d(self.average_intercept_) self.standard_intercept_ = np.atleast_1d(self.standard_intercept_) self.t_ += n_iter * X.shape[0] if self.average <= self.t_ - 1.0: self.coef_ = self.average_coef_ self.intercept_ = self.average_intercept_ else: self.coef_ = self.standard_coef_ self.intercept_ = self.standard_intercept_ else: self.coef_, self.intercept_ = \ plain_sgd(self.coef_, self.intercept_[0], loss_function, penalty_type, alpha, C, self.l1_ratio, dataset, n_iter, int(self.fit_intercept), int(self.verbose), int(self.shuffle), seed, 1.0, 1.0, learning_rate_type, self.eta0, self.power_t, self.t_, intercept_decay) self.t_ += n_iter * X.shape[0] self.intercept_ = np.atleast_1d(self.intercept_) class SGDRegressor(BaseSGDRegressor): """Linear model fitted by minimizing a regularized empirical loss with SGD SGD stands for Stochastic Gradient Descent: the gradient of the loss is estimated each sample at a time and the model is updated along the way with a decreasing strength schedule (aka learning rate). The regularizer is a penalty added to the loss function that shrinks model parameters towards the zero vector using either the squared euclidean norm L2 or the absolute norm L1 or a combination of both (Elastic Net). If the parameter update crosses the 0.0 value because of the regularizer, the update is truncated to 0.0 to allow for learning sparse models and achieve online feature selection. This implementation works with data represented as dense numpy arrays of floating point values for the features. Read more in the :ref:`User Guide <sgd>`. Parameters ---------- loss : str, 'squared_loss', 'huber', 'epsilon_insensitive', \ or 'squared_epsilon_insensitive' The loss function to be used. Defaults to 'squared_loss' which refers to the ordinary least squares fit. 'huber' modifies 'squared_loss' to focus less on getting outliers correct by switching from squared to linear loss past a distance of epsilon. 'epsilon_insensitive' ignores errors less than epsilon and is linear past that; this is the loss function used in SVR. 'squared_epsilon_insensitive' is the same but becomes squared loss past a tolerance of epsilon. penalty : str, 'none', 'l2', 'l1', or 'elasticnet' The penalty (aka regularization term) to be used. Defaults to 'l2' which is the standard regularizer for linear SVM models. 'l1' and 'elasticnet' might bring sparsity to the model (feature selection) not achievable with 'l2'. alpha : float Constant that multiplies the regularization term. Defaults to 0.0001 Also used to compute learning_rate when set to 'optimal'. l1_ratio : float The Elastic Net mixing parameter, with 0 <= l1_ratio <= 1. l1_ratio=0 corresponds to L2 penalty, l1_ratio=1 to L1. Defaults to 0.15. fit_intercept : bool Whether the intercept should be estimated or not. If False, the data is assumed to be already centered. Defaults to True. n_iter : int, optional The number of passes over the training data (aka epochs). The number of iterations is set to 1 if using partial_fit. Defaults to 5. shuffle : bool, optional Whether or not the training data should be shuffled after each epoch. Defaults to True. random_state : int seed, RandomState instance, or None (default) The seed of the pseudo random number generator to use when shuffling the data. verbose : integer, optional The verbosity level. epsilon : float Epsilon in the epsilon-insensitive loss functions; only if `loss` is 'huber', 'epsilon_insensitive', or 'squared_epsilon_insensitive'. For 'huber', determines the threshold at which it becomes less important to get the prediction exactly right. For epsilon-insensitive, any differences between the current prediction and the correct label are ignored if they are less than this threshold. learning_rate : string, optional The learning rate schedule: - 'constant': eta = eta0 - 'optimal': eta = 1.0 / (alpha * (t + t0)) [default] - 'invscaling': eta = eta0 / pow(t, power_t) where t0 is chosen by a heuristic proposed by Leon Bottou. eta0 : double, optional The initial learning rate [default 0.01]. power_t : double, optional The exponent for inverse scaling learning rate [default 0.25]. warm_start : bool, optional When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. average : bool or int, optional When set to True, computes the averaged SGD weights and stores the result in the ``coef_`` attribute. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches average. So ``average=10`` will begin averaging after seeing 10 samples. Attributes ---------- coef_ : array, shape (n_features,) Weights assigned to the features. intercept_ : array, shape (1,) The intercept term. average_coef_ : array, shape (n_features,) Averaged weights assigned to the features. average_intercept_ : array, shape (1,) The averaged intercept term. Examples -------- >>> import numpy as np >>> from sklearn import linear_model >>> n_samples, n_features = 10, 5 >>> np.random.seed(0) >>> y = np.random.randn(n_samples) >>> X = np.random.randn(n_samples, n_features) >>> clf = linear_model.SGDRegressor() >>> clf.fit(X, y) ... #doctest: +NORMALIZE_WHITESPACE SGDRegressor(alpha=0.0001, average=False, epsilon=0.1, eta0=0.01, fit_intercept=True, l1_ratio=0.15, learning_rate='invscaling', loss='squared_loss', n_iter=5, penalty='l2', power_t=0.25, random_state=None, shuffle=True, verbose=0, warm_start=False) See also -------- Ridge, ElasticNet, Lasso, SVR """ def __init__(self, loss="squared_loss", penalty="l2", alpha=0.0001, l1_ratio=0.15, fit_intercept=True, n_iter=5, shuffle=True, verbose=0, epsilon=DEFAULT_EPSILON, random_state=None, learning_rate="invscaling", eta0=0.01, power_t=0.25, warm_start=False, average=False): super(SGDRegressor, self).__init__(loss=loss, penalty=penalty, alpha=alpha, l1_ratio=l1_ratio, fit_intercept=fit_intercept, n_iter=n_iter, shuffle=shuffle, verbose=verbose, epsilon=epsilon, random_state=random_state, learning_rate=learning_rate, eta0=eta0, power_t=power_t, warm_start=warm_start, average=average)
bsd-3-clause
mhue/scikit-learn
examples/text/document_classification_20newsgroups.py
222
10500
""" ====================================================== Classification of text documents using sparse features ====================================================== This is an example showing how scikit-learn can be used to classify documents by topics using a bag-of-words approach. This example uses a scipy.sparse matrix to store the features and demonstrates various classifiers that can efficiently handle sparse matrices. The dataset used in this example is the 20 newsgroups dataset. It will be automatically downloaded, then cached. The bar plot indicates the accuracy, training time (normalized) and test time (normalized) of each classifier. """ # Author: Peter Prettenhofer <peter.prettenhofer@gmail.com> # Olivier Grisel <olivier.grisel@ensta.org> # Mathieu Blondel <mathieu@mblondel.org> # Lars Buitinck <L.J.Buitinck@uva.nl> # License: BSD 3 clause from __future__ import print_function import logging import numpy as np from optparse import OptionParser import sys from time import time import matplotlib.pyplot as plt from sklearn.datasets import fetch_20newsgroups from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.feature_extraction.text import HashingVectorizer from sklearn.feature_selection import SelectKBest, chi2 from sklearn.linear_model import RidgeClassifier from sklearn.pipeline import Pipeline from sklearn.svm import LinearSVC from sklearn.linear_model import SGDClassifier from sklearn.linear_model import Perceptron from sklearn.linear_model import PassiveAggressiveClassifier from sklearn.naive_bayes import BernoulliNB, MultinomialNB from sklearn.neighbors import KNeighborsClassifier from sklearn.neighbors import NearestCentroid from sklearn.ensemble import RandomForestClassifier from sklearn.utils.extmath import density from sklearn import metrics # Display progress logs on stdout logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s') # parse commandline arguments op = OptionParser() op.add_option("--report", action="store_true", dest="print_report", help="Print a detailed classification report.") op.add_option("--chi2_select", action="store", type="int", dest="select_chi2", help="Select some number of features using a chi-squared test") op.add_option("--confusion_matrix", action="store_true", dest="print_cm", help="Print the confusion matrix.") op.add_option("--top10", action="store_true", dest="print_top10", help="Print ten most discriminative terms per class" " for every classifier.") op.add_option("--all_categories", action="store_true", dest="all_categories", help="Whether to use all categories or not.") op.add_option("--use_hashing", action="store_true", help="Use a hashing vectorizer.") op.add_option("--n_features", action="store", type=int, default=2 ** 16, help="n_features when using the hashing vectorizer.") op.add_option("--filtered", action="store_true", help="Remove newsgroup information that is easily overfit: " "headers, signatures, and quoting.") (opts, args) = op.parse_args() if len(args) > 0: op.error("this script takes no arguments.") sys.exit(1) print(__doc__) op.print_help() print() ############################################################################### # Load some categories from the training set if opts.all_categories: categories = None else: categories = [ 'alt.atheism', 'talk.religion.misc', 'comp.graphics', 'sci.space', ] if opts.filtered: remove = ('headers', 'footers', 'quotes') else: remove = () print("Loading 20 newsgroups dataset for categories:") print(categories if categories else "all") data_train = fetch_20newsgroups(subset='train', categories=categories, shuffle=True, random_state=42, remove=remove) data_test = fetch_20newsgroups(subset='test', categories=categories, shuffle=True, random_state=42, remove=remove) print('data loaded') categories = data_train.target_names # for case categories == None def size_mb(docs): return sum(len(s.encode('utf-8')) for s in docs) / 1e6 data_train_size_mb = size_mb(data_train.data) data_test_size_mb = size_mb(data_test.data) print("%d documents - %0.3fMB (training set)" % ( len(data_train.data), data_train_size_mb)) print("%d documents - %0.3fMB (test set)" % ( len(data_test.data), data_test_size_mb)) print("%d categories" % len(categories)) print() # split a training set and a test set y_train, y_test = data_train.target, data_test.target print("Extracting features from the training data using a sparse vectorizer") t0 = time() if opts.use_hashing: vectorizer = HashingVectorizer(stop_words='english', non_negative=True, n_features=opts.n_features) X_train = vectorizer.transform(data_train.data) else: vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5, stop_words='english') X_train = vectorizer.fit_transform(data_train.data) duration = time() - t0 print("done in %fs at %0.3fMB/s" % (duration, data_train_size_mb / duration)) print("n_samples: %d, n_features: %d" % X_train.shape) print() print("Extracting features from the test data using the same vectorizer") t0 = time() X_test = vectorizer.transform(data_test.data) duration = time() - t0 print("done in %fs at %0.3fMB/s" % (duration, data_test_size_mb / duration)) print("n_samples: %d, n_features: %d" % X_test.shape) print() # mapping from integer feature name to original token string if opts.use_hashing: feature_names = None else: feature_names = vectorizer.get_feature_names() if opts.select_chi2: print("Extracting %d best features by a chi-squared test" % opts.select_chi2) t0 = time() ch2 = SelectKBest(chi2, k=opts.select_chi2) X_train = ch2.fit_transform(X_train, y_train) X_test = ch2.transform(X_test) if feature_names: # keep selected feature names feature_names = [feature_names[i] for i in ch2.get_support(indices=True)] print("done in %fs" % (time() - t0)) print() if feature_names: feature_names = np.asarray(feature_names) def trim(s): """Trim string to fit on terminal (assuming 80-column display)""" return s if len(s) <= 80 else s[:77] + "..." ############################################################################### # Benchmark classifiers def benchmark(clf): print('_' * 80) print("Training: ") print(clf) t0 = time() clf.fit(X_train, y_train) train_time = time() - t0 print("train time: %0.3fs" % train_time) t0 = time() pred = clf.predict(X_test) test_time = time() - t0 print("test time: %0.3fs" % test_time) score = metrics.accuracy_score(y_test, pred) print("accuracy: %0.3f" % score) if hasattr(clf, 'coef_'): print("dimensionality: %d" % clf.coef_.shape[1]) print("density: %f" % density(clf.coef_)) if opts.print_top10 and feature_names is not None: print("top 10 keywords per class:") for i, category in enumerate(categories): top10 = np.argsort(clf.coef_[i])[-10:] print(trim("%s: %s" % (category, " ".join(feature_names[top10])))) print() if opts.print_report: print("classification report:") print(metrics.classification_report(y_test, pred, target_names=categories)) if opts.print_cm: print("confusion matrix:") print(metrics.confusion_matrix(y_test, pred)) print() clf_descr = str(clf).split('(')[0] return clf_descr, score, train_time, test_time results = [] for clf, name in ( (RidgeClassifier(tol=1e-2, solver="lsqr"), "Ridge Classifier"), (Perceptron(n_iter=50), "Perceptron"), (PassiveAggressiveClassifier(n_iter=50), "Passive-Aggressive"), (KNeighborsClassifier(n_neighbors=10), "kNN"), (RandomForestClassifier(n_estimators=100), "Random forest")): print('=' * 80) print(name) results.append(benchmark(clf)) for penalty in ["l2", "l1"]: print('=' * 80) print("%s penalty" % penalty.upper()) # Train Liblinear model results.append(benchmark(LinearSVC(loss='l2', penalty=penalty, dual=False, tol=1e-3))) # Train SGD model results.append(benchmark(SGDClassifier(alpha=.0001, n_iter=50, penalty=penalty))) # Train SGD with Elastic Net penalty print('=' * 80) print("Elastic-Net penalty") results.append(benchmark(SGDClassifier(alpha=.0001, n_iter=50, penalty="elasticnet"))) # Train NearestCentroid without threshold print('=' * 80) print("NearestCentroid (aka Rocchio classifier)") results.append(benchmark(NearestCentroid())) # Train sparse Naive Bayes classifiers print('=' * 80) print("Naive Bayes") results.append(benchmark(MultinomialNB(alpha=.01))) results.append(benchmark(BernoulliNB(alpha=.01))) print('=' * 80) print("LinearSVC with L1-based feature selection") # The smaller C, the stronger the regularization. # The more regularization, the more sparsity. results.append(benchmark(Pipeline([ ('feature_selection', LinearSVC(penalty="l1", dual=False, tol=1e-3)), ('classification', LinearSVC()) ]))) # make some plots indices = np.arange(len(results)) results = [[x[i] for x in results] for i in range(4)] clf_names, score, training_time, test_time = results training_time = np.array(training_time) / np.max(training_time) test_time = np.array(test_time) / np.max(test_time) plt.figure(figsize=(12, 8)) plt.title("Score") plt.barh(indices, score, .2, label="score", color='r') plt.barh(indices + .3, training_time, .2, label="training time", color='g') plt.barh(indices + .6, test_time, .2, label="test time", color='b') plt.yticks(()) plt.legend(loc='best') plt.subplots_adjust(left=.25) plt.subplots_adjust(top=.95) plt.subplots_adjust(bottom=.05) for i, c in zip(indices, clf_names): plt.text(-.3, i, c) plt.show()
bsd-3-clause
vermouthmjl/scikit-learn
sklearn/gaussian_process/tests/test_gpr.py
23
11915
"""Testing for Gaussian process regression """ # Author: Jan Hendrik Metzen <jhm@informatik.uni-bremen.de> # Licence: BSD 3 clause import numpy as np from scipy.optimize import approx_fprime from sklearn.gaussian_process import GaussianProcessRegressor from sklearn.gaussian_process.kernels \ import RBF, ConstantKernel as C, WhiteKernel from sklearn.utils.testing \ import (assert_true, assert_greater, assert_array_less, assert_almost_equal, assert_equal) def f(x): return x * np.sin(x) X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T X2 = np.atleast_2d([2., 4., 5.5, 6.5, 7.5]).T y = f(X).ravel() fixed_kernel = RBF(length_scale=1.0, length_scale_bounds="fixed") kernels = [RBF(length_scale=1.0), fixed_kernel, RBF(length_scale=1.0, length_scale_bounds=(1e-3, 1e3)), C(1.0, (1e-2, 1e2)) * RBF(length_scale=1.0, length_scale_bounds=(1e-3, 1e3)), C(1.0, (1e-2, 1e2)) * RBF(length_scale=1.0, length_scale_bounds=(1e-3, 1e3)) + C(1e-5, (1e-5, 1e2)), C(0.1, (1e-2, 1e2)) * RBF(length_scale=1.0, length_scale_bounds=(1e-3, 1e3)) + C(1e-5, (1e-5, 1e2))] def test_gpr_interpolation(): """Test the interpolating property for different kernels.""" for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) y_pred, y_cov = gpr.predict(X, return_cov=True) assert_true(np.allclose(y_pred, y)) assert_true(np.allclose(np.diag(y_cov), 0.)) def test_lml_improving(): """ Test that hyperparameter-tuning improves log-marginal likelihood. """ for kernel in kernels: if kernel == fixed_kernel: continue gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) assert_greater(gpr.log_marginal_likelihood(gpr.kernel_.theta), gpr.log_marginal_likelihood(kernel.theta)) def test_lml_precomputed(): """ Test that lml of optimized kernel is stored correctly. """ for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) assert_equal(gpr.log_marginal_likelihood(gpr.kernel_.theta), gpr.log_marginal_likelihood()) def test_converged_to_local_maximum(): """ Test that we are in local maximum after hyperparameter-optimization.""" for kernel in kernels: if kernel == fixed_kernel: continue gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) lml, lml_gradient = \ gpr.log_marginal_likelihood(gpr.kernel_.theta, True) assert_true(np.all((np.abs(lml_gradient) < 1e-4) | (gpr.kernel_.theta == gpr.kernel_.bounds[:, 0]) | (gpr.kernel_.theta == gpr.kernel_.bounds[:, 1]))) def test_solution_inside_bounds(): """ Test that hyperparameter-optimization remains in bounds""" for kernel in kernels: if kernel == fixed_kernel: continue gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) bounds = gpr.kernel_.bounds max_ = np.finfo(gpr.kernel_.theta.dtype).max tiny = 1e-10 bounds[~np.isfinite(bounds[:, 1]), 1] = max_ assert_array_less(bounds[:, 0], gpr.kernel_.theta + tiny) assert_array_less(gpr.kernel_.theta, bounds[:, 1] + tiny) def test_lml_gradient(): """ Compare analytic and numeric gradient of log marginal likelihood. """ for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) lml, lml_gradient = gpr.log_marginal_likelihood(kernel.theta, True) lml_gradient_approx = \ approx_fprime(kernel.theta, lambda theta: gpr.log_marginal_likelihood(theta, False), 1e-10) assert_almost_equal(lml_gradient, lml_gradient_approx, 3) def test_prior(): """ Test that GP prior has mean 0 and identical variances.""" for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel) y_mean, y_cov = gpr.predict(X, return_cov=True) assert_almost_equal(y_mean, 0, 5) if len(gpr.kernel.theta) > 1: # XXX: quite hacky, works only for current kernels assert_almost_equal(np.diag(y_cov), np.exp(kernel.theta[0]), 5) else: assert_almost_equal(np.diag(y_cov), 1, 5) def test_sample_statistics(): """ Test that statistics of samples drawn from GP are correct.""" for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) y_mean, y_cov = gpr.predict(X2, return_cov=True) samples = gpr.sample_y(X2, 300000) # More digits accuracy would require many more samples assert_almost_equal(y_mean, np.mean(samples, 1), 1) assert_almost_equal(np.diag(y_cov) / np.diag(y_cov).max(), np.var(samples, 1) / np.diag(y_cov).max(), 1) def test_no_optimizer(): """ Test that kernel parameters are unmodified when optimizer is None.""" kernel = RBF(1.0) gpr = GaussianProcessRegressor(kernel=kernel, optimizer=None).fit(X, y) assert_equal(np.exp(gpr.kernel_.theta), 1.0) def test_predict_cov_vs_std(): """ Test that predicted std.-dev. is consistent with cov's diagonal.""" for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) y_mean, y_cov = gpr.predict(X2, return_cov=True) y_mean, y_std = gpr.predict(X2, return_std=True) assert_almost_equal(np.sqrt(np.diag(y_cov)), y_std) def test_anisotropic_kernel(): """ Test that GPR can identify meaningful anisotropic length-scales. """ # We learn a function which varies in one dimension ten-times slower # than in the other. The corresponding length-scales should differ by at # least a factor 5 rng = np.random.RandomState(0) X = rng.uniform(-1, 1, (50, 2)) y = X[:, 0] + 0.1 * X[:, 1] kernel = RBF([1.0, 1.0]) gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y) assert_greater(np.exp(gpr.kernel_.theta[1]), np.exp(gpr.kernel_.theta[0]) * 5) def test_random_starts(): """ Test that an increasing number of random-starts of GP fitting only increases the log marginal likelihood of the chosen theta. """ n_samples, n_features = 25, 2 np.random.seed(0) rng = np.random.RandomState(0) X = rng.randn(n_samples, n_features) * 2 - 1 y = np.sin(X).sum(axis=1) + np.sin(3 * X).sum(axis=1) \ + rng.normal(scale=0.1, size=n_samples) kernel = C(1.0, (1e-2, 1e2)) \ * RBF(length_scale=[1.0] * n_features, length_scale_bounds=[(1e-4, 1e+2)] * n_features) \ + WhiteKernel(noise_level=1e-5, noise_level_bounds=(1e-5, 1e1)) last_lml = -np.inf for n_restarts_optimizer in range(5): gp = GaussianProcessRegressor( kernel=kernel, n_restarts_optimizer=n_restarts_optimizer, random_state=0,).fit(X, y) lml = gp.log_marginal_likelihood(gp.kernel_.theta) assert_greater(lml, last_lml - np.finfo(np.float32).eps) last_lml = lml def test_y_normalization(): """ Test normalization of the target values in GP Fitting non-normalizing GP on normalized y and fitting normalizing GP on unnormalized y should yield identical results """ y_mean = y.mean(0) y_norm = y - y_mean for kernel in kernels: # Fit non-normalizing GP on normalized y gpr = GaussianProcessRegressor(kernel=kernel) gpr.fit(X, y_norm) # Fit normalizing GP on unnormalized y gpr_norm = GaussianProcessRegressor(kernel=kernel, normalize_y=True) gpr_norm.fit(X, y) # Compare predicted mean, std-devs and covariances y_pred, y_pred_std = gpr.predict(X2, return_std=True) y_pred = y_mean + y_pred y_pred_norm, y_pred_std_norm = gpr_norm.predict(X2, return_std=True) assert_almost_equal(y_pred, y_pred_norm) assert_almost_equal(y_pred_std, y_pred_std_norm) _, y_cov = gpr.predict(X2, return_cov=True) _, y_cov_norm = gpr_norm.predict(X2, return_cov=True) assert_almost_equal(y_cov, y_cov_norm) def test_y_multioutput(): """ Test that GPR can deal with multi-dimensional target values""" y_2d = np.vstack((y, y * 2)).T # Test for fixed kernel that first dimension of 2d GP equals the output # of 1d GP and that second dimension is twice as large kernel = RBF(length_scale=1.0) gpr = GaussianProcessRegressor(kernel=kernel, optimizer=None, normalize_y=False) gpr.fit(X, y) gpr_2d = GaussianProcessRegressor(kernel=kernel, optimizer=None, normalize_y=False) gpr_2d.fit(X, y_2d) y_pred_1d, y_std_1d = gpr.predict(X2, return_std=True) y_pred_2d, y_std_2d = gpr_2d.predict(X2, return_std=True) _, y_cov_1d = gpr.predict(X2, return_cov=True) _, y_cov_2d = gpr_2d.predict(X2, return_cov=True) assert_almost_equal(y_pred_1d, y_pred_2d[:, 0]) assert_almost_equal(y_pred_1d, y_pred_2d[:, 1] / 2) # Standard deviation and covariance do not depend on output assert_almost_equal(y_std_1d, y_std_2d) assert_almost_equal(y_cov_1d, y_cov_2d) y_sample_1d = gpr.sample_y(X2, n_samples=10) y_sample_2d = gpr_2d.sample_y(X2, n_samples=10) assert_almost_equal(y_sample_1d, y_sample_2d[:, 0]) # Test hyperparameter optimization for kernel in kernels: gpr = GaussianProcessRegressor(kernel=kernel, normalize_y=True) gpr.fit(X, y) gpr_2d = GaussianProcessRegressor(kernel=kernel, normalize_y=True) gpr_2d.fit(X, np.vstack((y, y)).T) assert_almost_equal(gpr.kernel_.theta, gpr_2d.kernel_.theta, 4) def test_custom_optimizer(): """ Test that GPR can use externally defined optimizers. """ # Define a dummy optimizer that simply tests 50 random hyperparameters def optimizer(obj_func, initial_theta, bounds): rng = np.random.RandomState(0) theta_opt, func_min = \ initial_theta, obj_func(initial_theta, eval_gradient=False) for _ in range(50): theta = np.atleast_1d(rng.uniform(np.maximum(-2, bounds[:, 0]), np.minimum(1, bounds[:, 1]))) f = obj_func(theta, eval_gradient=False) if f < func_min: theta_opt, func_min = theta, f return theta_opt, func_min for kernel in kernels: if kernel == fixed_kernel: continue gpr = GaussianProcessRegressor(kernel=kernel, optimizer=optimizer) gpr.fit(X, y) # Checks that optimizer improved marginal likelihood assert_greater(gpr.log_marginal_likelihood(gpr.kernel_.theta), gpr.log_marginal_likelihood(gpr.kernel.theta)) def test_duplicate_input(): """ Test GPR can handle two different output-values for the same input. """ for kernel in kernels: gpr_equal_inputs = \ GaussianProcessRegressor(kernel=kernel, alpha=1e-2) gpr_similar_inputs = \ GaussianProcessRegressor(kernel=kernel, alpha=1e-2) X_ = np.vstack((X, X[0])) y_ = np.hstack((y, y[0] + 1)) gpr_equal_inputs.fit(X_, y_) X_ = np.vstack((X, X[0] + 1e-15)) y_ = np.hstack((y, y[0] + 1)) gpr_similar_inputs.fit(X_, y_) X_test = np.linspace(0, 10, 100)[:, None] y_pred_equal, y_std_equal = \ gpr_equal_inputs.predict(X_test, return_std=True) y_pred_similar, y_std_similar = \ gpr_similar_inputs.predict(X_test, return_std=True) assert_almost_equal(y_pred_equal, y_pred_similar) assert_almost_equal(y_std_equal, y_std_similar)
bsd-3-clause
kushalbhola/MyStuff
Practice/PythonApplication/env/Lib/site-packages/pandas/tseries/frequencies.py
2
15559
from datetime import timedelta import re from typing import Dict import numpy as np from pytz import AmbiguousTimeError from pandas._libs.algos import unique_deltas from pandas._libs.tslibs import Timedelta, Timestamp from pandas._libs.tslibs.ccalendar import MONTH_ALIASES, int_to_weekday from pandas._libs.tslibs.fields import build_field_sarray import pandas._libs.tslibs.frequencies as libfreqs from pandas._libs.tslibs.offsets import _offset_to_period_map import pandas._libs.tslibs.resolution as libresolution from pandas._libs.tslibs.resolution import Resolution from pandas._libs.tslibs.timezones import UTC from pandas._libs.tslibs.tzconversion import tz_convert from pandas.util._decorators import cache_readonly from pandas.core.dtypes.common import ( is_datetime64_dtype, is_period_arraylike, is_timedelta64_dtype, ) from pandas.core.dtypes.generic import ABCSeries from pandas.core.algorithms import unique from pandas.tseries.offsets import ( DateOffset, Day, Hour, Micro, Milli, Minute, Nano, Second, prefix_mapping, ) _ONE_MICRO = 1000 _ONE_MILLI = _ONE_MICRO * 1000 _ONE_SECOND = _ONE_MILLI * 1000 _ONE_MINUTE = 60 * _ONE_SECOND _ONE_HOUR = 60 * _ONE_MINUTE _ONE_DAY = 24 * _ONE_HOUR # --------------------------------------------------------------------- # Offset names ("time rules") and related functions #: cache of previously seen offsets _offset_map = {} # type: Dict[str, DateOffset] def get_period_alias(offset_str): """ alias to closest period strings BQ->Q etc""" return _offset_to_period_map.get(offset_str, None) _name_to_offset_map = { "days": Day(1), "hours": Hour(1), "minutes": Minute(1), "seconds": Second(1), "milliseconds": Milli(1), "microseconds": Micro(1), "nanoseconds": Nano(1), } def to_offset(freq): """ Return DateOffset object from string or tuple representation or datetime.timedelta object Parameters ---------- freq : str, tuple, datetime.timedelta, DateOffset or None Returns ------- DateOffset None if freq is None. Raises ------ ValueError If freq is an invalid frequency See Also -------- DateOffset Examples -------- >>> to_offset('5min') <5 * Minutes> >>> to_offset('1D1H') <25 * Hours> >>> to_offset(('W', 2)) <2 * Weeks: weekday=6> >>> to_offset((2, 'B')) <2 * BusinessDays> >>> to_offset(datetime.timedelta(days=1)) <Day> >>> to_offset(Hour()) <Hour> """ if freq is None: return None if isinstance(freq, DateOffset): return freq if isinstance(freq, tuple): name = freq[0] stride = freq[1] if isinstance(stride, str): name, stride = stride, name name, _ = libfreqs._base_and_stride(name) delta = get_offset(name) * stride elif isinstance(freq, timedelta): delta = None freq = Timedelta(freq) try: for name in freq.components._fields: offset = _name_to_offset_map[name] stride = getattr(freq.components, name) if stride != 0: offset = stride * offset if delta is None: delta = offset else: delta = delta + offset except Exception: raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq)) else: delta = None stride_sign = None try: splitted = re.split(libfreqs.opattern, freq) if splitted[-1] != "" and not splitted[-1].isspace(): # the last element must be blank raise ValueError("last element must be blank") for sep, stride, name in zip( splitted[0::4], splitted[1::4], splitted[2::4] ): if sep != "" and not sep.isspace(): raise ValueError("separator must be spaces") prefix = libfreqs._lite_rule_alias.get(name) or name if stride_sign is None: stride_sign = -1 if stride.startswith("-") else 1 if not stride: stride = 1 if prefix in Resolution._reso_str_bump_map.keys(): stride, name = Resolution.get_stride_from_decimal( float(stride), prefix ) stride = int(stride) offset = get_offset(name) offset = offset * int(np.fabs(stride) * stride_sign) if delta is None: delta = offset else: delta = delta + offset except Exception: raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq)) if delta is None: raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(freq)) return delta def get_offset(name): """ Return DateOffset object associated with rule name Examples -------- get_offset('EOM') --> BMonthEnd(1) """ if name not in libfreqs._dont_uppercase: name = name.upper() name = libfreqs._lite_rule_alias.get(name, name) name = libfreqs._lite_rule_alias.get(name.lower(), name) else: name = libfreqs._lite_rule_alias.get(name, name) if name not in _offset_map: try: split = name.split("-") klass = prefix_mapping[split[0]] # handles case where there's no suffix (and will TypeError if too # many '-') offset = klass._from_name(*split[1:]) except (ValueError, TypeError, KeyError): # bad prefix or suffix raise ValueError(libfreqs.INVALID_FREQ_ERR_MSG.format(name)) # cache _offset_map[name] = offset return _offset_map[name] # --------------------------------------------------------------------- # Period codes def infer_freq(index, warn=True): """ Infer the most likely frequency given the input index. If the frequency is uncertain, a warning will be printed. Parameters ---------- index : DatetimeIndex or TimedeltaIndex if passed a Series will use the values of the series (NOT THE INDEX) warn : boolean, default True Returns ------- str or None None if no discernible frequency TypeError if the index is not datetime-like ValueError if there are less than three values. """ import pandas as pd if isinstance(index, ABCSeries): values = index._values if not ( is_datetime64_dtype(values) or is_timedelta64_dtype(values) or values.dtype == object ): raise TypeError( "cannot infer freq from a non-convertible dtype " "on a Series of {dtype}".format(dtype=index.dtype) ) index = values if is_period_arraylike(index): raise TypeError( "PeriodIndex given. Check the `freq` attribute " "instead of using infer_freq." ) elif is_timedelta64_dtype(index): # Allow TimedeltaIndex and TimedeltaArray inferer = _TimedeltaFrequencyInferer(index, warn=warn) return inferer.get_freq() if isinstance(index, pd.Index) and not isinstance(index, pd.DatetimeIndex): if isinstance(index, (pd.Int64Index, pd.Float64Index)): raise TypeError( "cannot infer freq from a non-convertible index " "type {type}".format(type=type(index)) ) index = index.values if not isinstance(index, pd.DatetimeIndex): try: index = pd.DatetimeIndex(index) except AmbiguousTimeError: index = pd.DatetimeIndex(index.asi8) inferer = _FrequencyInferer(index, warn=warn) return inferer.get_freq() class _FrequencyInferer: """ Not sure if I can avoid the state machine here """ def __init__(self, index, warn=True): self.index = index self.values = index.asi8 # This moves the values, which are implicitly in UTC, to the # the timezone so they are in local time if hasattr(index, "tz"): if index.tz is not None: self.values = tz_convert(self.values, UTC, index.tz) self.warn = warn if len(index) < 3: raise ValueError("Need at least 3 dates to infer frequency") self.is_monotonic = ( self.index._is_monotonic_increasing or self.index._is_monotonic_decreasing ) @cache_readonly def deltas(self): return unique_deltas(self.values) @cache_readonly def deltas_asi8(self): return unique_deltas(self.index.asi8) @cache_readonly def is_unique(self): return len(self.deltas) == 1 @cache_readonly def is_unique_asi8(self): return len(self.deltas_asi8) == 1 def get_freq(self): """ Find the appropriate frequency string to describe the inferred frequency of self.values Returns ------- str or None """ if not self.is_monotonic or not self.index._is_unique: return None delta = self.deltas[0] if _is_multiple(delta, _ONE_DAY): return self._infer_daily_rule() # Business hourly, maybe. 17: one day / 65: one weekend if self.hour_deltas in ([1, 17], [1, 65], [1, 17, 65]): return "BH" # Possibly intraday frequency. Here we use the # original .asi8 values as the modified values # will not work around DST transitions. See #8772 elif not self.is_unique_asi8: return None delta = self.deltas_asi8[0] if _is_multiple(delta, _ONE_HOUR): # Hours return _maybe_add_count("H", delta / _ONE_HOUR) elif _is_multiple(delta, _ONE_MINUTE): # Minutes return _maybe_add_count("T", delta / _ONE_MINUTE) elif _is_multiple(delta, _ONE_SECOND): # Seconds return _maybe_add_count("S", delta / _ONE_SECOND) elif _is_multiple(delta, _ONE_MILLI): # Milliseconds return _maybe_add_count("L", delta / _ONE_MILLI) elif _is_multiple(delta, _ONE_MICRO): # Microseconds return _maybe_add_count("U", delta / _ONE_MICRO) else: # Nanoseconds return _maybe_add_count("N", delta) @cache_readonly def day_deltas(self): return [x / _ONE_DAY for x in self.deltas] @cache_readonly def hour_deltas(self): return [x / _ONE_HOUR for x in self.deltas] @cache_readonly def fields(self): return build_field_sarray(self.values) @cache_readonly def rep_stamp(self): return Timestamp(self.values[0]) def month_position_check(self): return libresolution.month_position_check(self.fields, self.index.dayofweek) @cache_readonly def mdiffs(self): nmonths = self.fields["Y"] * 12 + self.fields["M"] return unique_deltas(nmonths.astype("i8")) @cache_readonly def ydiffs(self): return unique_deltas(self.fields["Y"].astype("i8")) def _infer_daily_rule(self): annual_rule = self._get_annual_rule() if annual_rule: nyears = self.ydiffs[0] month = MONTH_ALIASES[self.rep_stamp.month] alias = "{prefix}-{month}".format(prefix=annual_rule, month=month) return _maybe_add_count(alias, nyears) quarterly_rule = self._get_quarterly_rule() if quarterly_rule: nquarters = self.mdiffs[0] / 3 mod_dict = {0: 12, 2: 11, 1: 10} month = MONTH_ALIASES[mod_dict[self.rep_stamp.month % 3]] alias = "{prefix}-{month}".format(prefix=quarterly_rule, month=month) return _maybe_add_count(alias, nquarters) monthly_rule = self._get_monthly_rule() if monthly_rule: return _maybe_add_count(monthly_rule, self.mdiffs[0]) if self.is_unique: days = self.deltas[0] / _ONE_DAY if days % 7 == 0: # Weekly day = int_to_weekday[self.rep_stamp.weekday()] return _maybe_add_count("W-{day}".format(day=day), days / 7) else: return _maybe_add_count("D", days) if self._is_business_daily(): return "B" wom_rule = self._get_wom_rule() if wom_rule: return wom_rule def _get_annual_rule(self): if len(self.ydiffs) > 1: return None if len(unique(self.fields["M"])) > 1: return None pos_check = self.month_position_check() return {"cs": "AS", "bs": "BAS", "ce": "A", "be": "BA"}.get(pos_check) def _get_quarterly_rule(self): if len(self.mdiffs) > 1: return None if not self.mdiffs[0] % 3 == 0: return None pos_check = self.month_position_check() return {"cs": "QS", "bs": "BQS", "ce": "Q", "be": "BQ"}.get(pos_check) def _get_monthly_rule(self): if len(self.mdiffs) > 1: return None pos_check = self.month_position_check() return {"cs": "MS", "bs": "BMS", "ce": "M", "be": "BM"}.get(pos_check) def _is_business_daily(self): # quick check: cannot be business daily if self.day_deltas != [1, 3]: return False # probably business daily, but need to confirm first_weekday = self.index[0].weekday() shifts = np.diff(self.index.asi8) shifts = np.floor_divide(shifts, _ONE_DAY) weekdays = np.mod(first_weekday + np.cumsum(shifts), 7) return np.all( ((weekdays == 0) & (shifts == 3)) | ((weekdays > 0) & (weekdays <= 4) & (shifts == 1)) ) def _get_wom_rule(self): # wdiffs = unique(np.diff(self.index.week)) # We also need -47, -49, -48 to catch index spanning year boundary # if not lib.ismember(wdiffs, set([4, 5, -47, -49, -48])).all(): # return None weekdays = unique(self.index.weekday) if len(weekdays) > 1: return None week_of_months = unique((self.index.day - 1) // 7) # Only attempt to infer up to WOM-4. See #9425 week_of_months = week_of_months[week_of_months < 4] if len(week_of_months) == 0 or len(week_of_months) > 1: return None # get which week week = week_of_months[0] + 1 wd = int_to_weekday[weekdays[0]] return "WOM-{week}{weekday}".format(week=week, weekday=wd) class _TimedeltaFrequencyInferer(_FrequencyInferer): def _infer_daily_rule(self): if self.is_unique: days = self.deltas[0] / _ONE_DAY if days % 7 == 0: # Weekly wd = int_to_weekday[self.rep_stamp.weekday()] alias = "W-{weekday}".format(weekday=wd) return _maybe_add_count(alias, days / 7) else: return _maybe_add_count("D", days) def _is_multiple(us, mult): return us % mult == 0 def _maybe_add_count(base, count): if count != 1: assert count == int(count) count = int(count) return "{count}{base}".format(count=count, base=base) else: return base
apache-2.0
jmargeta/scikit-learn
sklearn/tests/test_pls.py
6
9383
import numpy as np from sklearn.utils.testing import assert_array_almost_equal from sklearn.datasets import load_linnerud from sklearn import pls def test_pls(): d = load_linnerud() X = d.data Y = d.target # 1) Canonical (symetric) PLS (PLS 2 blocks canonical mode A) # =========================================================== # Compare 2 algo.: nipals vs. svd # ------------------------------ pls_bynipals = pls.PLSCanonical(n_components=X.shape[1]) pls_bynipals.fit(X, Y) pls_bysvd = pls.PLSCanonical(algorithm="svd", n_components=X.shape[1]) pls_bysvd.fit(X, Y) # check equalities of loading (up to the sign of the second column) assert_array_almost_equal( pls_bynipals.x_loadings_, np.multiply(pls_bysvd.x_loadings_, np.array([1, -1, 1])), decimal=5, err_msg="nipals and svd implementation lead to different x loadings") assert_array_almost_equal( pls_bynipals.y_loadings_, np.multiply(pls_bysvd.y_loadings_, np.array([1, -1, 1])), decimal=5, err_msg="nipals and svd implementation lead to different y loadings") # Check PLS properties (with n_components=X.shape[1]) # --------------------------------------------------- plsca = pls.PLSCanonical(n_components=X.shape[1]) plsca.fit(X, Y) T = plsca.x_scores_ P = plsca.x_loadings_ Wx = plsca.x_weights_ U = plsca.y_scores_ Q = plsca.y_loadings_ Wy = plsca.y_weights_ def check_ortho(M, err_msg): K = np.dot(M.T, M) assert_array_almost_equal(K, np.diag(np.diag(K)), err_msg=err_msg) # Orthogonality of weights # ~~~~~~~~~~~~~~~~~~~~~~~~ check_ortho(Wx, "x weights are not orthogonal") check_ortho(Wy, "y weights are not orthogonal") # Orthogonality of latent scores # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ check_ortho(T, "x scores are not orthogonal") check_ortho(U, "y scores are not orthogonal") # Check X = TP' and Y = UQ' (with (p == q) components) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # center scale X, Y Xc, Yc, x_mean, y_mean, x_std, y_std =\ pls._center_scale_xy(X.copy(), Y.copy(), scale=True) assert_array_almost_equal(Xc, np.dot(T, P.T), err_msg="X != TP'") assert_array_almost_equal(Yc, np.dot(U, Q.T), err_msg="Y != UQ'") # Check that rotations on training data lead to scores # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Xr = plsca.transform(X) assert_array_almost_equal(Xr, plsca.x_scores_, err_msg="rotation on X failed") Xr, Yr = plsca.transform(X, Y) assert_array_almost_equal(Xr, plsca.x_scores_, err_msg="rotation on X failed") assert_array_almost_equal(Yr, plsca.y_scores_, err_msg="rotation on Y failed") # "Non regression test" on canonical PLS # -------------------------------------- # The results were checked against the R-package plspm pls_ca = pls.PLSCanonical(n_components=X.shape[1]) pls_ca.fit(X, Y) x_weights = np.array( [[-0.61330704, 0.25616119, -0.74715187], [-0.74697144, 0.11930791, 0.65406368], [-0.25668686, -0.95924297, -0.11817271]]) assert_array_almost_equal(pls_ca.x_weights_, x_weights) x_rotations = np.array( [[-0.61330704, 0.41591889, -0.62297525], [-0.74697144, 0.31388326, 0.77368233], [-0.25668686, -0.89237972, -0.24121788]]) assert_array_almost_equal(pls_ca.x_rotations_, x_rotations) y_weights = np.array( [[+0.58989127, 0.7890047, 0.1717553], [+0.77134053, -0.61351791, 0.16920272], [-0.23887670, -0.03267062, 0.97050016]]) assert_array_almost_equal(pls_ca.y_weights_, y_weights) y_rotations = np.array( [[+0.58989127, 0.7168115, 0.30665872], [+0.77134053, -0.70791757, 0.19786539], [-0.23887670, -0.00343595, 0.94162826]]) assert_array_almost_equal(pls_ca.y_rotations_, y_rotations) # 2) Regression PLS (PLS2): "Non regression test" # =============================================== # The results were checked against the R-packages plspm, misOmics and pls pls_2 = pls.PLSRegression(n_components=X.shape[1]) pls_2.fit(X, Y) x_weights = np.array( [[-0.61330704, -0.00443647, 0.78983213], [-0.74697144, -0.32172099, -0.58183269], [-0.25668686, 0.94682413, -0.19399983]]) assert_array_almost_equal(pls_2.x_weights_, x_weights) x_loadings = np.array( [[-0.61470416, -0.24574278, 0.78983213], [-0.65625755, -0.14396183, -0.58183269], [-0.51733059, 1.00609417, -0.19399983]]) assert_array_almost_equal(pls_2.x_loadings_, x_loadings) y_weights = np.array( [[+0.32456184, 0.29892183, 0.20316322], [+0.42439636, 0.61970543, 0.19320542], [-0.13143144, -0.26348971, -0.17092916]]) assert_array_almost_equal(pls_2.y_weights_, y_weights) y_loadings = np.array( [[+0.32456184, 0.29892183, 0.20316322], [+0.42439636, 0.61970543, 0.19320542], [-0.13143144, -0.26348971, -0.17092916]]) assert_array_almost_equal(pls_2.y_loadings_, y_loadings) # 3) Another non-regression test of Canonical PLS on random dataset # ================================================================= # The results were checked against the R-package plspm n = 500 p_noise = 10 q_noise = 5 # 2 latents vars: np.random.seed(11) l1 = np.random.normal(size=n) l2 = np.random.normal(size=n) latents = np.array([l1, l1, l2, l2]).T X = latents + np.random.normal(size=4 * n).reshape((n, 4)) Y = latents + np.random.normal(size=4 * n).reshape((n, 4)) X = np.concatenate( (X, np.random.normal(size=p_noise * n).reshape(n, p_noise)), axis=1) Y = np.concatenate( (Y, np.random.normal(size=q_noise * n).reshape(n, q_noise)), axis=1) np.random.seed(None) pls_ca = pls.PLSCanonical(n_components=3) pls_ca.fit(X, Y) x_weights = np.array( [[0.65803719, 0.19197924, 0.21769083], [0.7009113, 0.13303969, -0.15376699], [0.13528197, -0.68636408, 0.13856546], [0.16854574, -0.66788088, -0.12485304], [-0.03232333, -0.04189855, 0.40690153], [0.1148816, -0.09643158, 0.1613305], [0.04792138, -0.02384992, 0.17175319], [-0.06781, -0.01666137, -0.18556747], [-0.00266945, -0.00160224, 0.11893098], [-0.00849528, -0.07706095, 0.1570547], [-0.00949471, -0.02964127, 0.34657036], [-0.03572177, 0.0945091, 0.3414855], [0.05584937, -0.02028961, -0.57682568], [0.05744254, -0.01482333, -0.17431274]]) assert_array_almost_equal(pls_ca.x_weights_, x_weights) x_loadings = np.array( [[0.65649254, 0.1847647, 0.15270699], [0.67554234, 0.15237508, -0.09182247], [0.19219925, -0.67750975, 0.08673128], [0.2133631, -0.67034809, -0.08835483], [-0.03178912, -0.06668336, 0.43395268], [0.15684588, -0.13350241, 0.20578984], [0.03337736, -0.03807306, 0.09871553], [-0.06199844, 0.01559854, -0.1881785], [0.00406146, -0.00587025, 0.16413253], [-0.00374239, -0.05848466, 0.19140336], [0.00139214, -0.01033161, 0.32239136], [-0.05292828, 0.0953533, 0.31916881], [0.04031924, -0.01961045, -0.65174036], [0.06172484, -0.06597366, -0.1244497]]) assert_array_almost_equal(pls_ca.x_loadings_, x_loadings) y_weights = np.array( [[0.66101097, 0.18672553, 0.22826092], [0.69347861, 0.18463471, -0.23995597], [0.14462724, -0.66504085, 0.17082434], [0.22247955, -0.6932605, -0.09832993], [0.07035859, 0.00714283, 0.67810124], [0.07765351, -0.0105204, -0.44108074], [-0.00917056, 0.04322147, 0.10062478], [-0.01909512, 0.06182718, 0.28830475], [0.01756709, 0.04797666, 0.32225745]]) assert_array_almost_equal(pls_ca.y_weights_, y_weights) y_loadings = np.array( [[0.68568625, 0.1674376, 0.0969508], [0.68782064, 0.20375837, -0.1164448], [0.11712173, -0.68046903, 0.12001505], [0.17860457, -0.6798319, -0.05089681], [0.06265739, -0.0277703, 0.74729584], [0.0914178, 0.00403751, -0.5135078], [-0.02196918, -0.01377169, 0.09564505], [-0.03288952, 0.09039729, 0.31858973], [0.04287624, 0.05254676, 0.27836841]]) assert_array_almost_equal(pls_ca.y_loadings_, y_loadings) # Orthogonality of weights # ~~~~~~~~~~~~~~~~~~~~~~~~ check_ortho(pls_ca.x_weights_, "x weights are not orthogonal") check_ortho(pls_ca.y_weights_, "y weights are not orthogonal") # Orthogonality of latent scores # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ check_ortho(pls_ca.x_scores_, "x scores are not orthogonal") check_ortho(pls_ca.y_scores_, "y scores are not orthogonal") def test_scale(): d = load_linnerud() X = d.data Y = d.target # causes X[:, -1].std() to be zero X[:, -1] = 1.0 for clf in [pls.PLSCanonical(), pls.PLSRegression(), pls.CCA(), pls.PLSSVD()]: clf.set_params(scale=True) clf.fit(X, Y)
bsd-3-clause
Shatki/PyIMU
test/magnetosphere.py
1
1580
from mpl_toolkits.mplot3d import axes3d import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from socket import * import time # Объявляем все глобальные переменные HOST = '192.168.0.76' PORT = 21566 BUFSIZ = 512 ADDR = (HOST, PORT) bad_packet = 0 good_packet = 0 # fig, ax = plt.subplots() fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # Socket # tcpCliSock = socket(AF_INET, SOCK_STREAM) # tcpCliSock.connect(ADDR) # Запрет на ожидание plt.ion() tstart = time.time() # real-time plotting loop X, Y, Z = [], [], [] while True: try: # читаем данные из сети tcpCliSock.c data = tcpCliSock.recv(BUFSIZ) if data: print(len(X), data) data = data.decode().split(',') if len(data) == 9: # print('Data received', data) # tcpCliSock.send(b'Ok') good_packet += 1 else: bad_packet += 1 # читаем данные из сети data = tcpCliSock.recv(BUFSIZ) X.append(data[0]) Y.append(data[1]) Z.append(data[2]) frame = ax.scatter(X, Y, Z, c='b', marker='o') # Remove old line collection before drawing #if oldcol is not None: # ax.collections.remove(oldcol) plt.pause(0.001 / len(X)) except KeyboardInterrupt: tcpCliSock.close() print('FPS: %f' % (len(X) / (time.time() - tstart))) break
gpl-3.0
DamCB/tyssue
tyssue/draw/ipv_draw.py
2
8114
"""3D visualisation inside the notebook. """ import warnings import numpy as np import pandas as pd from matplotlib import cm from ipywidgets import interact from ..config.draw import sheet_spec from ..utils.utils import spec_updater, get_sub_eptm try: import ipyvolume as ipv except ImportError: print( """ This module needs ipyvolume to work. You can install it with: $ conda install -c conda-forge ipyvolume """ ) def browse_history(history, coords=["x", "y", "z"], **draw_specs_kw): times = history.time_stamps num_frames = times.size draw_specs = sheet_spec() spec_updater(draw_specs, draw_specs_kw) sheet = history.retrieve(0) ipv.clear() fig, meshes = sheet_view(sheet, coords, **draw_specs_kw) lim_inf = sheet.vert_df[sheet.coords].min().min() lim_sup = sheet.vert_df[sheet.coords].max().max() ipv.xyzlim(lim_inf, lim_sup) def set_frame(i=0): fig.animation = 0 t = times[i] meshes = _get_meshes(history.retrieve(t), coords, draw_specs) update_view(fig, meshes) ipv.show() interact(set_frame, i=(0, num_frames - 1)) def update_view(fig, meshes): for old, new in zip(fig.meshes, meshes): old.x = new.x old.y = new.y old.z = new.z old.color = new.color old.triangles = new.triangles old.lines = new.lines def sheet_view(sheet, coords=["x", "y", "z"], **draw_specs_kw): """ Creates a javascript renderer of the edge lines to be displayed in Jupyter Notebooks Returns ------- fig: a :class:`ipyvolume.widgets.Figure` widget mesh: a :class:`ipyvolume.widgets.Mesh` mesh widget """ # ipv.style.use(["dark", "minimal"]) draw_specs = sheet_spec() spec_updater(draw_specs, draw_specs_kw) fig = ipv.gcf() fig.meshes = fig.meshes + _get_meshes(sheet, coords, draw_specs) box_size = max(*(np.ptp(sheet.vert_df[u]) for u in sheet.coords)) border = 0.05 * box_size lim_inf = sheet.vert_df[sheet.coords].min().min() - border lim_sup = sheet.vert_df[sheet.coords].max().max() + border ipv.xyzlim(lim_inf, lim_sup) return fig, fig.meshes def view_ipv(sheet, coords=["x", "y", "z"], **edge_specs): """ Creates a javascript renderer of the edge lines to be displayed in Jupyter Notebooks Returns ------- fig: a :class:`ipyvolume.widgets.Figure` widget mesh: a :class:`ipyvolume.widgets.Mesh` mesh widget """ warnings.warn("`view_ipv` is deprecated, use the more generic `sheet_view`") mesh = edge_mesh(sheet, coords, **edge_specs) fig = ipv.gcf() fig.meshes = fig.meshes + [mesh] box_size = max(*(np.ptp(sheet.vert_df[u]) for u in sheet.coords)) border = 0.05 * box_size lim_inf = sheet.vert_df[sheet.coords].min().min() - border lim_sup = sheet.vert_df[sheet.coords].max().max() + border ipv.xyzlim(lim_inf, lim_sup) return fig, mesh def edge_mesh(sheet, coords, **edge_specs): """ Creates a ipyvolume Mesh of the edge lines to be displayed in Jupyter Notebooks Returns ------- mesh: a :class:`ipyvolume.widgets.Mesh` mesh widget """ spec = sheet_spec()["edge"] spec.update(**edge_specs) if callable(spec["color"]): spec["color"] = spec["color"](sheet) if isinstance(spec["color"], str): color = spec["color"] elif hasattr(spec["color"], "__len__"): color = _wire_color_from_sequence(spec, sheet)[:, :3] u, v, w = coords mesh = ipv.Mesh( x=sheet.vert_df[u], y=sheet.vert_df[v], z=sheet.vert_df[w], lines=sheet.edge_df[["srce", "trgt"]].astype(dtype=np.uint32), color=color, ) return mesh def face_mesh(sheet, coords, **face_draw_specs): """ Creates a ipyvolume Mesh of the face polygons """ Ne, Nf = sheet.Ne, sheet.Nf if callable(face_draw_specs["color"]): face_draw_specs["color"] = face_draw_specs["color"](sheet) if isinstance(face_draw_specs["color"], str): color = face_draw_specs["color"] elif hasattr(face_draw_specs["color"], "__len__"): color = _face_color_from_sequence(face_draw_specs, sheet)[:, :3] if "visible" in sheet.face_df.columns: edges = sheet.edge_df[sheet.upcast_face(sheet.face_df["visible"])].index _sheet = get_sub_eptm(sheet, edges) if _sheet is not None: sheet = _sheet if isinstance(color, np.ndarray): faces = sheet.face_df["face_o"].values.astype(np.uint32) edges = edges.values.astype(np.uint32) indexer = np.concatenate([faces, edges + Nf, edges + Ne + Nf]) color = color.take(indexer, axis=0) epsilon = face_draw_specs.get("epsilon", 0) up_srce = sheet.edge_df[["s" + c for c in coords]] up_trgt = sheet.edge_df[["t" + c for c in coords]] Ne, Nf = sheet.Ne, sheet.Nf if epsilon > 0: up_face = sheet.edge_df[["f" + c for c in coords]].values up_srce = (up_srce - up_face) * (1 - epsilon) + up_face up_trgt = (up_trgt - up_face) * (1 - epsilon) + up_face mesh_ = np.concatenate( [sheet.face_df[coords].values, up_srce.values, up_trgt.values] ) triangles = np.vstack( [sheet.edge_df["face"], np.arange(Ne) + Nf, np.arange(Ne) + Ne + Nf] ).T.astype(dtype=np.uint32) mesh = ipv.Mesh( x=mesh_[:, 0], y=mesh_[:, 1], z=mesh_[:, 2], triangles=triangles, color=color ) return mesh def _wire_color_from_sequence(edge_spec, sheet): """ """ color_ = edge_spec["color"] cmap = cm.get_cmap(edge_spec.get("colormap", "viridis")) if color_.shape in [(sheet.Nv, 3), (sheet.Nv, 4)]: return np.asarray(color_) if color_.shape == (sheet.Nv,): if np.ptp(color_) < 1e-10: return np.ones((sheet.Nv, 3)) * 0.7 return cmap((color_ - color_.min()) / np.ptp(color_)) if color_.shape in [(sheet.Ne, 3), (sheet.Ne, 4)]: color_ = pd.DataFrame(color_, index=sheet.edge_df.index) color_["srce"] = sheet.edge_df["srce"] color_ = color_.groupby("srce").mean().values return color_ if color_.shape == (sheet.Ne,): color_ = pd.DataFrame(color_, index=sheet.edge_df.index) color_["srce"] = sheet.edge_df["srce"] color_ = color_.groupby("srce").mean().values.ravel() if np.ptp(color_) < 1e-10: warnings.warn("Attempting to draw a colormap " "with a uniform value") return np.ones((sheet.Nv, 3)) * 0.7 return cmap((color_ - color_.min()) / np.ptp(color_)) else: raise ValueError("The 'color' value of the spec doesn't have a correct shape.") def _face_color_from_sequence(face_spec, sheet): color_ = face_spec["color"] cmap = cm.get_cmap(face_spec.get("colormap", "viridis")) Nf, Ne = sheet.Nf, sheet.Ne color_min, color_max = face_spec.get("color_range", (color_.min(), color_.max())) face_mesh_shape = Nf + 2 * Ne if color_.shape in [(sheet.Nf, 3), (sheet.Nf, 4)]: return np.concatenate([color_, color_, color_]) elif color_.shape == (sheet.Nf,): if np.ptp(color_) < 1e-10: # warnings.warn("Attempting to draw a colormap with a uniform value") return np.ones((face_mesh_shape, 3)) * 0.5 normed = (color_ - color_min) / (color_max - color_min) up_color = sheet.upcast_face(normed).values return cmap(np.concatenate([normed, up_color, up_color])) else: raise ValueError( "shape of `face_spec['color']` must be either (Nf, 3), (Nf, 4) or (Nf,)" ) def _get_meshes(sheet, coords, draw_specs): meshes = [] edge_spec = draw_specs["edge"] if edge_spec["visible"]: edges = edge_mesh(sheet, coords, **edge_spec) meshes.append(edges) else: edges = None face_spec = draw_specs["face"] if face_spec["visible"]: faces = face_mesh(sheet, coords, **face_spec) meshes.append(faces) else: faces = None return meshes
gpl-3.0
LiaoPan/scikit-learn
sklearn/cluster/tests/test_dbscan.py
114
11393
""" Tests for DBSCAN clustering algorithm """ import pickle import numpy as np from scipy.spatial import distance from scipy import sparse from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_in from sklearn.utils.testing import assert_not_in from sklearn.cluster.dbscan_ import DBSCAN from sklearn.cluster.dbscan_ import dbscan from sklearn.cluster.tests.common import generate_clustered_data from sklearn.metrics.pairwise import pairwise_distances n_clusters = 3 X = generate_clustered_data(n_clusters=n_clusters) def test_dbscan_similarity(): # Tests the DBSCAN algorithm with a similarity array. # Parameters chosen specifically for this task. eps = 0.15 min_samples = 10 # Compute similarities D = distance.squareform(distance.pdist(X)) D /= np.max(D) # Compute DBSCAN core_samples, labels = dbscan(D, metric="precomputed", eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - (1 if -1 in labels else 0) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric="precomputed", eps=eps, min_samples=min_samples) labels = db.fit(D).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_feature(): # Tests the DBSCAN algorithm with a feature vector array. # Parameters chosen specifically for this task. # Different eps to other test, because distance is not normalised. eps = 0.8 min_samples = 10 metric = 'euclidean' # Compute DBSCAN # parameters chosen for task core_samples, labels = dbscan(X, metric=metric, eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples) labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_sparse(): core_sparse, labels_sparse = dbscan(sparse.lil_matrix(X), eps=.8, min_samples=10) core_dense, labels_dense = dbscan(X, eps=.8, min_samples=10) assert_array_equal(core_dense, core_sparse) assert_array_equal(labels_dense, labels_sparse) def test_dbscan_no_core_samples(): rng = np.random.RandomState(0) X = rng.rand(40, 10) X[X < .8] = 0 for X_ in [X, sparse.csr_matrix(X)]: db = DBSCAN(min_samples=6).fit(X_) assert_array_equal(db.components_, np.empty((0, X_.shape[1]))) assert_array_equal(db.labels_, -1) assert_equal(db.core_sample_indices_.shape, (0,)) def test_dbscan_callable(): # Tests the DBSCAN algorithm with a callable metric. # Parameters chosen specifically for this task. # Different eps to other test, because distance is not normalised. eps = 0.8 min_samples = 10 # metric is the function reference, not the string key. metric = distance.euclidean # Compute DBSCAN # parameters chosen for task core_samples, labels = dbscan(X, metric=metric, eps=eps, min_samples=min_samples, algorithm='ball_tree') # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(metric=metric, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) def test_dbscan_balltree(): # Tests the DBSCAN algorithm with balltree for neighbor calculation. eps = 0.8 min_samples = 10 D = pairwise_distances(X) core_samples, labels = dbscan(D, metric="precomputed", eps=eps, min_samples=min_samples) # number of clusters, ignoring noise if present n_clusters_1 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_1, n_clusters) db = DBSCAN(p=2.0, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_2 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_2, n_clusters) db = DBSCAN(p=2.0, eps=eps, min_samples=min_samples, algorithm='kd_tree') labels = db.fit(X).labels_ n_clusters_3 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_3, n_clusters) db = DBSCAN(p=1.0, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_4 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_4, n_clusters) db = DBSCAN(leaf_size=20, eps=eps, min_samples=min_samples, algorithm='ball_tree') labels = db.fit(X).labels_ n_clusters_5 = len(set(labels)) - int(-1 in labels) assert_equal(n_clusters_5, n_clusters) def test_input_validation(): # DBSCAN.fit should accept a list of lists. X = [[1., 2.], [3., 4.]] DBSCAN().fit(X) # must not raise exception def test_dbscan_badargs(): # Test bad argument values: these should all raise ValueErrors assert_raises(ValueError, dbscan, X, eps=-1.0) assert_raises(ValueError, dbscan, X, algorithm='blah') assert_raises(ValueError, dbscan, X, metric='blah') assert_raises(ValueError, dbscan, X, leaf_size=-1) assert_raises(ValueError, dbscan, X, p=-1) def test_pickle(): obj = DBSCAN() s = pickle.dumps(obj) assert_equal(type(pickle.loads(s)), obj.__class__) def test_boundaries(): # ensure min_samples is inclusive of core point core, _ = dbscan([[0], [1]], eps=2, min_samples=2) assert_in(0, core) # ensure eps is inclusive of circumference core, _ = dbscan([[0], [1], [1]], eps=1, min_samples=2) assert_in(0, core) core, _ = dbscan([[0], [1], [1]], eps=.99, min_samples=2) assert_not_in(0, core) def test_weighted_dbscan(): # ensure sample_weight is validated assert_raises(ValueError, dbscan, [[0], [1]], sample_weight=[2]) assert_raises(ValueError, dbscan, [[0], [1]], sample_weight=[2, 3, 4]) # ensure sample_weight has an effect assert_array_equal([], dbscan([[0], [1]], sample_weight=None, min_samples=6)[0]) assert_array_equal([], dbscan([[0], [1]], sample_weight=[5, 5], min_samples=6)[0]) assert_array_equal([0], dbscan([[0], [1]], sample_weight=[6, 5], min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[6, 6], min_samples=6)[0]) # points within eps of each other: assert_array_equal([0, 1], dbscan([[0], [1]], eps=1.5, sample_weight=[5, 1], min_samples=6)[0]) # and effect of non-positive and non-integer sample_weight: assert_array_equal([], dbscan([[0], [1]], sample_weight=[5, 0], eps=1.5, min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[5.9, 0.1], eps=1.5, min_samples=6)[0]) assert_array_equal([0, 1], dbscan([[0], [1]], sample_weight=[6, 0], eps=1.5, min_samples=6)[0]) assert_array_equal([], dbscan([[0], [1]], sample_weight=[6, -1], eps=1.5, min_samples=6)[0]) # for non-negative sample_weight, cores should be identical to repetition rng = np.random.RandomState(42) sample_weight = rng.randint(0, 5, X.shape[0]) core1, label1 = dbscan(X, sample_weight=sample_weight) assert_equal(len(label1), len(X)) X_repeated = np.repeat(X, sample_weight, axis=0) core_repeated, label_repeated = dbscan(X_repeated) core_repeated_mask = np.zeros(X_repeated.shape[0], dtype=bool) core_repeated_mask[core_repeated] = True core_mask = np.zeros(X.shape[0], dtype=bool) core_mask[core1] = True assert_array_equal(np.repeat(core_mask, sample_weight), core_repeated_mask) # sample_weight should work with precomputed distance matrix D = pairwise_distances(X) core3, label3 = dbscan(D, sample_weight=sample_weight, metric='precomputed') assert_array_equal(core1, core3) assert_array_equal(label1, label3) # sample_weight should work with estimator est = DBSCAN().fit(X, sample_weight=sample_weight) core4 = est.core_sample_indices_ label4 = est.labels_ assert_array_equal(core1, core4) assert_array_equal(label1, label4) est = DBSCAN() label5 = est.fit_predict(X, sample_weight=sample_weight) core5 = est.core_sample_indices_ assert_array_equal(core1, core5) assert_array_equal(label1, label5) assert_array_equal(label1, est.labels_) def test_dbscan_core_samples_toy(): X = [[0], [2], [3], [4], [6], [8], [10]] n_samples = len(X) for algorithm in ['brute', 'kd_tree', 'ball_tree']: # Degenerate case: every sample is a core sample, either with its own # cluster or including other close core samples. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=1) assert_array_equal(core_samples, np.arange(n_samples)) assert_array_equal(labels, [0, 1, 1, 1, 2, 3, 4]) # With eps=1 and min_samples=2 only the 3 samples from the denser area # are core samples. All other points are isolated and considered noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=2) assert_array_equal(core_samples, [1, 2, 3]) assert_array_equal(labels, [-1, 0, 0, 0, -1, -1, -1]) # Only the sample in the middle of the dense area is core. Its two # neighbors are edge samples. Remaining samples are noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=3) assert_array_equal(core_samples, [2]) assert_array_equal(labels, [-1, 0, 0, 0, -1, -1, -1]) # It's no longer possible to extract core samples with eps=1: # everything is noise. core_samples, labels = dbscan(X, algorithm=algorithm, eps=1, min_samples=4) assert_array_equal(core_samples, []) assert_array_equal(labels, -np.ones(n_samples)) def test_dbscan_precomputed_metric_with_degenerate_input_arrays(): # see https://github.com/scikit-learn/scikit-learn/issues/4641 for # more details X = np.ones((10, 2)) labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_ assert_equal(len(set(labels)), 1) X = np.zeros((10, 2)) labels = DBSCAN(eps=0.5, metric='precomputed').fit(X).labels_ assert_equal(len(set(labels)), 1)
bsd-3-clause
ephes/scikit-learn
examples/decomposition/plot_faces_decomposition.py
204
4452
""" ============================ Faces dataset decompositions ============================ This example applies to :ref:`olivetti_faces` different unsupervised matrix decomposition (dimension reduction) methods from the module :py:mod:`sklearn.decomposition` (see the documentation chapter :ref:`decompositions`) . """ print(__doc__) # Authors: Vlad Niculae, Alexandre Gramfort # License: BSD 3 clause import logging from time import time from numpy.random import RandomState import matplotlib.pyplot as plt from sklearn.datasets import fetch_olivetti_faces from sklearn.cluster import MiniBatchKMeans from sklearn import decomposition # Display progress logs on stdout logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s') n_row, n_col = 2, 3 n_components = n_row * n_col image_shape = (64, 64) rng = RandomState(0) ############################################################################### # Load faces data dataset = fetch_olivetti_faces(shuffle=True, random_state=rng) faces = dataset.data n_samples, n_features = faces.shape # global centering faces_centered = faces - faces.mean(axis=0) # local centering faces_centered -= faces_centered.mean(axis=1).reshape(n_samples, -1) print("Dataset consists of %d faces" % n_samples) ############################################################################### def plot_gallery(title, images, n_col=n_col, n_row=n_row): plt.figure(figsize=(2. * n_col, 2.26 * n_row)) plt.suptitle(title, size=16) for i, comp in enumerate(images): plt.subplot(n_row, n_col, i + 1) vmax = max(comp.max(), -comp.min()) plt.imshow(comp.reshape(image_shape), cmap=plt.cm.gray, interpolation='nearest', vmin=-vmax, vmax=vmax) plt.xticks(()) plt.yticks(()) plt.subplots_adjust(0.01, 0.05, 0.99, 0.93, 0.04, 0.) ############################################################################### # List of the different estimators, whether to center and transpose the # problem, and whether the transformer uses the clustering API. estimators = [ ('Eigenfaces - RandomizedPCA', decomposition.RandomizedPCA(n_components=n_components, whiten=True), True), ('Non-negative components - NMF', decomposition.NMF(n_components=n_components, init='nndsvda', beta=5.0, tol=5e-3, sparseness='components'), False), ('Independent components - FastICA', decomposition.FastICA(n_components=n_components, whiten=True), True), ('Sparse comp. - MiniBatchSparsePCA', decomposition.MiniBatchSparsePCA(n_components=n_components, alpha=0.8, n_iter=100, batch_size=3, random_state=rng), True), ('MiniBatchDictionaryLearning', decomposition.MiniBatchDictionaryLearning(n_components=15, alpha=0.1, n_iter=50, batch_size=3, random_state=rng), True), ('Cluster centers - MiniBatchKMeans', MiniBatchKMeans(n_clusters=n_components, tol=1e-3, batch_size=20, max_iter=50, random_state=rng), True), ('Factor Analysis components - FA', decomposition.FactorAnalysis(n_components=n_components, max_iter=2), True), ] ############################################################################### # Plot a sample of the input data plot_gallery("First centered Olivetti faces", faces_centered[:n_components]) ############################################################################### # Do the estimation and plot it for name, estimator, center in estimators: print("Extracting the top %d %s..." % (n_components, name)) t0 = time() data = faces if center: data = faces_centered estimator.fit(data) train_time = (time() - t0) print("done in %0.3fs" % train_time) if hasattr(estimator, 'cluster_centers_'): components_ = estimator.cluster_centers_ else: components_ = estimator.components_ if hasattr(estimator, 'noise_variance_'): plot_gallery("Pixelwise variance", estimator.noise_variance_.reshape(1, -1), n_col=1, n_row=1) plot_gallery('%s - Train time %.1fs' % (name, train_time), components_[:n_components]) plt.show()
bsd-3-clause
devanshdalal/scikit-learn
examples/ensemble/plot_isolation_forest.py
39
2361
""" ========================================== IsolationForest example ========================================== An example using IsolationForest for anomaly detection. The IsolationForest 'isolates' observations by randomly selecting a feature and then randomly selecting a split value between the maximum and minimum values of the selected feature. Since recursive partitioning can be represented by a tree structure, the number of splittings required to isolate a sample is equivalent to the path length from the root node to the terminating node. This path length, averaged over a forest of such random trees, is a measure of normality and our decision function. Random partitioning produces noticeable shorter paths for anomalies. Hence, when a forest of random trees collectively produce shorter path lengths for particular samples, they are highly likely to be anomalies. .. [1] Liu, Fei Tony, Ting, Kai Ming and Zhou, Zhi-Hua. "Isolation forest." Data Mining, 2008. ICDM'08. Eighth IEEE International Conference on. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.ensemble import IsolationForest rng = np.random.RandomState(42) # Generate train data X = 0.3 * rng.randn(100, 2) X_train = np.r_[X + 2, X - 2] # Generate some regular novel observations X = 0.3 * rng.randn(20, 2) X_test = np.r_[X + 2, X - 2] # Generate some abnormal novel observations X_outliers = rng.uniform(low=-4, high=4, size=(20, 2)) # fit the model clf = IsolationForest(max_samples=100, random_state=rng) clf.fit(X_train) y_pred_train = clf.predict(X_train) y_pred_test = clf.predict(X_test) y_pred_outliers = clf.predict(X_outliers) # plot the line, the samples, and the nearest vectors to the plane xx, yy = np.meshgrid(np.linspace(-5, 5, 50), np.linspace(-5, 5, 50)) Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.title("IsolationForest") plt.contourf(xx, yy, Z, cmap=plt.cm.Blues_r) b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c='white') b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c='green') c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c='red') plt.axis('tight') plt.xlim((-5, 5)) plt.ylim((-5, 5)) plt.legend([b1, b2, c], ["training observations", "new regular observations", "new abnormal observations"], loc="upper left") plt.show()
bsd-3-clause
thp44/delphin_6_automation
data_process/2d_1d/archieve/moisture_content_comparison.py
1
18274
__author__ = "Christian Kongsgaard" __license__ = 'MIT' # -------------------------------------------------------------------------------------------------------------------- # # IMPORTS # Modules import pandas as pd import matplotlib.pyplot as plt # RiBuild Modules # -------------------------------------------------------------------------------------------------------------------- # # RIBuild out_folder = r'C:\Users\ocni\PycharmProjects\delphin_6_automation\data_process\2d_1d\processed_data' graphic_folder = r'U:\RIBuild\2D_1D\Processed Results\4A' hdf_file = out_folder + '/relative_moisture_content.h5' # Open HDF # Uninsulated dresdenzp_highratio_uninsulated_4a = pd.read_hdf(hdf_file, 'dresden_zp_high_ratio_uninsulated_4a') dresdenzd_highratio_uninsulated_4a = pd.read_hdf(hdf_file, 'dresden_zd_high_ratio_uninsulated_4a') postdam_highratio_uninsulated_4a = pd.read_hdf(hdf_file, 'potsdam_high_ratio_uninsulated_4a') dresdenzp_lowratio_uninsulated_4a = pd.read_hdf(hdf_file, 'dresden_zp_low_ratio_uninsulated_4a') dresdenzd_lowratio_uninsulated_4a = pd.read_hdf(hdf_file, 'dresden_zd_low_ratio_uninsulated_4a') postdam_lowratio_uninsulated_4a = pd.read_hdf(hdf_file, 'potsdam_low_ratio_uninsulated_4a') total_uninsulated_4a = pd.concat([dresdenzp_highratio_uninsulated_4a, dresdenzd_highratio_uninsulated_4a, postdam_highratio_uninsulated_4a, dresdenzp_lowratio_uninsulated_4a, dresdenzd_lowratio_uninsulated_4a, postdam_lowratio_uninsulated_4a]) # Insulated dresdenzp_highratio_insulated_4a = pd.read_hdf(hdf_file, 'dresden_zp_high_ratio_insulated_4a') dresdenzd_highratio_insulated_4a = pd.read_hdf(hdf_file, 'dresden_zd_high_ratio_insulated_4a') postdam_highratio_insulated_4a = pd.read_hdf(hdf_file, 'potsdam_high_ratio_insulated_4a') dresdenzp_lowratio_insulated_4a = pd.read_hdf(hdf_file, 'dresden_zp_low_ratio_insulated_4a') dresdenzd_lowratio_insulated_4a = pd.read_hdf(hdf_file, 'dresden_zd_low_ratio_insulated_4a') postdam_lowratio_insulated_4a = pd.read_hdf(hdf_file, 'potsdam_low_ratio_insulated_4a') total_insulated_4a = pd.concat([dresdenzp_highratio_insulated_4a, dresdenzd_highratio_insulated_4a, postdam_highratio_insulated_4a, dresdenzp_lowratio_insulated_4a, dresdenzd_lowratio_insulated_4a, postdam_lowratio_insulated_4a]) def plots(plot, save=False): """ Creates box plots from all the wall scenarios """ if plot == 'uninsulated' or plot == 'all': plt.figure('dresdenzp_highratio_uninsulated_4a_moisture', figsize=(16, 8), tight_layout=True) dresdenzp_highratio_uninsulated_4a.boxplot(showfliers=False) plt.ylim(-5, 1100) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Dresden ZP - Mortar: High Cement Ratio - Insulation: None') if save: plt.savefig(f"{graphic_folder}/dresdenzp_highratio_uninsulated_4a_moisture") plt.figure('dresdenzd_highratio_uninsulated_4a_moisture', figsize=(16, 8), tight_layout=True) dresdenzd_highratio_uninsulated_4a.boxplot(showfliers=False) plt.ylim(-5, 1100) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Dresden ZD - Mortar: High Cement Ratio - Insulation: None') if save: plt.savefig(f"{graphic_folder}/dresdenzd_highratio_uninsulated_4a_moisture") plt.figure('postdam_highratio_uninsulated_4a_moisture', figsize=(16, 8), tight_layout=True) postdam_highratio_uninsulated_4a.boxplot(showfliers=False) plt.ylim(-5, 1100) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Potsdam - Mortar: High Cement Ratio - Insulation: None') if save: plt.savefig(f"{graphic_folder}/postdam_highratio_uninsulated_4a_moisture") plt.figure('dresdenzp_lowratio_uninsulated_4a_moisture', figsize=(16, 8), tight_layout=True) dresdenzp_lowratio_uninsulated_4a.boxplot(showfliers=False) plt.ylim(-5, 1100) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Dresden ZP - Mortar: Low Cement Ratio - Insulation: None') if save: plt.savefig(f"{graphic_folder}/dresdenzp_lowratio_uninsulated_4a_moisture") plt.figure('dresdenzd_lowratio_uninsulated_4a_moisture', figsize=(16, 8), tight_layout=True) dresdenzd_lowratio_uninsulated_4a.boxplot(showfliers=False) plt.ylim(-5, 1100) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Dresden ZD - Mortar: Low Cement Ratio - Insulation: None') if save: plt.savefig(f"{graphic_folder}/dresdenzd_lowratio_uninsulated_4a_moisture") plt.figure('postdam_lowratio_uninsulated_4a_moisture', figsize=(16, 8), tight_layout=True) postdam_lowratio_uninsulated_4a.boxplot(showfliers=False) plt.ylim(-5, 1100) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Potsdam - Mortar: Low Cement Ratio - Insulation: None') if save: plt.savefig(f"{graphic_folder}/postdam_lowratio_uninsulated_4a_moisture") plt.figure('total_uninsulated_4a_moisture', figsize=(16, 8), tight_layout=True) total_uninsulated_4a.boxplot(showfliers=False) plt.ylim(-5, 1100) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: All - Mortar: All - Insulation: None') if save: plt.savefig(f"{graphic_folder}/total_uninsulated_4a_moisture") if plot == 'insulated' or plot == 'all': plt.figure('dresdenzp_highratio_insulated_4a_moisture', figsize=(16, 8), tight_layout=True) dresdenzp_highratio_insulated_4a.boxplot(showfliers=False) plt.ylim(-5, 2000) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Dresden ZP - Mortar: High Cement Ratio - Insulation: Calcium Silicate') if save: plt.savefig(f"{graphic_folder}/dresdenzp_highratio_insulated_4a_moisture") plt.figure('dresdenzd_highratio_insulated_4a_moisture', figsize=(16, 8), tight_layout=True) dresdenzd_highratio_insulated_4a.boxplot(showfliers=False) plt.ylim(-5, 2000) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Dresden ZD - Mortar: High Cement Ratio - Insulation: Calcium Silicate') if save: plt.savefig(f"{graphic_folder}/dresdenzd_highratio_insulated_4a_moisture") plt.figure('postdam_highratio_insulated_4a_moisture', figsize=(16, 8), tight_layout=True) postdam_highratio_insulated_4a.boxplot(showfliers=False) plt.ylim(-5, 2000) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Potsdam - Mortar: High Cement Ratio - Insulation: Calcium Silicate') if save: plt.savefig(f"{graphic_folder}/postdam_highratio_insulated_4a_moisture") plt.figure('dresdenzp_lowratio_insulated_4a_moisture', figsize=(16, 8), tight_layout=True) dresdenzp_lowratio_insulated_4a.boxplot(showfliers=False) plt.ylim(-5, 2000) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Dresden ZP - Mortar: Low Cement Ratio - Insulation: Calcium Silicate') if save: plt.savefig(f"{graphic_folder}/dresdenzp_lowratio_insulated_4a_moisture") plt.figure('dresdenzd_lowratio_insulated_4a_moisture', figsize=(16, 8), tight_layout=True) dresdenzd_lowratio_insulated_4a.boxplot(showfliers=False) plt.ylim(-5, 2000) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Dresden ZD - Mortar: Low Cement Ratio - Insulation: Calcium Silicate') if save: plt.savefig(f"{graphic_folder}/dresdenzd_lowratio_insulated_4a_moisture") plt.figure('postdam_lowratio_insulated_4a_moisture', figsize=(16, 8), tight_layout=True) postdam_lowratio_insulated_4a.boxplot(showfliers=False) plt.ylim(-5, 2000) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: Potsdam - Mortar: Low Cement Ratio - Insulation: Calcium Silicate') if save: plt.savefig(f"{graphic_folder}/postdam_lowratio_insulated_4a_moisture") plt.figure('total_insulated_4a_moisture', figsize=(16, 8), tight_layout=True) total_insulated_4a.boxplot(showfliers=False) plt.ylim(-5, 2000) plt.ylabel('Relative Difference in %') plt.title('Weighted Relative Difference between 1D and 2D\n' 'Moisture Content\n' 'Brick: All - Mortar: All - Insulation: Calcium Silicate') if save: plt.savefig(f"{graphic_folder}/total_insulated_4a_moisture") plt.show() plots('all', False) def std3_ratio(print_=False, excel=False): """Computes ratio of outliers in the data sets. Outliers is here defined as data points deviating with more the 3 standard deviations from the mean.""" std3_uninsulated_ratio_ = uninsulated() std3_insulated_ratio_ = insulated() if print_: print('Uninsulated') print(std3_uninsulated_ratio_) print('') print('Insulated') print(std3_insulated_ratio_) if excel: writer = pd.ExcelWriter(f'{out_folder}/moisture_std_ratios.xlsx') std3_uninsulated_ratio_.to_excel(writer, 'Uninsulated') std3_insulated_ratio_.to_excel(writer, 'Insulated') writer.save() def uninsulated(): """Computes the outliers for the uninsulated cases""" outliers_total_uninsulated = (total_uninsulated_4a.shape[0] - total_uninsulated_4a.sub(total_uninsulated_4a.mean()) .div(total_uninsulated_4a.std()).abs().lt(3).sum()) / total_uninsulated_4a.shape[0] outliers_zd_high_uninsulated = (dresdenzd_highratio_uninsulated_4a.shape[0] - dresdenzd_highratio_uninsulated_4a.sub(dresdenzd_highratio_uninsulated_4a.mean()) .div(dresdenzd_highratio_uninsulated_4a.std()).abs().lt(3).sum()) \ / dresdenzd_highratio_uninsulated_4a.shape[0] outliers_zp_high_uninsulated = (dresdenzp_highratio_uninsulated_4a.shape[0] - dresdenzp_highratio_uninsulated_4a.sub(dresdenzp_highratio_uninsulated_4a.mean()) .div(dresdenzp_highratio_uninsulated_4a.std()).abs().lt(3).sum()) \ / dresdenzp_highratio_uninsulated_4a.shape[0] outliers_pd_high_uninsulated = (postdam_highratio_uninsulated_4a.shape[0] - postdam_highratio_uninsulated_4a.sub(postdam_highratio_uninsulated_4a.mean()) .div(postdam_highratio_uninsulated_4a.std()).abs().lt(3).sum()) \ / postdam_highratio_uninsulated_4a.shape[0] outliers_zd_low_uninsulated = (dresdenzd_lowratio_uninsulated_4a.shape[0] - dresdenzd_lowratio_uninsulated_4a.sub(dresdenzd_lowratio_uninsulated_4a.mean()) .div(dresdenzd_lowratio_uninsulated_4a.std()).abs().lt(3).sum()) \ / dresdenzd_lowratio_uninsulated_4a.shape[0] outliers_zp_low_uninsulated = (dresdenzp_lowratio_uninsulated_4a.shape[0] - dresdenzp_lowratio_uninsulated_4a.sub(dresdenzp_lowratio_uninsulated_4a.mean()) .div(dresdenzp_lowratio_uninsulated_4a.std()).abs().lt(3).sum()) \ / dresdenzp_lowratio_uninsulated_4a.shape[0] outliers_pd_low_uninsulated = (postdam_lowratio_uninsulated_4a.shape[0] - postdam_lowratio_uninsulated_4a.sub(postdam_lowratio_uninsulated_4a.mean()) .div(postdam_lowratio_uninsulated_4a.std()).abs().lt(3).sum()) \ / postdam_lowratio_uninsulated_4a.shape[0] outliers_uninsulated_ratio_ = pd.concat([outliers_total_uninsulated, outliers_zd_high_uninsulated, outliers_zp_high_uninsulated, outliers_pd_high_uninsulated, outliers_zd_low_uninsulated, outliers_zp_low_uninsulated, outliers_pd_low_uninsulated], axis=1) outliers_uninsulated_ratio_.columns = ["Brick: All - Mortar: All - Insulation: None", "Brick: Dresden ZD - Mortar: High Cement Ratio - Insulation: None", "Brick: Dresden ZP - Mortar: High Cement Ratio - Insulation: None", "Brick: Potsdam - Mortar: High Cement Ratio - Insulation: None", "Brick: Dresden ZD - Mortar: Low Cement Ratio - Insulation: None", "Brick: Dresden ZP - Mortar: Low Cement Ratio - Insulation: None", "Brick: Potsdam - Mortar: Low Cement Ratio - Insulation: None"] return outliers_uninsulated_ratio_ def insulated(): """Computes the outliers for the insulated cases""" outliers_total_insulated = (total_insulated_4a.shape[0] - total_insulated_4a.sub(total_insulated_4a.mean()) .div(total_insulated_4a.std()).abs().lt(3).sum()) / total_insulated_4a.shape[0] outliers_zd_high_insulated = (dresdenzd_highratio_insulated_4a.shape[0] - dresdenzd_highratio_insulated_4a.sub(dresdenzd_highratio_insulated_4a.mean()) .div(dresdenzd_highratio_insulated_4a.std()).abs().lt(3).sum()) \ / dresdenzd_highratio_insulated_4a.shape[0] outliers_zp_high_insulated = (dresdenzp_highratio_insulated_4a.shape[0] - dresdenzp_highratio_insulated_4a.sub(dresdenzp_highratio_insulated_4a.mean()) .div(dresdenzp_highratio_insulated_4a.std()).abs().lt(3).sum()) \ / dresdenzp_highratio_insulated_4a.shape[0] outliers_pd_high_insulated = (postdam_highratio_insulated_4a.shape[0] - postdam_highratio_insulated_4a.sub(postdam_highratio_insulated_4a.mean()) .div(postdam_highratio_insulated_4a.std()).abs().lt(3).sum()) \ / postdam_highratio_insulated_4a.shape[0] outliers_zd_low_insulated = (dresdenzd_lowratio_insulated_4a.shape[0] - dresdenzd_lowratio_insulated_4a.sub(dresdenzd_lowratio_insulated_4a.mean()) .div(dresdenzd_lowratio_insulated_4a.std()).abs().lt(3).sum()) \ / dresdenzd_lowratio_insulated_4a.shape[0] outliers_zp_low_insulated = (dresdenzp_lowratio_insulated_4a.shape[0] - dresdenzp_lowratio_insulated_4a.sub(dresdenzp_lowratio_insulated_4a.mean()) .div(dresdenzp_lowratio_insulated_4a.std()).abs().lt(3).sum()) \ / dresdenzp_lowratio_insulated_4a.shape[0] outliers_pd_low_insulated = (postdam_lowratio_insulated_4a.shape[0] - postdam_lowratio_insulated_4a.sub(postdam_lowratio_insulated_4a.mean()) .div(postdam_lowratio_insulated_4a.std()).abs().lt(3).sum()) \ / postdam_lowratio_insulated_4a.shape[0] std2_insulated_ratio_ = pd.concat([outliers_total_insulated, outliers_zd_high_insulated, outliers_zp_high_insulated, outliers_pd_high_insulated, outliers_zd_low_insulated, outliers_zp_low_insulated, outliers_pd_low_insulated], axis=1) std2_insulated_ratio_.columns = ["Brick: All - Mortar: All - Insulation: None", "Brick: Dresden ZD - Mortar: High Cement Ratio - Insulation: Calcium Silicate", "Brick: Dresden ZP - Mortar: High Cement Ratio - Insulation: Calcium Silicate", "Brick: Potsdam - Mortar: High Cement Ratio - Insulation: Calcium Silicate", "Brick: Dresden ZD - Mortar: Low Cement Ratio - Insulation: Calcium Silicate", "Brick: Dresden ZP - Mortar: Low Cement Ratio - Insulation: Calcium Silicate", "Brick: Potsdam - Mortar: Low Cement Ratio - Insulation: Calcium Silicate"] return std2_insulated_ratio_ #std3_ratio(False, True)
mit
miaecle/deepchem
devtools/archive/jenkins/generate_graph.py
2
5220
import csv import os import numpy as np import matplotlib.pyplot as plt import time plt.switch_backend('agg') TODO = { ('tox21', 'random'): [ 'weave', 'graphconv', 'tf', 'tf_robust', 'irv', 'xgb', 'logreg', 'textcnn' ], ('clintox', 'random'): [ 'weave', 'graphconv', 'tf', 'tf_robust', 'irv', 'xgb', 'logreg', 'textcnn' ], ('sider', 'random'): [ 'weave', 'graphconv', 'tf', 'tf_robust', 'irv', 'xgb', 'logreg', 'textcnn' ], ('bbbp', 'scaffold'): ['weave', 'graphconv', 'tf', 'irv', 'xgb', 'logreg', 'textcnn'], ('bace_c', 'scaffold'): ['weave', 'graphconv', 'tf', 'irv', 'xgb', 'logreg', 'textcnn'], ('hiv', 'scaffold'): ['weave', 'graphconv', 'tf', 'irv', 'xgb', 'logreg', 'textcnn'], ('muv', 'random'): ['graphconv', 'tf', 'tf_robust', 'irv', 'xgb', 'logreg'], ('delaney', 'random'): [ 'weave_regression', 'graphconvreg', 'tf_regression', 'xgb_regression', 'krr', 'textcnn_regression', 'dag_regression', 'mpnn' ], ('sampl', 'random'): [ 'weave_regression', 'graphconvreg', 'tf_regression', 'xgb_regression', 'krr', 'textcnn_regression', 'dag_regression', 'mpnn' ], ('lipo', 'random'): [ 'weave_regression', 'graphconvreg', 'tf_regression', 'xgb_regression', 'krr', 'textcnn_regression', 'dag_regression', 'mpnn' ], ('qm7', 'stratified'): [ 'dtnn', 'graphconvreg', 'tf_regression_ft', 'krr_ft' ], ('qm8', 'random'): [ 'dtnn', 'graphconvreg', 'weave_regression', 'textcnn_regression', 'mpnn', 'tf_regression', 'tf_regression_ft' ], } ORDER = [ 'logreg', 'rf', 'rf_regression', 'xgb', 'xgb_regression', 'kernelsvm', 'krr', 'krr_ft', 'tf', 'tf_regression', 'tf_regression_ft', 'tf_robust', 'irv', 'textcnn', 'textcnn_regression', 'graphconv', 'graphconvreg', 'dag', 'dag_regression', 'ani', 'weave', 'weave_regression', 'dtnn', 'mpnn' ] COLOR = { 'logreg': '#3F3F3F', 'rf': '#67AD4F', 'rf_regression': '#67AD4F', 'xgb': '#0E766C', 'xgb_regression': '#0E766C', 'kernelsvm': '#FC926B', 'krr': '#FC926B', 'krr_ft': '#5A372A', 'tf': '#2B6596', 'tf_regression': '#2B6596', 'tf_regression_ft': '#162939', 'tf_robust': '#775183', 'irv': '#D9D9D9', 'graphconv': '#A4D192', 'graphconvreg': '#A4D192', 'dag': '#D06329', 'dag_regression': '#D06329', 'ani': '#D9D9D9', 'weave': '#8196AE', 'weave_regression': '#8196AE', 'textcnn': '#811B18', 'textcnn_regression': '#811B18', 'dtnn': '#D06329', 'mpnn': '#7B0A48' } TODO_list = set() for key in TODO.keys(): for val in TODO[key]: TODO_list.add((key[0], key[1], val)) def read_results(path): Results = set() with open(path, 'r') as f: reader = csv.reader(f) for line in reader: Results.add((line[0], line[1], line[3])) return Results def run_benchmark(path, deepchem_dir): finished = read_results(path) os.chdir(deepchem_dir) os.chdir('./examples') while len(TODO_list - finished) > 0: todo = TODO_list - finished for p in todo: os.system('python benchmark.py --seed 123 -d ' + p[0] + ' -s ' + p[1] + ' -m ' + p[2]) def plot(dataset, split, path, out_path): if dataset in [ 'bace_c', 'bbbp', 'clintox', 'hiv', 'muv', 'pcba', 'pcba_146', 'pcba_2475', 'sider', 'tox21', 'toxcast' ]: mode = 'classification' else: mode = 'regression' data = {} with open(path, 'r') as f: reader = csv.reader(f) for line in reader: if line[0] == dataset and line[1] == split: data[line[3]] = line[8] labels = [] values = [] colors = [] for model in ORDER: if model in data.keys(): labels.append(model) colors.append(COLOR[model]) values.append(float(data[model])) y_pos = np.arange(len(labels)) plt.rcdefaults() fig, ax = plt.subplots() ax.barh(y_pos, values, align='center', color='green') ax.set_yticks(y_pos) ax.set_yticklabels(labels) ax.invert_yaxis() if mode == 'regression': ax.set_xlabel('R square') ax.set_xlim(left=0., right=1.) else: ax.set_xlabel('ROC-AUC') ax.set_xlim(left=0.4, right=1.) t = time.localtime(time.time()) ax.set_title("Performance on %s (%s split), %i-%i-%i" % (dataset, split, t.tm_year, t.tm_mon, t.tm_mday)) plt.tight_layout() for i in range(len(colors)): ax.get_children()[i].set_color(colors[i]) ax.text( values[i] - 0.1, y_pos[i] + 0.1, str("%.3f" % values[i]), color='white') fig.savefig(os.path.join(out_path, dataset + '_' + split + '.png')) #plt.show() if __name__ == '__main__': current_dir = os.path.dirname(os.path.realpath(__file__)) DEEPCHEM_DIR = os.path.split(os.path.split(current_dir)[0])[0] FILE = os.path.join(os.path.join(DEEPCHEM_DIR, 'examples'), 'results.csv') #run_benchmark(FILE, DEEPCHEM_DIR) save_dir = os.path.join(DEEPCHEM_DIR, 'datasets/MolNet_pic') if not os.path.exists(save_dir): os.mkdir(save_dir) for pair in TODO.keys(): plot(pair[0], pair[1], FILE, save_dir) os.system('aws s3 sync ' + save_dir + ' s3://deepchem.io/trained_models/MolNet_pic')
mit
xiongzhenggang/xiongzhenggang.github.io
AI/data/deeplearning24054/planar_utils.py
2
2271
import matplotlib.pyplot as plt import numpy as np import sklearn import sklearn.datasets import sklearn.linear_model def plot_decision_boundary(model, X, Y): # Set min and max values and give it some padding x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1 y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1 h = 0.01 # Generate a grid of points with distance h between them xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # Predict the function value for the whole grid Z = model(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) # Plot the contour and training examples plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral) plt.ylabel('x2') plt.xlabel('x1') plt.scatter(X[0, :], X[1, :], c=Y.reshape(X[0,:].shape), cmap=plt.cm.Spectral) def sigmoid(x): """ Compute the sigmoid of x Arguments: x -- A scalar or numpy array of any size. Return: s -- sigmoid(x) """ s = 1/(1+np.exp(-x)) return s def load_planar_dataset(): np.random.seed(1) m = 400 # number of examples N = int(m/2) # number of points per class D = 2 # dimensionality X = np.zeros((m,D)) # data matrix where each row is a single example Y = np.zeros((m,1), dtype='uint8') # labels vector (0 for red, 1 for blue) a = 4 # maximum ray of the flower for j in range(2): ix = range(N*j,N*(j+1)) t = np.linspace(j*3.12,(j+1)*3.12,N) + np.random.randn(N)*0.2 # theta r = a*np.sin(4*t) + np.random.randn(N)*0.2 # radius X[ix] = np.c_[r*np.sin(t), r*np.cos(t)] Y[ix] = j X = X.T Y = Y.T return X, Y def load_extra_datasets(): N = 200 noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3) noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2) blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6) gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2, n_classes=2, shuffle=True, random_state=None) no_structure = np.random.rand(N, 2), np.random.rand(N, 2) return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure
gpl-3.0
airanmehr/bio
Scripts/TimeSeriesPaper/Plot/topSNPs.py
1
1589
''' Copyleft Oct 14, 2016 Arya Iranmehr, PhD Student, Bafna Lab, UC San Diego, Email: airanmehr@gmail.com ''' import numpy as np; np.set_printoptions(linewidth=200, precision=5, suppress=True) import pandas as pd; pd.options.display.max_rows = 20; pd.options.display.expand_frame_repr = False import seaborn as sns import matplotlib as mpl import os; home = os.path.expanduser('~') + '/' import Utils.Util as utl import Scripts.TimeSeriesPaper.RealData.Utils as rutl a = rutl.loadAllScores().groupby(level='h', axis=1).apply(rutl.HstatisticAll) df = pd.read_pickle(utl.outpath + 'real/scores.df') i = df.lrd.sort_values().index[-1] df.loc[i] cd = pd.read_pickle(utl.outpath + 'real/CD.F59.df') import Utils.Plots as pplt import pylab as plt names = rutl.loadSNPIDs() sns.set_style("white", {"grid.color": "0.9", 'axes.linewidth': .5, "grid.linewidth": "9.99"}) mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']}); mpl.rc('text', usetex=True) reload(pplt) f, ax = plt.subplots(1, 2, sharey=True, dpi=300, figsize=(4, 2)) i = a[0.5].sort_values().index[-1] sns.set_context("notebook", font_scale=1, rc={"lines.linewidth": 1.2}) pplt.plotSiteReal(cd.loc[i], ax=ax[0], legend=True) ax[0].set_title('{}:{:.0f} ({})'.format(i[0], i[1], names.loc[i]), fontsize=8) i = df.lrdiff.sort_values().index[-1] pplt.plotSiteReal(cd.loc[i], ax=ax[1]) sns.set_context("notebook", font_scale=1, rc={"lines.linewidth": 1.2}) ax[1].set_title('{}:{:.0f} ({})'.format(i[0], i[1], names.loc[i]), fontsize=8) plt.gcf().subplots_adjust(bottom=0.2) pplt.savefig('topSNPs', 300) plt.show()
mit
sighingnow/sighingnow.github.io
resource/k_nearest_neighbors/dating.py
1
3622
#! /usr/bin/env python # -*- coding: utf-8 ''' Name: dating.py(KNN algorithm) Training and test dataset: dating.txt Created on Feb 8, 2015 @author: Tao He ''' __author__ = 'Tao He' from numpy import array as nmarray from matplotlib import pyplot as plt LABEL_MAP = { 'didntLike': 1, 'smallDoses': 2, 'largeDoses': 3, } ATTR_MAP = { 1: 'Number of frequent flyer miles earned per year', 2: 'Percentage of time spent playing video games', 3: 'Liters of ice cream consumed per week', } def create_dataset(filename=None): ''' Return data group and labels. Get the data from file. If the filename is not specialed, return None. dataformat: flyerMiles, gameTime, icecream, label. ''' def normalize_data(data=None): ''' Normalized dataset. Normalize all data to range 0-1. ''' if data is None: return None for column in range(data[0].__len__()): max_val, min_val = max(data[:, column]), min(data[:, column]) for row in range(data.__len__()): data[row][column] = (data[row][column]-min_val)/(max_val-min_val) return data if filename == None: return (None, None) group = [] labels = [] with open(filename, mode='r') as fp_data: for line in fp_data: group.append([float(num) for num in line[:-1].split('\t')[0:3]]) labels.append(LABEL_MAP[line[:-1].split('\t')[3]]) return normalize_data(nmarray(group)), labels def draw_pic(group=None, labels=None, x=0, y=0): ''' Draw a subplot from data group. ''' if group is None or labels is None: return None name = 'knn-dating' figure = plt.figure(num=name, dpi=100) ax_main = figure.add_subplot(1, 1, 1, xlabel=ATTR_MAP[x+1], ylabel=ATTR_MAP[y+1], title=name) ax_main.scatter(group[:, x], group[:, y], s=15*nmarray(labels), c=[[i/LABEL_MAP.__len__()] for i in labels]) plt.show() ## plt.savefig('%s.png'%name, format='png', dpi=100) def knn_classify(group, labels, attrs, ratio=0.5, item=0, k=3): ''' Return the type of item. knn classify function. ''' def get_dist(i, j): ''' Return the distence of group[i] and group[j]. ''' dist = 0.0 for attr in attrs: dist += (group[i][attr]-group[j][attr])*(group[i][attr]-group[j][attr]) return dist length = group.__len__() distence = [] for i in range(int(length*ratio), length): distence.append((i, get_dist(item, i))) cnt = {} distence.sort(key=lambda item: item[1]) for i in range(k): label = labels[distence[i][0]] if label in cnt: cnt[label] += 1 else: cnt[label] = 1 return sorted(cnt.items(), key=lambda item: item[1], reverse=True)[0][0] def knn(): ''' KNN classify algorithm. ''' data, labels = create_dataset('dating.txt') ratio, attr = 0.5, [0, 1, 2] cnt, cnt_correct = 0, 0 length = data.__len__() for i in range(0, int(length*ratio)): cnt += 1 knn_type = knn_classify(data, labels, attr, ratio, i, 3) # print('case[%d]: real: %d, knn: %d'%(i, labels[i], knn_type)) if knn_type == labels[i]: cnt_correct += 1 print('total: %d, correct: %d, correct ratio: %f'%(cnt, cnt_correct, cnt_correct/cnt)) if __name__ == '__main__': knn() # vim: set sw=4, ts=4, fileencoding=utf-8
mit
ZenDevelopmentSystems/scikit-learn
sklearn/cluster/mean_shift_.py
96
15434
"""Mean shift clustering algorithm. Mean shift clustering aims to discover *blobs* in a smooth density of samples. It is a centroid based algorithm, which works by updating candidates for centroids to be the mean of the points within a given region. These candidates are then filtered in a post-processing stage to eliminate near-duplicates to form the final set of centroids. Seeding is performed using a binning technique for scalability. """ # Authors: Conrad Lee <conradlee@gmail.com> # Alexandre Gramfort <alexandre.gramfort@inria.fr> # Gael Varoquaux <gael.varoquaux@normalesup.org> # Martino Sorbaro <martino.sorbaro@ed.ac.uk> import numpy as np import warnings from collections import defaultdict from ..externals import six from ..utils.validation import check_is_fitted from ..utils import extmath, check_random_state, gen_batches, check_array from ..base import BaseEstimator, ClusterMixin from ..neighbors import NearestNeighbors from ..metrics.pairwise import pairwise_distances_argmin from ..externals.joblib import Parallel from ..externals.joblib import delayed def estimate_bandwidth(X, quantile=0.3, n_samples=None, random_state=0): """Estimate the bandwidth to use with the mean-shift algorithm. That this function takes time at least quadratic in n_samples. For large datasets, it's wise to set that parameter to a small value. Parameters ---------- X : array-like, shape=[n_samples, n_features] Input points. quantile : float, default 0.3 should be between [0, 1] 0.5 means that the median of all pairwise distances is used. n_samples : int, optional The number of samples to use. If not given, all samples are used. random_state : int or RandomState Pseudo-random number generator state used for random sampling. Returns ------- bandwidth : float The bandwidth parameter. """ random_state = check_random_state(random_state) if n_samples is not None: idx = random_state.permutation(X.shape[0])[:n_samples] X = X[idx] nbrs = NearestNeighbors(n_neighbors=int(X.shape[0] * quantile)) nbrs.fit(X) bandwidth = 0. for batch in gen_batches(len(X), 500): d, _ = nbrs.kneighbors(X[batch, :], return_distance=True) bandwidth += np.max(d, axis=1).sum() return bandwidth / X.shape[0] # separate function for each seed's iterative loop def _mean_shift_single_seed(my_mean, X, nbrs, max_iter): # For each seed, climb gradient until convergence or max_iter bandwidth = nbrs.get_params()['radius'] stop_thresh = 1e-3 * bandwidth # when mean has converged completed_iterations = 0 while True: # Find mean of points within bandwidth i_nbrs = nbrs.radius_neighbors([my_mean], bandwidth, return_distance=False)[0] points_within = X[i_nbrs] if len(points_within) == 0: break # Depending on seeding strategy this condition may occur my_old_mean = my_mean # save the old mean my_mean = np.mean(points_within, axis=0) # If converged or at max_iter, adds the cluster if (extmath.norm(my_mean - my_old_mean) < stop_thresh or completed_iterations == max_iter): return tuple(my_mean), len(points_within) completed_iterations += 1 def mean_shift(X, bandwidth=None, seeds=None, bin_seeding=False, min_bin_freq=1, cluster_all=True, max_iter=300, max_iterations=None, n_jobs=1): """Perform mean shift clustering of data using a flat kernel. Read more in the :ref:`User Guide <mean_shift>`. Parameters ---------- X : array-like, shape=[n_samples, n_features] Input data. bandwidth : float, optional Kernel bandwidth. If bandwidth is not given, it is determined using a heuristic based on the median of all pairwise distances. This will take quadratic time in the number of samples. The sklearn.cluster.estimate_bandwidth function can be used to do this more efficiently. seeds : array-like, shape=[n_seeds, n_features] or None Point used as initial kernel locations. If None and bin_seeding=False, each data point is used as a seed. If None and bin_seeding=True, see bin_seeding. bin_seeding : boolean, default=False If true, initial kernel locations are not locations of all points, but rather the location of the discretized version of points, where points are binned onto a grid whose coarseness corresponds to the bandwidth. Setting this option to True will speed up the algorithm because fewer seeds will be initialized. Ignored if seeds argument is not None. min_bin_freq : int, default=1 To speed up the algorithm, accept only those bins with at least min_bin_freq points as seeds. cluster_all : boolean, default True If true, then all points are clustered, even those orphans that are not within any kernel. Orphans are assigned to the nearest kernel. If false, then orphans are given cluster label -1. max_iter : int, default 300 Maximum number of iterations, per seed point before the clustering operation terminates (for that seed point), if has not converged yet. n_jobs : int The number of jobs to use for the computation. This works by computing each of the n_init runs in parallel. If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used. Returns ------- cluster_centers : array, shape=[n_clusters, n_features] Coordinates of cluster centers. labels : array, shape=[n_samples] Cluster labels for each point. Notes ----- See examples/cluster/plot_meanshift.py for an example. """ # FIXME To be removed in 0.18 if max_iterations is not None: warnings.warn("The `max_iterations` parameter has been renamed to " "`max_iter` from version 0.16. The `max_iterations` " "parameter will be removed in 0.18", DeprecationWarning) max_iter = max_iterations if bandwidth is None: bandwidth = estimate_bandwidth(X) elif bandwidth <= 0: raise ValueError("bandwidth needs to be greater than zero or None,\ got %f" % bandwidth) if seeds is None: if bin_seeding: seeds = get_bin_seeds(X, bandwidth, min_bin_freq) else: seeds = X n_samples, n_features = X.shape center_intensity_dict = {} nbrs = NearestNeighbors(radius=bandwidth).fit(X) # execute iterations on all seeds in parallel all_res = Parallel(n_jobs=n_jobs)( delayed(_mean_shift_single_seed) (seed, X, nbrs, max_iter) for seed in seeds) # copy results in a dictionary for i in range(len(seeds)): if all_res[i] is not None: center_intensity_dict[all_res[i][0]] = all_res[i][1] if not center_intensity_dict: # nothing near seeds raise ValueError("No point was within bandwidth=%f of any seed." " Try a different seeding strategy \ or increase the bandwidth." % bandwidth) # POST PROCESSING: remove near duplicate points # If the distance between two kernels is less than the bandwidth, # then we have to remove one because it is a duplicate. Remove the # one with fewer points. sorted_by_intensity = sorted(center_intensity_dict.items(), key=lambda tup: tup[1], reverse=True) sorted_centers = np.array([tup[0] for tup in sorted_by_intensity]) unique = np.ones(len(sorted_centers), dtype=np.bool) nbrs = NearestNeighbors(radius=bandwidth).fit(sorted_centers) for i, center in enumerate(sorted_centers): if unique[i]: neighbor_idxs = nbrs.radius_neighbors([center], return_distance=False)[0] unique[neighbor_idxs] = 0 unique[i] = 1 # leave the current point as unique cluster_centers = sorted_centers[unique] # ASSIGN LABELS: a point belongs to the cluster that it is closest to nbrs = NearestNeighbors(n_neighbors=1).fit(cluster_centers) labels = np.zeros(n_samples, dtype=np.int) distances, idxs = nbrs.kneighbors(X) if cluster_all: labels = idxs.flatten() else: labels.fill(-1) bool_selector = distances.flatten() <= bandwidth labels[bool_selector] = idxs.flatten()[bool_selector] return cluster_centers, labels def get_bin_seeds(X, bin_size, min_bin_freq=1): """Finds seeds for mean_shift. Finds seeds by first binning data onto a grid whose lines are spaced bin_size apart, and then choosing those bins with at least min_bin_freq points. Parameters ---------- X : array-like, shape=[n_samples, n_features] Input points, the same points that will be used in mean_shift. bin_size : float Controls the coarseness of the binning. Smaller values lead to more seeding (which is computationally more expensive). If you're not sure how to set this, set it to the value of the bandwidth used in clustering.mean_shift. min_bin_freq : integer, optional Only bins with at least min_bin_freq will be selected as seeds. Raising this value decreases the number of seeds found, which makes mean_shift computationally cheaper. Returns ------- bin_seeds : array-like, shape=[n_samples, n_features] Points used as initial kernel positions in clustering.mean_shift. """ # Bin points bin_sizes = defaultdict(int) for point in X: binned_point = np.round(point / bin_size) bin_sizes[tuple(binned_point)] += 1 # Select only those bins as seeds which have enough members bin_seeds = np.array([point for point, freq in six.iteritems(bin_sizes) if freq >= min_bin_freq], dtype=np.float32) if len(bin_seeds) == len(X): warnings.warn("Binning data failed with provided bin_size=%f," " using data points as seeds." % bin_size) return X bin_seeds = bin_seeds * bin_size return bin_seeds class MeanShift(BaseEstimator, ClusterMixin): """Mean shift clustering using a flat kernel. Mean shift clustering aims to discover "blobs" in a smooth density of samples. It is a centroid-based algorithm, which works by updating candidates for centroids to be the mean of the points within a given region. These candidates are then filtered in a post-processing stage to eliminate near-duplicates to form the final set of centroids. Seeding is performed using a binning technique for scalability. Read more in the :ref:`User Guide <mean_shift>`. Parameters ---------- bandwidth : float, optional Bandwidth used in the RBF kernel. If not given, the bandwidth is estimated using sklearn.cluster.estimate_bandwidth; see the documentation for that function for hints on scalability (see also the Notes, below). seeds : array, shape=[n_samples, n_features], optional Seeds used to initialize kernels. If not set, the seeds are calculated by clustering.get_bin_seeds with bandwidth as the grid size and default values for other parameters. bin_seeding : boolean, optional If true, initial kernel locations are not locations of all points, but rather the location of the discretized version of points, where points are binned onto a grid whose coarseness corresponds to the bandwidth. Setting this option to True will speed up the algorithm because fewer seeds will be initialized. default value: False Ignored if seeds argument is not None. min_bin_freq : int, optional To speed up the algorithm, accept only those bins with at least min_bin_freq points as seeds. If not defined, set to 1. cluster_all : boolean, default True If true, then all points are clustered, even those orphans that are not within any kernel. Orphans are assigned to the nearest kernel. If false, then orphans are given cluster label -1. n_jobs : int The number of jobs to use for the computation. This works by computing each of the n_init runs in parallel. If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used. Attributes ---------- cluster_centers_ : array, [n_clusters, n_features] Coordinates of cluster centers. labels_ : Labels of each point. Notes ----- Scalability: Because this implementation uses a flat kernel and a Ball Tree to look up members of each kernel, the complexity will is to O(T*n*log(n)) in lower dimensions, with n the number of samples and T the number of points. In higher dimensions the complexity will tend towards O(T*n^2). Scalability can be boosted by using fewer seeds, for example by using a higher value of min_bin_freq in the get_bin_seeds function. Note that the estimate_bandwidth function is much less scalable than the mean shift algorithm and will be the bottleneck if it is used. References ---------- Dorin Comaniciu and Peter Meer, "Mean Shift: A robust approach toward feature space analysis". IEEE Transactions on Pattern Analysis and Machine Intelligence. 2002. pp. 603-619. """ def __init__(self, bandwidth=None, seeds=None, bin_seeding=False, min_bin_freq=1, cluster_all=True, n_jobs=1): self.bandwidth = bandwidth self.seeds = seeds self.bin_seeding = bin_seeding self.cluster_all = cluster_all self.min_bin_freq = min_bin_freq self.n_jobs = n_jobs def fit(self, X, y=None): """Perform clustering. Parameters ----------- X : array-like, shape=[n_samples, n_features] Samples to cluster. """ X = check_array(X) self.cluster_centers_, self.labels_ = \ mean_shift(X, bandwidth=self.bandwidth, seeds=self.seeds, min_bin_freq=self.min_bin_freq, bin_seeding=self.bin_seeding, cluster_all=self.cluster_all, n_jobs=self.n_jobs) return self def predict(self, X): """Predict the closest cluster each sample in X belongs to. Parameters ---------- X : {array-like, sparse matrix}, shape=[n_samples, n_features] New data to predict. Returns ------- labels : array, shape [n_samples,] Index of the cluster each sample belongs to. """ check_is_fitted(self, "cluster_centers_") return pairwise_distances_argmin(X, self.cluster_centers_)
bsd-3-clause
Obus/scikit-learn
benchmarks/bench_plot_approximate_neighbors.py
85
6377
""" Benchmark for approximate nearest neighbor search using locality sensitive hashing forest. There are two types of benchmarks. First, accuracy of LSHForest queries are measured for various hyper-parameters and index sizes. Second, speed up of LSHForest queries compared to brute force method in exact nearest neighbors is measures for the aforementioned settings. In general, speed up is increasing as the index size grows. """ from __future__ import division import numpy as np from tempfile import gettempdir from time import time from sklearn.neighbors import NearestNeighbors from sklearn.neighbors.approximate import LSHForest from sklearn.datasets import make_blobs from sklearn.externals.joblib import Memory m = Memory(cachedir=gettempdir()) @m.cache() def make_data(n_samples, n_features, n_queries, random_state=0): """Create index and query data.""" print('Generating random blob-ish data') X, _ = make_blobs(n_samples=n_samples + n_queries, n_features=n_features, centers=100, shuffle=True, random_state=random_state) # Keep the last samples as held out query vectors: note since we used # shuffle=True we have ensured that index and query vectors are # samples from the same distribution (a mixture of 100 gaussians in this # case) return X[:n_samples], X[n_samples:] def calc_exact_neighbors(X, queries, n_queries, n_neighbors): """Measures average times for exact neighbor queries.""" print ('Building NearestNeighbors for %d samples in %d dimensions' % (X.shape[0], X.shape[1])) nbrs = NearestNeighbors(algorithm='brute', metric='cosine').fit(X) average_time = 0 t0 = time() neighbors = nbrs.kneighbors(queries, n_neighbors=n_neighbors, return_distance=False) average_time = (time() - t0) / n_queries return neighbors, average_time def calc_accuracy(X, queries, n_queries, n_neighbors, exact_neighbors, average_time_exact, **lshf_params): """Calculates accuracy and the speed up of LSHForest.""" print('Building LSHForest for %d samples in %d dimensions' % (X.shape[0], X.shape[1])) lshf = LSHForest(**lshf_params) t0 = time() lshf.fit(X) lshf_build_time = time() - t0 print('Done in %0.3fs' % lshf_build_time) accuracy = 0 t0 = time() approx_neighbors = lshf.kneighbors(queries, n_neighbors=n_neighbors, return_distance=False) average_time_approx = (time() - t0) / n_queries for i in range(len(queries)): accuracy += np.in1d(approx_neighbors[i], exact_neighbors[i]).mean() accuracy /= n_queries speed_up = average_time_exact / average_time_approx print('Average time for lshf neighbor queries: %0.3fs' % average_time_approx) print ('Average time for exact neighbor queries: %0.3fs' % average_time_exact) print ('Average Accuracy : %0.2f' % accuracy) print ('Speed up: %0.1fx' % speed_up) return speed_up, accuracy if __name__ == '__main__': import matplotlib.pyplot as plt # Initialize index sizes n_samples = [int(1e3), int(1e4), int(1e5), int(1e6)] n_features = int(1e2) n_queries = 100 n_neighbors = 10 X_index, X_query = make_data(np.max(n_samples), n_features, n_queries, random_state=0) params_list = [{'n_estimators': 3, 'n_candidates': 50}, {'n_estimators': 5, 'n_candidates': 70}, {'n_estimators': 10, 'n_candidates': 100}] accuracies = np.zeros((len(n_samples), len(params_list)), dtype=float) speed_ups = np.zeros((len(n_samples), len(params_list)), dtype=float) for i, sample_size in enumerate(n_samples): print ('==========================================================') print ('Sample size: %i' % sample_size) print ('------------------------') exact_neighbors, average_time_exact = calc_exact_neighbors( X_index[:sample_size], X_query, n_queries, n_neighbors) for j, params in enumerate(params_list): print ('LSHF parameters: n_estimators = %i, n_candidates = %i' % (params['n_estimators'], params['n_candidates'])) speed_ups[i, j], accuracies[i, j] = calc_accuracy( X_index[:sample_size], X_query, n_queries, n_neighbors, exact_neighbors, average_time_exact, random_state=0, **params) print ('') print ('==========================================================') # Set labels for LSHForest parameters colors = ['c', 'm', 'y'] p1 = plt.Rectangle((0, 0), 0.1, 0.1, fc=colors[0]) p2 = plt.Rectangle((0, 0), 0.1, 0.1, fc=colors[1]) p3 = plt.Rectangle((0, 0), 0.1, 0.1, fc=colors[2]) labels = ['n_estimators=' + str(params_list[0]['n_estimators']) + ', n_candidates=' + str(params_list[0]['n_candidates']), 'n_estimators=' + str(params_list[1]['n_estimators']) + ', n_candidates=' + str(params_list[1]['n_candidates']), 'n_estimators=' + str(params_list[2]['n_estimators']) + ', n_candidates=' + str(params_list[2]['n_candidates'])] # Plot precision plt.figure() plt.legend((p1, p2, p3), (labels[0], labels[1], labels[2]), loc='upper left') for i in range(len(params_list)): plt.scatter(n_samples, accuracies[:, i], c=colors[i]) plt.plot(n_samples, accuracies[:, i], c=colors[i]) plt.ylim([0, 1.3]) plt.xlim(np.min(n_samples), np.max(n_samples)) plt.semilogx() plt.ylabel("Precision@10") plt.xlabel("Index size") plt.grid(which='both') plt.title("Precision of first 10 neighbors with index size") # Plot speed up plt.figure() plt.legend((p1, p2, p3), (labels[0], labels[1], labels[2]), loc='upper left') for i in range(len(params_list)): plt.scatter(n_samples, speed_ups[:, i], c=colors[i]) plt.plot(n_samples, speed_ups[:, i], c=colors[i]) plt.ylim(0, np.max(speed_ups)) plt.xlim(np.min(n_samples), np.max(n_samples)) plt.semilogx() plt.ylabel("Speed up") plt.xlabel("Index size") plt.grid(which='both') plt.title("Relationship between Speed up and index size") plt.show()
bsd-3-clause
nightjean/Deep-Learning
tensorflow/examples/learn/text_classification_character_rnn.py
61
3350
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """This is an example of using recurrent neural networks over characters for DBpedia dataset to predict class from description of an entity. This model is similar to one described in this paper: "Character-level Convolutional Networks for Text Classification" http://arxiv.org/abs/1509.01626 and is somewhat alternative to the Lua code from here: https://github.com/zhangxiangxiao/Crepe """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import sys import numpy as np import pandas from sklearn import metrics import tensorflow as tf learn = tf.contrib.learn FLAGS = None MAX_DOCUMENT_LENGTH = 100 HIDDEN_SIZE = 20 def char_rnn_model(features, target): """Character level recurrent neural network model to predict classes.""" target = tf.one_hot(target, 15, 1, 0) byte_list = tf.one_hot(features, 256, 1, 0) byte_list = tf.unstack(byte_list, axis=1) cell = tf.contrib.rnn.GRUCell(HIDDEN_SIZE) _, encoding = tf.contrib.rnn.static_rnn(cell, byte_list, dtype=tf.float32) logits = tf.contrib.layers.fully_connected(encoding, 15, activation_fn=None) loss = tf.contrib.losses.softmax_cross_entropy(logits, target) train_op = tf.contrib.layers.optimize_loss( loss, tf.contrib.framework.get_global_step(), optimizer='Adam', learning_rate=0.01) return ({ 'class': tf.argmax(logits, 1), 'prob': tf.nn.softmax(logits) }, loss, train_op) def main(unused_argv): # Prepare training and testing data dbpedia = learn.datasets.load_dataset( 'dbpedia', test_with_fake_data=FLAGS.test_with_fake_data) x_train = pandas.DataFrame(dbpedia.train.data)[1] y_train = pandas.Series(dbpedia.train.target) x_test = pandas.DataFrame(dbpedia.test.data)[1] y_test = pandas.Series(dbpedia.test.target) # Process vocabulary char_processor = learn.preprocessing.ByteProcessor(MAX_DOCUMENT_LENGTH) x_train = np.array(list(char_processor.fit_transform(x_train))) x_test = np.array(list(char_processor.transform(x_test))) # Build model classifier = learn.Estimator(model_fn=char_rnn_model) # Train and predict classifier.fit(x_train, y_train, steps=100) y_predicted = [ p['class'] for p in classifier.predict( x_test, as_iterable=True) ] score = metrics.accuracy_score(y_test, y_predicted) print('Accuracy: {0:f}'.format(score)) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument( '--test_with_fake_data', default=False, help='Test the example code with fake data.', action='store_true') FLAGS, unparsed = parser.parse_known_args() tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
apache-2.0
cswiercz/sympy
sympy/physics/quantum/state.py
58
29186
"""Dirac notation for states.""" from __future__ import print_function, division from sympy import (cacheit, conjugate, Expr, Function, integrate, oo, sqrt, Tuple) from sympy.core.compatibility import u, range from sympy.printing.pretty.stringpict import stringPict from sympy.physics.quantum.qexpr import QExpr, dispatch_method __all__ = [ 'KetBase', 'BraBase', 'StateBase', 'State', 'Ket', 'Bra', 'TimeDepState', 'TimeDepBra', 'TimeDepKet', 'Wavefunction' ] #----------------------------------------------------------------------------- # States, bras and kets. #----------------------------------------------------------------------------- # ASCII brackets _lbracket = "<" _rbracket = ">" _straight_bracket = "|" # Unicode brackets # MATHEMATICAL ANGLE BRACKETS _lbracket_ucode = u("\N{MATHEMATICAL LEFT ANGLE BRACKET}") _rbracket_ucode = u("\N{MATHEMATICAL RIGHT ANGLE BRACKET}") # LIGHT VERTICAL BAR _straight_bracket_ucode = u("\N{LIGHT VERTICAL BAR}") # Other options for unicode printing of <, > and | for Dirac notation. # LEFT-POINTING ANGLE BRACKET # _lbracket = u"\u2329" # _rbracket = u"\u232A" # LEFT ANGLE BRACKET # _lbracket = u"\u3008" # _rbracket = u"\u3009" # VERTICAL LINE # _straight_bracket = u"\u007C" class StateBase(QExpr): """Abstract base class for general abstract states in quantum mechanics. All other state classes defined will need to inherit from this class. It carries the basic structure for all other states such as dual, _eval_adjoint and label. This is an abstract base class and you should not instantiate it directly, instead use State. """ @classmethod def _operators_to_state(self, ops, **options): """ Returns the eigenstate instance for the passed operators. This method should be overridden in subclasses. It will handle being passed either an Operator instance or set of Operator instances. It should return the corresponding state INSTANCE or simply raise a NotImplementedError. See cartesian.py for an example. """ raise NotImplementedError("Cannot map operators to states in this class. Method not implemented!") def _state_to_operators(self, op_classes, **options): """ Returns the operators which this state instance is an eigenstate of. This method should be overridden in subclasses. It will be called on state instances and be passed the operator classes that we wish to make into instances. The state instance will then transform the classes appropriately, or raise a NotImplementedError if it cannot return operator instances. See cartesian.py for examples, """ raise NotImplementedError( "Cannot map this state to operators. Method not implemented!") @property def operators(self): """Return the operator(s) that this state is an eigenstate of""" from .operatorset import state_to_operators # import internally to avoid circular import errors return state_to_operators(self) def _enumerate_state(self, num_states, **options): raise NotImplementedError("Cannot enumerate this state!") def _represent_default_basis(self, **options): return self._represent(basis=self.operators) #------------------------------------------------------------------------- # Dagger/dual #------------------------------------------------------------------------- @property def dual(self): """Return the dual state of this one.""" return self.dual_class()._new_rawargs(self.hilbert_space, *self.args) @classmethod def dual_class(self): """Return the class used to construt the dual.""" raise NotImplementedError( 'dual_class must be implemented in a subclass' ) def _eval_adjoint(self): """Compute the dagger of this state using the dual.""" return self.dual #------------------------------------------------------------------------- # Printing #------------------------------------------------------------------------- def _pretty_brackets(self, height, use_unicode=True): # Return pretty printed brackets for the state # Ideally, this could be done by pform.parens but it does not support the angled < and > # Setup for unicode vs ascii if use_unicode: lbracket, rbracket = self.lbracket_ucode, self.rbracket_ucode slash, bslash, vert = u('\N{BOX DRAWINGS LIGHT DIAGONAL UPPER RIGHT TO LOWER LEFT}'), \ u('\N{BOX DRAWINGS LIGHT DIAGONAL UPPER LEFT TO LOWER RIGHT}'), \ u('\N{BOX DRAWINGS LIGHT VERTICAL}') else: lbracket, rbracket = self.lbracket, self.rbracket slash, bslash, vert = '/', '\\', '|' # If height is 1, just return brackets if height == 1: return stringPict(lbracket), stringPict(rbracket) # Make height even height += (height % 2) brackets = [] for bracket in lbracket, rbracket: # Create left bracket if bracket in set([_lbracket, _lbracket_ucode]): bracket_args = [ ' ' * (height//2 - i - 1) + slash for i in range(height // 2)] bracket_args.extend( [ ' ' * i + bslash for i in range(height // 2)]) # Create right bracket elif bracket in set([_rbracket, _rbracket_ucode]): bracket_args = [ ' ' * i + bslash for i in range(height // 2)] bracket_args.extend([ ' ' * ( height//2 - i - 1) + slash for i in range(height // 2)]) # Create straight bracket elif bracket in set([_straight_bracket, _straight_bracket_ucode]): bracket_args = [vert for i in range(height)] else: raise ValueError(bracket) brackets.append( stringPict('\n'.join(bracket_args), baseline=height//2)) return brackets def _sympystr(self, printer, *args): contents = self._print_contents(printer, *args) return '%s%s%s' % (self.lbracket, contents, self.rbracket) def _pretty(self, printer, *args): from sympy.printing.pretty.stringpict import prettyForm # Get brackets pform = self._print_contents_pretty(printer, *args) lbracket, rbracket = self._pretty_brackets( pform.height(), printer._use_unicode) # Put together state pform = prettyForm(*pform.left(lbracket)) pform = prettyForm(*pform.right(rbracket)) return pform def _latex(self, printer, *args): contents = self._print_contents_latex(printer, *args) # The extra {} brackets are needed to get matplotlib's latex # rendered to render this properly. return '{%s%s%s}' % (self.lbracket_latex, contents, self.rbracket_latex) class KetBase(StateBase): """Base class for Kets. This class defines the dual property and the brackets for printing. This is an abstract base class and you should not instantiate it directly, instead use Ket. """ lbracket = _straight_bracket rbracket = _rbracket lbracket_ucode = _straight_bracket_ucode rbracket_ucode = _rbracket_ucode lbracket_latex = r'\left|' rbracket_latex = r'\right\rangle ' @classmethod def default_args(self): return ("psi",) @classmethod def dual_class(self): return BraBase def __mul__(self, other): """KetBase*other""" from sympy.physics.quantum.operator import OuterProduct if isinstance(other, BraBase): return OuterProduct(self, other) else: return Expr.__mul__(self, other) def __rmul__(self, other): """other*KetBase""" from sympy.physics.quantum.innerproduct import InnerProduct if isinstance(other, BraBase): return InnerProduct(other, self) else: return Expr.__rmul__(self, other) #------------------------------------------------------------------------- # _eval_* methods #------------------------------------------------------------------------- def _eval_innerproduct(self, bra, **hints): """Evaluate the inner product betweeen this ket and a bra. This is called to compute <bra|ket>, where the ket is ``self``. This method will dispatch to sub-methods having the format:: ``def _eval_innerproduct_BraClass(self, **hints):`` Subclasses should define these methods (one for each BraClass) to teach the ket how to take inner products with bras. """ return dispatch_method(self, '_eval_innerproduct', bra, **hints) def _apply_operator(self, op, **options): """Apply an Operator to this Ket. This method will dispatch to methods having the format:: ``def _apply_operator_OperatorName(op, **options):`` Subclasses should define these methods (one for each OperatorName) to teach the Ket how operators act on it. Parameters ========== op : Operator The Operator that is acting on the Ket. options : dict A dict of key/value pairs that control how the operator is applied to the Ket. """ return dispatch_method(self, '_apply_operator', op, **options) class BraBase(StateBase): """Base class for Bras. This class defines the dual property and the brackets for printing. This is an abstract base class and you should not instantiate it directly, instead use Bra. """ lbracket = _lbracket rbracket = _straight_bracket lbracket_ucode = _lbracket_ucode rbracket_ucode = _straight_bracket_ucode lbracket_latex = r'\left\langle ' rbracket_latex = r'\right|' @classmethod def _operators_to_state(self, ops, **options): state = self.dual_class().operators_to_state(ops, **options) return state.dual def _state_to_operators(self, op_classes, **options): return self.dual._state_to_operators(op_classes, **options) def _enumerate_state(self, num_states, **options): dual_states = self.dual._enumerate_state(num_states, **options) return [x.dual for x in dual_states] @classmethod def default_args(self): return self.dual_class().default_args() @classmethod def dual_class(self): return KetBase def __mul__(self, other): """BraBase*other""" from sympy.physics.quantum.innerproduct import InnerProduct if isinstance(other, KetBase): return InnerProduct(self, other) else: return Expr.__mul__(self, other) def __rmul__(self, other): """other*BraBase""" from sympy.physics.quantum.operator import OuterProduct if isinstance(other, KetBase): return OuterProduct(other, self) else: return Expr.__rmul__(self, other) def _represent(self, **options): """A default represent that uses the Ket's version.""" from sympy.physics.quantum.dagger import Dagger return Dagger(self.dual._represent(**options)) class State(StateBase): """General abstract quantum state used as a base class for Ket and Bra.""" pass class Ket(State, KetBase): """A general time-independent Ket in quantum mechanics. Inherits from State and KetBase. This class should be used as the base class for all physical, time-independent Kets in a system. This class and its subclasses will be the main classes that users will use for expressing Kets in Dirac notation [1]_. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the ket. This will usually be its symbol or its quantum numbers. For time-dependent state, this will include the time. Examples ======== Create a simple Ket and looking at its properties:: >>> from sympy.physics.quantum import Ket, Bra >>> from sympy import symbols, I >>> k = Ket('psi') >>> k |psi> >>> k.hilbert_space H >>> k.is_commutative False >>> k.label (psi,) Ket's know about their associated bra:: >>> k.dual <psi| >>> k.dual_class() <class 'sympy.physics.quantum.state.Bra'> Take a linear combination of two kets:: >>> k0 = Ket(0) >>> k1 = Ket(1) >>> 2*I*k0 - 4*k1 2*I*|0> - 4*|1> Compound labels are passed as tuples:: >>> n, m = symbols('n,m') >>> k = Ket(n,m) >>> k |nm> References ========== .. [1] http://en.wikipedia.org/wiki/Bra-ket_notation """ @classmethod def dual_class(self): return Bra class Bra(State, BraBase): """A general time-independent Bra in quantum mechanics. Inherits from State and BraBase. A Bra is the dual of a Ket [1]_. This class and its subclasses will be the main classes that users will use for expressing Bras in Dirac notation. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the ket. This will usually be its symbol or its quantum numbers. For time-dependent state, this will include the time. Examples ======== Create a simple Bra and look at its properties:: >>> from sympy.physics.quantum import Ket, Bra >>> from sympy import symbols, I >>> b = Bra('psi') >>> b <psi| >>> b.hilbert_space H >>> b.is_commutative False Bra's know about their dual Ket's:: >>> b.dual |psi> >>> b.dual_class() <class 'sympy.physics.quantum.state.Ket'> Like Kets, Bras can have compound labels and be manipulated in a similar manner:: >>> n, m = symbols('n,m') >>> b = Bra(n,m) - I*Bra(m,n) >>> b -I*<mn| + <nm| Symbols in a Bra can be substituted using ``.subs``:: >>> b.subs(n,m) <mm| - I*<mm| References ========== .. [1] http://en.wikipedia.org/wiki/Bra-ket_notation """ @classmethod def dual_class(self): return Ket #----------------------------------------------------------------------------- # Time dependent states, bras and kets. #----------------------------------------------------------------------------- class TimeDepState(StateBase): """Base class for a general time-dependent quantum state. This class is used as a base class for any time-dependent state. The main difference between this class and the time-independent state is that this class takes a second argument that is the time in addition to the usual label argument. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the ket. This will usually be its symbol or its quantum numbers. For time-dependent state, this will include the time as the final argument. """ #------------------------------------------------------------------------- # Initialization #------------------------------------------------------------------------- @classmethod def default_args(self): return ("psi", "t") #------------------------------------------------------------------------- # Properties #------------------------------------------------------------------------- @property def label(self): """The label of the state.""" return self.args[:-1] @property def time(self): """The time of the state.""" return self.args[-1] #------------------------------------------------------------------------- # Printing #------------------------------------------------------------------------- def _print_time(self, printer, *args): return printer._print(self.time, *args) _print_time_repr = _print_time _print_time_latex = _print_time def _print_time_pretty(self, printer, *args): pform = printer._print(self.time, *args) return pform def _print_contents(self, printer, *args): label = self._print_label(printer, *args) time = self._print_time(printer, *args) return '%s;%s' % (label, time) def _print_label_repr(self, printer, *args): label = self._print_sequence(self.label, ',', printer, *args) time = self._print_time_repr(printer, *args) return '%s,%s' % (label, time) def _print_contents_pretty(self, printer, *args): label = self._print_label_pretty(printer, *args) time = self._print_time_pretty(printer, *args) return printer._print_seq((label, time), delimiter=';') def _print_contents_latex(self, printer, *args): label = self._print_sequence( self.label, self._label_separator, printer, *args) time = self._print_time_latex(printer, *args) return '%s;%s' % (label, time) class TimeDepKet(TimeDepState, KetBase): """General time-dependent Ket in quantum mechanics. This inherits from ``TimeDepState`` and ``KetBase`` and is the main class that should be used for Kets that vary with time. Its dual is a ``TimeDepBra``. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the ket. This will usually be its symbol or its quantum numbers. For time-dependent state, this will include the time as the final argument. Examples ======== Create a TimeDepKet and look at its attributes:: >>> from sympy.physics.quantum import TimeDepKet >>> k = TimeDepKet('psi', 't') >>> k |psi;t> >>> k.time t >>> k.label (psi,) >>> k.hilbert_space H TimeDepKets know about their dual bra:: >>> k.dual <psi;t| >>> k.dual_class() <class 'sympy.physics.quantum.state.TimeDepBra'> """ @classmethod def dual_class(self): return TimeDepBra class TimeDepBra(TimeDepState, BraBase): """General time-dependent Bra in quantum mechanics. This inherits from TimeDepState and BraBase and is the main class that should be used for Bras that vary with time. Its dual is a TimeDepBra. Parameters ========== args : tuple The list of numbers or parameters that uniquely specify the ket. This will usually be its symbol or its quantum numbers. For time-dependent state, this will include the time as the final argument. Examples ======== >>> from sympy.physics.quantum import TimeDepBra >>> from sympy import symbols, I >>> b = TimeDepBra('psi', 't') >>> b <psi;t| >>> b.time t >>> b.label (psi,) >>> b.hilbert_space H >>> b.dual |psi;t> """ @classmethod def dual_class(self): return TimeDepKet class Wavefunction(Function): """Class for representations in continuous bases This class takes an expression and coordinates in its constructor. It can be used to easily calculate normalizations and probabilities. Parameters ========== expr : Expr The expression representing the functional form of the w.f. coords : Symbol or tuple The coordinates to be integrated over, and their bounds Examples ======== Particle in a box, specifying bounds in the more primitive way of using Piecewise: >>> from sympy import Symbol, Piecewise, pi, N >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x = Symbol('x', real=True) >>> n = 1 >>> L = 1 >>> g = Piecewise((0, x < 0), (0, x > L), (sqrt(2//L)*sin(n*pi*x/L), True)) >>> f = Wavefunction(g, x) >>> f.norm 1 >>> f.is_normalized True >>> p = f.prob() >>> p(0) 0 >>> p(L) 0 >>> p(0.5) 2 >>> p(0.85*L) 2*sin(0.85*pi)**2 >>> N(p(0.85*L)) 0.412214747707527 Additionally, you can specify the bounds of the function and the indices in a more compact way: >>> from sympy import symbols, pi, diff >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x, L = symbols('x,L', positive=True) >>> n = symbols('n', integer=True, positive=True) >>> g = sqrt(2/L)*sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.norm 1 >>> f(L+1) 0 >>> f(L-1) sqrt(2)*sin(pi*n*(L - 1)/L)/sqrt(L) >>> f(-1) 0 >>> f(0.85) sqrt(2)*sin(0.85*pi*n/L)/sqrt(L) >>> f(0.85, n=1, L=1) sqrt(2)*sin(0.85*pi) >>> f.is_commutative False All arguments are automatically sympified, so you can define the variables as strings rather than symbols: >>> expr = x**2 >>> f = Wavefunction(expr, 'x') >>> type(f.variables[0]) <class 'sympy.core.symbol.Symbol'> Derivatives of Wavefunctions will return Wavefunctions: >>> diff(f, x) Wavefunction(2*x, x) """ #Any passed tuples for coordinates and their bounds need to be #converted to Tuples before Function's constructor is called, to #avoid errors from calling is_Float in the constructor def __new__(cls, *args, **options): new_args = [None for i in args] ct = 0 for arg in args: if isinstance(arg, tuple): new_args[ct] = Tuple(*arg) else: new_args[ct] = arg ct += 1 return super(Function, cls).__new__(cls, *new_args, **options) def __call__(self, *args, **options): var = self.variables if len(args) != len(var): raise NotImplementedError( "Incorrect number of arguments to function!") ct = 0 #If the passed value is outside the specified bounds, return 0 for v in var: lower, upper = self.limits[v] #Do the comparison to limits only if the passed symbol is actually #a symbol present in the limits; #Had problems with a comparison of x > L if isinstance(args[ct], Expr) and \ not (lower in args[ct].free_symbols or upper in args[ct].free_symbols): continue if (args[ct] < lower) == True or (args[ct] > upper) == True: return 0 ct += 1 expr = self.expr #Allows user to make a call like f(2, 4, m=1, n=1) for symbol in list(expr.free_symbols): if str(symbol) in options.keys(): val = options[str(symbol)] expr = expr.subs(symbol, val) return expr.subs(zip(var, args)) def _eval_derivative(self, symbol): expr = self.expr deriv = expr._eval_derivative(symbol) return Wavefunction(deriv, *self.args[1:]) def _eval_conjugate(self): return Wavefunction(conjugate(self.expr), *self.args[1:]) def _eval_transpose(self): return self @property def free_symbols(self): return self.expr.free_symbols @property def is_commutative(self): """ Override Function's is_commutative so that order is preserved in represented expressions """ return False @classmethod def eval(self, *args): return None @property def variables(self): """ Return the coordinates which the wavefunction depends on Examples ======== >>> from sympy.physics.quantum.state import Wavefunction >>> from sympy import symbols >>> x,y = symbols('x,y') >>> f = Wavefunction(x*y, x, y) >>> f.variables (x, y) >>> g = Wavefunction(x*y, x) >>> g.variables (x,) """ var = [g[0] if isinstance(g, Tuple) else g for g in self._args[1:]] return tuple(var) @property def limits(self): """ Return the limits of the coordinates which the w.f. depends on If no limits are specified, defaults to ``(-oo, oo)``. Examples ======== >>> from sympy.physics.quantum.state import Wavefunction >>> from sympy import symbols >>> x, y = symbols('x, y') >>> f = Wavefunction(x**2, (x, 0, 1)) >>> f.limits {x: (0, 1)} >>> f = Wavefunction(x**2, x) >>> f.limits {x: (-oo, oo)} >>> f = Wavefunction(x**2 + y**2, x, (y, -1, 2)) >>> f.limits {x: (-oo, oo), y: (-1, 2)} """ limits = [(g[1], g[2]) if isinstance(g, Tuple) else (-oo, oo) for g in self._args[1:]] return dict(zip(self.variables, tuple(limits))) @property def expr(self): """ Return the expression which is the functional form of the Wavefunction Examples ======== >>> from sympy.physics.quantum.state import Wavefunction >>> from sympy import symbols >>> x, y = symbols('x, y') >>> f = Wavefunction(x**2, x) >>> f.expr x**2 """ return self._args[0] @property def is_normalized(self): """ Returns true if the Wavefunction is properly normalized Examples ======== >>> from sympy import symbols, pi >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x, L = symbols('x,L', positive=True) >>> n = symbols('n', integer=True, positive=True) >>> g = sqrt(2/L)*sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.is_normalized True """ return (self.norm == 1.0) @property @cacheit def norm(self): """ Return the normalization of the specified functional form. This function integrates over the coordinates of the Wavefunction, with the bounds specified. Examples ======== >>> from sympy import symbols, pi >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x, L = symbols('x,L', positive=True) >>> n = symbols('n', integer=True, positive=True) >>> g = sqrt(2/L)*sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.norm 1 >>> g = sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.norm sqrt(2)*sqrt(L)/2 """ exp = self.expr*conjugate(self.expr) var = self.variables limits = self.limits for v in var: curr_limits = limits[v] exp = integrate(exp, (v, curr_limits[0], curr_limits[1])) return sqrt(exp) def normalize(self): """ Return a normalized version of the Wavefunction Examples ======== >>> from sympy import symbols, pi >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x = symbols('x', real=True) >>> L = symbols('L', positive=True) >>> n = symbols('n', integer=True, positive=True) >>> g = sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.normalize() Wavefunction(sqrt(2)*sin(pi*n*x/L)/sqrt(L), (x, 0, L)) """ const = self.norm if const == oo: raise NotImplementedError("The function is not normalizable!") else: return Wavefunction((const)**(-1)*self.expr, *self.args[1:]) def prob(self): """ Return the absolute magnitude of the w.f., `|\psi(x)|^2` Examples ======== >>> from sympy import symbols, pi >>> from sympy.functions import sqrt, sin >>> from sympy.physics.quantum.state import Wavefunction >>> x, L = symbols('x,L', real=True) >>> n = symbols('n', integer=True) >>> g = sin(n*pi*x/L) >>> f = Wavefunction(g, (x, 0, L)) >>> f.prob() Wavefunction(sin(pi*n*x/L)**2, x) """ return Wavefunction(self.expr*conjugate(self.expr), *self.variables)
bsd-3-clause
ryandougherty/mwa-capstone
MWA_Tools/build/matplotlib/doc/mpl_examples/pylab_examples/demo_bboximage.py
12
1805
import matplotlib.pyplot as plt import numpy as np from matplotlib.image import BboxImage from matplotlib.transforms import Bbox, TransformedBbox if __name__ == "__main__": fig = plt.figure(1) ax = plt.subplot(121) txt = ax.text(0.5, 0.5, "test", size=30, ha="center", color="w") kwargs = dict() bbox_image = BboxImage(txt.get_window_extent, norm = None, origin=None, clip_on=False, **kwargs ) a = np.arange(256).reshape(1,256)/256. bbox_image.set_data(a) ax.add_artist(bbox_image) ax = plt.subplot(122) a = np.linspace(0, 1, 256).reshape(1,-1) a = np.vstack((a,a)) maps = sorted(m for m in plt.cm.datad if not m.endswith("_r")) #nmaps = len(maps) + 1 #fig.subplots_adjust(top=0.99, bottom=0.01, left=0.2, right=0.99) ncol = 2 nrow = len(maps)//ncol + 1 xpad_fraction = 0.3 dx = 1./(ncol + xpad_fraction*(ncol-1)) ypad_fraction = 0.3 dy = 1./(nrow + ypad_fraction*(nrow-1)) for i,m in enumerate(maps): ix, iy = divmod(i, nrow) #plt.figimage(a, 10, i*10, cmap=plt.get_cmap(m), origin='lower') bbox0 = Bbox.from_bounds(ix*dx*(1+xpad_fraction), 1.-iy*dy*(1+ypad_fraction)-dy, dx, dy) bbox = TransformedBbox(bbox0, ax.transAxes) bbox_image = BboxImage(bbox, cmap = plt.get_cmap(m), norm = None, origin=None, **kwargs ) bbox_image.set_data(a) ax.add_artist(bbox_image) plt.draw() plt.show()
gpl-2.0
bikong2/scikit-learn
sklearn/tests/test_discriminant_analysis.py
19
11711
try: # Python 2 compat reload except NameError: # Regular Python 3+ import from importlib import reload import numpy as np from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_almost_equal from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_raises from sklearn.utils.testing import assert_raise_message from sklearn.utils.testing import assert_warns from sklearn.utils.testing import assert_greater from sklearn.utils.testing import ignore_warnings from sklearn.datasets import make_blobs from sklearn.discriminant_analysis import LinearDiscriminantAnalysis from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis # Data is just 6 separable points in the plane X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]], dtype='f') y = np.array([1, 1, 1, 2, 2, 2]) y3 = np.array([1, 1, 2, 2, 3, 3]) # Degenerate data with only one feature (still should be separable) X1 = np.array([[-2, ], [-1, ], [-1, ], [1, ], [1, ], [2, ]], dtype='f') # Data is just 9 separable points in the plane X6 = np.array([[0, 0], [-2, -2], [-2, -1], [-1, -1], [-1, -2], [1, 3], [1, 2], [2, 1], [2, 2]]) y6 = np.array([1, 1, 1, 1, 1, 2, 2, 2, 2]) y7 = np.array([1, 2, 3, 2, 3, 1, 2, 3, 1]) # Degenerate data with 1 feature (still should be separable) X7 = np.array([[-3, ], [-2, ], [-1, ], [-1, ], [0, ], [1, ], [1, ], [2, ], [3, ]]) # Data that has zero variance in one dimension and needs regularization X2 = np.array([[-3, 0], [-2, 0], [-1, 0], [-1, 0], [0, 0], [1, 0], [1, 0], [2, 0], [3, 0]]) # One element class y4 = np.array([1, 1, 1, 1, 1, 1, 1, 1, 2]) # Data with less samples in a class than n_features X5 = np.c_[np.arange(8), np.zeros((8, 3))] y5 = np.array([0, 0, 0, 0, 0, 1, 1, 1]) solver_shrinkage = [('svd', None), ('lsqr', None), ('eigen', None), ('lsqr', 'auto'), ('lsqr', 0), ('lsqr', 0.43), ('eigen', 'auto'), ('eigen', 0), ('eigen', 0.43)] def test_lda_predict(): # Test LDA classification. # This checks that LDA implements fit and predict and returns correct # values for simple toy data. for test_case in solver_shrinkage: solver, shrinkage = test_case clf = LinearDiscriminantAnalysis(solver=solver, shrinkage=shrinkage) y_pred = clf.fit(X, y).predict(X) assert_array_equal(y_pred, y, 'solver %s' % solver) # Assert that it works with 1D data y_pred1 = clf.fit(X1, y).predict(X1) assert_array_equal(y_pred1, y, 'solver %s' % solver) # Test probability estimates y_proba_pred1 = clf.predict_proba(X1) assert_array_equal((y_proba_pred1[:, 1] > 0.5) + 1, y, 'solver %s' % solver) y_log_proba_pred1 = clf.predict_log_proba(X1) assert_array_almost_equal(np.exp(y_log_proba_pred1), y_proba_pred1, 8, 'solver %s' % solver) # Primarily test for commit 2f34950 -- "reuse" of priors y_pred3 = clf.fit(X, y3).predict(X) # LDA shouldn't be able to separate those assert_true(np.any(y_pred3 != y3), 'solver %s' % solver) # Test invalid shrinkages clf = LinearDiscriminantAnalysis(solver="lsqr", shrinkage=-0.2231) assert_raises(ValueError, clf.fit, X, y) clf = LinearDiscriminantAnalysis(solver="eigen", shrinkage="dummy") assert_raises(ValueError, clf.fit, X, y) clf = LinearDiscriminantAnalysis(solver="svd", shrinkage="auto") assert_raises(NotImplementedError, clf.fit, X, y) # Test unknown solver clf = LinearDiscriminantAnalysis(solver="dummy") assert_raises(ValueError, clf.fit, X, y) def test_lda_priors(): # Test priors (negative priors) priors = np.array([0.5, -0.5]) clf = LinearDiscriminantAnalysis(priors=priors) msg = "priors must be non-negative" assert_raise_message(ValueError, msg, clf.fit, X, y) # Test that priors passed as a list are correctly handled (run to see if # failure) clf = LinearDiscriminantAnalysis(priors=[0.5, 0.5]) clf.fit(X, y) # Test that priors always sum to 1 priors = np.array([0.5, 0.6]) prior_norm = np.array([0.45, 0.55]) clf = LinearDiscriminantAnalysis(priors=priors) clf.fit(X, y) assert_array_almost_equal(clf.priors_, prior_norm, 2) def test_lda_coefs(): # Test if the coefficients of the solvers are approximately the same. n_features = 2 n_classes = 2 n_samples = 1000 X, y = make_blobs(n_samples=n_samples, n_features=n_features, centers=n_classes, random_state=11) clf_lda_svd = LinearDiscriminantAnalysis(solver="svd") clf_lda_lsqr = LinearDiscriminantAnalysis(solver="lsqr") clf_lda_eigen = LinearDiscriminantAnalysis(solver="eigen") clf_lda_svd.fit(X, y) clf_lda_lsqr.fit(X, y) clf_lda_eigen.fit(X, y) assert_array_almost_equal(clf_lda_svd.coef_, clf_lda_lsqr.coef_, 1) assert_array_almost_equal(clf_lda_svd.coef_, clf_lda_eigen.coef_, 1) assert_array_almost_equal(clf_lda_eigen.coef_, clf_lda_lsqr.coef_, 1) def test_lda_transform(): # Test LDA transform. clf = LinearDiscriminantAnalysis(solver="svd", n_components=1) X_transformed = clf.fit(X, y).transform(X) assert_equal(X_transformed.shape[1], 1) clf = LinearDiscriminantAnalysis(solver="eigen", n_components=1) X_transformed = clf.fit(X, y).transform(X) assert_equal(X_transformed.shape[1], 1) clf = LinearDiscriminantAnalysis(solver="lsqr", n_components=1) clf.fit(X, y) msg = "transform not implemented for 'lsqr'" assert_raise_message(NotImplementedError, msg, clf.transform, X) def test_lda_explained_variance_ratio(): # Test if the sum of the normalized eigen vectors values equals 1 n_features = 2 n_classes = 2 n_samples = 1000 X, y = make_blobs(n_samples=n_samples, n_features=n_features, centers=n_classes, random_state=11) clf_lda_eigen = LinearDiscriminantAnalysis(solver="eigen") clf_lda_eigen.fit(X, y) assert_almost_equal(clf_lda_eigen.explained_variance_ratio_.sum(), 1.0, 3) def test_lda_orthogonality(): # arrange four classes with their means in a kite-shaped pattern # the longer distance should be transformed to the first component, and # the shorter distance to the second component. means = np.array([[0, 0, -1], [0, 2, 0], [0, -2, 0], [0, 0, 5]]) # We construct perfectly symmetric distributions, so the LDA can estimate # precise means. scatter = np.array([[0.1, 0, 0], [-0.1, 0, 0], [0, 0.1, 0], [0, -0.1, 0], [0, 0, 0.1], [0, 0, -0.1]]) X = (means[:, np.newaxis, :] + scatter[np.newaxis, :, :]).reshape((-1, 3)) y = np.repeat(np.arange(means.shape[0]), scatter.shape[0]) # Fit LDA and transform the means clf = LinearDiscriminantAnalysis(solver="svd").fit(X, y) means_transformed = clf.transform(means) d1 = means_transformed[3] - means_transformed[0] d2 = means_transformed[2] - means_transformed[1] d1 /= np.sqrt(np.sum(d1 ** 2)) d2 /= np.sqrt(np.sum(d2 ** 2)) # the transformed within-class covariance should be the identity matrix assert_almost_equal(np.cov(clf.transform(scatter).T), np.eye(2)) # the means of classes 0 and 3 should lie on the first component assert_almost_equal(np.abs(np.dot(d1[:2], [1, 0])), 1.0) # the means of classes 1 and 2 should lie on the second component assert_almost_equal(np.abs(np.dot(d2[:2], [0, 1])), 1.0) def test_lda_scaling(): # Test if classification works correctly with differently scaled features. n = 100 rng = np.random.RandomState(1234) # use uniform distribution of features to make sure there is absolutely no # overlap between classes. x1 = rng.uniform(-1, 1, (n, 3)) + [-10, 0, 0] x2 = rng.uniform(-1, 1, (n, 3)) + [10, 0, 0] x = np.vstack((x1, x2)) * [1, 100, 10000] y = [-1] * n + [1] * n for solver in ('svd', 'lsqr', 'eigen'): clf = LinearDiscriminantAnalysis(solver=solver) # should be able to separate the data perfectly assert_equal(clf.fit(x, y).score(x, y), 1.0, 'using covariance: %s' % solver) def test_qda(): # QDA classification. # This checks that QDA implements fit and predict and returns # correct values for a simple toy dataset. clf = QuadraticDiscriminantAnalysis() y_pred = clf.fit(X6, y6).predict(X6) assert_array_equal(y_pred, y6) # Assure that it works with 1D data y_pred1 = clf.fit(X7, y6).predict(X7) assert_array_equal(y_pred1, y6) # Test probas estimates y_proba_pred1 = clf.predict_proba(X7) assert_array_equal((y_proba_pred1[:, 1] > 0.5) + 1, y6) y_log_proba_pred1 = clf.predict_log_proba(X7) assert_array_almost_equal(np.exp(y_log_proba_pred1), y_proba_pred1, 8) y_pred3 = clf.fit(X6, y7).predict(X6) # QDA shouldn't be able to separate those assert_true(np.any(y_pred3 != y7)) # Classes should have at least 2 elements assert_raises(ValueError, clf.fit, X6, y4) def test_qda_priors(): clf = QuadraticDiscriminantAnalysis() y_pred = clf.fit(X6, y6).predict(X6) n_pos = np.sum(y_pred == 2) neg = 1e-10 clf = QuadraticDiscriminantAnalysis(priors=np.array([neg, 1 - neg])) y_pred = clf.fit(X6, y6).predict(X6) n_pos2 = np.sum(y_pred == 2) assert_greater(n_pos2, n_pos) def test_qda_store_covariances(): # The default is to not set the covariances_ attribute clf = QuadraticDiscriminantAnalysis().fit(X6, y6) assert_true(not hasattr(clf, 'covariances_')) # Test the actual attribute: clf = QuadraticDiscriminantAnalysis().fit(X6, y6, store_covariances=True) assert_true(hasattr(clf, 'covariances_')) assert_array_almost_equal( clf.covariances_[0], np.array([[0.7, 0.45], [0.45, 0.7]]) ) assert_array_almost_equal( clf.covariances_[1], np.array([[0.33333333, -0.33333333], [-0.33333333, 0.66666667]]) ) def test_qda_regularization(): # the default is reg_param=0. and will cause issues # when there is a constant variable clf = QuadraticDiscriminantAnalysis() with ignore_warnings(): y_pred = clf.fit(X2, y6).predict(X2) assert_true(np.any(y_pred != y6)) # adding a little regularization fixes the problem clf = QuadraticDiscriminantAnalysis(reg_param=0.01) with ignore_warnings(): clf.fit(X2, y6) y_pred = clf.predict(X2) assert_array_equal(y_pred, y6) # Case n_samples_in_a_class < n_features clf = QuadraticDiscriminantAnalysis(reg_param=0.1) with ignore_warnings(): clf.fit(X5, y5) y_pred5 = clf.predict(X5) assert_array_equal(y_pred5, y5) def test_deprecated_lda_qda_deprecation(): def import_lda_module(): import sklearn.lda # ensure that we trigger DeprecationWarning even if the sklearn.lda # was loaded previously by another test. reload(sklearn.lda) return sklearn.lda lda = assert_warns(DeprecationWarning, import_lda_module) assert lda.LDA is LinearDiscriminantAnalysis def import_qda_module(): import sklearn.qda # ensure that we trigger DeprecationWarning even if the sklearn.qda # was loaded previously by another test. reload(sklearn.qda) return sklearn.qda qda = assert_warns(DeprecationWarning, import_qda_module) assert qda.QDA is QuadraticDiscriminantAnalysis
bsd-3-clause
kgullikson88/General
Analyze_CCF.py
1
9048
""" This is a module to read in an HDF5 file with CCFs. Use this to determine the best parameters, and plot the best CCF for each star/date """ from collections import defaultdict import logging import h5py import numpy as np import pandas as pd from scipy.interpolate import InterpolatedUnivariateSpline as spline class CCF_Interface(object): def __init__(self, filename, vel=np.arange(-900, 900, 1)): self.hdf5 = h5py.File(filename, 'r') self.velocities = vel self._df = None def __getitem__(self, path): return self.hdf5[path] def list_stars(self, print2screen=False): """ List the stars available in the HDF5 file, and the dates available for each :return: A list of the stars """ if print2screen: for star in sorted(self.hdf5.keys()): print(star) for date in sorted(self.hdf5[star].keys()): print('\t{}'.format(date)) return sorted(self.hdf5.keys()) def list_dates(self, star, print2screen=False): """ List the dates available for the given star :param star: The name of the star :return: A list of dates the star was observed """ if print2screen: for date in sorted(self.hdf5[star].keys()): print(date) return sorted(self.hdf5[star].keys()) def load_cache(self, addmode='simple'): """ Read in the whole HDF5 file. This will take a while and take a few Gb of memory, but will speed things up considerably :keyword addmode: The way the individual CCFs were added. Options are: - 'simple' - 'ml' - 'all' (saves all addmodes) """ self._df = self._compile_data(addmode=addmode) def _compile_data(self, starname=None, date=None, addmode='simple', read_ccf=True): """ Private function. This reads in all the datasets for the given star and date :param starname: the name of the star. Must be in self.hdf5 :param date: The date to search. Must be in self.hdf5[star] :keyword addmode: The way the individual CCFs were added. Options are: - 'simple' - 'ml' - 'all' (saves all addmodes) :return: a pandas DataFrame with the columns: - star - date - temperature - log(g) - [Fe/H] - vsini - addmode - rv (at maximum CCF value) - CCF height (maximum) """ if starname is None: df_list = [] star_list = self.list_stars() for star in star_list: date_list = self.list_dates(star) for date in date_list: logging.debug('Reading in metadata for star {}, date {}'.format(star, date)) df_list.append(self._compile_data(star, date, addmode=addmode, read_ccf=read_ccf)) return pd.concat(df_list, ignore_index=True) elif starname is not None and date is None: df_list = [] date_list = self.list_dates(starname) for date in date_list: logging.debug('Reading in metadata for date {}'.format(date)) df_list.append(self._compile_data(starname, date, addmode=addmode, read_ccf=read_ccf)) return pd.concat(df_list, ignore_index=True) else: if self._df is not None: return self._df.loc[(self._df['Star'] == starname) & (self._df['Date'] == date)].copy() #print('Stars: ', self.list_stars()) datasets = self.hdf5[starname][date].keys() data = defaultdict(list) for ds_name, ds in self.hdf5[starname][date].iteritems(): # in datasets: #ds = self.hdf5[starname][date][ds_name] try: am = ds.attrs['addmode'] if addmode == 'all' or addmode == am: data['T'].append(ds.attrs['T']) data['logg'].append(ds.attrs['logg']) data['[Fe/H]'].append(ds.attrs['[Fe/H]']) data['vsini'].append(ds.attrs['vsini']) data['addmode'].append(am) data['name'].append(ds.name) try: data['ccf_max'].append(ds.attrs['ccf_max']) data['vel_max'].append(ds.attrs['vel_max']) except KeyError: vel, corr = ds.value idx = np.argmax(corr) data['ccf_max'].append(corr[idx]) data['vel_max'].append(vel[idx]) if read_ccf: v = ds.value vel, corr = v[0], v[1] sorter = np.argsort(vel) fcn = spline(vel[sorter], corr[sorter]) data['ccf'].append(fcn(self.velocities)) except: raise IOError('Something weird happened with dataset {}!'.format(ds.name)) data['Star'] = [starname] * len(data['T']) data['Date'] = [date] * len(data['T']) df = pd.DataFrame(data=data) return df def get_temperature_run(self, starname=None, date=None, df=None): """ Return the maximum ccf height for each temperature. Either starname AND date, or df must be given :param starname: The name of the star :param date: The date of the observation :param df: Input dataframe, such as from _compile_data. Overrides starname and date, if given :return: a pandas DataFrame with all the best parameters for each temperature """ # Get the dataframe if it isn't given if df is None: if starname is None or date is None: raise ValueError('Must give either starname or date to get_temperature_run!') df = self._compile_data(starname, date) # Find the maximum CCF for each set of parameters fcn = lambda row: (np.max(row), self.velocities[np.argmax(row)]) vals = df['ccf'].map(fcn) df['ccf_max'] = vals.map(lambda l: l[0]) df['rv'] = vals.map(lambda l: l[1]) # Find the best parameters for each temperature d = defaultdict(list) temperatures = pd.unique(df['T']) for T in temperatures: good = df.loc[df['T'] == T] best = good.loc[good.ccf_max == good.ccf_max.max()] d['vsini'].append(best['vsini'].item()) d['logg'].append(best['logg'].item()) d['[Fe/H]'].append(best['[Fe/H]'].item()) d['rv'].append(best['rv'].item()) d['ccf_value'].append(best.ccf_max.item()) d['T'].append(T) d['metal'].append(best['[Fe/H]'].item()) return pd.DataFrame(data=d) def get_ccf(self, params, df=None): """ Get the ccf with the given parameters. A dataframe can be given to speed things up :param params: All the parameters necessary to define a single ccf. This should be a python dictionary with the keys: - 'starname': The name of the star. Try self.list_stars() for the options. - 'date': The UT date of the observations. Try self.list_dates() for the options. - 'T': temperature of the model - 'logg': the log(g) of the model - 'vsini': the vsini by which the model was broadened before correlation - '[Fe/H]': the metallicity of the model - 'addmode': The way the order CCFs were added to make a total one. Can be: - 'simple' - 'ml' - 'weighted' - 'dc' :param df: a pandas DataFrame such as outputted by _compile_data :return: a pandas DataFrame with columns of velocity and CCF power """ if df is None: try: df = self._compile_data(params['starname'], params['date']) except KeyError: raise KeyError('Must give get_ccf params with starname and date keywords, if df is not given!') Tvals = df['T'].unique() T = Tvals[np.argmin(abs(Tvals - params['T']))] good = df.loc[(df['T'] == T) & (df.logg == params['logg']) & (df.vsini == params['vsini']) \ & (df['[Fe/H]'] == params['[Fe/H]']) & (df.addmode == params['addmode'])] return pd.DataFrame(data={'velocity': self.velocities, 'CCF': good['ccf'].item()})
gpl-3.0
Obus/scikit-learn
sklearn/setup.py
225
2856
import os from os.path import join import warnings def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration from numpy.distutils.system_info import get_info, BlasNotFoundError import numpy libraries = [] if os.name == 'posix': libraries.append('m') config = Configuration('sklearn', parent_package, top_path) config.add_subpackage('__check_build') config.add_subpackage('svm') config.add_subpackage('datasets') config.add_subpackage('datasets/tests') config.add_subpackage('feature_extraction') config.add_subpackage('feature_extraction/tests') config.add_subpackage('cluster') config.add_subpackage('cluster/tests') config.add_subpackage('covariance') config.add_subpackage('covariance/tests') config.add_subpackage('cross_decomposition') config.add_subpackage('decomposition') config.add_subpackage('decomposition/tests') config.add_subpackage("ensemble") config.add_subpackage("ensemble/tests") config.add_subpackage('feature_selection') config.add_subpackage('feature_selection/tests') config.add_subpackage('utils') config.add_subpackage('utils/tests') config.add_subpackage('externals') config.add_subpackage('mixture') config.add_subpackage('mixture/tests') config.add_subpackage('gaussian_process') config.add_subpackage('gaussian_process/tests') config.add_subpackage('neighbors') config.add_subpackage('neural_network') config.add_subpackage('preprocessing') config.add_subpackage('manifold') config.add_subpackage('metrics') config.add_subpackage('semi_supervised') config.add_subpackage("tree") config.add_subpackage("tree/tests") config.add_subpackage('metrics/tests') config.add_subpackage('metrics/cluster') config.add_subpackage('metrics/cluster/tests') # add cython extension module for isotonic regression config.add_extension( '_isotonic', sources=['_isotonic.c'], include_dirs=[numpy.get_include()], libraries=libraries, ) # some libs needs cblas, fortran-compiled BLAS will not be sufficient blas_info = get_info('blas_opt', 0) if (not blas_info) or ( ('NO_ATLAS_INFO', 1) in blas_info.get('define_macros', [])): config.add_library('cblas', sources=[join('src', 'cblas', '*.c')]) warnings.warn(BlasNotFoundError.__doc__) # the following packages depend on cblas, so they have to be build # after the above. config.add_subpackage('linear_model') config.add_subpackage('utils') # add the test directory config.add_subpackage('tests') return config if __name__ == '__main__': from numpy.distutils.core import setup setup(**configuration(top_path='').todict())
bsd-3-clause
cle1109/scot
doc/sphinxext/inheritance_diagram.py
4
13650
""" Defines a docutils directive for inserting inheritance diagrams. Provide the directive with one or more classes or modules (separated by whitespace). For modules, all of the classes in that module will be used. Example:: Given the following classes: class A: pass class B(A): pass class C(A): pass class D(B, C): pass class E(B): pass .. inheritance-diagram: D E Produces a graph like the following: A / \ B C / \ / E D The graph is inserted as a PNG+image map into HTML and a PDF in LaTeX. """ import inspect import os import re import subprocess try: from hashlib import md5 except ImportError: from md5 import md5 from docutils.nodes import Body, Element from docutils.parsers.rst import directives from sphinx.roles import xfileref_role def my_import(name): """Module importer - taken from the python documentation. This function allows importing names with dots in them.""" mod = __import__(name) components = name.split('.') for comp in components[1:]: mod = getattr(mod, comp) return mod class DotException(Exception): pass class InheritanceGraph(object): """ Given a list of classes, determines the set of classes that they inherit from all the way to the root "object", and then is able to generate a graphviz dot graph from them. """ def __init__(self, class_names, show_builtins=False): """ *class_names* is a list of child classes to show bases from. If *show_builtins* is True, then Python builtins will be shown in the graph. """ self.class_names = class_names self.classes = self._import_classes(class_names) self.all_classes = self._all_classes(self.classes) if len(self.all_classes) == 0: raise ValueError("No classes found for inheritance diagram") self.show_builtins = show_builtins py_sig_re = re.compile(r'''^([\w.]*\.)? # class names (\w+) \s* $ # optionally arguments ''', re.VERBOSE) def _import_class_or_module(self, name): """ Import a class using its fully-qualified *name*. """ try: path, base = self.py_sig_re.match(name).groups() except: raise ValueError( "Invalid class or module '%s' specified for inheritance diagram" % name) fullname = (path or '') + base path = (path and path.rstrip('.')) if not path: path = base try: module = __import__(path, None, None, []) # We must do an import of the fully qualified name. Otherwise if a # subpackage 'a.b' is requested where 'import a' does NOT provide # 'a.b' automatically, then 'a.b' will not be found below. This # second call will force the equivalent of 'import a.b' to happen # after the top-level import above. my_import(fullname) except ImportError: raise ValueError( "Could not import class or module '%s' specified for inheritance diagram" % name) try: todoc = module for comp in fullname.split('.')[1:]: todoc = getattr(todoc, comp) except AttributeError: raise ValueError( "Could not find class or module '%s' specified for inheritance diagram" % name) # If a class, just return it if inspect.isclass(todoc): return [todoc] elif inspect.ismodule(todoc): classes = [] for cls in todoc.__dict__.values(): if inspect.isclass(cls) and cls.__module__ == todoc.__name__: classes.append(cls) return classes raise ValueError( "'%s' does not resolve to a class or module" % name) def _import_classes(self, class_names): """ Import a list of classes. """ classes = [] for name in class_names: classes.extend(self._import_class_or_module(name)) return classes def _all_classes(self, classes): """ Return a list of all classes that are ancestors of *classes*. """ all_classes = {} def recurse(cls): all_classes[cls] = None for c in cls.__bases__: if c not in all_classes: recurse(c) for cls in classes: recurse(cls) return all_classes.keys() def class_name(self, cls, parts=0): """ Given a class object, return a fully-qualified name. This works for things I've tested in matplotlib so far, but may not be completely general. """ module = cls.__module__ if module == '__builtin__': fullname = cls.__name__ else: fullname = "%s.%s" % (module, cls.__name__) if parts == 0: return fullname name_parts = fullname.split('.') return '.'.join(name_parts[-parts:]) def get_all_class_names(self): """ Get all of the class names involved in the graph. """ return [self.class_name(x) for x in self.all_classes] # These are the default options for graphviz default_graph_options = { "rankdir": "LR", "size": '"8.0, 12.0"' } default_node_options = { "shape": "box", "fontsize": 10, "height": 0.25, "fontname": "Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans", "style": '"setlinewidth(0.5)"' } default_edge_options = { "arrowsize": 0.5, "style": '"setlinewidth(0.5)"' } def _format_node_options(self, options): return ','.join(["%s=%s" % x for x in options.items()]) def _format_graph_options(self, options): return ''.join(["%s=%s;\n" % x for x in options.items()]) def generate_dot(self, fd, name, parts=0, urls={}, graph_options={}, node_options={}, edge_options={}): """ Generate a graphviz dot graph from the classes that were passed in to __init__. *fd* is a Python file-like object to write to. *name* is the name of the graph *urls* is a dictionary mapping class names to http urls *graph_options*, *node_options*, *edge_options* are dictionaries containing key/value pairs to pass on as graphviz properties. """ g_options = self.default_graph_options.copy() g_options.update(graph_options) n_options = self.default_node_options.copy() n_options.update(node_options) e_options = self.default_edge_options.copy() e_options.update(edge_options) fd.write('digraph %s {\n' % name) fd.write(self._format_graph_options(g_options)) for cls in self.all_classes: if not self.show_builtins and cls in __builtins__.values(): continue name = self.class_name(cls, parts) # Write the node this_node_options = n_options.copy() url = urls.get(self.class_name(cls)) if url is not None: this_node_options['URL'] = '"%s"' % url fd.write(' "%s" [%s];\n' % (name, self._format_node_options(this_node_options))) # Write the edges for base in cls.__bases__: if not self.show_builtins and base in __builtins__.values(): continue base_name = self.class_name(base, parts) fd.write(' "%s" -> "%s" [%s];\n' % (base_name, name, self._format_node_options(e_options))) fd.write('}\n') def run_dot(self, args, name, parts=0, urls={}, graph_options={}, node_options={}, edge_options={}): """ Run graphviz 'dot' over this graph, returning whatever 'dot' writes to stdout. *args* will be passed along as commandline arguments. *name* is the name of the graph *urls* is a dictionary mapping class names to http urls Raises DotException for any of the many os and installation-related errors that may occur. """ try: dot = subprocess.Popen(['dot'] + list(args), stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True) except OSError: raise DotException("Could not execute 'dot'. Are you sure you have 'graphviz' installed?") except ValueError: raise DotException("'dot' called with invalid arguments") except: raise DotException("Unexpected error calling 'dot'") self.generate_dot(dot.stdin, name, parts, urls, graph_options, node_options, edge_options) dot.stdin.close() result = dot.stdout.read() returncode = dot.wait() if returncode != 0: raise DotException("'dot' returned the errorcode %d" % returncode) return result class inheritance_diagram(Body, Element): """ A docutils node to use as a placeholder for the inheritance diagram. """ pass def inheritance_diagram_directive(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """ Run when the inheritance_diagram directive is first encountered. """ node = inheritance_diagram() class_names = arguments # Create a graph starting with the list of classes graph = InheritanceGraph(class_names) # Create xref nodes for each target of the graph's image map and # add them to the doc tree so that Sphinx can resolve the # references to real URLs later. These nodes will eventually be # removed from the doctree after we're done with them. for name in graph.get_all_class_names(): refnodes, x = xfileref_role( 'class', ':class:`%s`' % name, name, 0, state) node.extend(refnodes) # Store the graph object so we can use it to generate the # dot file later node['graph'] = graph # Store the original content for use as a hash node['parts'] = options.get('parts', 0) node['content'] = " ".join(class_names) return [node] def get_graph_hash(node): return md5(node['content'] + str(node['parts'])).hexdigest()[-10:] def html_output_graph(self, node): """ Output the graph for HTML. This will insert a PNG with clickable image map. """ graph = node['graph'] parts = node['parts'] graph_hash = get_graph_hash(node) name = "inheritance%s" % graph_hash path = '_images' dest_path = os.path.join(setup.app.builder.outdir, path) if not os.path.exists(dest_path): os.makedirs(dest_path) png_path = os.path.join(dest_path, name + ".png") path = setup.app.builder.imgpath # Create a mapping from fully-qualified class names to URLs. urls = {} for child in node: if child.get('refuri') is not None: urls[child['reftitle']] = child.get('refuri') elif child.get('refid') is not None: urls[child['reftitle']] = '#' + child.get('refid') # These arguments to dot will save a PNG file to disk and write # an HTML image map to stdout. image_map = graph.run_dot(['-Tpng', '-o%s' % png_path, '-Tcmapx'], name, parts, urls) return ('<img src="%s/%s.png" usemap="#%s" class="inheritance"/>%s' % (path, name, name, image_map)) def latex_output_graph(self, node): """ Output the graph for LaTeX. This will insert a PDF. """ graph = node['graph'] parts = node['parts'] graph_hash = get_graph_hash(node) name = "inheritance%s" % graph_hash dest_path = os.path.abspath(os.path.join(setup.app.builder.outdir, '_images')) if not os.path.exists(dest_path): os.makedirs(dest_path) pdf_path = os.path.abspath(os.path.join(dest_path, name + ".pdf")) graph.run_dot(['-Tpdf', '-o%s' % pdf_path], name, parts, graph_options={'size': '"6.0,6.0"'}) return '\n\\includegraphics{%s}\n\n' % pdf_path def visit_inheritance_diagram(inner_func): """ This is just a wrapper around html/latex_output_graph to make it easier to handle errors and insert warnings. """ def visitor(self, node): try: content = inner_func(self, node) except DotException as e: # Insert the exception as a warning in the document warning = self.document.reporter.warning(str(e), line=node.line) warning.parent = node node.children = [warning] else: source = self.document.attributes['source'] self.body.append(content) node.children = [] return visitor def do_nothing(self, node): pass def setup(app): setup.app = app setup.confdir = app.confdir app.add_node( inheritance_diagram, latex=(visit_inheritance_diagram(latex_output_graph), do_nothing), html=(visit_inheritance_diagram(html_output_graph), do_nothing)) app.add_directive( 'inheritance-diagram', inheritance_diagram_directive, False, (1, 100, 0), parts = directives.nonnegative_int)
mit
great-expectations/great_expectations
tests/dataset/test_sparkdfdataset.py
1
14191
import importlib.util import json from unittest import mock import pandas as pd import pytest from great_expectations.dataset.sparkdf_dataset import SparkDFDataset from great_expectations.util import is_library_loadable def test_sparkdfdataset_persist(spark_session): df = pd.DataFrame({"a": [1, 2, 3]}) sdf = spark_session.createDataFrame(df) sdf.persist = mock.MagicMock() _ = SparkDFDataset(sdf, persist=True) sdf.persist.assert_called_once() sdf = spark_session.createDataFrame(df) sdf.persist = mock.MagicMock() _ = SparkDFDataset(sdf, persist=False) sdf.persist.assert_not_called() sdf = spark_session.createDataFrame(df) sdf.persist = mock.MagicMock() _ = SparkDFDataset(sdf) sdf.persist.assert_called_once() @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) @pytest.fixture def test_dataframe(spark_session): from pyspark.sql.types import IntegerType, StringType, StructField, StructType schema = StructType( [ StructField("name", StringType(), True), StructField("age", IntegerType(), True), StructField( "address", StructType( [ StructField("street", StringType(), True), StructField("city", StringType(), True), StructField("house_number", IntegerType(), True), ] ), False, ), StructField("name_duplicate", StringType(), True), StructField("non.nested", StringType(), True), StructField("name_with_duplicates", StringType(), True), StructField("age_with_duplicates", IntegerType(), True), StructField( "address_with_duplicates", StructType( [ StructField("street", StringType(), True), StructField("city", StringType(), True), StructField("house_number", IntegerType(), True), ] ), False, ), ] ) rows = [ ( "Alice", 1, ("Street 1", "Alabama", 10), "Alice", "a", "Alice", 1, ("Street 1", "Alabama", 12), ), ( "Bob", 2, ("Street 2", "Brooklyn", 11), "Bob", "b", "Bob", 2, ("Street 1", "Brooklyn", 12), ), ( "Charlie", 3, ("Street 3", "Alabama", 12), "Charlie", "c", "Charlie", 3, ("Street 1", "Alabama", 12), ), ( "Dan", 4, ("Street 4", "Boston", 12), "Dan", "d", "Charlie", 3, ("Street 1", "Boston", 12), ), ] rdd = spark_session.sparkContext.parallelize(rows) df = spark_session.createDataFrame(rdd, schema) return SparkDFDataset(df, persist=True) @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_column_values_to_be_of_type(spark_session, test_dataframe): """ data asset expectation """ from pyspark.sql.utils import AnalysisException assert test_dataframe.expect_column_values_to_be_of_type( "address.street", "StringType" ).success assert test_dataframe.expect_column_values_to_be_of_type( "`non.nested`", "StringType" ).success assert test_dataframe.expect_column_values_to_be_of_type( "name", "StringType" ).success with pytest.raises(AnalysisException): test_dataframe.expect_column_values_to_be_of_type("non.nested", "StringType") @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_column_values_to_be_of_type(spark_session, test_dataframe): """ data asset expectation """ from pyspark.sql.utils import AnalysisException assert test_dataframe.expect_column_values_to_be_of_type( "address.street", "StringType" ).success assert test_dataframe.expect_column_values_to_be_of_type( "`non.nested`", "StringType" ).success assert test_dataframe.expect_column_values_to_be_of_type( "name", "StringType" ).success with pytest.raises(AnalysisException): test_dataframe.expect_column_values_to_be_of_type("non.nested", "StringType") @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_column_values_to_be_in_type_list(spark_session, test_dataframe): """ data asset expectation """ from pyspark.sql.utils import AnalysisException assert test_dataframe.expect_column_values_to_be_in_type_list( "address.street", ["StringType", "IntegerType"] ).success assert test_dataframe.expect_column_values_to_be_in_type_list( "`non.nested`", ["StringType", "IntegerType"] ).success assert test_dataframe.expect_column_values_to_be_in_type_list( "name", ["StringType", "IntegerType"] ).success with pytest.raises(AnalysisException): test_dataframe.expect_column_values_to_be_of_type("non.nested", "StringType") @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_column_pair_values_to_be_equal(spark_session, test_dataframe): """ column_pair_map_expectation """ from pyspark.sql.utils import AnalysisException assert test_dataframe.expect_column_pair_values_to_be_equal( "name", "name_duplicate" ).success assert not test_dataframe.expect_column_pair_values_to_be_equal( "name", "address.street" ).success assert not test_dataframe.expect_column_pair_values_to_be_equal( "name", "`non.nested`" ).success # Expectation should fail when no `` surround a non-nested column with dot notation with pytest.raises(AnalysisException): test_dataframe.expect_column_pair_values_to_be_equal("name", "non.nested") @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_column_pair_values_A_to_be_greater_than_B( spark_session, test_dataframe ): """ column_pair_map_expectation """ assert test_dataframe.expect_column_pair_values_A_to_be_greater_than_B( "address.house_number", "age" ).success assert test_dataframe.expect_column_pair_values_A_to_be_greater_than_B( "age", "age", or_equal=True ).success @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_select_column_values_to_be_unique_within_record( spark_session, test_dataframe ): """ multicolumn_map_expectation """ from pyspark.sql.utils import AnalysisException assert test_dataframe.expect_select_column_values_to_be_unique_within_record( ["name", "age"] ).success assert test_dataframe.expect_select_column_values_to_be_unique_within_record( ["address.street", "name"] ).success assert test_dataframe.expect_select_column_values_to_be_unique_within_record( ["address.street", "`non.nested`"] ).success # Expectation should fail when no `` surround a non-nested column with dot notation with pytest.raises(AnalysisException): test_dataframe.expect_select_column_values_to_be_unique_within_record( ["address.street", "non.nested"] ) @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_compound_columns_to_be_unique(spark_session, test_dataframe): """ multicolumn_map_expectation """ from pyspark.sql.utils import AnalysisException # Positive tests assert test_dataframe.expect_compound_columns_to_be_unique(["name", "age"]).success assert test_dataframe.expect_compound_columns_to_be_unique( ["address.street", "name"] ).success assert test_dataframe.expect_compound_columns_to_be_unique( ["address.street", "address.city"] ).success assert test_dataframe.expect_compound_columns_to_be_unique( ["name_with_duplicates", "age_with_duplicates", "name"] ).success assert test_dataframe.expect_compound_columns_to_be_unique( ["address.street", "`non.nested`"] ).success assert test_dataframe.expect_compound_columns_to_be_unique( ["name", "name_with_duplicates"] ).success assert test_dataframe.expect_compound_columns_to_be_unique( [ "name", "name_with_duplicates", "address_with_duplicates.street", "address_with_duplicates.city", "address_with_duplicates.house_number", ] ).success # Negative tests assert not test_dataframe.expect_compound_columns_to_be_unique( ["address_with_duplicates.city", "address_with_duplicates.house_number"] ).success assert not test_dataframe.expect_compound_columns_to_be_unique( ["name_with_duplicates"] ).success assert not test_dataframe.expect_compound_columns_to_be_unique( ["name_with_duplicates", "address_with_duplicates.street"] ).success assert not test_dataframe.expect_compound_columns_to_be_unique( [ "name_with_duplicates", "address_with_duplicates.street", "address_with_duplicates.house_number", ] ).success # Expectation should fail when no `` surround a non-nested column with dot notation with pytest.raises(AnalysisException): test_dataframe.expect_compound_columns_to_be_unique( ["address.street", "non.nested"] ) @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_column_values_to_be_unique(spark_session, test_dataframe): """ column_map_expectation """ from pyspark.sql.utils import AnalysisException assert test_dataframe.expect_column_values_to_be_unique("name").success assert not test_dataframe.expect_column_values_to_be_unique("address.city").success assert test_dataframe.expect_column_values_to_be_unique("`non.nested`").success # Expectation should fail when no `` surround a non-nested column with dot notation with pytest.raises(AnalysisException): test_dataframe.expect_column_values_to_be_unique("non.nested") @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_column_value_lengths_to_be_between(spark_session, test_dataframe): """ column_map_expectation """ assert test_dataframe.expect_column_value_lengths_to_be_between( "name", 3, 7 ).success assert test_dataframe.expect_column_value_lengths_to_be_between( "address.street", 1, 10 ).success @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_column_value_lengths_to_equal(spark_session, test_dataframe): """ column_map_expectation """ assert test_dataframe.expect_column_value_lengths_to_equal("age", 1).success assert test_dataframe.expect_column_value_lengths_to_equal( "address.street", 8 ).success @pytest.mark.skipif( not is_library_loadable(library_name="pyspark"), reason="pyspark must be installed", ) def test_expect_column_values_to_be_json_parseable(spark_session): d1 = json.dumps({"i": [1, 2, 3], "j": 35, "k": {"x": "five", "y": 5, "z": "101"}}) d2 = json.dumps({"i": 1, "j": 2, "k": [3, 4, 5]}) d3 = json.dumps({"i": "a", "j": "b", "k": "c"}) d4 = json.dumps( {"i": [4, 5], "j": [6, 7], "k": [8, 9], "l": {4: "x", 5: "y", 6: "z"}} ) inner = { "json_col": [d1, d2, d3, d4], "not_json": [4, 5, 6, 7], "py_dict": [ {"a": 1, "out": 1}, {"b": 2, "out": 4}, {"c": 3, "out": 9}, {"d": 4, "out": 16}, ], "most": [d1, d2, d3, "d4"], } data_reshaped = list(zip(*[v for _, v in inner.items()])) df = spark_session.createDataFrame( data_reshaped, ["json_col", "not_json", "py_dict", "most"] ) D = SparkDFDataset(df) D.set_default_expectation_argument("result_format", "COMPLETE") T = [ { "in": {"column": "json_col"}, "out": { "success": True, "unexpected_list": [], }, }, { "in": {"column": "not_json"}, "out": { "success": False, "unexpected_list": [4, 5, 6, 7], }, }, { "in": {"column": "py_dict"}, "out": { "success": False, "unexpected_list": [ {"a": 1, "out": 1}, {"b": 2, "out": 4}, {"c": 3, "out": 9}, {"d": 4, "out": 16}, ], }, }, { "in": {"column": "most"}, "out": { "success": False, "unexpected_list": ["d4"], }, }, { "in": {"column": "most", "mostly": 0.75}, "out": { "success": True, "unexpected_index_list": [3], "unexpected_list": ["d4"], }, }, ] for t in T: out = D.expect_column_values_to_be_json_parseable(**t["in"]) assert t["out"]["success"] == out.success assert t["out"]["unexpected_list"] == out.result["unexpected_list"]
apache-2.0
hdmetor/scikit-learn
examples/text/hashing_vs_dict_vectorizer.py
284
3265
""" =========================================== FeatureHasher and DictVectorizer Comparison =========================================== Compares FeatureHasher and DictVectorizer by using both to vectorize text documents. The example demonstrates syntax and speed only; it doesn't actually do anything useful with the extracted vectors. See the example scripts {document_classification_20newsgroups,clustering}.py for actual learning on text documents. A discrepancy between the number of terms reported for DictVectorizer and for FeatureHasher is to be expected due to hash collisions. """ # Author: Lars Buitinck <L.J.Buitinck@uva.nl> # License: BSD 3 clause from __future__ import print_function from collections import defaultdict import re import sys from time import time import numpy as np from sklearn.datasets import fetch_20newsgroups from sklearn.feature_extraction import DictVectorizer, FeatureHasher def n_nonzero_columns(X): """Returns the number of non-zero columns in a CSR matrix X.""" return len(np.unique(X.nonzero()[1])) def tokens(doc): """Extract tokens from doc. This uses a simple regex to break strings into tokens. For a more principled approach, see CountVectorizer or TfidfVectorizer. """ return (tok.lower() for tok in re.findall(r"\w+", doc)) def token_freqs(doc): """Extract a dict mapping tokens from doc to their frequencies.""" freq = defaultdict(int) for tok in tokens(doc): freq[tok] += 1 return freq categories = [ 'alt.atheism', 'comp.graphics', 'comp.sys.ibm.pc.hardware', 'misc.forsale', 'rec.autos', 'sci.space', 'talk.religion.misc', ] # Uncomment the following line to use a larger set (11k+ documents) #categories = None print(__doc__) print("Usage: %s [n_features_for_hashing]" % sys.argv[0]) print(" The default number of features is 2**18.") print() try: n_features = int(sys.argv[1]) except IndexError: n_features = 2 ** 18 except ValueError: print("not a valid number of features: %r" % sys.argv[1]) sys.exit(1) print("Loading 20 newsgroups training data") raw_data = fetch_20newsgroups(subset='train', categories=categories).data data_size_mb = sum(len(s.encode('utf-8')) for s in raw_data) / 1e6 print("%d documents - %0.3fMB" % (len(raw_data), data_size_mb)) print() print("DictVectorizer") t0 = time() vectorizer = DictVectorizer() vectorizer.fit_transform(token_freqs(d) for d in raw_data) duration = time() - t0 print("done in %fs at %0.3fMB/s" % (duration, data_size_mb / duration)) print("Found %d unique terms" % len(vectorizer.get_feature_names())) print() print("FeatureHasher on frequency dicts") t0 = time() hasher = FeatureHasher(n_features=n_features) X = hasher.transform(token_freqs(d) for d in raw_data) duration = time() - t0 print("done in %fs at %0.3fMB/s" % (duration, data_size_mb / duration)) print("Found %d unique terms" % n_nonzero_columns(X)) print() print("FeatureHasher on raw tokens") t0 = time() hasher = FeatureHasher(n_features=n_features, input_type="string") X = hasher.transform(tokens(d) for d in raw_data) duration = time() - t0 print("done in %fs at %0.3fMB/s" % (duration, data_size_mb / duration)) print("Found %d unique terms" % n_nonzero_columns(X))
bsd-3-clause
davidtrem/ThunderStorm
thunderstorm/lightning/utils.py
1
5027
# -*- coding: utf-8 -*- # Copyright (C) 2010-2013 Trémouilles David #This file is part of Thunderstorm. # #ThunderStrom is free software: you can redistribute it and/or modify #it under the terms of the GNU Lesser General Public License as published by #the Free Software Foundation, either version 3 of the License, or #(at your option) any later version. # #ThunderStorm is distributed in the hope that it will be useful, #but WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #GNU Lesser General Public License for more details. # #You should have received a copy of the GNU Lesser General Public License #along with ThunderStorm. If not, see <http://www.gnu.org/licenses/>. """ Various utility functions """ import matplotlib from weakref import WeakValueDictionary from weakref import WeakKeyDictionary import warnings class UniversalCursors(object): def __init__(self): self.all_cursor_orient = WeakKeyDictionary() self.all_canvas = WeakValueDictionary() self.all_axes = WeakValueDictionary() self.backgrounds = {} self.visible = True self.needclear = False def _onmove(self, event): for canvas in self.all_canvas.values(): if not canvas.widgetlock.available(self): return if event.inaxes is None or not self.visible: if self.needclear: self._update(event) for canvas in self.all_canvas.values(): canvas.draw() self.needclear = False return self._update(event) def _update(self, event): # 1/ Reset background for canvas in self.all_canvas.values(): canvas.restore_region(self.backgrounds[id(canvas)]) # 2/ update cursors for cursors in self.all_cursor_orient.keys(): orient = self.all_cursor_orient[cursors] if (event.inaxes in [line.get_axes() for line in cursors] and self.visible): visible = True self.needclear = True else: visible = False for line in cursors: if orient == 'vertical': line.set_xdata((event.xdata, event.xdata)) if orient == 'horizontal': line.set_ydata((event.ydata, event.ydata)) line.set_visible(visible) ax = line.get_axes() ax.draw_artist(line) # 3/ update canvas for canvas in self.all_canvas.values(): canvas.blit(canvas.figure.bbox) def _clear(self, event): """clear the cursor""" self.backgrounds = {} for canvas in self.all_canvas.values(): self.backgrounds[id(canvas)] = ( canvas.copy_from_bbox(canvas.figure.bbox)) for cursor in self.all_cursor_orient.keys(): for line in cursor: line.set_visible(False) def add_cursor(self, axes=(), orient='vertical', **lineprops): class CursorList(list): def __hash__(self): return hash(tuple(self)) cursors = CursorList() # Required to keep weakref for ax in axes: self.all_axes[id(ax)] = ax ax_canvas = ax.get_figure().canvas if ax_canvas not in self.all_canvas.values(): #if not ax_canvas.supports_blit: # warnings.warn("Must use canvas that support blit") # return self.all_canvas[id(ax_canvas)] = ax_canvas ax_canvas.mpl_connect('motion_notify_event', self._onmove) ax_canvas.mpl_connect('draw_event', self._clear) if orient == 'vertical': line = ax.axvline(ax.get_xbound()[0], visible=False, animated=True, **lineprops) if orient == 'horizontal': line = ax.axhline(ax.get_ybound()[0], visible=False, animated=True, **lineprops) cursors.append(line) self.all_cursor_orient[cursors] = orient return cursors def autoscale_visible_lines(axs): """ Function to autoscale only on visible lines. """ mplt_ver = [int(elem) for elem in matplotlib.__version__.split('.')[0:2]] ignore = True for line in (axs.lines): if not line.get_visible(): continue # jump to next line if this one is not visible if mplt_ver[0] == 0 and mplt_ver[1] < 98: axs.dataLim.update_numerix(line.get_xdata(), line.get_ydata(), ignore) else: axs.dataLim.update_from_data_xy(line.get_xydata(), ignore) ignore = False axs.autoscale_view() return None def neg_bool_list(a_list): return [not elem for elem in a_list]
gpl-3.0
klahnakoski/ActiveData
vendor/mo_testing/fuzzytestcase.py
1
9712
# encoding: utf-8 # # # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/. # # Contact: Kyle Lahnakoski (kyle@lahnakoski.com) # from __future__ import unicode_literals import datetime import types import unittest from mo_collections.unique_index import UniqueIndex import mo_dots from mo_dots import coalesce, is_container, is_list, literal_field, unwrap, to_data, is_data, is_many from mo_future import is_text, zip_longest, first from mo_logs import Except, Log, suppress_exception from mo_logs.strings import expand_template, quote import mo_math from mo_math import is_number, log10 from mo_times import dates class FuzzyTestCase(unittest.TestCase): """ COMPARE STRUCTURE AND NUMBERS! ONLY THE ATTRIBUTES IN THE expected STRUCTURE ARE TESTED TO EXIST EXTRA ATTRIBUTES ARE IGNORED. NUMBERS ARE MATCHED BY ... * places (UP TO GIVEN SIGNIFICANT DIGITS) * digits (UP TO GIVEN DECIMAL PLACES, WITH NEGATIVE MEANING LEFT-OF-UNITS) * delta (MAXIMUM ABSOLUTE DIFFERENCE FROM expected) """ def __init__(self, *args, **kwargs): unittest.TestCase.__init__(self, *args, **kwargs) self.default_places=15 def set_default_places(self, places): """ WHEN COMPARING float, HOW MANY DIGITS ARE SIGNIFICANT BY DEFAULT """ self.default_places=places def assertAlmostEqual(self, test_value, expected, msg=None, digits=None, places=None, delta=None): if delta or digits: assertAlmostEqual(test_value, expected, msg=msg, digits=digits, places=places, delta=delta) else: assertAlmostEqual(test_value, expected, msg=msg, digits=digits, places=coalesce(places, self.default_places), delta=delta) def assertEqual(self, test_value, expected, msg=None, digits=None, places=None, delta=None): self.assertAlmostEqual(test_value, expected, msg=msg, digits=digits, places=places, delta=delta) def assertRaises(self, problem=None, function=None, *args, **kwargs): if function is None: return RaiseContext(self, problem=problem or Exception) with RaiseContext(self, problem=problem): function(*args, **kwargs) class RaiseContext(object): def __init__(self, this, problem=Exception): self.this = this self.problem = problem def __enter__(self): pass def __exit__(self, exc_type, exc_val, exc_tb): if not exc_val: Log.error("Expecting an error") f = Except.wrap(exc_val) if isinstance(self.problem, (list, tuple)): problems = self.problem else: problems = [self.problem] causes = [] for problem in problems: if isinstance(problem, object.__class__) and issubclass(problem, BaseException) and isinstance(exc_val, problem): return True try: self.this.assertIn(problem, f) return True except Exception as cause: causes.append(cause) Log.error("problem is not raised", cause=first(causes)) def assertAlmostEqual(test, expected, digits=None, places=None, msg=None, delta=None): show_detail = True test = unwrap(test) expected = unwrap(expected) try: if test is None and (is_null_op(expected) or expected is None): return elif test is expected: return elif is_text(expected): assertAlmostEqualValue(test, expected, msg=msg, digits=digits, places=places, delta=delta) elif isinstance(test, UniqueIndex): if test ^ expected: Log.error("Sets do not match") elif is_data(expected) and is_data(test): for k, e in unwrap(expected).items(): t = test.get(k) assertAlmostEqual(t, e, msg=coalesce(msg, "")+"key "+quote(k)+": ", digits=digits, places=places, delta=delta) elif is_data(expected): if is_many(test): test = list(test) if len(test) != 1: Log.error("Expecting data, not a list") test = test[0] for k, e in expected.items(): try: t = test[k] assertAlmostEqual(t, e, msg=msg, digits=digits, places=places, delta=delta) continue except: pass t = mo_dots.get_attr(test, literal_field(k)) assertAlmostEqual(t, e, msg=msg, digits=digits, places=places, delta=delta) elif is_container(test) and isinstance(expected, set): test = set(to_data(t) for t in test) if len(test) != len(expected): Log.error( "Sets do not match, element count different:\n{{test|json|indent}}\nexpecting{{expectedtest|json|indent}}", test=test, expected=expected ) try: return len(set(test)|expected) == len(expected) except: for e in expected: for t in test: try: assertAlmostEqual(t, e, msg=msg, digits=digits, places=places, delta=delta) break except Exception as _: pass else: Log.error("Sets do not match. {{value|json}} not found in {{test|json}}", value=e, test=test) elif isinstance(expected, types.FunctionType): return expected(test) elif hasattr(test, "__iter__") and hasattr(expected, "__iter__"): if test.__class__.__name__ == "ndarray": # numpy test = test.tolist() elif test.__class__.__name__ == "DataFrame": # pandas test = test[test.columns[0]].values.tolist() elif test.__class__.__name__ == "Series": # pandas test = test.values.tolist() if not expected and test == None: return if expected == None: expected = [] # REPRESENT NOTHING for t, e in zip_longest(test, expected): assertAlmostEqual(t, e, msg=msg, digits=digits, places=places, delta=delta) else: assertAlmostEqualValue(test, expected, msg=msg, digits=digits, places=places, delta=delta) except Exception as cause: Log.error( "{{test|json|limit(10000)}} does not match expected {{expected|json|limit(10000)}}", test=test if show_detail else "[can not show]", expected=expected if show_detail else "[can not show]", cause=cause ) def assertAlmostEqualValue(test, expected, digits=None, places=None, msg=None, delta=None): """ Snagged from unittest/case.py, then modified (Aug2014) """ if is_null_op(expected): if test == None: # pandas dataframes reject any comparision with an exception! return else: raise AssertionError(expand_template("{{test|json}} != NULL", locals())) if expected == None: # None has no expectations return if test == expected: # shortcut return if isinstance(expected, (dates.Date, datetime.datetime, datetime.date)): return assertAlmostEqualValue( dates.Date(test).unix, dates.Date(expected).unix, msg=msg, digits=digits, places=places, delta=delta ) if not is_number(expected): # SOME SPECIAL CASES, EXPECTING EMPTY CONTAINERS IS THE SAME AS EXPECTING NULL if is_list(expected) and len(expected) == 0 and test == None: return if is_data(expected) and not expected.keys() and test == None: return if test != expected: raise AssertionError(expand_template("{{test|json}} != {{expected|json}}", locals())) return elif not is_number(test): try: # ASSUME IT IS A UTC DATE test = dates.parse(test).unix except Exception as e: raise AssertionError(expand_template("{{test|json}} != {{expected}}", locals())) num_param = 0 if digits != None: num_param += 1 if places != None: num_param += 1 if delta != None: num_param += 1 if num_param > 1: raise TypeError("specify only one of digits, places or delta") if digits is not None: with suppress_exception: diff = log10(abs(test-expected)) if diff < digits: return standardMsg = expand_template("{{test|json}} != {{expected|json}} within {{digits}} decimal places", locals()) elif delta is not None: if abs(test - expected) <= delta: return standardMsg = expand_template("{{test|json}} != {{expected|json}} within {{delta}} delta", locals()) else: if places is None: places = 15 with suppress_exception: diff = mo_math.log10(abs(test-expected)) if diff == None: return # Exactly the same if diff < mo_math.ceiling(mo_math.log10(abs(test)))-places: return standardMsg = expand_template("{{test|json}} != {{expected|json}} within {{places}} places", locals()) raise AssertionError(coalesce(msg, "") + ": (" + standardMsg + ")") def is_null_op(v): return v.__class__.__name__ == "NullOp"
mpl-2.0
tbullmann/heuhaufen
publication/generators_and_depth/aggregate.py
1
5320
import os import pandas import numpy as np from bokeh.palettes import Viridis4 as palette from bokeh.layouts import layout, column, row from bokeh.plotting import figure, output_file, show, ColumnDataSource from bokeh.models import HoverTool, Div, DataTable, TableColumn, NumberFormatter, LinearAxis, Select, CustomJS, Slider, Button import json # must be imported after bokeh def main(test_path='temp/publication/how_deep/test'): labels = ['membranes', 'synapses', 'mitochondria'] # concatenate the evaluation and parameters for all runs dfs = [] for label in labels: for run in range(1,21): df = read_run_from_json_and_csv(test_path, run, label) dfs.append(df) data = pandas.concat(dfs) # save aggregated data (in long format) data.to_csv(os.path.join(test_path, 'summary_long.csv')) # convert long to wide: label x metric --> label_metric metrics = data.columns.to_series().groupby(data.dtypes).groups[np.dtype('float64')] data2 = data.pivot_table(index=['generator', 'layers', 'sample'], columns='label', values=metrics) data2.columns = ['{}_{}'.format(x, y) for x, y in zip(data2.columns.get_level_values(1), data2.columns.get_level_values(0))] data2 = data2.reset_index() # save aggregated data (in wide format) data2.to_csv(os.path.join(test_path, 'summary_wide.csv')) # TODO: interactive plot with bokeh # bokeh_plot(data2, test_path) # not fully functional, e.g. cannot change label and metric def read_run_from_json_and_csv(test_path, run, label): # path to the test result for a particular model base_path = os.path.join(test_path, '%d' % run) # getting parameters from the options json file with open(os.path.join(base_path, "options.json")) as f: options = dict(json.loads(f.read()).items()) generator = options['generator'] # calculate the number of layers depending on generator network and its specific parameters if generator == 'unet': layers = options['u_depth'] * 2 # 1 for down sampling and 1 for up sampling at each level elif generator == 'densenet': layers = options['n_dense_blocks'] * options['n_dense_layers'] + 6 # 3 for each encoder and decoder elif generator == 'resnet': layers = options['n_res_blocks'] * 2 + 6 # 2 for transformation, 3 for each encoder and decoder elif generator == 'highwaynet': layers = options['n_highway_units'] * 2 + 6 # 2 for transformation, 3 for each encoder and decoder # read evaluation results df = pandas.read_csv(os.path.join(base_path, 'evaluation/%s.csv' % label)) # no index_col # add parameters df['generator'] = generator df['layers'] = layers df['label'] = label df['run'] = run return df def bokeh_plot(data, test_path): networks = ['unet', 'resnet', 'highwaynet', 'densenet'] # assuming all float values are metrics metrics = data.columns.to_series().groupby(data.dtypes).groups[np.dtype('float64')] # calculate mean for each data_mean = data.groupby(['generator', 'layers'])[metrics].mean().reset_index() source = dict() source_mean = dict() for network in networks: source[network] = ColumnDataSource(data[data.generator == network]) source_mean[network] = ColumnDataSource(data_mean[data_mean.generator == network]) output_file(os.path.join(test_path, "select.html")) description = Div(text=""" <h1>Evaluation of network type and depth for generator</h1> <p> Interact with the widgets to select metric and evaluated label. </p> """, width=1000) fig = figure(plot_width=1000, plot_height=1000, tools=['box_select', 'reset']) fig.xaxis.axis_label = "layers" fig.yaxis.axis_label = "value of metric" plots = [] for network, column_color in zip(networks, palette): plot = fig.line('layers', metrics[0], legend=dict(value=network), color=column_color, source=source_mean[network]) plot = fig.scatter('layers', metrics[0], legend=dict(value=network), color=column_color, source=source[network]) # legend which can hide/select a specific metric fig.legend.location = "bottom_right" fig.legend.click_policy = "hide" choices = metrics axis = 'y' axis_callback_code = """ plot.glyph.{axis}.field = cb_obj.value axis.attributes.axis_label = cb_obj.value; axis.trigger('change'); source.change.emit(); """ if axis == 'x': fig.xaxis.visible = None position = 'below' initial_choice = 0 else: fig.yaxis.visible = None position = 'left' initial_choice = 1 linear_axis = LinearAxis(axis_label=choices[initial_choice]) fig.add_layout(linear_axis, position) callback1 = CustomJS(args=dict(source=source[network], axis=linear_axis, plot=plot), code=axis_callback_code.format(axis=axis)) ticker = Select(value=choices[initial_choice], options=choices, title=axis + '-axis') ticker.js_on_change('value', callback1) l = layout([ [description], [ticker], [fig] ], sizing_mode='fixed') show(l) if __name__ == "__main__": main() else: main()
mit
attia42/twitter_word2vec
kmeans/experimentm.py
1
3559
import csv import nltk from nltk.tokenize import word_tokenize import string from nltk import pos_tag from gensim.models.word2vec import Word2Vec from gensim import matutils from numpy import array, float32 as REAL from sklearn.cluster import MiniBatchKMeans, KMeans from multiprocessing import Pool from collections import Counter #string.punctuation #string.digits file = 'training.1600000.processed.noemoticon2.csv' #file = 'testdata.manual.2009.06.14.csv' tags = ["NNP", "NN", "NNS"] ncls = 1000 niters = 1000 nreplay_kmeans = 1 lower = False redundant = ["aw", "aww", "awww", "awwww", "haha", "lol", "wow", "wtf", "xd", "yay", "http", "www", "com", "ah", "ahh", "ahhh", "amp"] def preprocess(tweet): ret_tweet = "" i = -1 nn = [] raw_tweet = tweet for ch in string.punctuation.replace("'","") + string.digits: tweet = tweet.replace(ch, " ") tweet_pos = {} if lower: tweet = tweet.lower() try: toks = word_tokenize(tweet) pos = pos_tag(toks) nn = [p for p in pos if p[1] in tags] #nn = [p for p in pos if p == 'NNP'] except: pass if(len(nn)): tweet_pos["NN"] = nn ret_tweet = tweet_pos return ret_tweet raw = [] with open(file, 'rb') as csvfile: content = csv.reader(csvfile, delimiter=',', quotechar='"') for row in content: tweet = row[5] raw.append(tweet) p = Pool(6) tweets = p.map(preprocess, raw) t1 = [] t2 = [] for i in range(len(tweets)): if len(tweets[i]): t1.append(raw[i]) t2.append(tweets[i]) raw = t1 tweets = t2 print "Loading model..." wv = Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True) vectors = [] for i in range(len(tweets)): tweet = tweets[i] nns = tweet['NN'] vector = [] #print nns mean = [] no_wv_tweet = True for w in nns: if len(w[0]) > 1 and w[0] in wv and w[0].lower() not in redundant: no_wv_tweet = False #print w[0] weight = 1 if w[1] == 'NNP': weight = 100 mean.append(weight * wv[w[0]]) if(len(mean)): vectors.append(matutils.unitvec(array(mean).mean(axis=0)).astype(REAL)) else: vectors.append([]) t1 = [] t2 = [] t3 = [] for i in range(len(vectors)): if vectors[i] != None and len(vectors[i]): t1.append(raw[i]) t2.append(tweets[i]) t3.append(vectors[i]) raw = t1 tweets = t2 vectors = t3 #kmeans = KMeans(init='k-means++', n_clusters=ncls, n_init=1) kmeans = MiniBatchKMeans(init='k-means++', n_clusters=ncls, n_init=nreplay_kmeans, max_iter=niters) kmeans.fit(vectors) clss = kmeans.predict(vectors) clusters = [[] for i in range(ncls)] for i in range(len(vectors)): cls = clss[i] clusters[cls].append(i) clusterstags = [[] for i in range(ncls)] countarr = [] for c in clusters: counts = Counter() for i in c: t = [x[0] for x in tweets[i]["NN"] ]#if x[1] == "NNP"] #tn = [x[1] for x in tweets[i]["NN"]] sentence = " ".join(t) #+ tn) counts.update(word.strip('.,?!"\'').lower() for word in sentence.split()) countarr.append(counts) output = "" for i in range(ncls): output = "Most common words for this cluster:\n" output += str(countarr[i].most_common(12)) output += "\n\n\n\n\n\n" output += "Word2vec space of related words:\n" wv_rel = wv.most_similar([kmeans.cluster_centers_[i]], topn=10) output += str(wv_rel) output += "\n\n\n\n\n\n" for t in clusters[i]: output += str(raw[t]) + "\n" #output += "\n\n\n" nm = [x[0] for x in countarr[i].most_common(5)] nm = str(" ".join(nm)) for ch in string.punctuation: nm = nm.replace(ch, " ") f = open('clusters/' + nm +'.txt', 'wb') f.write(output) f.close()
mit
mtconley/turntable
test/lib/python2.7/site-packages/scipy/interpolate/fitpack2.py
7
57978
""" fitpack --- curve and surface fitting with splines fitpack is based on a collection of Fortran routines DIERCKX by P. Dierckx (see http://www.netlib.org/dierckx/) transformed to double routines by Pearu Peterson. """ # Created by Pearu Peterson, June,August 2003 from __future__ import division, print_function, absolute_import __all__ = [ 'UnivariateSpline', 'InterpolatedUnivariateSpline', 'LSQUnivariateSpline', 'BivariateSpline', 'LSQBivariateSpline', 'SmoothBivariateSpline', 'LSQSphereBivariateSpline', 'SmoothSphereBivariateSpline', 'RectBivariateSpline', 'RectSphereBivariateSpline'] import warnings from numpy import zeros, concatenate, alltrue, ravel, all, diff, array, ones import numpy as np from . import fitpack from . import dfitpack ################ Univariate spline #################### _curfit_messages = {1:""" The required storage space exceeds the available storage space, as specified by the parameter nest: nest too small. If nest is already large (say nest > m/2), it may also indicate that s is too small. The approximation returned is the weighted least-squares spline according to the knots t[0],t[1],...,t[n-1]. (n=nest) the parameter fp gives the corresponding weighted sum of squared residuals (fp>s). """, 2:""" A theoretically impossible result was found during the iteration proces for finding a smoothing spline with fp = s: s too small. There is an approximation returned but the corresponding weighted sum of squared residuals does not satisfy the condition abs(fp-s)/s < tol.""", 3:""" The maximal number of iterations maxit (set to 20 by the program) allowed for finding a smoothing spline with fp=s has been reached: s too small. There is an approximation returned but the corresponding weighted sum of squared residuals does not satisfy the condition abs(fp-s)/s < tol.""", 10:""" Error on entry, no approximation returned. The following conditions must hold: xb<=x[0]<x[1]<...<x[m-1]<=xe, w[i]>0, i=0..m-1 if iopt=-1: xb<t[k+1]<t[k+2]<...<t[n-k-2]<xe""" } # UnivariateSpline, ext parameter can be an int or a string _extrap_modes = {0: 0, 'extrapolate': 0, 1: 1, 'zeros': 1, 2: 2, 'raise': 2, 3: 3, 'const': 3} class UnivariateSpline(object): """ One-dimensional smoothing spline fit to a given set of data points. Fits a spline y = spl(x) of degree `k` to the provided `x`, `y` data. `s` specifies the number of knots by specifying a smoothing condition. Parameters ---------- x : (N,) array_like 1-D array of independent input data. Must be increasing. y : (N,) array_like 1-D array of dependent input data, of the same length as `x`. w : (N,) array_like, optional Weights for spline fitting. Must be positive. If None (default), weights are all equal. bbox : (2,) array_like, optional 2-sequence specifying the boundary of the approximation interval. If None (default), ``bbox=[x[0], x[-1]]``. k : int, optional Degree of the smoothing spline. Must be <= 5. Default is k=3, a cubic spline. s : float or None, optional Positive smoothing factor used to choose the number of knots. Number of knots will be increased until the smoothing condition is satisfied:: sum((w[i] * (y[i]-spl(x[i])))**2, axis=0) <= s If None (default), ``s = len(w)`` which should be a good value if ``1/w[i]`` is an estimate of the standard deviation of ``y[i]``. If 0, spline will interpolate through all data points. ext : int or str, optional Controls the extrapolation mode for elements not in the interval defined by the knot sequence. * if ext=0 or 'extrapolate', return the extrapolated value. * if ext=1 or 'zeros', return 0 * if ext=2 or 'raise', raise a ValueError * if ext=3 of 'const', return the boundary value. The default value is 0. See Also -------- InterpolatedUnivariateSpline : Subclass with smoothing forced to 0 LSQUnivariateSpline : Subclass in which knots are user-selected instead of being set by smoothing condition splrep : An older, non object-oriented wrapping of FITPACK splev, sproot, splint, spalde BivariateSpline : A similar class for two-dimensional spline interpolation Notes ----- The number of data points must be larger than the spline degree `k`. **NaN handling**: If the input arrays contain ``nan`` values, the result is not useful, since the underlying spline fitting routines cannot deal with ``nan`` . A workaround is to use zero weights for not-a-number data points: >>> w = np.isnan(y) >>> y[w] = 0. >>> spl = UnivariateSpline(x, y, w=~w) Notice the need to replace a ``nan`` by a numerical value (precise value does not matter as long as the corresponding weight is zero.) Examples -------- >>> import matplotlib.pyplot as plt >>> from scipy.interpolate import UnivariateSpline >>> x = np.linspace(-3, 3, 50) >>> y = np.exp(-x**2) + 0.1 * np.random.randn(50) >>> plt.plot(x, y, 'ro', ms=5) Use the default value for the smoothing parameter: >>> spl = UnivariateSpline(x, y) >>> xs = np.linspace(-3, 3, 1000) >>> plt.plot(xs, spl(xs), 'g', lw=3) Manually change the amount of smoothing: >>> spl.set_smoothing_factor(0.5) >>> plt.plot(xs, spl(xs), 'b', lw=3) >>> plt.show() """ def __init__(self, x, y, w=None, bbox=[None]*2, k=3, s=None, ext=0): # _data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier try: self.ext = _extrap_modes[ext] except KeyError: raise ValueError("Unknown extrapolation mode %s." % ext) data = dfitpack.fpcurf0(x,y,k,w=w, xb=bbox[0],xe=bbox[1],s=s) if data[-1] == 1: # nest too small, setting to maximum bound data = self._reset_nest(data) self._data = data self._reset_class() @classmethod def _from_tck(cls, tck, ext=0): """Construct a spline object from given tck""" self = cls.__new__(cls) t, c, k = tck self._eval_args = tck #_data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier self._data = (None,None,None,None,None,k,None,len(t),t, c,None,None,None,None) self.ext = ext return self def _reset_class(self): data = self._data n,t,c,k,ier = data[7],data[8],data[9],data[5],data[-1] self._eval_args = t[:n],c[:n],k if ier == 0: # the spline returned has a residual sum of squares fp # such that abs(fp-s)/s <= tol with tol a relative # tolerance set to 0.001 by the program pass elif ier == -1: # the spline returned is an interpolating spline self._set_class(InterpolatedUnivariateSpline) elif ier == -2: # the spline returned is the weighted least-squares # polynomial of degree k. In this extreme case fp gives # the upper bound fp0 for the smoothing factor s. self._set_class(LSQUnivariateSpline) else: # error if ier == 1: self._set_class(LSQUnivariateSpline) message = _curfit_messages.get(ier,'ier=%s' % (ier)) warnings.warn(message) def _set_class(self, cls): self._spline_class = cls if self.__class__ in (UnivariateSpline, InterpolatedUnivariateSpline, LSQUnivariateSpline): self.__class__ = cls else: # It's an unknown subclass -- don't change class. cf. #731 pass def _reset_nest(self, data, nest=None): n = data[10] if nest is None: k,m = data[5],len(data[0]) nest = m+k+1 # this is the maximum bound for nest else: if not n <= nest: raise ValueError("`nest` can only be increased") t, c, fpint, nrdata = [np.resize(data[j], nest) for j in [8,9,11,12]] args = data[:8] + (t,c,n,fpint,nrdata,data[13]) data = dfitpack.fpcurf1(*args) return data def set_smoothing_factor(self, s): """ Continue spline computation with the given smoothing factor s and with the knots found at the last call. This routine modifies the spline in place. """ data = self._data if data[6] == -1: warnings.warn('smoothing factor unchanged for' 'LSQ spline with fixed knots') return args = data[:6] + (s,) + data[7:] data = dfitpack.fpcurf1(*args) if data[-1] == 1: # nest too small, setting to maximum bound data = self._reset_nest(data) self._data = data self._reset_class() def __call__(self, x, nu=0, ext=None): """ Evaluate spline (or its nu-th derivative) at positions x. Parameters ---------- x : array_like A 1-D array of points at which to return the value of the smoothed spline or its derivatives. Note: x can be unordered but the evaluation is more efficient if x is (partially) ordered. nu : int The order of derivative of the spline to compute. ext : int Controls the value returned for elements of ``x`` not in the interval defined by the knot sequence. * if ext=0 or 'extrapolate', return the extrapolated value. * if ext=1 or 'zeros', return 0 * if ext=2 or 'raise', raise a ValueError * if ext=3 or 'const', return the boundary value. The default value is 0, passed from the initialization of UnivariateSpline. """ x = np.asarray(x) # empty input yields empty output if x.size == 0: return array([]) # if nu is None: # return dfitpack.splev(*(self._eval_args+(x,))) # return dfitpack.splder(nu=nu,*(self._eval_args+(x,))) if ext is None: ext = self.ext else: try: ext = _extrap_modes[ext] except KeyError: raise ValueError("Unknown extrapolation mode %s." % ext) return fitpack.splev(x, self._eval_args, der=nu, ext=ext) def get_knots(self): """ Return positions of (boundary and interior) knots of the spline. """ data = self._data k,n = data[5],data[7] return data[8][k:n-k] def get_coeffs(self): """Return spline coefficients.""" data = self._data k,n = data[5],data[7] return data[9][:n-k-1] def get_residual(self): """Return weighted sum of squared residuals of the spline approximation: ``sum((w[i] * (y[i]-spl(x[i])))**2, axis=0)``. """ return self._data[10] def integral(self, a, b): """ Return definite integral of the spline between two given points. """ return dfitpack.splint(*(self._eval_args+(a,b))) def derivatives(self, x): """ Return all derivatives of the spline at the point x.""" d,ier = dfitpack.spalde(*(self._eval_args+(x,))) if not ier == 0: raise ValueError("Error code returned by spalde: %s" % ier) return d def roots(self): """ Return the zeros of the spline. Restriction: only cubic splines are supported by fitpack. """ k = self._data[5] if k == 3: z,m,ier = dfitpack.sproot(*self._eval_args[:2]) if not ier == 0: raise ValueError("Error code returned by spalde: %s" % ier) return z[:m] raise NotImplementedError('finding roots unsupported for ' 'non-cubic splines') def derivative(self, n=1): """ Construct a new spline representing the derivative of this spline. Parameters ---------- n : int, optional Order of derivative to evaluate. Default: 1 Returns ------- spline : UnivariateSpline Spline of order k2=k-n representing the derivative of this spline. See Also -------- splder, antiderivative Notes ----- .. versionadded:: 0.13.0 Examples -------- This can be used for finding maxima of a curve: >>> from scipy.interpolate import UnivariateSpline >>> x = np.linspace(0, 10, 70) >>> y = np.sin(x) >>> spl = UnivariateSpline(x, y, k=4, s=0) Now, differentiate the spline and find the zeros of the derivative. (NB: `sproot` only works for order 3 splines, so we fit an order 4 spline): >>> spl.derivative().roots() / np.pi array([ 0.50000001, 1.5 , 2.49999998]) This agrees well with roots :math:`\pi/2 + n\pi` of `cos(x) = sin'(x)`. """ tck = fitpack.splder(self._eval_args, n) return UnivariateSpline._from_tck(tck, self.ext) def antiderivative(self, n=1): """ Construct a new spline representing the antiderivative of this spline. Parameters ---------- n : int, optional Order of antiderivative to evaluate. Default: 1 Returns ------- spline : UnivariateSpline Spline of order k2=k+n representing the antiderivative of this spline. Notes ----- .. versionadded:: 0.13.0 See Also -------- splantider, derivative Examples -------- >>> from scipy.interpolate import UnivariateSpline >>> x = np.linspace(0, np.pi/2, 70) >>> y = 1 / np.sqrt(1 - 0.8*np.sin(x)**2) >>> spl = UnivariateSpline(x, y, s=0) The derivative is the inverse operation of the antiderivative, although some floating point error accumulates: >>> spl(1.7), spl.antiderivative().derivative()(1.7) (array(2.1565429877197317), array(2.1565429877201865)) Antiderivative can be used to evaluate definite integrals: >>> ispl = spl.antiderivative() >>> ispl(np.pi/2) - ispl(0) 2.2572053588768486 This is indeed an approximation to the complete elliptic integral :math:`K(m) = \\int_0^{\\pi/2} [1 - m\\sin^2 x]^{-1/2} dx`: >>> from scipy.special import ellipk >>> ellipk(0.8) 2.2572053268208538 """ tck = fitpack.splantider(self._eval_args, n) return UnivariateSpline._from_tck(tck, self.ext) class InterpolatedUnivariateSpline(UnivariateSpline): """ One-dimensional interpolating spline for a given set of data points. Fits a spline y = spl(x) of degree `k` to the provided `x`, `y` data. Spline function passes through all provided points. Equivalent to `UnivariateSpline` with s=0. Parameters ---------- x : (N,) array_like Input dimension of data points -- must be increasing y : (N,) array_like input dimension of data points w : (N,) array_like, optional Weights for spline fitting. Must be positive. If None (default), weights are all equal. bbox : (2,) array_like, optional 2-sequence specifying the boundary of the approximation interval. If None (default), ``bbox=[x[0], x[-1]]``. k : int, optional Degree of the smoothing spline. Must be 1 <= `k` <= 5. ext : int or str, optional Controls the extrapolation mode for elements not in the interval defined by the knot sequence. * if ext=0 or 'extrapolate', return the extrapolated value. * if ext=1 or 'zeros', return 0 * if ext=2 or 'raise', raise a ValueError * if ext=3 of 'const', return the boundary value. The default value is 0. See Also -------- UnivariateSpline : Superclass -- allows knots to be selected by a smoothing condition LSQUnivariateSpline : spline for which knots are user-selected splrep : An older, non object-oriented wrapping of FITPACK splev, sproot, splint, spalde BivariateSpline : A similar class for two-dimensional spline interpolation Notes ----- The number of data points must be larger than the spline degree `k`. Examples -------- >>> import matplotlib.pyplot as plt >>> from scipy.interpolate import InterpolatedUnivariateSpline >>> x = np.linspace(-3, 3, 50) >>> y = np.exp(-x**2) + 0.1 * np.random.randn(50) >>> spl = InterpolatedUnivariateSpline(x, y) >>> plt.plot(x, y, 'ro', ms=5) >>> xs = np.linspace(-3, 3, 1000) >>> plt.plot(xs, spl(xs), 'g', lw=3, alpha=0.7) >>> plt.show() Notice that the ``spl(x)`` interpolates `y`: >>> spl.get_residual() 0.0 """ def __init__(self, x, y, w=None, bbox=[None]*2, k=3, ext=0): # _data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier self._data = dfitpack.fpcurf0(x,y,k,w=w, xb=bbox[0],xe=bbox[1],s=0) self._reset_class() try: self.ext = _extrap_modes[ext] except KeyError: raise ValueError("Unknown extrapolation mode %s." % ext) _fpchec_error_string = """The input parameters have been rejected by fpchec. \ This means that at least one of the following conditions is violated: 1) k+1 <= n-k-1 <= m 2) t(1) <= t(2) <= ... <= t(k+1) t(n-k) <= t(n-k+1) <= ... <= t(n) 3) t(k+1) < t(k+2) < ... < t(n-k) 4) t(k+1) <= x(i) <= t(n-k) 5) The conditions specified by Schoenberg and Whitney must hold for at least one subset of data points, i.e., there must be a subset of data points y(j) such that t(j) < y(j) < t(j+k+1), j=1,2,...,n-k-1 """ class LSQUnivariateSpline(UnivariateSpline): """ One-dimensional spline with explicit internal knots. Fits a spline y = spl(x) of degree `k` to the provided `x`, `y` data. `t` specifies the internal knots of the spline Parameters ---------- x : (N,) array_like Input dimension of data points -- must be increasing y : (N,) array_like Input dimension of data points t : (M,) array_like interior knots of the spline. Must be in ascending order and:: bbox[0] < t[0] < ... < t[-1] < bbox[-1] w : (N,) array_like, optional weights for spline fitting. Must be positive. If None (default), weights are all equal. bbox : (2,) array_like, optional 2-sequence specifying the boundary of the approximation interval. If None (default), ``bbox = [x[0], x[-1]]``. k : int, optional Degree of the smoothing spline. Must be 1 <= `k` <= 5. Default is k=3, a cubic spline. ext : int or str, optional Controls the extrapolation mode for elements not in the interval defined by the knot sequence. * if ext=0 or 'extrapolate', return the extrapolated value. * if ext=1 or 'zeros', return 0 * if ext=2 or 'raise', raise a ValueError * if ext=3 of 'const', return the boundary value. The default value is 0. Raises ------ ValueError If the interior knots do not satisfy the Schoenberg-Whitney conditions See Also -------- UnivariateSpline : Superclass -- knots are specified by setting a smoothing condition InterpolatedUnivariateSpline : spline passing through all points splrep : An older, non object-oriented wrapping of FITPACK splev, sproot, splint, spalde BivariateSpline : A similar class for two-dimensional spline interpolation Notes ----- The number of data points must be larger than the spline degree `k`. Knots `t` must satisfy the Schoenberg-Whitney conditions, i.e., there must be a subset of data points ``x[j]`` such that ``t[j] < x[j] < t[j+k+1]``, for ``j=0, 1,...,n-k-2``. Examples -------- >>> from scipy.interpolate import LSQUnivariateSpline >>> import matplotlib.pyplot as plt >>> x = np.linspace(-3, 3, 50) >>> y = np.exp(-x**2) + 0.1 * np.random.randn(50) Fit a smoothing spline with a pre-defined internal knots: >>> t = [-1, 0, 1] >>> spl = LSQUnivariateSpline(x, y, t) >>> xs = np.linspace(-3, 3, 1000) >>> plt.plot(x, y, 'ro', ms=5) >>> plt.plot(xs, spl(xs), 'g-', lw=3) >>> plt.show() Check the knot vector: >>> spl.get_knots() array([-3., -1., 0., 1., 3.]) """ def __init__(self, x, y, t, w=None, bbox=[None]*2, k=3, ext=0): # _data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier xb = bbox[0] xe = bbox[1] if xb is None: xb = x[0] if xe is None: xe = x[-1] t = concatenate(([xb]*(k+1), t, [xe]*(k+1))) n = len(t) if not alltrue(t[k+1:n-k]-t[k:n-k-1] > 0, axis=0): raise ValueError('Interior knots t must satisfy ' 'Schoenberg-Whitney conditions') if not dfitpack.fpchec(x, t, k) == 0: raise ValueError(_fpchec_error_string) data = dfitpack.fpcurfm1(x, y, k, t, w=w, xb=xb, xe=xe) self._data = data[:-3] + (None, None, data[-1]) self._reset_class() try: self.ext = _extrap_modes[ext] except KeyError: raise ValueError("Unknown extrapolation mode %s." % ext) ################ Bivariate spline #################### class _BivariateSplineBase(object): """ Base class for Bivariate spline s(x,y) interpolation on the rectangle [xb,xe] x [yb, ye] calculated from a given set of data points (x,y,z). See Also -------- bisplrep, bisplev : an older wrapping of FITPACK BivariateSpline : implementation of bivariate spline interpolation on a plane grid SphereBivariateSpline : implementation of bivariate spline interpolation on a spherical grid """ def get_residual(self): """ Return weighted sum of squared residuals of the spline approximation: sum ((w[i]*(z[i]-s(x[i],y[i])))**2,axis=0) """ return self.fp def get_knots(self): """ Return a tuple (tx,ty) where tx,ty contain knots positions of the spline with respect to x-, y-variable, respectively. The position of interior and additional knots are given as t[k+1:-k-1] and t[:k+1]=b, t[-k-1:]=e, respectively. """ return self.tck[:2] def get_coeffs(self): """ Return spline coefficients.""" return self.tck[2] def __call__(self, x, y, mth=None, dx=0, dy=0, grid=True): """ Evaluate the spline or its derivatives at given positions. Parameters ---------- x, y : array-like Input coordinates. If `grid` is False, evaluate the spline at points ``(x[i], y[i]), i=0, ..., len(x)-1``. Standard Numpy broadcasting is obeyed. If `grid` is True: evaluate spline at the grid points defined by the coordinate arrays x, y. The arrays must be sorted to increasing order. dx : int Order of x-derivative .. versionadded:: 0.14.0 dy : int Order of y-derivative .. versionadded:: 0.14.0 grid : bool Whether to evaluate the results on a grid spanned by the input arrays, or at points specified by the input arrays. .. versionadded:: 0.14.0 mth : str Deprecated argument. Has no effect. """ x = np.asarray(x) y = np.asarray(y) if mth is not None: warnings.warn("The `mth` argument is deprecated and will be removed", FutureWarning) tx, ty, c = self.tck[:3] kx, ky = self.degrees if grid: if x.size == 0 or y.size == 0: return np.zeros((x.size, y.size), dtype=self.tck[2].dtype) if dx or dy: z,ier = dfitpack.parder(tx,ty,c,kx,ky,dx,dy,x,y) if not ier == 0: raise ValueError("Error code returned by parder: %s" % ier) else: z,ier = dfitpack.bispev(tx,ty,c,kx,ky,x,y) if not ier == 0: raise ValueError("Error code returned by bispev: %s" % ier) else: # standard Numpy broadcasting if x.shape != y.shape: x, y = np.broadcast_arrays(x, y) shape = x.shape x = x.ravel() y = y.ravel() if x.size == 0 or y.size == 0: return np.zeros(shape, dtype=self.tck[2].dtype) if dx or dy: z,ier = dfitpack.pardeu(tx,ty,c,kx,ky,dx,dy,x,y) if not ier == 0: raise ValueError("Error code returned by pardeu: %s" % ier) else: z,ier = dfitpack.bispeu(tx,ty,c,kx,ky,x,y) if not ier == 0: raise ValueError("Error code returned by bispeu: %s" % ier) z = z.reshape(shape) return z _surfit_messages = {1:""" The required storage space exceeds the available storage space: nxest or nyest too small, or s too small. The weighted least-squares spline corresponds to the current set of knots.""", 2:""" A theoretically impossible result was found during the iteration process for finding a smoothing spline with fp = s: s too small or badly chosen eps. Weighted sum of squared residuals does not satisfy abs(fp-s)/s < tol.""", 3:""" the maximal number of iterations maxit (set to 20 by the program) allowed for finding a smoothing spline with fp=s has been reached: s too small. Weighted sum of squared residuals does not satisfy abs(fp-s)/s < tol.""", 4:""" No more knots can be added because the number of b-spline coefficients (nx-kx-1)*(ny-ky-1) already exceeds the number of data points m: either s or m too small. The weighted least-squares spline corresponds to the current set of knots.""", 5:""" No more knots can be added because the additional knot would (quasi) coincide with an old one: s too small or too large a weight to an inaccurate data point. The weighted least-squares spline corresponds to the current set of knots.""", 10:""" Error on entry, no approximation returned. The following conditions must hold: xb<=x[i]<=xe, yb<=y[i]<=ye, w[i]>0, i=0..m-1 If iopt==-1, then xb<tx[kx+1]<tx[kx+2]<...<tx[nx-kx-2]<xe yb<ty[ky+1]<ty[ky+2]<...<ty[ny-ky-2]<ye""", -3:""" The coefficients of the spline returned have been computed as the minimal norm least-squares solution of a (numerically) rank deficient system (deficiency=%i). If deficiency is large, the results may be inaccurate. Deficiency may strongly depend on the value of eps.""" } class BivariateSpline(_BivariateSplineBase): """ Base class for bivariate splines. This describes a spline ``s(x, y)`` of degrees ``kx`` and ``ky`` on the rectangle ``[xb, xe] * [yb, ye]`` calculated from a given set of data points ``(x, y, z)``. This class is meant to be subclassed, not instantiated directly. To construct these splines, call either `SmoothBivariateSpline` or `LSQBivariateSpline`. See Also -------- UnivariateSpline : a similar class for univariate spline interpolation SmoothBivariateSpline : to create a BivariateSpline through the given points LSQBivariateSpline : to create a BivariateSpline using weighted least-squares fitting SphereBivariateSpline : bivariate spline interpolation in spherical cooridinates bisplrep : older wrapping of FITPACK bisplev : older wrapping of FITPACK """ @classmethod def _from_tck(cls, tck): """Construct a spline object from given tck and degree""" self = cls.__new__(cls) if len(tck) != 5: raise ValueError("tck should be a 5 element tuple of tx, ty, c, kx, ky") self.tck = tck[:3] self.degrees = tck[3:] return self def ev(self, xi, yi, dx=0, dy=0): """ Evaluate the spline at points Returns the interpolated value at ``(xi[i], yi[i]), i=0,...,len(xi)-1``. Parameters ---------- xi, yi : array-like Input coordinates. Standard Numpy broadcasting is obeyed. dx : int Order of x-derivative .. versionadded:: 0.14.0 dy : int Order of y-derivative .. versionadded:: 0.14.0 """ return self.__call__(xi, yi, dx=dx, dy=dy, grid=False) def integral(self, xa, xb, ya, yb): """ Evaluate the integral of the spline over area [xa,xb] x [ya,yb]. Parameters ---------- xa, xb : float The end-points of the x integration interval. ya, yb : float The end-points of the y integration interval. Returns ------- integ : float The value of the resulting integral. """ tx,ty,c = self.tck[:3] kx,ky = self.degrees return dfitpack.dblint(tx,ty,c,kx,ky,xa,xb,ya,yb) class SmoothBivariateSpline(BivariateSpline): """ Smooth bivariate spline approximation. Parameters ---------- x, y, z : array_like 1-D sequences of data points (order is not important). w : array_like, optional Positive 1-D sequence of weights, of same length as `x`, `y` and `z`. bbox : array_like, optional Sequence of length 4 specifying the boundary of the rectangular approximation domain. By default, ``bbox=[min(x,tx),max(x,tx), min(y,ty),max(y,ty)]``. kx, ky : ints, optional Degrees of the bivariate spline. Default is 3. s : float, optional Positive smoothing factor defined for estimation condition: ``sum((w[i]*(z[i]-s(x[i], y[i])))**2, axis=0) <= s`` Default ``s=len(w)`` which should be a good value if ``1/w[i]`` is an estimate of the standard deviation of ``z[i]``. eps : float, optional A threshold for determining the effective rank of an over-determined linear system of equations. `eps` should have a value between 0 and 1, the default is 1e-16. See Also -------- bisplrep : an older wrapping of FITPACK bisplev : an older wrapping of FITPACK UnivariateSpline : a similar class for univariate spline interpolation LSQUnivariateSpline : to create a BivariateSpline using weighted Notes ----- The length of `x`, `y` and `z` should be at least ``(kx+1) * (ky+1)``. """ def __init__(self, x, y, z, w=None, bbox=[None] * 4, kx=3, ky=3, s=None, eps=None): xb,xe,yb,ye = bbox nx,tx,ny,ty,c,fp,wrk1,ier = dfitpack.surfit_smth(x,y,z,w, xb,xe,yb,ye, kx,ky,s=s, eps=eps,lwrk2=1) if ier > 10: # lwrk2 was to small, re-run nx,tx,ny,ty,c,fp,wrk1,ier = dfitpack.surfit_smth(x,y,z,w, xb,xe,yb,ye, kx,ky,s=s, eps=eps,lwrk2=ier) if ier in [0,-1,-2]: # normal return pass else: message = _surfit_messages.get(ier,'ier=%s' % (ier)) warnings.warn(message) self.fp = fp self.tck = tx[:nx],ty[:ny],c[:(nx-kx-1)*(ny-ky-1)] self.degrees = kx,ky class LSQBivariateSpline(BivariateSpline): """ Weighted least-squares bivariate spline approximation. Parameters ---------- x, y, z : array_like 1-D sequences of data points (order is not important). tx, ty : array_like Strictly ordered 1-D sequences of knots coordinates. w : array_like, optional Positive 1-D array of weights, of the same length as `x`, `y` and `z`. bbox : (4,) array_like, optional Sequence of length 4 specifying the boundary of the rectangular approximation domain. By default, ``bbox=[min(x,tx),max(x,tx), min(y,ty),max(y,ty)]``. kx, ky : ints, optional Degrees of the bivariate spline. Default is 3. s : float, optional Positive smoothing factor defined for estimation condition: ``sum((w[i]*(z[i]-s(x[i], y[i])))**2, axis=0) <= s`` Default ``s=len(w)`` which should be a good value if ``1/w[i]`` is an estimate of the standard deviation of ``z[i]``. eps : float, optional A threshold for determining the effective rank of an over-determined linear system of equations. `eps` should have a value between 0 and 1, the default is 1e-16. See Also -------- bisplrep : an older wrapping of FITPACK bisplev : an older wrapping of FITPACK UnivariateSpline : a similar class for univariate spline interpolation SmoothBivariateSpline : create a smoothing BivariateSpline Notes ----- The length of `x`, `y` and `z` should be at least ``(kx+1) * (ky+1)``. """ def __init__(self, x, y, z, tx, ty, w=None, bbox=[None]*4, kx=3, ky=3, eps=None): nx = 2*kx+2+len(tx) ny = 2*ky+2+len(ty) tx1 = zeros((nx,),float) ty1 = zeros((ny,),float) tx1[kx+1:nx-kx-1] = tx ty1[ky+1:ny-ky-1] = ty xb,xe,yb,ye = bbox tx1,ty1,c,fp,ier = dfitpack.surfit_lsq(x,y,z,tx1,ty1,w, xb,xe,yb,ye, kx,ky,eps,lwrk2=1) if ier > 10: tx1,ty1,c,fp,ier = dfitpack.surfit_lsq(x,y,z,tx1,ty1,w, xb,xe,yb,ye, kx,ky,eps,lwrk2=ier) if ier in [0,-1,-2]: # normal return pass else: if ier < -2: deficiency = (nx-kx-1)*(ny-ky-1)+ier message = _surfit_messages.get(-3) % (deficiency) else: message = _surfit_messages.get(ier, 'ier=%s' % (ier)) warnings.warn(message) self.fp = fp self.tck = tx1, ty1, c self.degrees = kx, ky class RectBivariateSpline(BivariateSpline): """ Bivariate spline approximation over a rectangular mesh. Can be used for both smoothing and interpolating data. Parameters ---------- x,y : array_like 1-D arrays of coordinates in strictly ascending order. z : array_like 2-D array of data with shape (x.size,y.size). bbox : array_like, optional Sequence of length 4 specifying the boundary of the rectangular approximation domain. By default, ``bbox=[min(x,tx),max(x,tx), min(y,ty),max(y,ty)]``. kx, ky : ints, optional Degrees of the bivariate spline. Default is 3. s : float, optional Positive smoothing factor defined for estimation condition: ``sum((w[i]*(z[i]-s(x[i], y[i])))**2, axis=0) <= s`` Default is ``s=0``, which is for interpolation. See Also -------- SmoothBivariateSpline : a smoothing bivariate spline for scattered data bisplrep : an older wrapping of FITPACK bisplev : an older wrapping of FITPACK UnivariateSpline : a similar class for univariate spline interpolation """ def __init__(self, x, y, z, bbox=[None] * 4, kx=3, ky=3, s=0): x, y = ravel(x), ravel(y) if not all(diff(x) > 0.0): raise TypeError('x must be strictly increasing') if not all(diff(y) > 0.0): raise TypeError('y must be strictly increasing') if not ((x.min() == x[0]) and (x.max() == x[-1])): raise TypeError('x must be strictly ascending') if not ((y.min() == y[0]) and (y.max() == y[-1])): raise TypeError('y must be strictly ascending') if not x.size == z.shape[0]: raise TypeError('x dimension of z must have same number of ' 'elements as x') if not y.size == z.shape[1]: raise TypeError('y dimension of z must have same number of ' 'elements as y') z = ravel(z) xb, xe, yb, ye = bbox nx, tx, ny, ty, c, fp, ier = dfitpack.regrid_smth(x, y, z, xb, xe, yb, ye, kx, ky, s) if ier not in [0, -1, -2]: msg = _surfit_messages.get(ier, 'ier=%s' % (ier)) raise ValueError(msg) self.fp = fp self.tck = tx[:nx], ty[:ny], c[:(nx - kx - 1) * (ny - ky - 1)] self.degrees = kx, ky _spherefit_messages = _surfit_messages.copy() _spherefit_messages[10] = """ ERROR. On entry, the input data are controlled on validity. The following restrictions must be satisfied: -1<=iopt<=1, m>=2, ntest>=8 ,npest >=8, 0<eps<1, 0<=teta(i)<=pi, 0<=phi(i)<=2*pi, w(i)>0, i=1,...,m lwrk1 >= 185+52*v+10*u+14*u*v+8*(u-1)*v**2+8*m kwrk >= m+(ntest-7)*(npest-7) if iopt=-1: 8<=nt<=ntest , 9<=np<=npest 0<tt(5)<tt(6)<...<tt(nt-4)<pi 0<tp(5)<tp(6)<...<tp(np-4)<2*pi if iopt>=0: s>=0 if one of these conditions is found to be violated,control is immediately repassed to the calling program. in that case there is no approximation returned.""" _spherefit_messages[-3] = """ WARNING. The coefficients of the spline returned have been computed as the minimal norm least-squares solution of a (numerically) rank deficient system (deficiency=%i, rank=%i). Especially if the rank deficiency, which is computed by 6+(nt-8)*(np-7)+ier, is large, the results may be inaccurate. They could also seriously depend on the value of eps.""" class SphereBivariateSpline(_BivariateSplineBase): """ Bivariate spline s(x,y) of degrees 3 on a sphere, calculated from a given set of data points (theta,phi,r). .. versionadded:: 0.11.0 See Also -------- bisplrep, bisplev : an older wrapping of FITPACK UnivariateSpline : a similar class for univariate spline interpolation SmoothUnivariateSpline : to create a BivariateSpline through the given points LSQUnivariateSpline : to create a BivariateSpline using weighted least-squares fitting """ def __call__(self, theta, phi, dtheta=0, dphi=0, grid=True): """ Evaluate the spline or its derivatives at given positions. Parameters ---------- theta, phi : array-like Input coordinates. If `grid` is False, evaluate the spline at points ``(theta[i], phi[i]), i=0, ..., len(x)-1``. Standard Numpy broadcasting is obeyed. If `grid` is True: evaluate spline at the grid points defined by the coordinate arrays theta, phi. The arrays must be sorted to increasing order. dtheta : int Order of theta-derivative .. versionadded:: 0.14.0 dphi : int Order of phi-derivative .. versionadded:: 0.14.0 grid : bool Whether to evaluate the results on a grid spanned by the input arrays, or at points specified by the input arrays. .. versionadded:: 0.14.0 """ theta = np.asarray(theta) phi = np.asarray(phi) if theta.size > 0 and (theta.min() < 0. or theta.max() > np.pi): raise ValueError("requested theta out of bounds.") if phi.size > 0 and (phi.min() < 0. or phi.max() > 2. * np.pi): raise ValueError("requested phi out of bounds.") return _BivariateSplineBase.__call__(self, theta, phi, dx=dtheta, dy=dphi, grid=grid) def ev(self, theta, phi, dtheta=0, dphi=0): """ Evaluate the spline at points Returns the interpolated value at ``(theta[i], phi[i]), i=0,...,len(theta)-1``. Parameters ---------- theta, phi : array-like Input coordinates. Standard Numpy broadcasting is obeyed. dtheta : int Order of theta-derivative .. versionadded:: 0.14.0 dphi : int Order of phi-derivative .. versionadded:: 0.14.0 """ return self.__call__(theta, phi, dtheta=dtheta, dphi=dphi, grid=False) class SmoothSphereBivariateSpline(SphereBivariateSpline): """ Smooth bivariate spline approximation in spherical coordinates. .. versionadded:: 0.11.0 Parameters ---------- theta, phi, r : array_like 1-D sequences of data points (order is not important). Coordinates must be given in radians. Theta must lie within the interval (0, pi), and phi must lie within the interval (0, 2pi). w : array_like, optional Positive 1-D sequence of weights. s : float, optional Positive smoothing factor defined for estimation condition: ``sum((w(i)*(r(i) - s(theta(i), phi(i))))**2, axis=0) <= s`` Default ``s=len(w)`` which should be a good value if 1/w[i] is an estimate of the standard deviation of r[i]. eps : float, optional A threshold for determining the effective rank of an over-determined linear system of equations. `eps` should have a value between 0 and 1, the default is 1e-16. Notes ----- For more information, see the FITPACK_ site about this function. .. _FITPACK: http://www.netlib.org/dierckx/sphere.f Examples -------- Suppose we have global data on a coarse grid (the input data does not have to be on a grid): >>> theta = np.linspace(0., np.pi, 7) >>> phi = np.linspace(0., 2*np.pi, 9) >>> data = np.empty((theta.shape[0], phi.shape[0])) >>> data[:,0], data[0,:], data[-1,:] = 0., 0., 0. >>> data[1:-1,1], data[1:-1,-1] = 1., 1. >>> data[1,1:-1], data[-2,1:-1] = 1., 1. >>> data[2:-2,2], data[2:-2,-2] = 2., 2. >>> data[2,2:-2], data[-3,2:-2] = 2., 2. >>> data[3,3:-2] = 3. >>> data = np.roll(data, 4, 1) We need to set up the interpolator object >>> lats, lons = np.meshgrid(theta, phi) >>> from scipy.interpolate import SmoothSphereBivariateSpline >>> lut = SmoothSphereBivariateSpline(lats.ravel(), lons.ravel(), data.T.ravel(),s=3.5) As a first test, we'll see what the algorithm returns when run on the input coordinates >>> data_orig = lut(theta, phi) Finally we interpolate the data to a finer grid >>> fine_lats = np.linspace(0., np.pi, 70) >>> fine_lons = np.linspace(0., 2 * np.pi, 90) >>> data_smth = lut(fine_lats, fine_lons) >>> fig = plt.figure() >>> ax1 = fig.add_subplot(131) >>> ax1.imshow(data, interpolation='nearest') >>> ax2 = fig.add_subplot(132) >>> ax2.imshow(data_orig, interpolation='nearest') >>> ax3 = fig.add_subplot(133) >>> ax3.imshow(data_smth, interpolation='nearest') >>> plt.show() """ def __init__(self, theta, phi, r, w=None, s=0., eps=1E-16): if np.issubclass_(w, float): w = ones(len(theta)) * w nt_, tt_, np_, tp_, c, fp, ier = dfitpack.spherfit_smth(theta, phi, r, w=w, s=s, eps=eps) if ier not in [0, -1, -2]: message = _spherefit_messages.get(ier, 'ier=%s' % (ier)) raise ValueError(message) self.fp = fp self.tck = tt_[:nt_], tp_[:np_], c[:(nt_ - 4) * (np_ - 4)] self.degrees = (3, 3) class LSQSphereBivariateSpline(SphereBivariateSpline): """ Weighted least-squares bivariate spline approximation in spherical coordinates. .. versionadded:: 0.11.0 Parameters ---------- theta, phi, r : array_like 1-D sequences of data points (order is not important). Coordinates must be given in radians. Theta must lie within the interval (0, pi), and phi must lie within the interval (0, 2pi). tt, tp : array_like Strictly ordered 1-D sequences of knots coordinates. Coordinates must satisfy ``0 < tt[i] < pi``, ``0 < tp[i] < 2*pi``. w : array_like, optional Positive 1-D sequence of weights, of the same length as `theta`, `phi` and `r`. eps : float, optional A threshold for determining the effective rank of an over-determined linear system of equations. `eps` should have a value between 0 and 1, the default is 1e-16. Notes ----- For more information, see the FITPACK_ site about this function. .. _FITPACK: http://www.netlib.org/dierckx/sphere.f Examples -------- Suppose we have global data on a coarse grid (the input data does not have to be on a grid): >>> theta = np.linspace(0., np.pi, 7) >>> phi = np.linspace(0., 2*np.pi, 9) >>> data = np.empty((theta.shape[0], phi.shape[0])) >>> data[:,0], data[0,:], data[-1,:] = 0., 0., 0. >>> data[1:-1,1], data[1:-1,-1] = 1., 1. >>> data[1,1:-1], data[-2,1:-1] = 1., 1. >>> data[2:-2,2], data[2:-2,-2] = 2., 2. >>> data[2,2:-2], data[-3,2:-2] = 2., 2. >>> data[3,3:-2] = 3. >>> data = np.roll(data, 4, 1) We need to set up the interpolator object. Here, we must also specify the coordinates of the knots to use. >>> lats, lons = np.meshgrid(theta, phi) >>> knotst, knotsp = theta.copy(), phi.copy() >>> knotst[0] += .0001 >>> knotst[-1] -= .0001 >>> knotsp[0] += .0001 >>> knotsp[-1] -= .0001 >>> from scipy.interpolate import LSQSphereBivariateSpline >>> lut = LSQSphereBivariateSpline(lats.ravel(), lons.ravel(), data.T.ravel(),knotst,knotsp) As a first test, we'll see what the algorithm returns when run on the input coordinates >>> data_orig = lut(theta, phi) Finally we interpolate the data to a finer grid >>> fine_lats = np.linspace(0., np.pi, 70) >>> fine_lons = np.linspace(0., 2*np.pi, 90) >>> data_lsq = lut(fine_lats, fine_lons) >>> fig = plt.figure() >>> ax1 = fig.add_subplot(131) >>> ax1.imshow(data, interpolation='nearest') >>> ax2 = fig.add_subplot(132) >>> ax2.imshow(data_orig, interpolation='nearest') >>> ax3 = fig.add_subplot(133) >>> ax3.imshow(data_lsq, interpolation='nearest') >>> plt.show() """ def __init__(self, theta, phi, r, tt, tp, w=None, eps=1E-16): if np.issubclass_(w, float): w = ones(len(theta)) * w nt_, np_ = 8 + len(tt), 8 + len(tp) tt_, tp_ = zeros((nt_,), float), zeros((np_,), float) tt_[4:-4], tp_[4:-4] = tt, tp tt_[-4:], tp_[-4:] = np.pi, 2. * np.pi tt_, tp_, c, fp, ier = dfitpack.spherfit_lsq(theta, phi, r, tt_, tp_, w=w, eps=eps) if ier < -2: deficiency = 6 + (nt_ - 8) * (np_ - 7) + ier message = _spherefit_messages.get(-3) % (deficiency, -ier) warnings.warn(message) elif ier not in [0, -1, -2]: message = _spherefit_messages.get(ier, 'ier=%s' % (ier)) raise ValueError(message) self.fp = fp self.tck = tt_, tp_, c self.degrees = (3, 3) _spfit_messages = _surfit_messages.copy() _spfit_messages[10] = """ ERROR: on entry, the input data are controlled on validity the following restrictions must be satisfied. -1<=iopt(1)<=1, 0<=iopt(2)<=1, 0<=iopt(3)<=1, -1<=ider(1)<=1, 0<=ider(2)<=1, ider(2)=0 if iopt(2)=0. -1<=ider(3)<=1, 0<=ider(4)<=1, ider(4)=0 if iopt(3)=0. mu >= mumin (see above), mv >= 4, nuest >=8, nvest >= 8, kwrk>=5+mu+mv+nuest+nvest, lwrk >= 12+nuest*(mv+nvest+3)+nvest*24+4*mu+8*mv+max(nuest,mv+nvest) 0< u(i-1)<u(i)< pi,i=2,..,mu, -pi<=v(1)< pi, v(1)<v(i-1)<v(i)<v(1)+2*pi, i=3,...,mv if iopt(1)=-1: 8<=nu<=min(nuest,mu+6+iopt(2)+iopt(3)) 0<tu(5)<tu(6)<...<tu(nu-4)< pi 8<=nv<=min(nvest,mv+7) v(1)<tv(5)<tv(6)<...<tv(nv-4)<v(1)+2*pi the schoenberg-whitney conditions, i.e. there must be subset of grid co-ordinates uu(p) and vv(q) such that tu(p) < uu(p) < tu(p+4) ,p=1,...,nu-4 (iopt(2)=1 and iopt(3)=1 also count for a uu-value tv(q) < vv(q) < tv(q+4) ,q=1,...,nv-4 (vv(q) is either a value v(j) or v(j)+2*pi) if iopt(1)>=0: s>=0 if s=0: nuest>=mu+6+iopt(2)+iopt(3), nvest>=mv+7 if one of these conditions is found to be violated,control is immediately repassed to the calling program. in that case there is no approximation returned.""" class RectSphereBivariateSpline(SphereBivariateSpline): """ Bivariate spline approximation over a rectangular mesh on a sphere. Can be used for smoothing data. .. versionadded:: 0.11.0 Parameters ---------- u : array_like 1-D array of latitude coordinates in strictly ascending order. Coordinates must be given in radians and lie within the interval (0, pi). v : array_like 1-D array of longitude coordinates in strictly ascending order. Coordinates must be given in radians, and must lie within (0, 2pi). r : array_like 2-D array of data with shape ``(u.size, v.size)``. s : float, optional Positive smoothing factor defined for estimation condition (``s=0`` is for interpolation). pole_continuity : bool or (bool, bool), optional Order of continuity at the poles ``u=0`` (``pole_continuity[0]``) and ``u=pi`` (``pole_continuity[1]``). The order of continuity at the pole will be 1 or 0 when this is True or False, respectively. Defaults to False. pole_values : float or (float, float), optional Data values at the poles ``u=0`` and ``u=pi``. Either the whole parameter or each individual element can be None. Defaults to None. pole_exact : bool or (bool, bool), optional Data value exactness at the poles ``u=0`` and ``u=pi``. If True, the value is considered to be the right function value, and it will be fitted exactly. If False, the value will be considered to be a data value just like the other data values. Defaults to False. pole_flat : bool or (bool, bool), optional For the poles at ``u=0`` and ``u=pi``, specify whether or not the approximation has vanishing derivatives. Defaults to False. See Also -------- RectBivariateSpline : bivariate spline approximation over a rectangular mesh Notes ----- Currently, only the smoothing spline approximation (``iopt[0] = 0`` and ``iopt[0] = 1`` in the FITPACK routine) is supported. The exact least-squares spline approximation is not implemented yet. When actually performing the interpolation, the requested `v` values must lie within the same length 2pi interval that the original `v` values were chosen from. For more information, see the FITPACK_ site about this function. .. _FITPACK: http://www.netlib.org/dierckx/spgrid.f Examples -------- Suppose we have global data on a coarse grid >>> lats = np.linspace(10, 170, 9) * np.pi / 180. >>> lons = np.linspace(0, 350, 18) * np.pi / 180. >>> data = np.dot(np.atleast_2d(90. - np.linspace(-80., 80., 18)).T, np.atleast_2d(180. - np.abs(np.linspace(0., 350., 9)))).T We want to interpolate it to a global one-degree grid >>> new_lats = np.linspace(1, 180, 180) * np.pi / 180 >>> new_lons = np.linspace(1, 360, 360) * np.pi / 180 >>> new_lats, new_lons = np.meshgrid(new_lats, new_lons) We need to set up the interpolator object >>> from scipy.interpolate import RectSphereBivariateSpline >>> lut = RectSphereBivariateSpline(lats, lons, data) Finally we interpolate the data. The `RectSphereBivariateSpline` object only takes 1-D arrays as input, therefore we need to do some reshaping. >>> data_interp = lut.ev(new_lats.ravel(), ... new_lons.ravel()).reshape((360, 180)).T Looking at the original and the interpolated data, one can see that the interpolant reproduces the original data very well: >>> fig = plt.figure() >>> ax1 = fig.add_subplot(211) >>> ax1.imshow(data, interpolation='nearest') >>> ax2 = fig.add_subplot(212) >>> ax2.imshow(data_interp, interpolation='nearest') >>> plt.show() Chosing the optimal value of ``s`` can be a delicate task. Recommended values for ``s`` depend on the accuracy of the data values. If the user has an idea of the statistical errors on the data, she can also find a proper estimate for ``s``. By assuming that, if she specifies the right ``s``, the interpolator will use a spline ``f(u,v)`` which exactly reproduces the function underlying the data, she can evaluate ``sum((r(i,j)-s(u(i),v(j)))**2)`` to find a good estimate for this ``s``. For example, if she knows that the statistical errors on her ``r(i,j)``-values are not greater than 0.1, she may expect that a good ``s`` should have a value not larger than ``u.size * v.size * (0.1)**2``. If nothing is known about the statistical error in ``r(i,j)``, ``s`` must be determined by trial and error. The best is then to start with a very large value of ``s`` (to determine the least-squares polynomial and the corresponding upper bound ``fp0`` for ``s``) and then to progressively decrease the value of ``s`` (say by a factor 10 in the beginning, i.e. ``s = fp0 / 10, fp0 / 100, ...`` and more carefully as the approximation shows more detail) to obtain closer fits. The interpolation results for different values of ``s`` give some insight into this process: >>> fig2 = plt.figure() >>> s = [3e9, 2e9, 1e9, 1e8] >>> for ii in xrange(len(s)): >>> lut = RectSphereBivariateSpline(lats, lons, data, s=s[ii]) >>> data_interp = lut.ev(new_lats.ravel(), ... new_lons.ravel()).reshape((360, 180)).T >>> ax = fig2.add_subplot(2, 2, ii+1) >>> ax.imshow(data_interp, interpolation='nearest') >>> ax.set_title("s = %g" % s[ii]) >>> plt.show() """ def __init__(self, u, v, r, s=0., pole_continuity=False, pole_values=None, pole_exact=False, pole_flat=False): iopt = np.array([0, 0, 0], dtype=int) ider = np.array([-1, 0, -1, 0], dtype=int) if pole_values is None: pole_values = (None, None) elif isinstance(pole_values, (float, np.float32, np.float64)): pole_values = (pole_values, pole_values) if isinstance(pole_continuity, bool): pole_continuity = (pole_continuity, pole_continuity) if isinstance(pole_exact, bool): pole_exact = (pole_exact, pole_exact) if isinstance(pole_flat, bool): pole_flat = (pole_flat, pole_flat) r0, r1 = pole_values iopt[1:] = pole_continuity if r0 is None: ider[0] = -1 else: ider[0] = pole_exact[0] if r1 is None: ider[2] = -1 else: ider[2] = pole_exact[1] ider[1], ider[3] = pole_flat u, v = np.ravel(u), np.ravel(v) if not np.all(np.diff(u) > 0.0): raise TypeError('u must be strictly increasing') if not np.all(np.diff(v) > 0.0): raise TypeError('v must be strictly increasing') if not u.size == r.shape[0]: raise TypeError('u dimension of r must have same number of ' 'elements as u') if not v.size == r.shape[1]: raise TypeError('v dimension of r must have same number of ' 'elements as v') if pole_continuity[1] is False and pole_flat[1] is True: raise TypeError('if pole_continuity is False, so must be ' 'pole_flat') if pole_continuity[0] is False and pole_flat[0] is True: raise TypeError('if pole_continuity is False, so must be ' 'pole_flat') r = np.ravel(r) nu, tu, nv, tv, c, fp, ier = dfitpack.regrid_smth_spher(iopt, ider, u.copy(), v.copy(), r.copy(), r0, r1, s) if ier not in [0, -1, -2]: msg = _spfit_messages.get(ier, 'ier=%s' % (ier)) raise ValueError(msg) self.fp = fp self.tck = tu[:nu], tv[:nv], c[:(nu - 4) * (nv-4)] self.degrees = (3, 3)
mit
nilbody/h2o-3
h2o-py/tests/testdir_golden/pyunit_svd_1_golden.py
1
2402
from __future__ import print_function from builtins import zip import sys sys.path.insert(1,"../../") import h2o from tests import pyunit_utils def svd_1_golden(): print("Importing USArrests.csv data...") arrestsH2O = h2o.upload_file(pyunit_utils.locate("smalldata/pca_test/USArrests.csv")) print("Compare with SVD") fitH2O = h2o.svd(x=arrestsH2O[0:4], nv=4, transform="NONE", max_iterations=2000) print("Compare singular values (D)") h2o_d = fitH2O._model_json['output']['d'] r_d = [1419.06139509772, 194.825846110138, 45.6613376308754, 18.0695566224677] print("R Singular Values: {0}".format(r_d)) print("H2O Singular Values: {0}".format(h2o_d)) for r, h in zip(r_d, h2o_d): assert abs(r - h) < 1e-6, "H2O got {0}, but R got {1}".format(h, r) print("Compare right singular vectors (V)") h2o_v = h2o.as_list(h2o.get_frame(fitH2O._model_json['output']['v_key']['name']), use_pandas=False) h2o_v.pop(0) r_v = [[-0.04239181, 0.01616262, -0.06588426, 0.99679535], [-0.94395706, 0.32068580, 0.06655170, -0.04094568], [-0.30842767, -0.93845891, 0.15496743, 0.01234261], [-0.10963744, -0.12725666, -0.98347101, -0.06760284]] print("R Right Singular Vectors: {0}".format(r_v)) print("H2O Right Singular Vectors: {0}".format(h2o_v)) for rl, hl in zip(r_v, h2o_v): for r, h in zip(rl, hl): assert abs(abs(r) - abs(float(h))) < 1e-5, "H2O got {0}, but R got {1}".format(h, r) print("Compare left singular vectors (U)") h2o_u = h2o.as_list(h2o.get_frame(fitH2O._model_json['output']['u_key']['name']), use_pandas=False) h2o_u.pop(0) r_u = [[-0.1716251, 0.096325710, 0.06515480, 0.15369551], [-0.1891166, 0.173452566, -0.42665785, -0.17801438], [-0.2155930, 0.078998111, 0.02063740, -0.28070784], [-0.1390244, 0.059889811, 0.01392269, 0.01610418], [-0.2067788, -0.009812026, -0.17633244, -0.21867425], [-0.1558794, -0.064555293, -0.28288280, -0.11797419]] print("R Left Singular Vectors: {0}".format(r_u)) print("H2O Left Singular Vectors: {0}".format(h2o_u)) for rl, hl in zip(r_u, h2o_u): for r, h in zip(rl, hl): assert abs(abs(r) - abs(float(h))) < 1e-5, "H2O got {0}, but R got {1}".format(h, r) if __name__ == "__main__": pyunit_utils.standalone_test(svd_1_golden) else: svd_1_golden()
apache-2.0
UKPLab/emnlp2017-claim-identification
src/main/python/process_data_se_WithDevel.py
1
4976
import cPickle import numpy as np import pandas as pd import re import sys from collections import defaultdict def build_data_cv(data_folder, cv=10, clean_string=True): """ Loads data. """ revs = [] pos_file = data_folder[0] # train file neg_file = data_folder[1] # test file devel_file = data_folder[2] vocab = defaultdict(float) for (mysplit,myfile) in [(0,pos_file),(1,neg_file),(2,devel_file)]: with open(myfile, "rb") as f: for line in f: rev = [] strippedLine = line.strip() try: lline,label = strippedLine.split("\t") except ValueError: lline = "" label = strippedLine rev.append(lline.strip()) if clean_string: orig_rev = clean_str(" ".join(rev)) else: orig_rev = " ".join(rev).lower() words = set(orig_rev.split()) for word in words: vocab[word] += 1 datum = {"y":int(label), "text": orig_rev, "num_words": len(orig_rev.split()), "split": mysplit} revs.append(datum) #print revs return revs, vocab def get_W(word_vecs, k=300): """ Get word matrix. W[i] is the vector for word indexed by i """ vocab_size = len(word_vecs) word_idx_map = dict() W = np.zeros(shape=(vocab_size+1, k), dtype='float32') W[0] = np.zeros(k, dtype='float32') i = 1 for word in word_vecs: W[i] = word_vecs[word] word_idx_map[word] = i i += 1 return W, word_idx_map def load_bin_vec(fname, vocab): """ Loads 300x1 word vecs from Google (Mikolov) word2vec """ word_vecs = {} with open(fname, "rb") as f: header = f.readline() vocab_size, layer1_size = map(int, header.split()) binary_len = np.dtype('float32').itemsize * layer1_size for line in xrange(vocab_size): word = [] while True: ch = f.read(1) if ch == ' ': word = ''.join(word) break if ch != '\n': word.append(ch) if word in vocab: word_vecs[word] = np.fromstring(f.read(binary_len), dtype='float32') else: f.read(binary_len) return word_vecs def add_unknown_words(word_vecs, vocab, min_df=1, k=300): """ For words that occur in at least min_df documents, create a separate word vector. 0.25 is chosen so the unknown vectors have (approximately) same variance as pre-trained ones """ for word in vocab: if word not in word_vecs and vocab[word] >= min_df: word_vecs[word] = np.random.uniform(-0.25,0.25,k) def clean_str(string, TREC=False): """ Tokenization/string cleaning for all datasets except for SST. Every dataset is lower cased except for TREC """ string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string) string = re.sub(r"\'s", " \'s", string) string = re.sub(r"\'ve", " \'ve", string) string = re.sub(r"n\'t", " n\'t", string) string = re.sub(r"\'re", " \'re", string) string = re.sub(r"\'d", " \'d", string) string = re.sub(r"\'ll", " \'ll", string) string = re.sub(r",", " , ", string) string = re.sub(r"!", " ! ", string) string = re.sub(r"\(", " \( ", string) string = re.sub(r"\)", " \) ", string) string = re.sub(r"\?", " \? ", string) string = re.sub(r"\s{2,}", " ", string) return string.strip() if TREC else string.strip().lower() def clean_str_sst(string): """ Tokenization/string cleaning for the SST dataset """ string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string) string = re.sub(r"\s{2,}", " ", string) return string.strip().lower() if __name__=="__main__": w2v_file = sys.argv[1] trainFile = sys.argv[2] testFile = sys.argv[3] develFile = sys.argv[4] saveFile = sys.argv[5] data_folder = [trainFile,testFile,develFile] print "loading data...", revs, vocab = build_data_cv(data_folder, cv=10, clean_string=True) max_l = np.max(pd.DataFrame(revs)["num_words"]) print "data loaded!" print "number of sentences: " + str(len(revs)) print "vocab size: " + str(len(vocab)) print "max sentence length: " + str(max_l) print "loading word2vec vectors...", sys.stdout.flush() w2v = load_bin_vec(w2v_file, vocab) print "word2vec loaded!" print "num words already in word2vec: " + str(len(w2v)) add_unknown_words(w2v, vocab) W, word_idx_map = get_W(w2v) rand_vecs = {} add_unknown_words(rand_vecs, vocab) W2, _ = get_W(rand_vecs) cPickle.dump([revs, W, W2, word_idx_map, vocab], open(saveFile, "wb")) print "dataset created!" #sys.exit(1) # SE
apache-2.0
imatge-upc/saliency
shallow/train.py
2
3064
# add to kfkd.py from lasagne import layers from lasagne.updates import nesterov_momentum from nolearn.lasagne import NeuralNet,BatchIterator import os import numpy as np from sklearn.utils import shuffle import cPickle as pickle import matplotlib.pyplot as plt import Image import ImageOps from scipy import misc import scipy.io import theano def load(): f = file('data_Salicon_T.cPickle', 'rb') loaded_obj = pickle.load(f) f.close() X, y = loaded_obj return X, y def float32(k): return np.cast['float32'](k) class AdjustVariable(object): def __init__(self, name, start=0.03, stop=0.001): self.name = name self.start, self.stop = start, stop self.ls = None def __call__(self, nn, train_history): if self.ls is None: self.ls = np.linspace(self.start, self.stop, nn.max_epochs) epoch = train_history[-1]['epoch'] new_value = float32(self.ls[epoch - 1]) getattr(nn, self.name).set_value(new_value) class FlipBatchIterator(BatchIterator): def transform(self, Xb, yb): Xb, yb = super(FlipBatchIterator, self).transform(Xb, yb) # Flip half of the images in this batch at random: bs = Xb.shape[0] indices = np.random.choice(bs, bs / 2, replace=False) Xb[indices] = Xb[indices, :, :, ::-1] tmp = yb[indices].reshape(bs/2,1,48,48) mirror = tmp[ :,:,:, ::-1] yb[indices] = mirror.reshape(bs/2,48*48) return Xb, yb net2 = NeuralNet( layers=[ ('input', layers.InputLayer), ('conv1', layers.Conv2DLayer), ('pool1', layers.MaxPool2DLayer), ('conv2', layers.Conv2DLayer), ('pool2', layers.MaxPool2DLayer), ('conv3', layers.Conv2DLayer), ('pool3', layers.MaxPool2DLayer), ('hidden4', layers.DenseLayer), ('maxout6',layers.FeaturePoolLayer), ('output', layers.DenseLayer), ], input_shape=(None, 3, 96, 96), conv1_num_filters=32, conv1_filter_size=(5, 5), pool1_pool_size=(2, 2), conv2_num_filters=64, conv2_filter_size=(3, 3), pool2_pool_size=(2, 2), conv3_num_filters=64, conv3_filter_size=(3, 3), pool3_pool_size=(2, 2), hidden4_num_units=48*48*2, maxout6_pool_size=2,output_num_units=48*48,output_nonlinearity=None, update_learning_rate=theano.shared(float32(0.05)), update_momentum=theano.shared(float32(0.9)), regression=True, on_epoch_finished=[ AdjustVariable('update_learning_rate', start=0.05, stop=0.0001), AdjustVariable('update_momentum', start=0.9, stop=0.999), ], batch_iterator_train=FlipBatchIterator(batch_size=128), max_epochs=1200, verbose=1, ) X, y = load() print("X.shape == {}; X.min == {:.3f}; X.max == {:.3f}".format( X.shape, X.min(), X.max())) print("y.shape == {}; y.min == {:.3f}; y.max == {:.3f}".format( y.shape, y.min(), y.max())) X = X.astype(np.float32) y = y.astype(np.float32) net.fit(X, y) with open('JuntingNet_SALICON.pickle', 'wb') as f: pickle.dump(net2, f, -1)
mit
bromjiri/Presto
predictor/predictor_new.py
1
8137
import settings import pandas as pd import numpy as np import os from datetime import datetime from datetime import timedelta import predictor.predictor_classifier as cls import predictor.predictor_statistic as stat import random import nltk class Stock: def __init__(self, subject): input_file = settings.PREDICTOR_STOCK + "/" + subject + ".csv" self.stock_df = pd.read_csv(input_file, sep=',', index_col='Date') def create_dict(self, from_date, to_date): self.stock_ser = self.stock_df['Diff'].loc[from_date:to_date] # binning self.stock_ser = self.stock_ser.apply(binning_none) self.stock_dict = self.stock_ser.dropna().astype(int).to_dict() def get_dict(self): return self.stock_dict def get_stock_dates(self): return self.stock_ser.index.values class Sent: def __init__(self, subject, source): input_file = settings.PREDICTOR_SENTIMENT + "/" + source + "/" + source + "-sent-" + subject + ".csv" self.sent_df = pd.read_csv(input_file, sep=',', index_col='Date') def get_weekend(self, col_name, stock_dates): weekend_df = np.round(self.sent_df, 2) aggreg = 0 days = 1 for idx, row in weekend_df.iterrows(): value = row[col_name] date = pd.to_datetime(idx) date_plus = date + timedelta(days=1) if str(date_plus.date()) not in stock_dates: # print("weekend") value += aggreg aggreg = value days += 1 else: total = value + aggreg mean = total / days aggreg = 0 days = 1 weekend_df.set_value(idx, col_name, mean) # print(date.date(), row[col_name], value) return np.round(weekend_df[col_name].diff().loc[stock_dates], 2) def create_dict(self, precision, method, from_date, to_date, stock_dates, binning): sentiment_col = "Sent" + precision sent_ser = self.sent_df[sentiment_col] if method == "Natural": sent_ser = sent_ser.diff().loc[from_date:to_date] elif method == "Friday": sent_ser = sent_ser.loc[stock_dates].diff() elif method == "Sunday": sent_ser = sent_ser.diff().loc[stock_dates] elif method == "Weekend": sent_ser = self.get_weekend(sentiment_col, stock_dates) # binning std_dev1 = sent_ser.std() / 4 std_dev2 = sent_ser.std() if binning == 'none': sent_ser_new = sent_ser.apply(binning_none) elif binning == 'low': sent_ser_new = sent_ser.apply(binning_low, args=(std_dev1,)) else: sent_ser_new = sent_ser.apply(binning_high, args=(std_dev1, std_dev2,)) # print(pd.concat([sent_ser, sent_ser_new], axis=1)) self.sent_dict = sent_ser_new.dropna().astype(int).to_dict() self.key_list = sorted(self.sent_dict.keys()) def get_dict(self): return self.sent_dict def get_features(self, key): index = self.key_list.index(key) features = dict() features['d1'] = self.sent_dict[self.key_list[index-3]] features['d2'] = self.sent_dict[self.key_list[index-2]] features['d3'] = self.sent_dict[self.key_list[index-1]] return features def binning_none(row): if row > 0: return 4 elif row < 0: return 0 else: return row def binning_low(row, std_dev1): if row > std_dev1: return 4 elif row < std_dev1 and row > -std_dev1: return 2 elif row < -std_dev1: return 0 else: return row def binning_high(row, std_dev1, std_dev2): if row > std_dev2: return 4 elif row < std_dev2 and row > std_dev1: return 3 elif row < std_dev1 and row > -std_dev1: return 2 elif row < -std_dev1 and row > -std_dev2: return 1 elif row < -std_dev2: return 0 else: return row def run_one(source, subject, precision, method, from_date, to_date, binning, filename_nltk, filename_skl): # stock dataframe stock = Stock(subject) stock.create_dict(from_date, to_date) stock_dict = stock.get_dict() # print(sorted(stock_dict.items())) indexes = ["djia", "snp", "nasdaq"] # if subject in indexes: # subject = "the" # sentiment dataframe sent = Sent(subject, source) sent.create_dict(precision, method, from_date, to_date, stock.get_stock_dates(), binning) # print(sorted(sent.get_dict().items())) # features features_list = list() for key in sorted(stock_dict)[3:]: features = sent.get_features(key) features_list.append([features, stock_dict[key]]) # print([key, sorted(features.items()), stock_dict[key]]) features_list_pos = list() features_list_neg = list() for feature in features_list: if feature[1] == 0: features_list_neg.append(feature) else: features_list_pos.append(feature) statistic = stat.Statistic(source, subject, precision, method, binning) # print(len(features_list), len(features_list_pos), len(features_list_neg)) max_half = min(len(features_list_pos), len(features_list_neg)) train_border = int(max_half * 4 / 5) # print(train_border, max_half) # exit() cycles = 50 for x in range(0, cycles): random.shuffle(features_list_pos) random.shuffle(features_list_neg) # random.shuffle(features_list) trainfeats = features_list_pos[:train_border] + features_list_neg[:train_border] testfeats = features_list_pos[train_border:max_half] + features_list_neg[train_border:max_half] # print(len(trainfeats), len(testfeats)) # trainfeats = features_list[:170] # testfeats = features_list[170:] nlt_output, skl_output = cls.train(trainfeats, testfeats, nlt=nltk_run, skl=sklearn_run) # print(nlt_output['most1']) # exit() if nltk_run: statistic.add_nltk(nlt_output) if sklearn_run: statistic.add_skl(skl_output) if nltk_run: statistic.mean_nltk(cycles) statistic.print_nltk() # statistic.write_nltk(filename_nltk) if sklearn_run: statistic.mean_skl(cycles) statistic.print_skl() statistic.print_stddev() # statistic.write_skl(filename_skl) nltk_run = True sklearn_run = True from_date = '2016-11-01' to_date = '2017-08-31' source = "stwits-comb" binnings = ['none', 'low', 'high'] # subjects = ["snp", "djia", "nasdaq"] subjects = ["djia", "snp", "nasdaq"] # subjects = ["microsoft"] precisions = ["0.6", "0.8", "1.0"] # precisions = ["0.6"] methods = ["Friday", "Natural", "Weekend", "Sunday"] # methods = ["Friday"] for subject in subjects: folder = settings.PREDICTOR_PREDICTION + '/' + source + '/' + subject + '/' os.makedirs(folder, exist_ok=True) filename_nltk = folder + source + '-prediction-' + subject + "-nltk.csv" filename_skl = folder + source + '-prediction-' + subject + "-skl.csv" # if nltk_run: # open(filename_nltk, 'w').close() # # if sklearn_run: # open(filename_skl, 'w').close() for method in methods: # if nltk_run: # f = open(filename_nltk, 'a') # f.write(source + ", " + subject + ", " + method + ", NLTK\n") # f.write("precision, binning, accuracy, pos_prec, neg_prec, pos_rec, neg_rec, d1, d2, d3\n") # f.close() # # if sklearn_run: # f = open(filename_skl, 'a') # f.write(source + ", " + subject + ", " + method + ", SKL\n") # f.write("precision, binning, mnb, bnb, lr, lsvc, nsvc, voted\n") # f.close() for precision in precisions: for binning in binnings: # print(source, subject, precision, method) run_one(source, subject, precision, method, from_date, to_date, binning, filename_nltk, filename_skl)
mit
dingmingliu/quanttrade
bt/core.py
1
37660
""" Contains the core building blocks of the framework. """ import math from copy import deepcopy import pandas as pd import numpy as np import cython as cy class Node(object): """ The Node is the main building block in bt's tree structure design. Both StrategyBase and SecurityBase inherit Node. It contains the core functionality of a tree node. Args: * name (str): The Node name * parent (Node): The parent Node * children (dict, list): A collection of children. If dict, the format is {name: child}, if list then list of children. Attributes: * name (str): Node name * parent (Node): Node parent * root (Node): Root node of the tree (topmost node) * children (dict): Node's children * now (datetime): Used when backtesting to store current date * stale (bool): Flag used to determine if Node is stale and need updating * prices (TimeSeries): Prices of the Node. Prices for a security will be the security's price, for a strategy it will be an index that reflects the value of the strategy over time. * price (float): last price * value (float): last value * weight (float): weight in parent * full_name (str): Name including parents' names * members (list): Current Node + node's children """ _price = cy.declare(cy.double) _value = cy.declare(cy.double) _weight = cy.declare(cy.double) _issec = cy.declare(cy.bint) _has_strat_children = cy.declare(cy.bint) def __init__(self, name, parent=None, children=None): self.name = name # strategy children helpers self._has_strat_children = False self._strat_children = [] # if children is not None, we assume that we want to limit the # available children space to the provided list. if children is not None: if isinstance(children, list): # if all strings - just save as universe_filter if all(isinstance(x, str) for x in children): self._universe_tickers = children # empty dict - don't want to uselessly create # tons of children when they might not be needed children = {} else: # this will be case if we pass in children # (say a bunch of sub-strategies) tmp = {} ut = [] for c in children: if type(c) == str: tmp[c] = SecurityBase(c) ut.append(c) else: # deepcopy object for possible later reuse tmp[c.name] = deepcopy(c) # if strategy, turn on flag and add name to list # strategy children have special treatment if isinstance(c, StrategyBase): self._has_strat_children = True self._strat_children.append(c.name) # if not strategy, then we will want to add this to # universe_tickers to filter on setup else: ut.append(c.name) children = tmp # we want to keep whole universe in this case # so set to None self._universe_tickers = ut if parent is None: self.parent = self self.root = self else: self.parent = parent self.root = parent.root parent._add_child(self) # default children if children is None: children = {} self._universe_tickers = None self.children = children self._childrenv = children.values() for c in self._childrenv: c.parent = self c.root = self.root # set default value for now self.now = 0 # make sure root has stale flag # used to avoid unncessary update # sometimes we change values in the tree and we know that we will need # to update if another node tries to access a given value (say weight). # This avoid calling the update until it is actually needed. self.root.stale = False # helper vars self._price = 0 self._value = 0 self._weight = 0 # is security flag - used to avoid updating 0 pos securities self._issec = False def __getitem__(self, key): return self.children[key] @property def prices(self): """ A TimeSeries of the Node's price. """ # can optimize depending on type - # securities don't need to check stale to # return latest prices, whereas strategies do... raise NotImplementedError() @property def price(self): """ Current price of the Node """ # can optimize depending on type - # securities don't need to check stale to # return latest prices, whereas strategies do... raise NotImplementedError() @property def value(self): """ Current value of the Node """ if self.root.stale: self.root.update(self.root.now, None) return self._value @property def weight(self): """ Current weight of the Node (with respect to the parent). """ if self.root.stale: self.root.update(self.root.now, None) return self._weight def setup(self, dates): """ Setup method used to initialize a Node with a set of dates. """ raise NotImplementedError() def _add_child(self, child): child.parent = self child.root = self.root if self.children is None: self.children = {child.name: child} else: self.children[child.name] = child self._childrenv = self.children.values() def update(self, date, data=None, inow=None): """ Update Node with latest date, and optionally some data. """ raise NotImplementedError() def adjust(self, amount, update=True, isflow=True): """ Adjust Node value by amount. """ raise NotImplementedError() def allocate(self, amount, update=True): """ Allocate capital to Node. """ raise NotImplementedError() @property def members(self): """ Node members. Members include current node as well as Node's children. """ res = [self] for c in self.children.values(): res.extend(c.members) return res @property def full_name(self): if self.parent == self: return self.name else: return '%s>%s' % (self.parent.full_name, self.name) class StrategyBase(Node): """ Strategy Node. Used to define strategy logic within a tree. A Strategy's role is to allocate capital to it's children based on a function. Args: * name (str): Strategy name * children (dict, list): A collection of children. If dict, the format is {name: child}, if list then list of children. Children can be any type of Node. * parent (Node): The parent Node Attributes: * name (str): Strategy name * parent (Strategy): Strategy parent * root (Strategy): Root node of the tree (topmost node) * children (dict): Strategy's children * now (datetime): Used when backtesting to store current date * stale (bool): Flag used to determine if Strategy is stale and need updating * prices (TimeSeries): Prices of the Strategy - basically an index that reflects the value of the strategy over time. * price (float): last price * value (float): last value * weight (float): weight in parent * full_name (str): Name including parents' names * members (list): Current Strategy + strategy's children * commission_fn (fn(quantity, price)): A function used to determine the commission (transaction fee) amount. Could be used to model slippage (implementation shortfall). Note that often fees are symmetric for buy and sell and absolute value of quantity should be used for calculation. * capital (float): Capital amount in Strategy - cash * universe (DataFrame): Data universe available at the current time. Universe contains the data passed in when creating a Backtest. Use this data to determine strategy logic. """ _capital = cy.declare(cy.double) _net_flows = cy.declare(cy.double) _last_value = cy.declare(cy.double) _last_price = cy.declare(cy.double) _last_fee = cy.declare(cy.double) _paper_trade = cy.declare(cy.bint) bankrupt = cy.declare(cy.bint) def __init__(self, name, children=None, parent=None): Node.__init__(self, name, children=children, parent=parent) self._capital = 0 self._weight = 1 self._value = 0 self._price = 100 # helper vars self._net_flows = 0 self._last_value = 0 self._last_price = 100 self._last_fee = 0 # default commission function self.commission_fn = self._dflt_comm_fn self._paper_trade = False self._positions = None self.bankrupt = False @property def price(self): """ Current price. """ if self.root.stale: self.root.update(self.now, None) return self._price @property def prices(self): """ TimeSeries of prices. """ if self.root.stale: self.root.update(self.now, None) return self._prices.ix[:self.now] @property def values(self): """ TimeSeries of values. """ if self.root.stale: self.root.update(self.now, None) return self._values.ix[:self.now] @property def capital(self): """ Current capital - amount of unallocated capital left in strategy. """ # no stale check needed return self._capital @property def cash(self): """ TimeSeries of unallocated capital. """ # no stale check needed return self._cash @property def fees(self): """ TimeSeries of fees. """ # no stale check needed return self._fees @property def universe(self): """ Data universe available at the current time. Universe contains the data passed in when creating a Backtest. Use this data to determine strategy logic. """ # avoid windowing every time # if calling and on same date return # cached value if self.now == self._last_chk: return self._funiverse else: self._last_chk = self.now self._funiverse = self._universe.ix[:self.now] return self._funiverse @property def positions(self): """ TimeSeries of positions. """ # if accessing and stale - update first if self.root.stale: self.root.update(self.root.now, None) if self._positions is not None: return self._positions else: vals = pd.DataFrame({x.name: x.positions for x in self.members if isinstance(x, SecurityBase)}) self._positions = vals return vals def setup(self, universe): """ Setup strategy with universe. This will speed up future calculations and updates. """ # save full universe in case we need it self._original_data = universe # determine if needs paper trading # and setup if so if self is not self.parent: self._paper_trade = True self._paper_amount = 1000000 paper = deepcopy(self) paper.parent = paper paper.root = paper paper._paper_trade = False paper.setup(self._original_data) paper.adjust(self._paper_amount) self._paper = paper # setup universe funiverse = universe if self._universe_tickers is not None: # if we have universe_tickers defined, limit universe to # those tickers valid_filter = list(set(universe.columns) .intersection(self._universe_tickers)) funiverse = universe[valid_filter].copy() # if we have strat children, we will need to create their columns # in the new universe if self._has_strat_children: for c in self._strat_children: funiverse[c] = np.nan # must create to avoid pandas warning funiverse = pd.DataFrame(funiverse) self._universe = funiverse # holds filtered universe self._funiverse = funiverse self._last_chk = None # We're not bankrupt yet self.bankrupt = False # setup internal data self.data = pd.DataFrame(index=funiverse.index, columns=['price', 'value', 'cash', 'fees'], data=0.0) self._prices = self.data['price'] self._values = self.data['value'] self._cash = self.data['cash'] self._fees = self.data['fees'] # setup children as well - use original universe here - don't want to # pollute with potential strategy children in funiverse if self.children is not None: [c.setup(universe) for c in self._childrenv] @cy.locals(newpt=cy.bint, val=cy.double, ret=cy.double) def update(self, date, data=None, inow=None): """ Update strategy. Updates prices, values, weight, etc. """ # resolve stale state self.root.stale = False # update helpers on date change # also set newpt flag newpt = False if self.now == 0: newpt = True elif date != self.now: self._net_flows = 0 self._last_price = self._price self._last_value = self._value self._last_fee = 0.0 newpt = True # update now self.now = date if inow is None: if self.now == 0: inow = 0 else: inow = self.data.index.get_loc(date) # update children if any and calculate value val = self._capital # default if no children if self.children is not None: for c in self._childrenv: # avoid useless update call if c._issec and not c._needupdate: continue c.update(date, data, inow) val += c.value if self.root == self: if (val < 0) and not self.bankrupt: # Declare a bankruptcy self.bankrupt = True self.flatten() # update data if this value is different or # if now has changed - avoid all this if not since it # won't change if newpt or self._value != val: self._value = val self._values.values[inow] = val try: ret = self._value / (self._last_value + self._net_flows) - 1 except ZeroDivisionError: if self._value == 0: ret = 0 else: raise ZeroDivisionError( 'Could not update %s. Last value ' 'was %s and net flows were %s. Current' 'value is %s. Therefore, ' 'we are dividing by zero to obtain the return ' 'for the period.' % (self.name, self._last_value, self._net_flows, self._value)) self._price = self._last_price * (1 + ret) self._prices.values[inow] = self._price # update children weights if self.children is not None: for c in self._childrenv: # avoid useless update call if c._issec and not c._needupdate: continue try: c._weight = c.value / val except ZeroDivisionError: c._weight = 0.0 # if we have strategy children, we will need to update them in universe if self._has_strat_children: for c in self._strat_children: # TODO: optimize ".loc" here as well self._universe.loc[date, c] = self.children[c].price # Cash should track the unallocated capital at the end of the day, so # we should update it every time we call "update". # Same for fees self._cash.values[inow] = self._capital self._fees.values[inow] = self._last_fee # update paper trade if necessary if newpt and self._paper_trade: self._paper.update(date) self._paper.run() self._paper.update(date) # update price self._price = self._paper.price self._prices.values[inow] = self._price @cy.locals(amount=cy.double, update=cy.bint, flow=cy.bint, fees=cy.double) def adjust(self, amount, update=True, flow=True, fee=0.0): """ Adjust capital - used to inject capital to a Strategy. This injection of capital will have no effect on the children. Args: * amount (float): Amount to adjust by. * update (bool): Force update? * flow (bool): Is this adjustment a flow? Basically a flow will have an impact on the price index. Examples of flows are commissions. """ # adjust capital self._capital += amount self._last_fee += fee # if flow - increment net_flows - this will not affect # performance. Commissions and other fees are not flows since # they have a performance impact if flow: self._net_flows += amount if update: # indicates that data is now stale and must # be updated before access self.root.stale = True @cy.locals(amount=cy.double, update=cy.bint) def allocate(self, amount, child=None, update=True): """ Allocate capital to Strategy. By default, capital is allocated recursively down the children, proportionally to the children's weights. If a child is specified, capital will be allocated to that specific child. Allocation also have a side-effect. They will deduct the same amount from the parent's "account" to offset the allocation. If there is remaining capital after allocation, it will remain in Strategy. Args: * amount (float): Amount to allocate. * child (str): If specified, allocation will be directed to child only. Specified by name. * update (bool): Force update. """ # allocate to child if child is not None: if child not in self.children: c = SecurityBase(child) c.setup(self._universe) # update to bring up to speed c.update(self.now) # add child to tree self._add_child(c) # allocate to child self.children[child].allocate(amount) # allocate to self else: # adjust parent's capital # no need to update now - avoids repetition if self.parent == self: self.parent.adjust(-amount, update=False, flow=True) else: # do NOT set as flow - parent will be another strategy # and therefore should not incur flow self.parent.adjust(-amount, update=False, flow=False) # adjust self's capital self.adjust(amount, update=False, flow=True) # push allocation down to children if any # use _weight to avoid triggering an update if self.children is not None: [c.allocate(amount * c._weight, update=False) for c in self._childrenv] # mark as stale if update requested if update: self.root.stale = True @cy.locals(delta=cy.double, weight=cy.double, base=cy.double) def rebalance(self, weight, child, base=np.nan, update=True): """ Rebalance a child to a given weight. This is a helper method to simplify code logic. This method is used when we want to se the weight of a particular child to a set amount. It is similar to allocate, but it calculates the appropriate allocation based on the current weight. Args: * weight (float): The target weight. Usually between -1.0 and 1.0. * child (str): child to allocate to - specified by name. * base (float): If specified, this is the base amount all weight delta calculations will be based off of. This is useful when we determine a set of weights and want to rebalance each child given these new weights. However, as we iterate through each child and call this method, the base (which is by default the current value) will change. Therefore, we can set this base to the original value before the iteration to ensure the proper allocations are made. * update (bool): Force update? """ # if weight is 0 - we want to close child if weight == 0: if child in self.children: return self.close(child) else: return # if no base specified use self's value if np.isnan(base): base = self.value # else make sure we have child if child not in self.children: c = SecurityBase(child) c.setup(self._universe) # update child to bring up to speed c.update(self.now) self._add_child(c) # allocate to child # figure out weight delta c = self.children[child] delta = weight - c.weight c.allocate(delta * base) def close(self, child): """ Close a child position - alias for rebalance(0, child). This will also flatten (close out all) the child's children. Args: * child (str): Child, specified by name. """ c = self.children[child] # flatten if children not None if c.children is not None and len(c.children) != 0: c.flatten() c.allocate(-c.value) def flatten(self): """ Close all child positions. """ # go right to base alloc [c.allocate(-c.value) for c in self._childrenv if c.value != 0] def run(self): """ This is the main logic method. Override this method to provide some algorithm to execute on each date change. This method is called by backtester. """ pass def set_commissions(self, fn): """ Set commission (transaction fee) function. Args: fn (fn(quantity, price)): Function used to determine commission amount. """ self.commission_fn = fn for c in self._childrenv: if isinstance(c, StrategyBase): c.set_commissions(fn) @cy.locals(q=cy.double, p=cy.double) def _dflt_comm_fn(self, q, p): return max(1, abs(q) * 0.01) class SecurityBase(Node): """ Security Node. Used to define a security within a tree. A Security's has no children. It simply models an asset that can be bought or sold. Args: * name (str): Security name * multiplier (float): security multiplier - typically used for derivatives. Attributes: * name (str): Security name * parent (Security): Security parent * root (Security): Root node of the tree (topmost node) * now (datetime): Used when backtesting to store current date * stale (bool): Flag used to determine if Security is stale and need updating * prices (TimeSeries): Security prices. * price (float): last price * value (float): last value - basically position * price * multiplier * weight (float): weight in parent * full_name (str): Name including parents' names * members (list): Current Security + strategy's children * position (float): Current position (quantity). """ _last_pos = cy.declare(cy.double) _position = cy.declare(cy.double) multiplier = cy.declare(cy.double) _prices_set = cy.declare(cy.bint) _needupdate = cy.declare(cy.bint) @cy.locals(multiplier=cy.double) def __init__(self, name, multiplier=1): Node.__init__(self, name, parent=None, children=None) self._value = 0 self._price = 0 self._weight = 0 self._position = 0 self.multiplier = multiplier # opt self._last_pos = 0 self._issec = True self._needupdate = True @property def price(self): """ Current price. """ # if accessing and stale - update first if self._needupdate or self.now != self.parent.now: self.update(self.root.now) return self._price @property def prices(self): """ TimeSeries of prices. """ # if accessing and stale - update first if self._needupdate or self.now != self.parent.now: self.update(self.root.now) return self._prices.ix[:self.now] @property def values(self): """ TimeSeries of values. """ # if accessing and stale - update first if self._needupdate or self.now != self.parent.now: self.update(self.root.now) if self.root.stale: self.root.update(self.root.now, None) return self._values.ix[:self.now] @property def position(self): """ Current position """ # no stale check needed return self._position @property def positions(self): """ TimeSeries of positions. """ # if accessing and stale - update first if self._needupdate: self.update(self.root.now) if self.root.stale: self.root.update(self.root.now, None) return self._positions.ix[:self.now] def setup(self, universe): """ Setup Security with universe. Speeds up future runs. Args: * universe (DataFrame): DataFrame of prices with security's name as one of the columns. """ # if we already have all the prices, we will store them to speed up # future udpates try: prices = universe[self.name] except KeyError: prices = None # setup internal data if prices is not None: self._prices = prices self.data = pd.DataFrame(index=universe.index, columns=['value', 'position'], data=0.0) self._prices_set = True else: self.data = pd.DataFrame(index=universe.index, columns=['price', 'value', 'position']) self._prices = self.data['price'] self._prices_set = False self._values = self.data['value'] self._positions = self.data['position'] @cy.locals(prc=cy.double) def update(self, date, data=None, inow=None): """ Update security with a given date and optionally, some data. This will update price, value, weight, etc. """ # filter for internal calls when position has not changed - nothing to # do. Internal calls (stale root calls) have None data. Also want to # make sure date has not changed, because then we do indeed want to # update. if date == self.now and self._last_pos == self._position: return if inow is None: if date == 0: inow = 0 else: inow = self.data.index.get_loc(date) # date change - update price if date != self.now: # update now self.now = date if self._prices_set: self._price = self._prices.values[inow] # traditional data update elif data is not None: prc = data[self.name] self._price = prc self._prices.values[inow] = prc self._positions.values[inow] = self._position self._last_pos = self._position self._value = self._position * self._price * self.multiplier self._values.values[inow] = self._value if self._weight == 0 and self._position == 0: self._needupdate = False @cy.locals(amount=cy.double, update=cy.bint, q=cy.double, outlay=cy.double) def allocate(self, amount, update=True): """ This allocates capital to the Security. This is the method used to buy/sell the security. A given amount of shares will be determined on the current price, a commisison will be calculated based on the parent's commission fn, and any remaining capital will be passed back up to parent as an adjustment. Args: * amount (float): Amount of adjustment. * update (bool): Force update? """ # will need to update if this has been idle for a while... # update if needupdate or if now is stale # fetch parent's now since our now is stale if self._needupdate or self.now != self.parent.now: self.update(self.parent.now) # ignore 0 alloc # Note that if the price of security has dropped to zero, then it should # never be selected by SelectAll, SelectN etc. I.e. we should not open # the position at zero price. At the same time, we are able to close # it at zero price, because at that point amount=0. # Note also that we don't erase the position in an asset which price has # dropped to zero (though the weight will indeed be = 0) if amount == 0: return if self.parent is self or self.parent is None: raise Exception( 'Cannot allocate capital to a parentless security') if self._price == 0 or np.isnan(self._price): raise Exception( 'Cannot allocate capital to ' '%s because price is 0 or nan as of %s' % (self.name, self.parent.now)) # buy/sell # determine quantity - must also factor in commission # closing out? if amount == -self._value: q = -self._position else: if (self._position > 0) or ((self._position == 0) and (amount > 0)): # if we're going long or changing long position q = math.floor(amount / (self._price * self.multiplier)) else: # if we're going short or changing short position q = math.ceil(amount / (self._price * self.multiplier)) # if q is 0 nothing to do if q == 0 or np.isnan(q): return # this security will need an update, even if pos is 0 (for example if # we close the positions, value and pos is 0, but still need to do that # last update) self._needupdate = True # adjust position & value self._position += q # calculate proper adjustment for parent # parent passed down amount so we want to pass # -outlay back up to parent to adjust for capital # used outlay, fee = self.outlay(q) # call parent self.parent.adjust(-outlay, update=update, flow=False, fee=fee) @cy.locals(q=cy.double, p=cy.double) def commission(self, q, p): """ Calculates the commission (transaction fee) based on quantity and price. Uses the parent's commission_fn. Args: * q (float): quantity * p (float): price """ return self.parent.commission_fn(q, p) @cy.locals(q=cy.double) def outlay(self, q): """ Determines the complete cash outlay (including commission) necessary given a quantity q. Second returning parameter is a commission itself. Args: * q (float): quantity """ fee = self.commission(q, self._price * self.multiplier) full_outlay = q * self._price * self.multiplier + fee return full_outlay, fee def run(self): """ Does nothing - securities have nothing to do on run. """ pass class Algo(object): """ Algos are used to modularize strategy logic so that strategy logic becomes modular, composable, more testable and less error prone. Basically, the Algo should follow the unix philosophy - do one thing well. In practice, algos are simply a function that receives one argument, the Strategy (refered to as target) and are expected to return a bool. When some state preservation is necessary between calls, the Algo object can be used (this object). The __call___ method should be implemented and logic defined therein to mimic a function call. A simple function may also be used if no state preservation is neceesary. Args: * name (str): Algo name """ def __init__(self, name=None): self._name = name @property def name(self): """ Algo name. """ if self._name is None: self._name = self.__class__.__name__ return self._name def __call__(self, target): raise NotImplementedError("%s not implemented!" % self.name) class AlgoStack(Algo): """ An AlgoStack derives from Algo runs multiple Algos until a failure is encountered. The purpose of an AlgoStack is to group a logic set of Algos together. Each Algo in the stack is run. Execution stops if one Algo returns False. Args: * algos (list): List of algos. """ def __init__(self, *algos): super(AlgoStack, self).__init__() self.algos = algos self.check_run_always = any(hasattr(x, 'run_always') for x in self.algos) def __call__(self, target): # normal runing mode if not self.check_run_always: for algo in self.algos: if not algo(target): return False return True # run mode when at least one algo has a run_always attribute else: # store result in res # allows continuation to check for and run # algos that have run_always set to True res = True for algo in self.algos: if res: res = algo(target) elif hasattr(algo, 'run_always'): if algo.run_always: algo(target) return res class Strategy(StrategyBase): """ Strategy expands on the StrategyBase and incorporates Algos. Basically, a Strategy is built by passing in a set of algos. These algos will be placed in an Algo stack and the run function will call the stack. Furthermore, two class attributes are created to pass data between algos. perm for permanent data, temp for temporary data. Args: * name (str): Strategy name * algos (list): List of Algos to be passed into an AlgoStack * children (dict, list): Children - useful when you want to create strategies of strategies Attributes: * stack (AlgoStack): The stack * temp (dict): A dict containing temporary data - cleared on each call to run. This can be used to pass info to other algos. * perm (dict): Permanent data used to pass info from one algo to another. Not cleared on each pass. """ def __init__(self, name, algos=[], children=None): super(Strategy, self).__init__(name, children=children) self.stack = AlgoStack(*algos) self.temp = {} self.perm = {} def run(self): # clear out temp data self.temp = {} # run algo stack self.stack(self) # run children for c in self.children.values(): c.run()
apache-2.0
MikeLing/shogun
examples/undocumented/python/graphical/interactive_svm_demo.py
6
12586
""" Shogun demo, based on PyQT Demo by Eli Bendersky Christian Widmer Soeren Sonnenburg License: GPLv3 """ import numpy import sys, os, csv from PyQt4.QtCore import * from PyQt4.QtGui import * import matplotlib from matplotlib.colorbar import make_axes, Colorbar from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar from matplotlib.figure import Figure from shogun import * import util class Form(QMainWindow): def __init__(self, parent=None): super(Form, self).__init__(parent) self.setWindowTitle('SHOGUN interactive demo') self.data = DataHolder() self.series_list_model = QStandardItemModel() self.create_menu() self.create_main_frame() self.create_status_bar() self.on_show() def load_file(self, filename=None): filename = QFileDialog.getOpenFileName(self, 'Open a data file', '.', 'CSV files (*.csv);;All Files (*.*)') if filename: self.data.load_from_file(filename) self.fill_series_list(self.data.series_names()) self.status_text.setText("Loaded " + filename) def on_show(self): self.axes.clear() self.axes.grid(True) self.axes.plot(self.data.x1_pos, self.data.x2_pos, 'ro') self.axes.plot(self.data.x1_neg, self.data.x2_neg, 'bo') self.axes.set_xlim((-5,5)) self.axes.set_ylim((-5,5)) self.canvas.draw() self.fill_series_list(self.data.get_stats()) def on_about(self): msg = __doc__ QMessageBox.about(self, "About the demo", msg.strip()) def fill_series_list(self, names): self.series_list_model.clear() for name in names: item = QStandardItem(name) item.setCheckState(Qt.Unchecked) item.setCheckable(False) self.series_list_model.appendRow(item) def onclick(self, event): print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(event.button, event.x, event.y, event.xdata, event.ydata) if event.button==1: label = 1.0 else: label = -1.0 self.data.add_example(event.xdata, event.ydata, label) self.on_show() def clear(self): self.data.clear() self.on_show() def enable_widgets(self): kernel_name = self.kernel_combo.currentText() if kernel_name == "LinearKernel": self.sigma.setDisabled(True) self.degree.setDisabled(True) elif kernel_name == "PolynomialKernel": self.sigma.setDisabled(True) self.degree.setEnabled(True) elif kernel_name == "GaussianKernel": self.sigma.setEnabled(True) self.degree.setDisabled(True) def train_svm(self): width = float(self.sigma.text()) degree = int(self.degree.text()) self.axes.clear() self.axes.grid(True) self.axes.plot(self.data.x1_pos, self.data.x2_pos, 'ro') self.axes.plot(self.data.x1_neg, self.data.x2_neg, 'bo') # train svm labels = self.data.get_labels() print type(labels) lab = BinaryLabels(labels) features = self.data.get_examples() train = RealFeatures(features) kernel_name = self.kernel_combo.currentText() print "current kernel is %s" % (kernel_name) if kernel_name == "LinearKernel": gk = LinearKernel(train, train) gk.set_normalizer(IdentityKernelNormalizer()) elif kernel_name == "PolynomialKernel": gk = PolyKernel(train, train, degree, True) gk.set_normalizer(IdentityKernelNormalizer()) elif kernel_name == "GaussianKernel": gk = GaussianKernel(train, train, width) cost = float(self.cost.text()) print "cost", cost svm = LibSVM(cost, gk, lab) svm.train() svm.set_epsilon(1e-2) x, y, z = util.compute_output_plot_isolines(svm, gk, train) plt=self.axes.pcolor(x, y, z) CS=self.axes.contour(x, y, z, [-1,0,1], linewidths=1, colors='black', hold=True) #CS=self.axes.contour(x, y, z, linewidths=1, colors='black', hold=True) #CS=self.axes.contour(x, y, z, 5, linewidths=1, colors='black', hold=True) matplotlib.pyplot.clabel(CS, inline=1, fontsize=10) self.axes.set_xlim((-5,5)) self.axes.set_ylim((-5,5)) cmap = matplotlib.cm.jet norm = matplotlib.colors.Normalize(numpy.min(z), numpy.max(z)) print CS.get_clim() if not self.cax: self.cax, kw = make_axes(self.axes) # ColorbarBase derives from ScalarMappable and puts a colorbar # in a specified axes, so it has everything needed for a # standalone colorbar. There are many more kwargs, but the # following gives a basic continuous colorbar with ticks # and labels. cb1 = matplotlib.colorbar.ColorbarBase(self.cax, cmap=cmap, norm=norm) self.canvas.draw() def create_main_frame(self): self.main_frame = QWidget() plot_frame = QWidget() self.dpi = 100 self.fig = Figure((6.0, 6.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) cid = self.canvas.mpl_connect('button_press_event', self.onclick) self.axes = self.fig.add_subplot(111) self.cax = None #self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) log_label = QLabel("Number of examples:") self.series_list_view = QListView() self.series_list_view.setModel(self.series_list_model) cost_label = QLabel('C') #self.cost = QSpinBox()#QLineEdit() self.cost = QLineEdit() self.cost.setText("1.0") #self.cost.setMinimum(1) spin_label2 = QLabel('sigma') self.sigma = QLineEdit() self.sigma.setText("1.2") #self.sigma.setMinimum(1) self.degree = QLineEdit() self.degree.setText("2") #self.sigma.setMinimum(1) spins_hbox = QHBoxLayout() spins_hbox.addWidget(cost_label) spins_hbox.addWidget(self.cost) spins_hbox.addWidget(spin_label2) spins_hbox.addWidget(self.sigma) spins_hbox.addWidget(self.degree) spins_hbox.addStretch(1) self.legend_cb = QCheckBox("Show Support Vectors") self.legend_cb.setChecked(False) self.show_button = QPushButton("&Train SVM") self.connect(self.show_button, SIGNAL('clicked()'), self.train_svm) self.clear_button = QPushButton("&Clear") self.connect(self.clear_button, SIGNAL('clicked()'), self.clear) self.kernel_combo = QComboBox() self.kernel_combo.insertItem(-1, "GaussianKernel") self.kernel_combo.insertItem(-1, "PolynomialKernel") self.kernel_combo.insertItem(-1, "LinearKernel") self.kernel_combo.maximumSize = QSize(300, 50) self.connect(self.kernel_combo, SIGNAL("currentIndexChanged(QString)"), self.enable_widgets) left_vbox = QVBoxLayout() left_vbox.addWidget(self.canvas) #left_vbox.addWidget(self.mpl_toolbar) right0_vbox = QVBoxLayout() right0_vbox.addWidget(log_label) right0_vbox.addWidget(self.series_list_view) #right0_vbox.addWidget(self.legend_cb) right0_vbox.addStretch(1) right2_vbox = QVBoxLayout() right2_label = QLabel("Settings") right2_vbox.addWidget(right2_label) right2_vbox.addWidget(self.show_button) right2_vbox.addWidget(self.kernel_combo) right2_vbox.addLayout(spins_hbox) right2_clearlabel = QLabel("Remove Data") right2_vbox.addWidget(right2_clearlabel) right2_vbox.addWidget(self.clear_button) right2_vbox.addStretch(1) right_vbox = QHBoxLayout() right_vbox.addLayout(right0_vbox) right_vbox.addLayout(right2_vbox) hbox = QVBoxLayout() hbox.addLayout(left_vbox) hbox.addLayout(right_vbox) self.main_frame.setLayout(hbox) self.setCentralWidget(self.main_frame) self.enable_widgets() def create_status_bar(self): self.status_text = QLabel("") self.statusBar().addWidget(self.status_text, 1) def create_menu(self): self.file_menu = self.menuBar().addMenu("&File") load_action = self.create_action("&Load file", shortcut="Ctrl+L", slot=self.load_file, tip="Load a file") quit_action = self.create_action("&Quit", slot=self.close, shortcut="Ctrl+Q", tip="Close the application") self.add_actions(self.file_menu, (load_action, None, quit_action)) self.help_menu = self.menuBar().addMenu("&Help") about_action = self.create_action("&About", shortcut='F1', slot=self.on_about, tip='About the demo') self.add_actions(self.help_menu, (about_action,)) def add_actions(self, target, actions): for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def create_action( self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, signal="triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(":/%s.png" % icon)) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action class DataHolder(object): """ Just a thin wrapper over a dictionary that holds integer data series. Each series has a name and a list of numbers as its data. The length of all series is assumed to be the same. The series can be read from a CSV file, where each line is a separate series. In each series, the first item in the line is the name, and the rest are data numbers. """ def __init__(self, filename=None): self.clear() self.load_from_file(filename) def clear(self): self.x1_pos = [] self.x2_pos = [] self.x1_neg = [] self.x2_neg = [] def get_stats(self): num_neg = len(self.x1_neg) num_pos = len(self.x1_pos) str_neg = "num negative examples: %i" % num_neg str_pos = "num positive examples: %i" % num_pos return (str_neg, str_pos) def get_labels(self): return numpy.array([1]*len(self.x1_pos) + [-1]*len(self.x1_neg), dtype=numpy.float64) def get_examples(self): num_pos = len(self.x1_pos) num_neg = len(self.x1_neg) examples = numpy.zeros((2,num_pos+num_neg)) for i in xrange(num_pos): examples[0,i] = self.x1_pos[i] examples[1,i] = self.x2_pos[i] for i in xrange(num_neg): examples[0,i+num_pos] = self.x1_neg[i] examples[1,i+num_pos] = self.x2_neg[i] return examples def add_example(self, x1, x2, label): if label==1: self.x1_pos.append(x1) self.x2_pos.append(x2) else: self.x1_neg.append(x1) self.x2_neg.append(x2) def load_from_file(self, filename=None): self.data = {} self.names = [] if filename: for line in csv.reader(open(filename, 'rb')): self.names.append(line[0]) self.data[line[0]] = map(int, line[1:]) self.datalen = len(line[1:]) def series_names(self): """ Names of the data series """ return self.names def series_len(self): """ Length of a data series """ return self.datalen def series_count(self): return len(self.data) def get_series_data(self, name): return self.data[name] def main(): app = QApplication(sys.argv) form = Form() form.show() app.exec_() if __name__ == "__main__": main() #~ dh = DataHolder('qt_mpl_data.csv') #~ print dh.data #~ print dh.get_series_data('1991 Sales') #~ print dh.series_names() #~ print dh.series_count()
gpl-3.0
jlcarmic/producthunt_simulator
venv/lib/python2.7/site-packages/scipy/integrate/odepack.py
62
9420
# Author: Travis Oliphant from __future__ import division, print_function, absolute_import __all__ = ['odeint'] from . import _odepack from copy import copy import warnings class ODEintWarning(Warning): pass _msgs = {2: "Integration successful.", 1: "Nothing was done; the integration time was 0.", -1: "Excess work done on this call (perhaps wrong Dfun type).", -2: "Excess accuracy requested (tolerances too small).", -3: "Illegal input detected (internal error).", -4: "Repeated error test failures (internal error).", -5: "Repeated convergence failures (perhaps bad Jacobian or tolerances).", -6: "Error weight became zero during problem.", -7: "Internal workspace insufficient to finish (internal error)." } def odeint(func, y0, t, args=(), Dfun=None, col_deriv=0, full_output=0, ml=None, mu=None, rtol=None, atol=None, tcrit=None, h0=0.0, hmax=0.0, hmin=0.0, ixpr=0, mxstep=0, mxhnil=0, mxordn=12, mxords=5, printmessg=0): """ Integrate a system of ordinary differential equations. Solve a system of ordinary differential equations using lsoda from the FORTRAN library odepack. Solves the initial value problem for stiff or non-stiff systems of first order ode-s:: dy/dt = func(y, t0, ...) where y can be a vector. *Note*: The first two arguments of ``func(y, t0, ...)`` are in the opposite order of the arguments in the system definition function used by the `scipy.integrate.ode` class. Parameters ---------- func : callable(y, t0, ...) Computes the derivative of y at t0. y0 : array Initial condition on y (can be a vector). t : array A sequence of time points for which to solve for y. The initial value point should be the first element of this sequence. args : tuple, optional Extra arguments to pass to function. Dfun : callable(y, t0, ...) Gradient (Jacobian) of `func`. col_deriv : bool, optional True if `Dfun` defines derivatives down columns (faster), otherwise `Dfun` should define derivatives across rows. full_output : bool, optional True if to return a dictionary of optional outputs as the second output printmessg : bool, optional Whether to print the convergence message Returns ------- y : array, shape (len(t), len(y0)) Array containing the value of y for each desired time in t, with the initial value `y0` in the first row. infodict : dict, only returned if full_output == True Dictionary containing additional output information ======= ============================================================ key meaning ======= ============================================================ 'hu' vector of step sizes successfully used for each time step. 'tcur' vector with the value of t reached for each time step. (will always be at least as large as the input times). 'tolsf' vector of tolerance scale factors, greater than 1.0, computed when a request for too much accuracy was detected. 'tsw' value of t at the time of the last method switch (given for each time step) 'nst' cumulative number of time steps 'nfe' cumulative number of function evaluations for each time step 'nje' cumulative number of jacobian evaluations for each time step 'nqu' a vector of method orders for each successful step. 'imxer' index of the component of largest magnitude in the weighted local error vector (e / ewt) on an error return, -1 otherwise. 'lenrw' the length of the double work array required. 'leniw' the length of integer work array required. 'mused' a vector of method indicators for each successful time step: 1: adams (nonstiff), 2: bdf (stiff) ======= ============================================================ Other Parameters ---------------- ml, mu : int, optional If either of these are not None or non-negative, then the Jacobian is assumed to be banded. These give the number of lower and upper non-zero diagonals in this banded matrix. For the banded case, `Dfun` should return a matrix whose rows contain the non-zero bands (starting with the lowest diagonal). Thus, the return matrix `jac` from `Dfun` should have shape ``(ml + mu + 1, len(y0))`` when ``ml >=0`` or ``mu >=0``. The data in `jac` must be stored such that ``jac[i - j + mu, j]`` holds the derivative of the `i`th equation with respect to the `j`th state variable. If `col_deriv` is True, the transpose of this `jac` must be returned. rtol, atol : float, optional The input parameters `rtol` and `atol` determine the error control performed by the solver. The solver will control the vector, e, of estimated local errors in y, according to an inequality of the form ``max-norm of (e / ewt) <= 1``, where ewt is a vector of positive error weights computed as ``ewt = rtol * abs(y) + atol``. rtol and atol can be either vectors the same length as y or scalars. Defaults to 1.49012e-8. tcrit : ndarray, optional Vector of critical points (e.g. singularities) where integration care should be taken. h0 : float, (0: solver-determined), optional The step size to be attempted on the first step. hmax : float, (0: solver-determined), optional The maximum absolute step size allowed. hmin : float, (0: solver-determined), optional The minimum absolute step size allowed. ixpr : bool, optional Whether to generate extra printing at method switches. mxstep : int, (0: solver-determined), optional Maximum number of (internally defined) steps allowed for each integration point in t. mxhnil : int, (0: solver-determined), optional Maximum number of messages printed. mxordn : int, (0: solver-determined), optional Maximum order to be allowed for the non-stiff (Adams) method. mxords : int, (0: solver-determined), optional Maximum order to be allowed for the stiff (BDF) method. See Also -------- ode : a more object-oriented integrator based on VODE. quad : for finding the area under a curve. Examples -------- The second order differential equation for the angle `theta` of a pendulum acted on by gravity with friction can be written:: theta''(t) + b*theta'(t) + c*sin(theta(t)) = 0 where `b` and `c` are positive constants, and a prime (') denotes a derivative. To solve this equation with `odeint`, we must first convert it to a system of first order equations. By defining the angular velocity ``omega(t) = theta'(t)``, we obtain the system:: theta'(t) = omega(t) omega'(t) = -b*omega(t) - c*sin(theta(t)) Let `y` be the vector [`theta`, `omega`]. We implement this system in python as: >>> def pend(y, t, b, c): ... theta, omega = y ... dydt = [omega, -b*omega - c*np.sin(theta)] ... return dydt ... We assume the constants are `b` = 0.25 and `c` = 5.0: >>> b = 0.25 >>> c = 5.0 For initial conditions, we assume the pendulum is nearly vertical with `theta(0)` = `pi` - 0.1, and it initially at rest, so `omega(0)` = 0. Then the vector of initial conditions is >>> y0 = [np.pi - 0.1, 0.0] We generate a solution 101 evenly spaced samples in the interval 0 <= `t` <= 10. So our array of times is: >>> t = np.linspace(0, 10, 101) Call `odeint` to generate the solution. To pass the parameters `b` and `c` to `pend`, we give them to `odeint` using the `args` argument. >>> from scipy.integrate import odeint >>> sol = odeint(pend, y0, t, args=(b, c)) The solution is an array with shape (101, 2). The first column is `theta(t)`, and the second is `omega(t)`. The following code plots both components. >>> import matplotlib.pyplot as plt >>> plt.plot(t, sol[:, 0], 'b', label='theta(t)') >>> plt.plot(t, sol[:, 1], 'g', label='omega(t)') >>> plt.legend(loc='best') >>> plt.xlabel('t') >>> plt.grid() >>> plt.show() """ if ml is None: ml = -1 # changed to zero inside function call if mu is None: mu = -1 # changed to zero inside function call t = copy(t) y0 = copy(y0) output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu, full_output, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords) if output[-1] < 0: warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information." warnings.warn(warning_msg, ODEintWarning) elif printmessg: warning_msg = _msgs[output[-1]] warnings.warn(warning_msg, ODEintWarning) if full_output: output[1]['message'] = _msgs[output[-1]] output = output[:-1] if len(output) == 1: return output[0] else: return output
mit
blab/antibody-response-pulse
bcell-array/code/Virus_Bcell_IgM_IgG_Infection_OAS_new.py
1
13195
# coding: utf-8 # # Antibody Response Pulse # https://github.com/blab/antibody-response-pulse # # ### B-cells evolution --- cross-reactive antibody response after influenza virus infection or vaccination # ### Adaptive immune response for repeated infection # In[3]: ''' author: Alvason Zhenhua Li date: 04/09/2015 ''' get_ipython().magic(u'matplotlib inline') import numpy as np import matplotlib.pyplot as plt import os from matplotlib.ticker import FuncFormatter import alva_machinery_event_OAS_new as alva AlvaFontSize = 23 AlvaFigSize = (15, 5) numberingFig = 0 # plotting dir_path = '/Users/al/Desktop/GitHub/antibody-response-pulse/bcell-array/figure' file_name = 'Virus-Bcell-IgM-IgG' figure_name = '-equation' file_suffix = '.png' save_figure = os.path.join(dir_path, file_name + figure_name + file_suffix) numberingFig = numberingFig + 1 plt.figure(numberingFig, figsize=(12, 5)) plt.axis('off') plt.title(r'$ Virus-Bcell-IgM-IgG \ equations \ (antibody-response \ for \ repeated-infection) $' , fontsize = AlvaFontSize) plt.text(0, 7.0/9, r'$ \frac{\partial V_n(t)}{\partial t} = +\mu_{v}V_{n}(t)(1 - \frac{V_n(t)}{V_{max}}) - \phi_{m} M_{n}(t) V_{n}(t) - \phi_{g} G_{n}(t) V_{n}(t) $' , fontsize = 1.2*AlvaFontSize) plt.text(0, 5.0/9, r'$ \frac{\partial B_n(t)}{\partial t} = +\mu_{b}V_{n}(t)(1 - \frac{V_n(t)}{V_{max}}) + (\beta_{m} + \beta_{g}) V_{n}(t) B_{n}(t) - \mu_{b} B_{n}(t) + m_b V_{n}(t)\frac{B_{i-1}(t) - 2B_i(t) + B_{i+1}(t)}{(\Delta i)^2} $' , fontsize = 1.2*AlvaFontSize) plt.text(0, 3.0/9,r'$ \frac{\partial M_n(t)}{\partial t} = +\xi_{m} B_{n}(t) - \phi_{m} M_{n}(t) V_{n}(t) - \mu_{m} M_{n}(t) $' , fontsize = 1.2*AlvaFontSize) plt.text(0, 1.0/9,r'$ \frac{\partial G_n(t)}{\partial t} = +\xi_{g} B_{n}(t) - \phi_{g} G_{n}(t) V_{n}(t) - \mu_{g} G_{n}(t) + m_a V_{n}(t)\frac{G_{i-1}(t) - 2G_i(t) + G_{i+1}(t)}{(\Delta i)^2} $' , fontsize = 1.2*AlvaFontSize) plt.savefig(save_figure, dpi = 100) plt.show() # define the V-M-G partial differential equations def dVdt_array(VBMGxt = [], *args): # naming V = VBMGxt[0] B = VBMGxt[1] M = VBMGxt[2] G = VBMGxt[3] x_totalPoint = VBMGxt.shape[1] # there are n dSdt dV_dt_array = np.zeros(x_totalPoint) # each dSdt with the same equation form dV_dt_array[:] = +inRateV*V[:]*(1 - V[:]/maxV) - killRateVm*M[:]*V[:] - killRateVg*G[:]*V[:] return(dV_dt_array) def dBdt_array(VBMGxt = [], *args): # naming V = VBMGxt[0] B = VBMGxt[1] M = VBMGxt[2] G = VBMGxt[3] x_totalPoint = VBMGxt.shape[1] # there are n dSdt dB_dt_array = np.zeros(x_totalPoint) # each dSdt with the same equation form Bcopy = np.copy(B) centerX = Bcopy[:] leftX = np.roll(Bcopy[:], 1) rightX = np.roll(Bcopy[:], -1) leftX[0] = centerX[0] rightX[-1] = centerX[-1] dB_dt_array[:] = +inRateB*V[:]*(1 - V[:]/maxV) + (actRateBm + alva.event_active + alva.event_OAS_B)*V[:]*B[:] - outRateB*B[:] + mutatRateB*V[:]*(leftX[:] - 2*centerX[:] + rightX[:])/(dx**2) return(dB_dt_array) def dMdt_array(VBMGxt = [], *args): # naming V = VBMGxt[0] B = VBMGxt[1] M = VBMGxt[2] G = VBMGxt[3] x_totalPoint = VBMGxt.shape[1] # there are n dSdt dM_dt_array = np.zeros(x_totalPoint) # each dSdt with the same equation form dM_dt_array[:] = +inRateM*B[:] - consumeRateM*M[:]*V[:] - outRateM*M[:] return(dM_dt_array) def dGdt_array(VBMGxt = [], *args): # naming V = VBMGxt[0] B = VBMGxt[1] M = VBMGxt[2] G = VBMGxt[3] x_totalPoint = VBMGxt.shape[1] # there are n dSdt dG_dt_array = np.zeros(x_totalPoint) # each dSdt with the same equation form Gcopy = np.copy(G) centerX = Gcopy[:] leftX = np.roll(Gcopy[:], 1) rightX = np.roll(Gcopy[:], -1) leftX[0] = centerX[0] rightX[-1] = centerX[-1] dG_dt_array[:] = +(inRateG + alva.event_OAS)*B[:] - consumeRateG*G[:]*V[:] - outRateG*G[:] + mutatRateA*(leftX[:] - 2*centerX[:] + rightX[:])/(dx**2) return(dG_dt_array) # In[7]: # setting parameter timeUnit = 'day' if timeUnit == 'hour': hour = float(1) day = float(24) elif timeUnit == 'day': day = float(1) hour = float(1)/24 elif timeUnit == 'year': year = float(1) day = float(1)/365 hour = float(1)/24/365 maxV = float(50) # max virus/micro-liter inRateV = 0.2/hour # in-rate of virus killRateVm = 0.0003/hour # kill-rate of virus by antibody-IgM killRateVg = killRateVm # kill-rate of virus by antibody-IgG inRateB = 0.06/hour # in-rate of B-cell outRateB = inRateB/8 # out-rate of B-cell actRateBm = killRateVm # activation rate of naive B-cell inRateM = 0.16/hour # in-rate of antibody-IgM from naive B-cell outRateM = inRateM/1 # out-rate of antibody-IgM from naive B-cell consumeRateM = killRateVm # consume-rate of antibody-IgM by cleaning virus inRateG = inRateM/10 # in-rate of antibody-IgG from memory B-cell outRateG = outRateM/250 # out-rate of antibody-IgG from memory B-cell consumeRateG = killRateVg # consume-rate of antibody-IgG by cleaning virus mutatRateB = 0.00003/hour # B-cell mutation rate mutatRateA = 0.0001/hour # antibody mutation rate mutatRateB = 0.0000/hour # B-cell mutation rate mutatRateA = 0.000/hour # antibody mutation rate # time boundary and griding condition minT = float(0) maxT = float(6*28*day) totalPoint_T = int(1*10**3 + 1) gT = np.linspace(minT, maxT, totalPoint_T) spacingT = np.linspace(minT, maxT, num = totalPoint_T, retstep = True) gT = spacingT[0] dt = spacingT[1] # space boundary and griding condition minX = float(0) maxX = float(3) totalPoint_X = int(maxX - minX + 1) gX = np.linspace(minX, maxX, totalPoint_X) gridingX = np.linspace(minX, maxX, num = totalPoint_X, retstep = True) gX = gridingX[0] dx = gridingX[1] gV_array = np.zeros([totalPoint_X, totalPoint_T]) gB_array = np.zeros([totalPoint_X, totalPoint_T]) gM_array = np.zeros([totalPoint_X, totalPoint_T]) gG_array = np.zeros([totalPoint_X, totalPoint_T]) # initial output condition #gV_array[1, 0] = float(2) #[pre-parameter, post-parameter, recovered-day, OAS+, OSA-] actRateBg_1st = 0.0002/hour # activation rate of memory B-cell at 1st time (pre-) actRateBg_2nd = actRateBg_1st*10 # activation rate of memory B-cell at 2nd time (post-) origin_virus = int(1) current_virus = int(2) event_parameter = np.array([[actRateBg_1st, actRateBg_2nd, 14*day, +5/hour, -actRateBm - actRateBg_1st + (actRateBm + actRateBg_1st)/3, origin_virus, current_virus]]) # [viral population, starting time, first] # [viral population, starting time] ---first infection_period = 1*28*day viral_population = np.zeros(int(maxX + 1)) viral_population[origin_virus:current_virus + 1] = 3 infection_starting_time = np.arange(int(maxX + 1))*infection_period event_1st = np.zeros([int(maxX + 1), 2]) event_1st[:, 0] = viral_population event_1st[:, 1] = infection_starting_time print ('event_1st = {:}'.format(event_1st)) # [viral population, starting time] ---2nd] viral_population = np.zeros(int(maxX + 1)) viral_population[origin_virus:current_virus + 1] = 0 infection_starting_time = np.arange(int(maxX + 1))*0 event_2nd = np.zeros([int(maxX + 1), 2]) event_2nd[:, 0] = viral_population event_2nd[:, 1] = infection_starting_time print ('event_2nd = {:}'.format(event_2nd)) event_table = np.array([event_parameter, event_1st, event_2nd]) # Runge Kutta numerical solution pde_array = np.array([dVdt_array, dBdt_array, dMdt_array, dGdt_array]) initial_Out = np.array([gV_array, gB_array, gM_array, gG_array]) gOut_array = alva.AlvaRungeKutta4XT(pde_array, initial_Out, minX, maxX, totalPoint_X, minT, maxT, totalPoint_T, event_table) # plotting gV = gOut_array[0] gB = gOut_array[1] gM = gOut_array[2] gG = gOut_array[3] numberingFig = numberingFig + 1 for i in range(totalPoint_X): figure_name = '-response-%i'%(i) figure_suffix = '.png' save_figure = os.path.join(dir_path, file_name + figure_name + file_suffix) plt.figure(numberingFig, figsize = AlvaFigSize) plt.plot(gT, gV[i], color = 'red', label = r'$ V_{%i}(t) $'%(i), linewidth = 3.0, alpha = 0.5) plt.plot(gT, gM[i], color = 'blue', label = r'$ IgM_{%i}(t) $'%(i), linewidth = 3.0, alpha = 0.5) plt.plot(gT, gG[i], color = 'green', label = r'$ IgG_{%i}(t) $'%(i), linewidth = 3.0, alpha = 0.5) plt.plot(gT, gM[i] + gG[i], color = 'gray', linewidth = 5.0, alpha = 0.5, linestyle = 'dashed' , label = r'$ IgM_{%i}(t) + IgG_{%i}(t) $'%(i, i)) plt.grid(True, which = 'both') plt.title(r'$ Antibody \ from \ Virus-{%i} $'%(i), fontsize = AlvaFontSize) plt.xlabel(r'$time \ (%s)$'%(timeUnit), fontsize = AlvaFontSize) plt.ylabel(r'$ Neutralization \ \ titer $', fontsize = AlvaFontSize) plt.xlim([minT, maxT]) plt.xticks(fontsize = AlvaFontSize*0.6) plt.yticks(fontsize = AlvaFontSize*0.6) plt.ylim([2**0, 2**14]) plt.yscale('log', basey = 2) plt.legend(loc = (1,0), fontsize = AlvaFontSize) plt.savefig(save_figure, dpi = 100) plt.show() # In[5]: # Experimental lab data from OAS paper gT_lab = np.array([28, 28 + 7, 28 + 14, 28 + 28]) + 28 gPR8_lab = np.array([2**(9 + 1.0/10), 2**(13 - 1.0/5), 2**(13 + 1.0/3), 2**(13 - 1.0/4)]) standard_PR8 = gPR8_lab**(3.0/4) gFM1_lab = np.array([0, 2**(6 - 1.0/5), 2**(7 - 1.0/4), 2**(8 + 1.0/4)]) standard_FM1 = gFM1_lab**(3.0/4) bar_width = 2.0 # Sequential immunization graph numberingFig = numberingFig + 1 plt.figure(numberingFig, figsize = (12, 6)) plt.subplot(111) plt.plot(gT, (gM[origin_virus] + gG[origin_virus]), linewidth = 5.0, alpha = 0.5, color = 'gray' , label = r'$ Origin-virus $') plt.plot(gT, (gM[origin_virus + 1] + gG[origin_virus + 1]), linewidth = 5.0, alpha = 0.5, color = 'red' , label = r'$ Subsequence-virus $') plt.bar(gT_lab - bar_width/2, gPR8_lab, bar_width, alpha = 0.6, color = 'gray', yerr = standard_PR8 , error_kw = dict(elinewidth = 1, ecolor = 'black'), label = r'$ PR8-virus $') plt.bar(gT_lab + bar_width/2, gFM1_lab, bar_width, alpha = 0.6, color = 'red', yerr = standard_FM1 , error_kw = dict(elinewidth = 1, ecolor = 'black'), label = r'$ FM1-virus $') plt.grid(True, which = 'both') plt.title(r'$ Original \ Antigenic \ Sin \ (sequential-infection)$', fontsize = AlvaFontSize) plt.xlabel(r'$time \ (%s)$'%(timeUnit), fontsize = AlvaFontSize) plt.ylabel(r'$ Neutralization \ \ titer $', fontsize = AlvaFontSize) plt.xticks(fontsize = AlvaFontSize*0.6) plt.yticks(fontsize = AlvaFontSize*0.6) plt.xlim([minT, 6*30*day]) plt.ylim([2**5, 2**14]) plt.yscale('log', basey = 2) # gca()---GetCurrentAxis and Format the ticklabel to be 2**x plt.gca().yaxis.set_major_formatter(FuncFormatter(lambda x, pos: int(2**(np.log(x)/np.log(2))))) #plt.gca().xaxis.set_major_locator(plt.MultipleLocator(7)) plt.legend(loc = (1, 0), fontsize = AlvaFontSize) plt.show() # In[6]: # Experimental lab data from OAS paper gT_lab = np.array([28, 28 + 7, 28 + 14, 28 + 28]) + 28 gPR8_lab = np.array([2**(9 + 1.0/10), 2**(13 - 1.0/5), 2**(13 + 1.0/3), 2**(13 - 1.0/4)]) standard_PR8 = gPR8_lab**(3.0/4) gFM1_lab = np.array([0, 2**(6 - 1.0/5), 2**(7 - 1.0/4), 2**(8 + 1.0/4)]) standard_FM1 = gFM1_lab**(3.0/4) bar_width = 1.0 # Sequential immunization graph figure_name = '-Original-Antigenic-Sin-infection' figure_suffix = '.png' save_figure = os.path.join(dir_path, file_name + figure_name + file_suffix) numberingFig = numberingFig + 1 plt.figure(numberingFig, figsize = (12, 6)) plt.subplot(111) plt.plot(gT, (gM[origin_virus] + gG[origin_virus]), linewidth = 5.0, alpha = 0.5, color = 'gray' , label = r'$ Origin-virus $') plt.plot(gT, (gM[origin_virus + 1] + gG[origin_virus + 1]), linewidth = 5.0, alpha = 0.5, color = 'red' , label = r'$ Subsequence-virus $') plt.bar(gT_lab - bar_width/2, gPR8_lab, bar_width, alpha = 0.6, color = 'gray', yerr = standard_PR8 , error_kw = dict(elinewidth = 1, ecolor = 'black'), label = r'$ PR8-virus $') plt.bar(gT_lab + bar_width/2, gFM1_lab, bar_width, alpha = 0.6, color = 'red', yerr = standard_FM1 , error_kw = dict(elinewidth = 1, ecolor = 'black'), label = r'$ FM1-virus $') plt.grid(True, which = 'both') plt.title(r'$ Original \ Antigenic \ Sin \ (sequential-infection)$', fontsize = AlvaFontSize) plt.xlabel(r'$time \ (%s)$'%(timeUnit), fontsize = AlvaFontSize) plt.ylabel(r'$ Neutralization \ \ titer $', fontsize = AlvaFontSize) plt.xticks(fontsize = AlvaFontSize*0.6) plt.yticks(fontsize = AlvaFontSize*0.6) plt.xlim([minT, 3*30*day]) plt.ylim([2**5, 2**14]) plt.yscale('log', basey = 2) # gca()---GetCurrentAxis and Format the ticklabel to be 2**x plt.gca().yaxis.set_major_formatter(FuncFormatter(lambda x, pos: int(2**(np.log(x)/np.log(2))))) plt.gca().xaxis.set_major_locator(plt.MultipleLocator(7)) plt.legend(loc = (1, 0), fontsize = AlvaFontSize) plt.savefig(save_figure, dpi = 100, bbox_inches='tight') plt.show() # In[ ]:
gpl-2.0
adhix11/pmtk3
python/demos/linregDemo1.py
26
1104
#!/usr/bin/python2.4 import numpy import scipy.stats import matplotlib.pyplot as plt def main(): # true parameters w = 2 w0 = 3 sigma = 2 # make data numpy.random.seed(1) Ntrain = 20 xtrain = numpy.linspace(0,10,Ntrain) ytrain = w*xtrain + w0 + numpy.random.random(Ntrain)*sigma Ntest = 100 xtest = numpy.linspace(0,10,Ntest) ytest = w*xtest + w0 + numpy.random.random(Ntest)*sigma # from http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/python/lin_reg/ # fit west, w0est, r_value, p_value, std_err = scipy.stats.linregress(xtrain, ytrain) # display print "Param \t True \t Est" print "w0 \t %5.3f \t %5.3f" % (w0, w0est) print "w \t %5.3f \t %5.3f" % (w, west) # plot plt.close() plt.plot(xtrain, ytrain, 'ro') plt.hold(True) #plt.plot(xtest, ytest, 'ka-') ytestPred = west*xtest + w0est #ndx = range(0, Ntest, 10) #h = plt.plot(xtest[ndx], ytestPred[ndx], 'b*') h = plt.plot(xtest, ytestPred, 'b-') plt.setp(h, 'markersize', 12) if __name__ == '__main__': main()
mit
marcusmueller/gnuradio
gr-filter/examples/fft_filter_ccc.py
7
4367
#!/usr/bin/env python # # Copyright 2013 Free Software Foundation, Inc. # # This file is part of GNU Radio # # GNU Radio is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3, or (at your option) # any later version. # # GNU Radio is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with GNU Radio; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, # Boston, MA 02110-1301, USA. # from __future__ import print_function from __future__ import division from __future__ import unicode_literals from gnuradio import gr, filter from gnuradio import analog from gnuradio import blocks from gnuradio import eng_notation from gnuradio.eng_arg import eng_float, intx from argparse import ArgumentParser import sys import numpy try: from matplotlib import pyplot except ImportError: print("Error: could not from matplotlib import pyplot (http://matplotlib.sourceforge.net/)") sys.exit(1) class example_fft_filter_ccc(gr.top_block): def __init__(self, N, fs, bw0, bw1, tw, atten, D): gr.top_block.__init__(self) self._nsamps = N self._fs = fs self._bw0 = bw0 self._bw1 = bw1 self._tw = tw self._at = atten self._decim = D taps = filter.firdes.complex_band_pass_2(1, self._fs, self._bw0, self._bw1, self._tw, self._at) print("Num. Taps: ", len(taps)) self.src = analog.noise_source_c(analog.GR_GAUSSIAN, 1) self.head = blocks.head(gr.sizeof_gr_complex, self._nsamps) self.filt0 = filter.fft_filter_ccc(self._decim, taps) self.vsnk_src = blocks.vector_sink_c() self.vsnk_out = blocks.vector_sink_c() self.connect(self.src, self.head, self.vsnk_src) self.connect(self.head, self.filt0, self.vsnk_out) def main(): parser = ArgumentParser(conflict_handler="resolve") parser.add_argument("-N", "--nsamples", type=int, default=10000, help="Number of samples to process [default=%(default)r]") parser.add_argument("-s", "--samplerate", type=eng_float, default=8000, help="System sample rate [default=%(default)r]") parser.add_argument("-S", "--start-pass", type=eng_float, default=1000, help="Start of Passband [default=%(default)r]") parser.add_argument("-E", "--end-pass", type=eng_float, default=2000, help="End of Passband [default=%(default)r]") parser.add_argument("-T", "--transition", type=eng_float, default=100, help="Transition band [default=%(default)r]") parser.add_argument("-A", "--attenuation", type=eng_float, default=80, help="Stopband attenuation [default=%(default)r]") parser.add_argument("-D", "--decimation", type=int, default=1, help="Decmation factor [default=%(default)r]") args = parser.parse_args() put = example_fft_filter_ccc(args.nsamples, args.samplerate, args.start_pass, args.end_pass, args.transition, args.attenuation, args.decimation) put.run() data_src = numpy.array(put.vsnk_src.data()) data_snk = numpy.array(put.vsnk_out.data()) # Plot the signals PSDs nfft = 1024 f1 = pyplot.figure(1, figsize=(12,10)) s1 = f1.add_subplot(1,1,1) s1.psd(data_src, NFFT=nfft, noverlap=nfft / 4, Fs=args.samplerate) s1.psd(data_snk, NFFT=nfft, noverlap=nfft / 4, Fs=args.samplerate) f2 = pyplot.figure(2, figsize=(12,10)) s2 = f2.add_subplot(1,1,1) s2.plot(data_src) s2.plot(data_snk.real, 'g') pyplot.show() if __name__ == "__main__": try: main() except KeyboardInterrupt: pass
gpl-3.0
devs1991/test_edx_docmode
venv/lib/python2.7/site-packages/sklearn/lda.py
3
9301
""" The :mod:`sklearn.lda` module implements Linear Discriminant Analysis (LDA). """ # Authors: Matthieu Perrot # Mathieu Blondel import warnings import numpy as np from scipy import linalg from .base import BaseEstimator, ClassifierMixin, TransformerMixin from .utils.extmath import logsumexp from .utils.fixes import unique from .utils import check_arrays __all__ = ['LDA'] class LDA(BaseEstimator, ClassifierMixin, TransformerMixin): """ Linear Discriminant Analysis (LDA) A classifier with a linear decision boundary, generated by fitting class conditional densities to the data and using Bayes' rule. The model fits a Gaussian density to each class, assuming that all classes share the same covariance matrix. The fitted model can also be used to reduce the dimensionality of the input, by projecting it to the most discriminative directions. Parameters ---------- n_components: int Number of components (< n_classes - 1) for dimensionality reduction priors : array, optional, shape = [n_classes] Priors on classes Attributes ---------- `means_` : array-like, shape = [n_classes, n_features] Class means `xbar_` : float, shape = [n_features] Over all mean `priors_` : array-like, shape = [n_classes] Class priors (sum to 1) `covariance_` : array-like, shape = [n_features, n_features] Covariance matrix (shared by all classes) Examples -------- >>> import numpy as np >>> from sklearn.lda import LDA >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) >>> y = np.array([1, 1, 1, 2, 2, 2]) >>> clf = LDA() >>> clf.fit(X, y) LDA(n_components=None, priors=None) >>> print(clf.predict([[-0.8, -1]])) [1] See also -------- sklearn.qda.QDA: Quadratic discriminant analysis """ def __init__(self, n_components=None, priors=None): self.n_components = n_components self.priors = np.asarray(priors) if priors is not None else None if self.priors is not None: if (self.priors < 0).any(): raise ValueError('priors must be non-negative') if self.priors.sum() != 1: print 'warning: the priors do not sum to 1. Renormalizing' self.priors = self.priors / self.priors.sum() def fit(self, X, y, store_covariance=False, tol=1.0e-4): """ Fit the LDA model according to the given training data and parameters. Parameters ---------- X : array-like, shape = [n_samples, n_features] Training vector, where n_samples in the number of samples and n_features is the number of features. y : array, shape = [n_samples] Target values (integers) store_covariance : boolean If True the covariance matrix (shared by all classes) is computed and stored in `self.covariance_` attribute. """ X, y = check_arrays(X, y, sparse_format='dense') self.classes_, y = unique(y, return_inverse=True) n_samples, n_features = X.shape n_classes = len(self.classes_) if n_classes < 2: raise ValueError('y has less than 2 classes') if self.priors is None: self.priors_ = np.bincount(y) / float(n_samples) else: self.priors_ = self.priors # Group means n_classes*n_features matrix means = [] Xc = [] cov = None if store_covariance: cov = np.zeros((n_features, n_features)) for ind in xrange(n_classes): Xg = X[y == ind, :] meang = Xg.mean(0) means.append(meang) # centered group data Xgc = Xg - meang Xc.append(Xgc) if store_covariance: cov += np.dot(Xgc.T, Xgc) if store_covariance: cov /= (n_samples - n_classes) self.covariance_ = cov self.means_ = np.asarray(means) Xc = np.concatenate(Xc, 0) # ---------------------------- # 1) within (univariate) scaling by with classes std-dev std = Xc.std(axis=0) # avoid division by zero in normalization std[std == 0] = 1. fac = float(1) / (n_samples - n_classes) # ---------------------------- # 2) Within variance scaling X = np.sqrt(fac) * (Xc / std) # SVD of centered (within)scaled data U, S, V = linalg.svd(X, full_matrices=0) rank = np.sum(S > tol) if rank < n_features: warnings.warn("Variables are collinear") # Scaling of within covariance is: V' 1/S scaling = (V[:rank] / std).T / S[:rank] ## ---------------------------- ## 3) Between variance scaling # Overall mean xbar = np.dot(self.priors_, self.means_) # Scale weighted centers X = np.dot(((np.sqrt((n_samples * self.priors_) * fac)) * (means - xbar).T).T, scaling) # Centers are living in a space with n_classes-1 dim (maximum) # Use svd to find projection in the space spanned by the # (n_classes) centers _, S, V = linalg.svd(X, full_matrices=0) rank = np.sum(S > tol * S[0]) # compose the scalings self.scaling = np.dot(scaling, V.T[:, :rank]) self.xbar_ = xbar # weight vectors / centroids self.coef_ = np.dot(self.means_ - self.xbar_, self.scaling) self.intercept_ = -0.5 * np.sum(self.coef_ ** 2, axis=1) + \ np.log(self.priors_) return self @property def classes(self): warnings.warn("LDA.classes is deprecated and will be removed in 0.14. " "Use LDA.classes_ instead.", DeprecationWarning, stacklevel=2) return self.classes_ def _decision_function(self, X): X = np.asarray(X) # center and scale data X = np.dot(X - self.xbar_, self.scaling) return np.dot(X, self.coef_.T) + self.intercept_ def decision_function(self, X): """ This function return the decision function values related to each class on an array of test vectors X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- C : array, shape = [n_samples, n_classes] or [n_samples,] Decision function values related to each class, per sample. In the two-class case, the shape is [n_samples,], giving the log likelihood ratio of the positive class. """ dec_func = self._decision_function(X) if len(self.classes_) == 2: return dec_func[:, 1] - dec_func[:, 0] return dec_func def transform(self, X): """ Project the data so as to maximize class separation (large separation between projected class means and small variance within each class). Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- X_new : array, shape = [n_samples, n_components] """ X = np.asarray(X) # center and scale data X = np.dot(X - self.xbar_, self.scaling) n_comp = X.shape[1] if self.n_components is None else self.n_components return np.dot(X, self.coef_[:n_comp].T) def predict(self, X): """ This function does classification on an array of test vectors X. The predicted class C for each sample in X is returned. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- C : array, shape = [n_samples] """ d = self._decision_function(X) y_pred = self.classes_.take(d.argmax(1)) return y_pred def predict_proba(self, X): """ This function return posterior probabilities of classification according to each class on an array of test vectors X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- C : array, shape = [n_samples, n_classes] """ values = self._decision_function(X) # compute the likelihood of the underlying gaussian models # up to a multiplicative constant. likelihood = np.exp(values - values.max(axis=1)[:, np.newaxis]) # compute posterior probabilities return likelihood / likelihood.sum(axis=1)[:, np.newaxis] def predict_log_proba(self, X): """ This function return posterior log-probabilities of classification according to each class on an array of test vectors X. Parameters ---------- X : array-like, shape = [n_samples, n_features] Returns ------- C : array, shape = [n_samples, n_classes] """ values = self._decision_function(X) loglikelihood = (values - values.max(axis=1)[:, np.newaxis]) normalization = logsumexp(loglikelihood, axis=1) return loglikelihood - normalization[:, np.newaxis]
agpl-3.0
grimfang/panda3d
samples/carousel/main.py
25
9571
#!/usr/bin/env python # Author: Shao Zhang, Phil Saltzman, and Eddie Canaan # Last Updated: 2015-03-13 # # This tutorial will demonstrate some uses for intervals in Panda # to move objects in your panda world. # Intervals are tools that change a value of something, like position, # rotation or anything else, linearly, over a set period of time. They can be # also be combined to work in sequence or in Parallel # # In this lesson, we will simulate a carousel in motion using intervals. # The carousel will spin using an hprInterval while 4 pandas will represent # the horses on a traditional carousel. The 4 pandas will rotate with the # carousel and also move up and down on their poles using a LerpFunc interval. # Finally there will also be lights on the outer edge of the carousel that # will turn on and off by switching their texture with intervals in Sequence # and Parallel from direct.showbase.ShowBase import ShowBase from panda3d.core import AmbientLight, DirectionalLight, LightAttrib from panda3d.core import NodePath from panda3d.core import LVector3 from direct.interval.IntervalGlobal import * # Needed to use Intervals from direct.gui.DirectGui import * # Importing math constants and functions from math import pi, sin class CarouselDemo(ShowBase): def __init__(self): # Initialize the ShowBase class from which we inherit, which will # create a window and set up everything we need for rendering into it. ShowBase.__init__(self) # This creates the on screen title that is in every tutorial self.title = OnscreenText(text="Panda3D: Tutorial - Carousel", parent=base.a2dBottomCenter, fg=(1, 1, 1, 1), shadow=(0, 0, 0, .5), pos=(0, .1), scale=.1) base.disableMouse() # Allow manual positioning of the camera camera.setPosHpr(0, -8, 2.5, 0, -9, 0) # Set the cameras' position # and orientation self.loadModels() # Load and position our models self.setupLights() # Add some basic lighting self.startCarousel() # Create the needed intervals and put the # carousel into motion def loadModels(self): # Load the carousel base self.carousel = loader.loadModel("models/carousel_base") self.carousel.reparentTo(render) # Attach it to render # Load the modeled lights that are on the outer rim of the carousel # (not Panda lights) # There are 2 groups of lights. At any given time, one group will have # the "on" texture and the other will have the "off" texture. self.lights1 = loader.loadModel("models/carousel_lights") self.lights1.reparentTo(self.carousel) # Load the 2nd set of lights self.lights2 = loader.loadModel("models/carousel_lights") # We need to rotate the 2nd so it doesn't overlap with the 1st set. self.lights2.setH(36) self.lights2.reparentTo(self.carousel) # Load the textures for the lights. One texture is for the "on" state, # the other is for the "off" state. self.lightOffTex = loader.loadTexture("models/carousel_lights_off.jpg") self.lightOnTex = loader.loadTexture("models/carousel_lights_on.jpg") # Create an list (self.pandas) with filled with 4 dummy nodes attached # to the carousel. # This uses a python concept called "Array Comprehensions." Check the # Python manual for more information on how they work self.pandas = [self.carousel.attachNewNode("panda" + str(i)) for i in range(4)] self.models = [loader.loadModel("models/carousel_panda") for i in range(4)] self.moves = [0] * 4 for i in range(4): # set the position and orientation of the ith panda node we just created # The Z value of the position will be the base height of the pandas. # The headings are multiplied by i to put each panda in its own position # around the carousel self.pandas[i].setPosHpr(0, 0, 1.3, i * 90, 0, 0) # Load the actual panda model, and parent it to its dummy node self.models[i].reparentTo(self.pandas[i]) # Set the distance from the center. This distance is based on the way the # carousel was modeled in Maya self.models[i].setY(.85) # Load the environment (Sky sphere and ground plane) self.env = loader.loadModel("models/env") self.env.reparentTo(render) self.env.setScale(7) # Panda Lighting def setupLights(self): # Create some lights and add them to the scene. By setting the lights on # render they affect the entire scene # Check out the lighting tutorial for more information on lights ambientLight = AmbientLight("ambientLight") ambientLight.setColor((.4, .4, .35, 1)) directionalLight = DirectionalLight("directionalLight") directionalLight.setDirection(LVector3(0, 8, -2.5)) directionalLight.setColor((0.9, 0.8, 0.9, 1)) render.setLight(render.attachNewNode(directionalLight)) render.setLight(render.attachNewNode(ambientLight)) # Explicitly set the environment to not be lit self.env.setLightOff() def startCarousel(self): # Here's where we actually create the intervals to move the carousel # The first type of interval we use is one created directly from a NodePath # This interval tells the NodePath to vary its orientation (hpr) from its # current value (0,0,0) to (360,0,0) over 20 seconds. Intervals created from # NodePaths also exist for position, scale, color, and shear self.carouselSpin = self.carousel.hprInterval(20, LVector3(360, 0, 0)) # Once an interval is created, we need to tell it to actually move. # start() will cause an interval to play once. loop() will tell an interval # to repeat once it finished. To keep the carousel turning, we use # loop() self.carouselSpin.loop() # The next type of interval we use is called a LerpFunc interval. It is # called that becuase it linearly interpolates (aka Lerp) values passed to # a function over a given amount of time. # In this specific case, horses on a carousel don't move contantly up, # suddenly stop, and then contantly move down again. Instead, they start # slowly, get fast in the middle, and slow down at the top. This motion is # close to a sine wave. This LerpFunc calls the function oscillatePanda # (which we will create below), which changes the height of the panda based # on the sin of the value passed in. In this way we achieve non-linear # motion by linearly changing the input to a function for i in range(4): self.moves[i] = LerpFunc( self.oscillatePanda, # function to call duration=3, # 3 second duration fromData=0, # starting value (in radians) toData=2 * pi, # ending value (2pi radians = 360 degrees) # Additional information to pass to # self.oscialtePanda extraArgs=[self.models[i], pi * (i % 2)] ) # again, we want these to play continuously so we start them with # loop() self.moves[i].loop() # Finally, we combine Sequence, Parallel, Func, and Wait intervals, # to schedule texture swapping on the lights to simulate the lights turning # on and off. # Sequence intervals play other intervals in a sequence. In other words, # it waits for the current interval to finish before playing the next # one. # Parallel intervals play a group of intervals at the same time # Wait intervals simply do nothing for a given amount of time # Func intervals simply make a single function call. This is helpful because # it allows us to schedule functions to be called in a larger sequence. They # take virtually no time so they don't cause a Sequence to wait. self.lightBlink = Sequence( # For the first step in our sequence we will set the on texture on one # light and set the off texture on the other light at the same time Parallel( Func(self.lights1.setTexture, self.lightOnTex, 1), Func(self.lights2.setTexture, self.lightOffTex, 1)), Wait(1), # Then we will wait 1 second # Then we will switch the textures at the same time Parallel( Func(self.lights1.setTexture, self.lightOffTex, 1), Func(self.lights2.setTexture, self.lightOnTex, 1)), Wait(1) # Then we will wait another second ) self.lightBlink.loop() # Loop this sequence continuously def oscillatePanda(self, rad, panda, offset): # This is the oscillation function mentioned earlier. It takes in a # degree value, a NodePath to set the height on, and an offset. The # offset is there so that the different pandas can move opposite to # each other. The .2 is the amplitude, so the height of the panda will # vary from -.2 to .2 panda.setZ(sin(rad + offset) * .2) demo = CarouselDemo() demo.run()
bsd-3-clause
harisbal/pandas
pandas/core/tools/datetimes.py
4
30680
from functools import partial from datetime import datetime, time from collections import MutableMapping import numpy as np from pandas._libs import tslib, tslibs from pandas._libs.tslibs.strptime import array_strptime from pandas._libs.tslibs import parsing, conversion, Timestamp from pandas._libs.tslibs.parsing import ( # noqa parse_time_string, DateParseError, _format_is_iso, _guess_datetime_format) from pandas.core.dtypes.common import ( ensure_object, is_datetime64_ns_dtype, is_datetime64_dtype, is_datetime64tz_dtype, is_integer_dtype, is_integer, is_float, is_list_like, is_scalar, is_numeric_dtype, is_object_dtype) from pandas.core.dtypes.generic import ( ABCIndexClass, ABCSeries, ABCDataFrame) from pandas.core.dtypes.missing import notna from pandas.core import algorithms from pandas.compat import zip def _guess_datetime_format_for_array(arr, **kwargs): # Try to guess the format based on the first non-NaN element non_nan_elements = notna(arr).nonzero()[0] if len(non_nan_elements): return _guess_datetime_format(arr[non_nan_elements[0]], **kwargs) def _maybe_cache(arg, format, cache, convert_listlike): """ Create a cache of unique dates from an array of dates Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series format : string Strftime format to parse time cache : boolean True attempts to create a cache of converted values convert_listlike : function Conversion function to apply on dates Returns ------- cache_array : Series Cache of converted, unique dates. Can be empty """ from pandas import Series cache_array = Series() if cache: # Perform a quicker unique check from pandas import Index if not Index(arg).is_unique: unique_dates = algorithms.unique(arg) cache_dates = convert_listlike(unique_dates, True, format) cache_array = Series(cache_dates, index=unique_dates) return cache_array def _convert_and_box_cache(arg, cache_array, box, errors, name=None): """ Convert array of dates with a cache and box the result Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series cache_array : Series Cache of converted, unique dates box : boolean True boxes result as an Index-like, False returns an ndarray errors : string 'ignore' plus box=True will convert result to Index name : string, default None Name for a DatetimeIndex Returns ------- result : datetime of converted dates Returns: - Index-like if box=True - ndarray if box=False """ from pandas import Series, DatetimeIndex, Index result = Series(arg).map(cache_array) if box: if errors == 'ignore': return Index(result, name=name) else: return DatetimeIndex(result, name=name) return result.values def _return_parsed_timezone_results(result, timezones, box, tz, name): """ Return results from array_strptime if a %z or %Z directive was passed. Parameters ---------- result : ndarray int64 date representations of the dates timezones : ndarray pytz timezone objects box : boolean True boxes result as an Index-like, False returns an ndarray tz : object None or pytz timezone object name : string, default None Name for a DatetimeIndex Returns ------- tz_result : ndarray of parsed dates with timezone Returns: - Index-like if box=True - ndarray of Timestamps if box=False """ if tz is not None: raise ValueError("Cannot pass a tz argument when " "parsing strings with timezone " "information.") tz_results = np.array([Timestamp(res).tz_localize(zone) for res, zone in zip(result, timezones)]) if box: from pandas import Index return Index(tz_results, name=name) return tz_results def _convert_listlike_datetimes(arg, box, format, name=None, tz=None, unit=None, errors=None, infer_datetime_format=None, dayfirst=None, yearfirst=None, exact=None): """ Helper function for to_datetime. Performs the conversions of 1D listlike of dates Parameters ---------- arg : list, tuple, ndarray, Series, Index date to be parced box : boolean True boxes result as an Index-like, False returns an ndarray name : object None or string for the Index name tz : object None or 'utc' unit : string None or string of the frequency of the passed data errors : string error handing behaviors from to_datetime, 'raise', 'coerce', 'ignore' infer_datetime_format : boolean inferring format behavior from to_datetime dayfirst : boolean dayfirst parsing behavior from to_datetime yearfirst : boolean yearfirst parsing behavior from to_datetime exact : boolean exact format matching behavior from to_datetime Returns ------- ndarray of parsed dates Returns: - Index-like if box=True - ndarray of Timestamps if box=False """ from pandas import DatetimeIndex if isinstance(arg, (list, tuple)): arg = np.array(arg, dtype='O') # these are shortcutable if is_datetime64tz_dtype(arg): if not isinstance(arg, DatetimeIndex): return DatetimeIndex(arg, tz=tz, name=name) if tz == 'utc': arg = arg.tz_convert(None).tz_localize(tz) return arg elif is_datetime64_ns_dtype(arg): if box and not isinstance(arg, DatetimeIndex): try: return DatetimeIndex(arg, tz=tz, name=name) except ValueError: pass return arg elif unit is not None: if format is not None: raise ValueError("cannot specify both format and unit") arg = getattr(arg, 'values', arg) result = tslib.array_with_unit_to_datetime(arg, unit, errors=errors) if box: if errors == 'ignore': from pandas import Index return Index(result, name=name) return DatetimeIndex(result, tz=tz, name=name) return result elif getattr(arg, 'ndim', 1) > 1: raise TypeError('arg must be a string, datetime, list, tuple, ' '1-d array, or Series') arg = ensure_object(arg) require_iso8601 = False if infer_datetime_format and format is None: format = _guess_datetime_format_for_array(arg, dayfirst=dayfirst) if format is not None: # There is a special fast-path for iso8601 formatted # datetime strings, so in those cases don't use the inferred # format because this path makes process slower in this # special case format_is_iso8601 = _format_is_iso(format) if format_is_iso8601: require_iso8601 = not infer_datetime_format format = None try: result = None if format is not None: # shortcut formatting here if format == '%Y%m%d': try: result = _attempt_YYYYMMDD(arg, errors=errors) except (ValueError, TypeError, tslibs.OutOfBoundsDatetime): raise ValueError("cannot convert the input to " "'%Y%m%d' date format") # fallback if result is None: try: result, timezones = array_strptime( arg, format, exact=exact, errors=errors) if '%Z' in format or '%z' in format: return _return_parsed_timezone_results( result, timezones, box, tz, name) except tslibs.OutOfBoundsDatetime: if errors == 'raise': raise result = arg except ValueError: # if format was inferred, try falling back # to array_to_datetime - terminate here # for specified formats if not infer_datetime_format: if errors == 'raise': raise result = arg if result is None and (format is None or infer_datetime_format): result, tz_parsed = tslib.array_to_datetime( arg, errors=errors, utc=tz == 'utc', dayfirst=dayfirst, yearfirst=yearfirst, require_iso8601=require_iso8601 ) if tz_parsed is not None: if box: # We can take a shortcut since the datetime64 numpy array # is in UTC return DatetimeIndex._simple_new(result, name=name, tz=tz_parsed) else: # Convert the datetime64 numpy array to an numpy array # of datetime objects result = [Timestamp(ts, tz=tz_parsed).to_pydatetime() for ts in result] return np.array(result, dtype=object) if box: # Ensure we return an Index in all cases where box=True if is_datetime64_dtype(result): return DatetimeIndex(result, tz=tz, name=name) elif is_object_dtype(result): # e.g. an Index of datetime objects from pandas import Index return Index(result, name=name) return result except ValueError as e: try: values, tz = conversion.datetime_to_datetime64(arg) return DatetimeIndex._simple_new(values, name=name, tz=tz) except (ValueError, TypeError): raise e def _adjust_to_origin(arg, origin, unit): """ Helper function for to_datetime. Adjust input argument to the specified origin Parameters ---------- arg : list, tuple, ndarray, Series, Index date to be adjusted origin : 'julian' or Timestamp origin offset for the arg unit : string passed unit from to_datetime, must be 'D' Returns ------- ndarray or scalar of adjusted date(s) """ if origin == 'julian': original = arg j0 = Timestamp(0).to_julian_date() if unit != 'D': raise ValueError("unit must be 'D' for origin='julian'") try: arg = arg - j0 except TypeError: raise ValueError("incompatible 'arg' type for given " "'origin'='julian'") # premptively check this for a nice range j_max = Timestamp.max.to_julian_date() - j0 j_min = Timestamp.min.to_julian_date() - j0 if np.any(arg > j_max) or np.any(arg < j_min): raise tslibs.OutOfBoundsDatetime( "{original} is Out of Bounds for " "origin='julian'".format(original=original)) else: # arg must be numeric if not ((is_scalar(arg) and (is_integer(arg) or is_float(arg))) or is_numeric_dtype(np.asarray(arg))): raise ValueError( "'{arg}' is not compatible with origin='{origin}'; " "it must be numeric with a unit specified ".format( arg=arg, origin=origin)) # we are going to offset back to unix / epoch time try: offset = Timestamp(origin) except tslibs.OutOfBoundsDatetime: raise tslibs.OutOfBoundsDatetime( "origin {origin} is Out of Bounds".format(origin=origin)) except ValueError: raise ValueError("origin {origin} cannot be converted " "to a Timestamp".format(origin=origin)) if offset.tz is not None: raise ValueError( "origin offset {} must be tz-naive".format(offset)) offset -= Timestamp(0) # convert the offset to the unit of the arg # this should be lossless in terms of precision offset = offset // tslibs.Timedelta(1, unit=unit) # scalars & ndarray-like can handle the addition if is_list_like(arg) and not isinstance( arg, (ABCSeries, ABCIndexClass, np.ndarray)): arg = np.asarray(arg) arg = arg + offset return arg def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False, origin='unix', cache=False): """ Convert argument to datetime. Parameters ---------- arg : integer, float, string, datetime, list, tuple, 1-d array, Series .. versionadded:: 0.18.1 or DataFrame/dict-like errors : {'ignore', 'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception - If 'coerce', then invalid parsing will be set as NaT - If 'ignore', then invalid parsing will return the input dayfirst : boolean, default False Specify a date parse order if `arg` is str or its list-likes. If True, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10. Warning: dayfirst=True is not strict, but will prefer to parse with day first (this is a known bug, based on dateutil behavior). yearfirst : boolean, default False Specify a date parse order if `arg` is str or its list-likes. - If True parses dates with the year first, eg 10/11/12 is parsed as 2010-11-12. - If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil). Warning: yearfirst=True is not strict, but will prefer to parse with year first (this is a known bug, based on dateutil behavior). .. versionadded:: 0.16.1 utc : boolean, default None Return UTC DatetimeIndex if True (converting any tz-aware datetime.datetime objects as well). box : boolean, default True - If True returns a DatetimeIndex or Index-like object - If False returns ndarray of values. format : string, default None strftime to parse time, eg "%d/%m/%Y", note that "%f" will parse all the way up to nanoseconds. exact : boolean, True by default - If True, require an exact format match. - If False, allow the format to match anywhere in the target string. unit : string, default 'ns' unit of the arg (D,s,ms,us,ns) denote the unit, which is an integer or float number. This will be based off the origin. Example, with unit='ms' and origin='unix' (the default), this would calculate the number of milliseconds to the unix epoch start. infer_datetime_format : boolean, default False If True and no `format` is given, attempt to infer the format of the datetime strings, and if it can be inferred, switch to a faster method of parsing them. In some cases this can increase the parsing speed by ~5-10x. origin : scalar, default is 'unix' Define the reference date. The numeric values would be parsed as number of units (defined by `unit`) since this reference date. - If 'unix' (or POSIX) time; origin is set to 1970-01-01. - If 'julian', unit must be 'D', and origin is set to beginning of Julian Calendar. Julian day number 0 is assigned to the day starting at noon on January 1, 4713 BC. - If Timestamp convertible, origin is set to Timestamp identified by origin. .. versionadded:: 0.20.0 cache : boolean, default False If True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. .. versionadded:: 0.23.0 Returns ------- ret : datetime if parsing succeeded. Return type depends on input: - list-like: DatetimeIndex - Series: Series of datetime64 dtype - scalar: Timestamp In case when it is not possible to return designated types (e.g. when any element of input is before Timestamp.min or after Timestamp.max) return will have datetime.datetime type (or corresponding array/Series). Examples -------- Assembling a datetime from multiple columns of a DataFrame. The keys can be common abbreviations like ['year', 'month', 'day', 'minute', 'second', 'ms', 'us', 'ns']) or plurals of the same >>> df = pd.DataFrame({'year': [2015, 2016], 'month': [2, 3], 'day': [4, 5]}) >>> pd.to_datetime(df) 0 2015-02-04 1 2016-03-05 dtype: datetime64[ns] If a date does not meet the `timestamp limitations <http://pandas.pydata.org/pandas-docs/stable/timeseries.html #timeseries-timestamp-limits>`_, passing errors='ignore' will return the original input instead of raising any exception. Passing errors='coerce' will force an out-of-bounds date to NaT, in addition to forcing non-dates (or non-parseable dates) to NaT. >>> pd.to_datetime('13000101', format='%Y%m%d', errors='ignore') datetime.datetime(1300, 1, 1, 0, 0) >>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce') NaT Passing infer_datetime_format=True can often-times speedup a parsing if its not an ISO8601 format exactly, but in a regular format. >>> s = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000']*1000) >>> s.head() 0 3/11/2000 1 3/12/2000 2 3/13/2000 3 3/11/2000 4 3/12/2000 dtype: object >>> %timeit pd.to_datetime(s,infer_datetime_format=True) 100 loops, best of 3: 10.4 ms per loop >>> %timeit pd.to_datetime(s,infer_datetime_format=False) 1 loop, best of 3: 471 ms per loop Using a unix epoch time >>> pd.to_datetime(1490195805, unit='s') Timestamp('2017-03-22 15:16:45') >>> pd.to_datetime(1490195805433502912, unit='ns') Timestamp('2017-03-22 15:16:45.433502912') .. warning:: For float arg, precision rounding might happen. To prevent unexpected behavior use a fixed-width exact type. Using a non-unix epoch origin >>> pd.to_datetime([1, 2, 3], unit='D', origin=pd.Timestamp('1960-01-01')) 0 1960-01-02 1 1960-01-03 2 1960-01-04 See also -------- pandas.DataFrame.astype : Cast argument to a specified dtype. pandas.to_timedelta : Convert argument to timedelta. """ if arg is None: return None if origin != 'unix': arg = _adjust_to_origin(arg, origin, unit) tz = 'utc' if utc else None convert_listlike = partial(_convert_listlike_datetimes, tz=tz, unit=unit, dayfirst=dayfirst, yearfirst=yearfirst, errors=errors, exact=exact, infer_datetime_format=infer_datetime_format) if isinstance(arg, Timestamp): result = arg elif isinstance(arg, ABCSeries): cache_array = _maybe_cache(arg, format, cache, convert_listlike) if not cache_array.empty: result = arg.map(cache_array) else: from pandas import Series values = convert_listlike(arg._values, True, format) result = Series(values, index=arg.index, name=arg.name) elif isinstance(arg, (ABCDataFrame, MutableMapping)): result = _assemble_from_unit_mappings(arg, errors=errors) elif isinstance(arg, ABCIndexClass): cache_array = _maybe_cache(arg, format, cache, convert_listlike) if not cache_array.empty: result = _convert_and_box_cache(arg, cache_array, box, errors, name=arg.name) else: convert_listlike = partial(convert_listlike, name=arg.name) result = convert_listlike(arg, box, format) elif is_list_like(arg): cache_array = _maybe_cache(arg, format, cache, convert_listlike) if not cache_array.empty: result = _convert_and_box_cache(arg, cache_array, box, errors) else: result = convert_listlike(arg, box, format) else: result = convert_listlike(np.array([arg]), box, format)[0] return result # mappings for assembling units _unit_map = {'year': 'year', 'years': 'year', 'month': 'month', 'months': 'month', 'day': 'day', 'days': 'day', 'hour': 'h', 'hours': 'h', 'minute': 'm', 'minutes': 'm', 'second': 's', 'seconds': 's', 'ms': 'ms', 'millisecond': 'ms', 'milliseconds': 'ms', 'us': 'us', 'microsecond': 'us', 'microseconds': 'us', 'ns': 'ns', 'nanosecond': 'ns', 'nanoseconds': 'ns' } def _assemble_from_unit_mappings(arg, errors): """ assemble the unit specified fields from the arg (DataFrame) Return a Series for actual parsing Parameters ---------- arg : DataFrame errors : {'ignore', 'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception - If 'coerce', then invalid parsing will be set as NaT - If 'ignore', then invalid parsing will return the input Returns ------- Series """ from pandas import to_timedelta, to_numeric, DataFrame arg = DataFrame(arg) if not arg.columns.is_unique: raise ValueError("cannot assemble with duplicate keys") # replace passed unit with _unit_map def f(value): if value in _unit_map: return _unit_map[value] # m is case significant if value.lower() in _unit_map: return _unit_map[value.lower()] return value unit = {k: f(k) for k in arg.keys()} unit_rev = {v: k for k, v in unit.items()} # we require at least Ymd required = ['year', 'month', 'day'] req = sorted(list(set(required) - set(unit_rev.keys()))) if len(req): raise ValueError("to assemble mappings requires at least that " "[year, month, day] be specified: [{required}] " "is missing".format(required=','.join(req))) # keys we don't recognize excess = sorted(list(set(unit_rev.keys()) - set(_unit_map.values()))) if len(excess): raise ValueError("extra keys have been passed " "to the datetime assemblage: " "[{excess}]".format(excess=','.join(excess))) def coerce(values): # we allow coercion to if errors allows values = to_numeric(values, errors=errors) # prevent overflow in case of int8 or int16 if is_integer_dtype(values): values = values.astype('int64', copy=False) return values values = (coerce(arg[unit_rev['year']]) * 10000 + coerce(arg[unit_rev['month']]) * 100 + coerce(arg[unit_rev['day']])) try: values = to_datetime(values, format='%Y%m%d', errors=errors) except (TypeError, ValueError) as e: raise ValueError("cannot assemble the " "datetimes: {error}".format(error=e)) for u in ['h', 'm', 's', 'ms', 'us', 'ns']: value = unit_rev.get(u) if value is not None and value in arg: try: values += to_timedelta(coerce(arg[value]), unit=u, errors=errors) except (TypeError, ValueError) as e: raise ValueError("cannot assemble the datetimes [{value}]: " "{error}".format(value=value, error=e)) return values def _attempt_YYYYMMDD(arg, errors): """ try to parse the YYYYMMDD/%Y%m%d format, try to deal with NaT-like, arg is a passed in as an object dtype, but could really be ints/strings with nan-like/or floats (e.g. with nan) Parameters ---------- arg : passed value errors : 'raise','ignore','coerce' """ def calc(carg): # calculate the actual result carg = carg.astype(object) parsed = parsing.try_parse_year_month_day(carg / 10000, carg / 100 % 100, carg % 100) return tslib.array_to_datetime(parsed, errors=errors)[0] def calc_with_mask(carg, mask): result = np.empty(carg.shape, dtype='M8[ns]') iresult = result.view('i8') iresult[~mask] = tslibs.iNaT masked_result = calc(carg[mask].astype(np.float64).astype(np.int64)) result[mask] = masked_result.astype('M8[ns]') return result # try intlike / strings that are ints try: return calc(arg.astype(np.int64)) except ValueError: pass # a float with actual np.nan try: carg = arg.astype(np.float64) return calc_with_mask(carg, notna(carg)) except ValueError: pass # string with NaN-like try: mask = ~algorithms.isin(arg, list(tslib.nat_strings)) return calc_with_mask(arg, mask) except ValueError: pass return None # Fixed time formats for time parsing _time_formats = ["%H:%M", "%H%M", "%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p", "%I%M%S%p"] def _guess_time_format_for_array(arr): # Try to guess the format based on the first non-NaN element non_nan_elements = notna(arr).nonzero()[0] if len(non_nan_elements): element = arr[non_nan_elements[0]] for time_format in _time_formats: try: datetime.strptime(element, time_format) return time_format except ValueError: pass return None def to_time(arg, format=None, infer_time_format=False, errors='raise'): """ Parse time strings to time objects using fixed strptime formats ("%H:%M", "%H%M", "%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p", "%I%M%S%p") Use infer_time_format if all the strings are in the same format to speed up conversion. Parameters ---------- arg : string in time format, datetime.time, list, tuple, 1-d array, Series format : str, default None Format used to convert arg into a time object. If None, fixed formats are used. infer_time_format: bool, default False Infer the time format based on the first non-NaN element. If all strings are in the same format, this will speed up conversion. errors : {'ignore', 'raise', 'coerce'}, default 'raise' - If 'raise', then invalid parsing will raise an exception - If 'coerce', then invalid parsing will be set as None - If 'ignore', then invalid parsing will return the input Returns ------- datetime.time """ from pandas.core.series import Series def _convert_listlike(arg, format): if isinstance(arg, (list, tuple)): arg = np.array(arg, dtype='O') elif getattr(arg, 'ndim', 1) > 1: raise TypeError('arg must be a string, datetime, list, tuple, ' '1-d array, or Series') arg = ensure_object(arg) if infer_time_format and format is None: format = _guess_time_format_for_array(arg) times = [] if format is not None: for element in arg: try: times.append(datetime.strptime(element, format).time()) except (ValueError, TypeError): if errors == 'raise': msg = ("Cannot convert {element} to a time with given " "format {format}").format(element=element, format=format) raise ValueError(msg) elif errors == 'ignore': return arg else: times.append(None) else: formats = _time_formats[:] format_found = False for element in arg: time_object = None for time_format in formats: try: time_object = datetime.strptime(element, time_format).time() if not format_found: # Put the found format in front fmt = formats.pop(formats.index(time_format)) formats.insert(0, fmt) format_found = True break except (ValueError, TypeError): continue if time_object is not None: times.append(time_object) elif errors == 'raise': raise ValueError("Cannot convert arg {arg} to " "a time".format(arg=arg)) elif errors == 'ignore': return arg else: times.append(None) return times if arg is None: return arg elif isinstance(arg, time): return arg elif isinstance(arg, Series): values = _convert_listlike(arg._values, format) return Series(values, index=arg.index, name=arg.name) elif isinstance(arg, ABCIndexClass): return _convert_listlike(arg, format) elif is_list_like(arg): return _convert_listlike(arg, format) return _convert_listlike(np.array([arg]), format)[0]
bsd-3-clause
ttthy1/2017sejongAI
week14/Mnist.py
1
2273
# Lab 7 Learning rate and Evaluation import tensorflow as tf import random import matplotlib.pyplot as plt tf.set_random_seed(777) # for reproducibility from tensorflow.examples.tutorials.mnist import input_data # Check out https://www.tensorflow.org/get_started/mnist/beginners for # more information about the mnist dataset mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) nb_classes = 10 # MNIST data image of shape 28 * 28 = 784 X = tf.placeholder(tf.float32, [None, 784]) # 0 - 9 digits recognition = 10 classes Y = tf.placeholder(tf.float32, [None, nb_classes]) W = tf.Variable(tf.random_normal([784, nb_classes])) b = tf.Variable(tf.random_normal([nb_classes])) # Hypothesis (using softmax) hypothesis = tf.nn.softmax(tf.matmul(X, W) + b) cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) # Test model is_correct = tf.equal(tf.arg_max(hypothesis, 1), tf.arg_max(Y, 1)) # Calculate accuracy accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) # parameters training_epochs = 15 batch_size = 100 with tf.Session() as sess: # Initialize TensorFlow variables sess.run(tf.global_variables_initializer()) # Training cycle for epoch in range(training_epochs): avg_cost = 0 total_batch = int(mnist.train.num_examples / batch_size) for i in range(total_batch): batch_xs, batch_ys = mnist.train.next_batch(batch_size) c, _ = sess.run([cost, optimizer], feed_dict={ X: batch_xs, Y: batch_ys}) avg_cost += c / total_batch print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost)) print("Learning finished") # Test the model using test sets print("Accuracy: ", accuracy.eval(session=sess, feed_dict={ X: mnist.test.images, Y: mnist.test.labels})) # Get one and predict r = random.randint(0, mnist.test.num_examples - 1) print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1))) print("Prediction: ", sess.run( tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]})) plt.imshow( mnist.test.images[r:r + 1].reshape(28, 28), cmap='Greys', interpolation='nearest') plt.show()
gpl-3.0
jaidevd/scikit-learn
sklearn/externals/joblib/testing.py
23
3042
""" Helper for testing. """ import sys import warnings import os.path import re import subprocess import threading from sklearn.externals.joblib._compat import PY3_OR_LATER def warnings_to_stdout(): """ Redirect all warnings to stdout. """ showwarning_orig = warnings.showwarning def showwarning(msg, cat, fname, lno, file=None, line=0): showwarning_orig(msg, cat, os.path.basename(fname), line, sys.stdout) warnings.showwarning = showwarning #warnings.simplefilter('always') try: from nose.tools import assert_raises_regex except ImportError: # For Python 2.7 try: from nose.tools import assert_raises_regexp as assert_raises_regex except ImportError: # for Python 2.6 def assert_raises_regex(expected_exception, expected_regexp, callable_obj=None, *args, **kwargs): """Helper function to check for message patterns in exceptions""" not_raised = False try: callable_obj(*args, **kwargs) not_raised = True except Exception as e: error_message = str(e) if not re.compile(expected_regexp).search(error_message): raise AssertionError("Error message should match pattern " "%r. %r does not." % (expected_regexp, error_message)) if not_raised: raise AssertionError("Should have raised %r" % expected_exception(expected_regexp)) def check_subprocess_call(cmd, timeout=1, stdout_regex=None, stderr_regex=None): """Runs a command in a subprocess with timeout in seconds. Also checks returncode is zero, stdout if stdout_regex is set, and stderr if stderr_regex is set. """ proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) def kill_process(): proc.kill() timer = threading.Timer(timeout, kill_process) try: timer.start() stdout, stderr = proc.communicate() if PY3_OR_LATER: stdout, stderr = stdout.decode(), stderr.decode() if proc.returncode != 0: message = ( 'Non-zero return code: {0}.\nStdout:\n{1}\n' 'Stderr:\n{2}').format( proc.returncode, stdout, stderr) raise ValueError(message) if (stdout_regex is not None and not re.search(stdout_regex, stdout)): raise ValueError( "Unexpected stdout: {0!r} does not match:\n{1!r}".format( stdout_regex, stdout)) if (stderr_regex is not None and not re.search(stderr_regex, stderr)): raise ValueError( "Unexpected stderr: {0!r} does not match:\n{1!r}".format( stderr_regex, stderr)) finally: timer.cancel()
bsd-3-clause
trungnt13/scikit-learn
sklearn/feature_selection/__init__.py
244
1088
""" The :mod:`sklearn.feature_selection` module implements feature selection algorithms. It currently includes univariate filter selection methods and the recursive feature elimination algorithm. """ from .univariate_selection import chi2 from .univariate_selection import f_classif from .univariate_selection import f_oneway from .univariate_selection import f_regression from .univariate_selection import SelectPercentile from .univariate_selection import SelectKBest from .univariate_selection import SelectFpr from .univariate_selection import SelectFdr from .univariate_selection import SelectFwe from .univariate_selection import GenericUnivariateSelect from .variance_threshold import VarianceThreshold from .rfe import RFE from .rfe import RFECV __all__ = ['GenericUnivariateSelect', 'RFE', 'RFECV', 'SelectFdr', 'SelectFpr', 'SelectFwe', 'SelectKBest', 'SelectPercentile', 'VarianceThreshold', 'chi2', 'f_classif', 'f_oneway', 'f_regression']
bsd-3-clause
Micket/CCBuilder
make_cc.py
1
8680
#!/usr/bin/env python3 from __future__ import print_function from __future__ import division import argparse import pickle import time import CCBuilder as ccb import CCBuilder_c as ccb_c import numpy as np import scipy.special def uniform_dist(x): """ Returns uniform distributions of given range """ return lambda: np.random.uniform(x[0], x[1]) def weibull_dist(a, mu): """ Returns Weibull distributions for given shape parameter and average """ return lambda: np.random.weibull(a) * mu / scipy.special.gamma(1/a + 1) def parse_dist(arg): # Parses input string for given distribution. # Returns a distribution, and the average d, params = arg.split(':') params = [float(x) for x in params.split(',')] if d == 'U': return uniform_dist(params), np.mean(params) elif d == 'W': a, mu = params return weibull_dist(a, mu), mu parser = argparse.ArgumentParser(description='''Generate a WC microstructure. Grain shape/size supports 2 types of distributions: Uniform: U:low,high Weibull: U:a,mu (a=k in some notation, mu=mean) ''', formatter_class=argparse.ArgumentDefaultsHelpFormatter) # parser.add_argument('-V', dest='verbose', action='store_true', help='Verbose mode.') parser.add_argument('-f', dest='fname', metavar='basename', required=True, help='Output base filename.') parser.add_argument('-L', dest='L', metavar='length', required=True, type=float, help='Cell length (volume is L^3)') parser.add_argument('-m', dest='m', metavar='m', required=True, type=int, help='Grid resolution. Total number of voxels are (m*L)^3') parser.add_argument('--vol_frac_goal', dest="vol_frac_goal", metavar='v', required=True, type=float, help='Goal for volume fraction WC (excluding overlap)') parser.add_argument('-s', dest='seed', metavar='s', default=None, type=int, help='Seed for RNG. Given identical parameters, ' + 'CCBuilder will generate identical output given a controlled seed.') parser.add_argument('--stray_cleanup', action='store_true', help='Clean up stray voxels') group = parser.add_argument_group('WC grain shape') group.add_argument('-k', dest='k_dist', metavar='type,[params]', default='U:0.4,1.4', help='k distribution') group.add_argument('-r', dest='r_dist', metavar='type,[params]', default='U:0.1,0.4', help='r distribution') group.add_argument('-d', dest='d_dist', metavar='type,[params]', default='U:0.5,1.5', help='d distribution') group = parser.add_argument_group('Packing') group.add_argument('--use_potential', action='store_true', help='Use repulsive potential.') group.add_argument('--nr_tries', dest='nr_tries', metavar='n', default=2500, type=int, help='Number of random translations.') group.add_argument('--delta', dest='delta', metavar='d', type=float, help='Maximum distance for randomized translations.') group.add_argument('--m_coarse', dest="m_coarse", metavar='mc', default=10, help='Grid resolution during packing.') group = parser.add_argument_group('Potts simulation') group.add_argument('--mc_steps', dest="mc_steps", metavar='steps', default=0.05, type=float, help='Monte-Carlo steps (scales with (m*L)^4. Set to zero to turn off.') group.add_argument('--tau', dest='tau', metavar='t', default=0.5, type=float, help='Ficticious temperature in Potts model.') options = parser.parse_args() if options.seed is not None: np.random.seed(options.seed) # Heuristic mapping from actual to goal volume fraction # vol_frac_goal = (alpha - 2)/(2 * alpha) + 1/alpha * np.sqrt(1 - alpha * np.log(-2*(vol_frac - 1))) d_eq, d_0 = parse_dist(options.d_dist) r, r_0 = parse_dist(options.r_dist) k, k_0 = parse_dist(options.k_dist) fname = options.fname # to avoid confusion with types: m = np.int(options.m) m_coarse = np.int(options.m_coarse) L = np.float(options.L) mc_steps = np.float(options.mc_steps) vol_frac_goal = np.double(options.vol_frac_goal) tau = np.double(options.tau) nr_tries = np.int(options.nr_tries) delta_x = d_0/float(m) M = np.int(m * L / d_0) M_coarse = np.int(m_coarse * L / d_0) idelta = M idelta_coarse = M_coarse if options.delta: idelta = np.int(M * options.delta / L) idelta_coarse = np.int(M_coarse * options.delta / L) trunc_triangles = ccb.prepare_triangles(vol_frac_goal, L, r, k, d_eq) # trunc_triangles = trunc_triangles[:1] # trunc_triangles[0].rot_matrix = np.eye(3) # trunc_triangles[0].rot_matrix_tr = np.eye(3) # trunc_triangles[0].midpoint = np.array([2., 2., 2.]) # Sort triangles w.r.t. volume, so that large triangles are added to the box first (better packing) trunc_triangles.sort(key=lambda x: x.volume, reverse=True) print('Prepared', len(trunc_triangles), 'triangles') if options.use_potential: ccb.optimize_midpoints(L, trunc_triangles) if m_coarse == m: grain_ids, overlaps, voxel_indices = ccb_c.populate_voxels(M, L, trunc_triangles, nr_tries, idelta, 1.0) else: if nr_tries > 0: # Optimization: Use coarser grid for packing, then insert packed grains into fine grid # No need to get the return values, trunc_triangles ccb_c.populate_voxels(M_coarse, L, trunc_triangles, nr_tries, idelta_coarse, 1.0) grain_ids, overlaps, voxel_indices = ccb_c.populate_voxels(M, L, trunc_triangles, 1, 0, 1.0) if mc_steps > 0: start_time = time.time() # Do Potts on coarse grid first for an improved initial guess. M_coarseMC = M//2 grain_ids_coarse, overlaps_coarse, voxel_indices_coarse = ccb_c.populate_voxels(M_coarseMC, L, trunc_triangles, 0, 0, 1.0) _, gb_voxels_coarse, _ = ccb_c.calc_surface_prop(M_coarseMC, grain_ids_coarse) ccb_c.make_mcp_bound(M_coarseMC, grain_ids_coarse, gb_voxels_coarse, overlaps_coarse, voxel_indices_coarse, np.int(mc_steps * M_coarseMC**4), tau) # Copy over that solution to the overlap regions of the fine grid as a starting point M2 = M**2 i = np.nonzero(overlaps)[0] iz = i // M2 iy = (i - iz*M2) // M ix = i - iz*M2 - iy*M cix = ix * M_coarseMC // M ciy = iy * M_coarseMC // M ciz = iz * M_coarseMC // M ci = cix + ciy*M_coarseMC + ciz*M_coarseMC**2 gid = grain_ids_coarse[ci] # Could use a Cython implementation for efficiency. for ii, g in zip(i, gid): if g != grain_ids[ii] and np.searchsorted(voxel_indices[g-2], ii) < len(voxel_indices[g-2]): grain_ids[ii] = g # This might change a few voxels to a value that they shouldn't obtain, but it's barely noticeable # grain_ids_1[i] = grain_ids_coarse[ci] _, gb_voxels, _ = ccb_c.calc_surface_prop(M, grain_ids) # and run the full resolution MCP: ccb_c.make_mcp_bound(M, grain_ids, gb_voxels, overlaps, voxel_indices, np.int(mc_steps * M ** 4), tau) print('Potts model took {} seconds'.format(np.str(time.time() - start_time))) if options.stray_cleanup: start_time = time.time() ccb_c.stray_cleanup(M, grain_ids) print('Stray voxel cleanup took {} seconds'.format(np.str(time.time() - start_time))) surface_voxels, gb_voxels, interface_voxels = ccb_c.calc_surface_prop(M, grain_ids) phases, good_voxels, euler_angles = ccb_c.calc_grain_prop(M, grain_ids, trunc_triangles) phase_volumes = np.bincount(phases) vol_frac_WC = phase_volumes[2] / np.float(M ** 3) vol_frac_Co = 1 - vol_frac_WC mass_frac_WC = ccb.mass_fraction(vol_frac_WC) sum_gb_voxels = np.sum(gb_voxels) contiguity = sum_gb_voxels / np.float(sum_gb_voxels + np.sum(interface_voxels)) print('Contiguity {:5f}, Co volume frac {:.5f}, mass frac {:.5f}'.format( contiguity, 1 - vol_frac_WC, ccb.mass_fraction(vol_frac_WC))) ccb.write_dream3d(fname, 3 * [M], 3 * [delta_x], trunc_triangles, grain_ids, phases, good_voxels, euler_angles, surface_voxels, gb_voxels, interface_voxels, overlaps) with open(fname + '_trunc_triangles.data', 'wb') as f: pickle.dump([t.rot_matrix for t in trunc_triangles], f) # Saving grain volume data if False: grain_volumes = np.bincount(grain_ids) d_eq = ccb.volume_to_eq_d(grain_volumes[2:] * delta_x ** 3) # np.savetxt(fname + '_d_orig.txt', [t.d_eq for t in trunc_triangles]) np.savetxt(fname + '_d.txt', d_eq) # Plot initial and final distributions import matplotlib.pyplot as plt plt.hist(np.array([t.d_eq for t in trunc_triangles]), alpha=0.5, bins=15, normed=True, label='Initial') plt.hist(d_eq, alpha=0.5, bins=15, normed=True, label='Final') plt.legend(loc='upper right') plt.show()
gpl-3.0
andersgs/dingo
dingo/random_forest.py
1
2551
''' Some functions to fit a random forest ''' import sklearn.ensemble import pandas import progressbar bar = progressbar.ProgressBar() def test_max_features(max_features): if (max_features not in ['sqrt', 'auto', 'log2', None]): try: max_features = int(max_features) except ValueError: print("max_features has to be an integer or one of 'sqrt', 'auto', 'log2' or None.") raise return max_features def learn(X,y, n_trees = 10, criterion = 'entropy', max_features = "sqrt", max_depth = None, min_samples_split = 2, min_samples_leaf = 1, min_weight_fraction_leaf = 0, max_leaf_nodes = None, min_impurity_split = 1e-7, bootstrap = False, oob_score = False, n_jobs = 10, random_state = None, warm_start = False, class_weight = 'balanced_subsample'): rf = sklearn.ensemble.RandomForestClassifier(n_estimators = n_trees, \ criterion = criterion, \ max_features = max_features, \ max_depth = max_depth, \ min_samples_split = min_samples_split, \ min_samples_leaf = min_samples_leaf, \ min_weight_fraction_leaf = min_weight_fraction_leaf, \ max_leaf_nodes = max_leaf_nodes, \ min_impurity_split = min_impurity_split, \ bootstrap = bootstrap, \ oob_score = oob_score, \ n_jobs = n_jobs, \ random_state = random_state, \ warm_start = warm_start, \ class_weight = class_weight, \ verbose = 1 ) rf.fit(X, y) return rf def importance(rf, kmers): importance = rf.estimators_[0].feature_importances_ for est in bar(rf.estimators_[1:]): importance += est.feature_importances_ importance = importance/rf.n_estimators d = {"kmer": kmers, "importance": importance} d = pandas.DataFrame(d) d = d.sort_values(by = "importance", ascending = 0) d = d.loc[d.importance > 0] return d
bsd-3-clause
BlueBrain/NEST
testsuite/manualtests/cross_check_test_mip_corrdet.py
13
2594
# -*- coding: utf-8 -*- # # cross_check_test_mip_corrdet.py # # This file is part of NEST. # # Copyright (C) 2004 The NEST Initiative # # NEST is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # NEST is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with NEST. If not, see <http://www.gnu.org/licenses/>. # Script to check correlation_detector. # Calculates spike cross correlation function of both spike trains in # spike_detector-0-0-3.gdf. The file is generated after running the # testscript testsuite/unittests/test_mip_corrdet.sli # # Author: Helias # Date: 08-04-07 # from scipy import * from matplotlib.pylab import * # for plot # Auto- and crosscorrelation functions for spike trains. # # A time bin of size tbin is centered around the time difference it # represents If the correlation function is calculated for tau in # [-tau_max, tau_max], the pair events contributing to the left-most # bin are those for which tau in [-tau_max-tbin/2, tau_max+tbin/2) and # so on. # correlate two spike trains with each other # assumes spike times to be ordered in time # tau > 0 means spike2 is later than spike1 # # tau_max: maximum time lag in ms correlation function # tbin: bin size # spike1: first spike train [tspike...] # spike2: second spike train [tspike...] # def corr_spikes_sorted(spike1, spike2, tbin, tau_max, h): tau_max_i = int(tau_max/h) tbin_i = int(tbin/h) cross = zeros(int(2*tau_max_i/tbin_i+1), 'd') j0 = 0 for spki in spike1: j = j0 while j < len(spike2) and spike2[j] - spki < -tau_max_i - tbin_i/2.0: j += 1 j0 = j while j < len(spike2) and spike2[j] - spki < tau_max_i + tbin_i/2.0: cross[int((spike2[j] - spki + tau_max_i + 0.5*tbin_i)/tbin_i)] += 1.0 j += 1 return cross def main(): # resolution h = 0.1 tau_max = 100.0 # ms correlation window t_bin = 10.0 # ms bin size # read input from spike detector spikes = load('spike_detector-0-0-3.gdf') sp1 = spikes[find(spikes[:,0] == 4), 1] sp2 = spikes[find(spikes[:,0] == 5), 1] cross = corr_spikes_sorted(sp1, sp2, t_bin, tau_max, h) print cross print sum(cross) main()
gpl-2.0
NicovincX2/Python-3.5
Algèbre/Opération/scalar_product.py
1
1933
# -*- coding: utf-8 -*- import os import seaborn seaborn.set() colors = seaborn.color_palette() import utils # For 3D plotting we need to import some extra stuff from mpl_toolkits.mplot3d import Axes3D # First create two random vectors in 3 dimensional space v1 = rand(3, 1) v2 = rand(3, 1) # And scale them to unit length v1 = v1 / norm(v1) v2 = v2 / norm(v2) # Plot the vectors o = zeros(3) # origin # We'll use the object oriented plotting interface f = figure(figsize=(8, 8)) ax = f.add_subplot(111, projection="3d", axisbg="white") ax.plot(*[[o[i], v1[i]] for i in range(3)], linewidth=3, label="vector1") ax.plot(*[[o[i], v2[i]] for i in range(3)], linewidth=3, label="vector2") for axisl in ["x", "y", "z"]: getattr(ax, "set_%slabel" % axisl)(axisl) # Here's a fun trick legend() f = figure(figsize=(8, 8)) ax = f.add_subplot(111, projection="3d", axisbg="white") ax.plot(*[[o[i], v1[i]] for i in range(3)], linewidth=3, label="vector1") ax.plot(*[[o[i], v2[i]] for i in range(3)], linewidth=3, label="vector2") for axisl in ["x", "y", "z"]: getattr(ax, "set_%slabel" % axisl)(axisl) # Here's a fun trick legend() for i in range(100): # generate a point that is a weighted sum of the 2 vectors w1 = randn(1) w2 = randn(1) point = w1 * v1 + w2 * v2 ax.plot(*point, marker=".", color="k") # We can find a vector that is orthogonal to the plane defined by v1 and v2 # by taking the vector cross product. See the wikipedia page for a # definition of cross product # Must be right shape for cross() v3 = cross(v1.reshape(1, 3), v2.reshape(1, 3)).squeeze() ax.plot(*[[o[i], v3[i]] for i in range(3)], linewidth=3, label="orthogonal vector") legend() print(v3[0] * v1[0] + v3[1] * v1[1] + v3[2] * v1[2]) print(dot(v3, v1)) theta = arccos(dot(v2.T, v1)).squeeze() # and radians can be converted to degrees theta_deg = theta * (180 / pi) print(theta, theta_deg) os.system("pause")
gpl-3.0
jcasner/nupic
external/linux32/lib/python2.6/site-packages/matplotlib/path.py
69
20263
""" Contains a class for managing paths (polylines). """ import math from weakref import WeakValueDictionary import numpy as np from numpy import ma from matplotlib._path import point_in_path, get_path_extents, \ point_in_path_collection, get_path_collection_extents, \ path_in_path, path_intersects_path, convert_path_to_polygons from matplotlib.cbook import simple_linear_interpolation class Path(object): """ :class:`Path` represents a series of possibly disconnected, possibly closed, line and curve segments. The underlying storage is made up of two parallel numpy arrays: - *vertices*: an Nx2 float array of vertices - *codes*: an N-length uint8 array of vertex types These two arrays always have the same length in the first dimension. For example, to represent a cubic curve, you must provide three vertices as well as three codes ``CURVE3``. The code types are: - ``STOP`` : 1 vertex (ignored) A marker for the end of the entire path (currently not required and ignored) - ``MOVETO`` : 1 vertex Pick up the pen and move to the given vertex. - ``LINETO`` : 1 vertex Draw a line from the current position to the given vertex. - ``CURVE3`` : 1 control point, 1 endpoint Draw a quadratic Bezier curve from the current position, with the given control point, to the given end point. - ``CURVE4`` : 2 control points, 1 endpoint Draw a cubic Bezier curve from the current position, with the given control points, to the given end point. - ``CLOSEPOLY`` : 1 vertex (ignored) Draw a line segment to the start point of the current polyline. Users of Path objects should not access the vertices and codes arrays directly. Instead, they should use :meth:`iter_segments` to get the vertex/code pairs. This is important, since many :class:`Path` objects, as an optimization, do not store a *codes* at all, but have a default one provided for them by :meth:`iter_segments`. Note also that the vertices and codes arrays should be treated as immutable -- there are a number of optimizations and assumptions made up front in the constructor that will not change when the data changes. """ # Path codes STOP = 0 # 1 vertex MOVETO = 1 # 1 vertex LINETO = 2 # 1 vertex CURVE3 = 3 # 2 vertices CURVE4 = 4 # 3 vertices CLOSEPOLY = 5 # 1 vertex NUM_VERTICES = [1, 1, 1, 2, 3, 1] code_type = np.uint8 def __init__(self, vertices, codes=None): """ Create a new path with the given vertices and codes. *vertices* is an Nx2 numpy float array, masked array or Python sequence. *codes* is an N-length numpy array or Python sequence of type :attr:`matplotlib.path.Path.code_type`. These two arrays must have the same length in the first dimension. If *codes* is None, *vertices* will be treated as a series of line segments. If *vertices* contains masked values, they will be converted to NaNs which are then handled correctly by the Agg PathIterator and other consumers of path data, such as :meth:`iter_segments`. """ if ma.isMaskedArray(vertices): vertices = vertices.astype(np.float_).filled(np.nan) else: vertices = np.asarray(vertices, np.float_) if codes is not None: codes = np.asarray(codes, self.code_type) assert codes.ndim == 1 assert len(codes) == len(vertices) assert vertices.ndim == 2 assert vertices.shape[1] == 2 self.should_simplify = (len(vertices) >= 128 and (codes is None or np.all(codes <= Path.LINETO))) self.has_nonfinite = not np.isfinite(vertices).all() self.codes = codes self.vertices = vertices #@staticmethod def make_compound_path(*args): """ (staticmethod) Make a compound path from a list of Path objects. Only polygons (not curves) are supported. """ for p in args: assert p.codes is None lengths = [len(x) for x in args] total_length = sum(lengths) vertices = np.vstack([x.vertices for x in args]) vertices.reshape((total_length, 2)) codes = Path.LINETO * np.ones(total_length) i = 0 for length in lengths: codes[i] = Path.MOVETO i += length return Path(vertices, codes) make_compound_path = staticmethod(make_compound_path) def __repr__(self): return "Path(%s, %s)" % (self.vertices, self.codes) def __len__(self): return len(self.vertices) def iter_segments(self, simplify=None): """ Iterates over all of the curve segments in the path. Each iteration returns a 2-tuple (*vertices*, *code*), where *vertices* is a sequence of 1 - 3 coordinate pairs, and *code* is one of the :class:`Path` codes. If *simplify* is provided, it must be a tuple (*width*, *height*) defining the size of the figure, in native units (e.g. pixels or points). Simplification implies both removing adjacent line segments that are very close to parallel, and removing line segments outside of the figure. The path will be simplified *only* if :attr:`should_simplify` is True, which is determined in the constructor by this criteria: - No curves - More than 128 vertices """ vertices = self.vertices if not len(vertices): return codes = self.codes len_vertices = len(vertices) isfinite = np.isfinite NUM_VERTICES = self.NUM_VERTICES MOVETO = self.MOVETO LINETO = self.LINETO CLOSEPOLY = self.CLOSEPOLY STOP = self.STOP if simplify is not None and self.should_simplify: polygons = self.to_polygons(None, *simplify) for vertices in polygons: yield vertices[0], MOVETO for v in vertices[1:]: yield v, LINETO elif codes is None: if self.has_nonfinite: next_code = MOVETO for v in vertices: if np.isfinite(v).all(): yield v, next_code next_code = LINETO else: next_code = MOVETO else: yield vertices[0], MOVETO for v in vertices[1:]: yield v, LINETO else: i = 0 was_nan = False while i < len_vertices: code = codes[i] if code == CLOSEPOLY: yield [], code i += 1 elif code == STOP: return else: num_vertices = NUM_VERTICES[int(code)] curr_vertices = vertices[i:i+num_vertices].flatten() if not isfinite(curr_vertices).all(): was_nan = True elif was_nan: yield curr_vertices[-2:], MOVETO was_nan = False else: yield curr_vertices, code i += num_vertices def transformed(self, transform): """ Return a transformed copy of the path. .. seealso:: :class:`matplotlib.transforms.TransformedPath`: A specialized path class that will cache the transformed result and automatically update when the transform changes. """ return Path(transform.transform(self.vertices), self.codes) def contains_point(self, point, transform=None): """ Returns *True* if the path contains the given point. If *transform* is not *None*, the path will be transformed before performing the test. """ if transform is not None: transform = transform.frozen() return point_in_path(point[0], point[1], self, transform) def contains_path(self, path, transform=None): """ Returns *True* if this path completely contains the given path. If *transform* is not *None*, the path will be transformed before performing the test. """ if transform is not None: transform = transform.frozen() return path_in_path(self, None, path, transform) def get_extents(self, transform=None): """ Returns the extents (*xmin*, *ymin*, *xmax*, *ymax*) of the path. Unlike computing the extents on the *vertices* alone, this algorithm will take into account the curves and deal with control points appropriately. """ from transforms import Bbox if transform is not None: transform = transform.frozen() return Bbox(get_path_extents(self, transform)) def intersects_path(self, other, filled=True): """ Returns *True* if this path intersects another given path. *filled*, when True, treats the paths as if they were filled. That is, if one path completely encloses the other, :meth:`intersects_path` will return True. """ return path_intersects_path(self, other, filled) def intersects_bbox(self, bbox, filled=True): """ Returns *True* if this path intersects a given :class:`~matplotlib.transforms.Bbox`. *filled*, when True, treats the path as if it was filled. That is, if one path completely encloses the other, :meth:`intersects_path` will return True. """ from transforms import BboxTransformTo rectangle = self.unit_rectangle().transformed( BboxTransformTo(bbox)) result = self.intersects_path(rectangle, filled) return result def interpolated(self, steps): """ Returns a new path resampled to length N x steps. Does not currently handle interpolating curves. """ vertices = simple_linear_interpolation(self.vertices, steps) codes = self.codes if codes is not None: new_codes = Path.LINETO * np.ones(((len(codes) - 1) * steps + 1, )) new_codes[0::steps] = codes else: new_codes = None return Path(vertices, new_codes) def to_polygons(self, transform=None, width=0, height=0): """ Convert this path to a list of polygons. Each polygon is an Nx2 array of vertices. In other words, each polygon has no ``MOVETO`` instructions or curves. This is useful for displaying in backends that do not support compound paths or Bezier curves, such as GDK. If *width* and *height* are both non-zero then the lines will be simplified so that vertices outside of (0, 0), (width, height) will be clipped. """ if len(self.vertices) == 0: return [] if transform is not None: transform = transform.frozen() if self.codes is None and (width == 0 or height == 0): if transform is None: return [self.vertices] else: return [transform.transform(self.vertices)] # Deal with the case where there are curves and/or multiple # subpaths (using extension code) return convert_path_to_polygons(self, transform, width, height) _unit_rectangle = None #@classmethod def unit_rectangle(cls): """ (staticmethod) Returns a :class:`Path` of the unit rectangle from (0, 0) to (1, 1). """ if cls._unit_rectangle is None: cls._unit_rectangle = \ Path([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]]) return cls._unit_rectangle unit_rectangle = classmethod(unit_rectangle) _unit_regular_polygons = WeakValueDictionary() #@classmethod def unit_regular_polygon(cls, numVertices): """ (staticmethod) Returns a :class:`Path` for a unit regular polygon with the given *numVertices* and radius of 1.0, centered at (0, 0). """ if numVertices <= 16: path = cls._unit_regular_polygons.get(numVertices) else: path = None if path is None: theta = (2*np.pi/numVertices * np.arange(numVertices + 1).reshape((numVertices + 1, 1))) # This initial rotation is to make sure the polygon always # "points-up" theta += np.pi / 2.0 verts = np.concatenate((np.cos(theta), np.sin(theta)), 1) path = Path(verts) cls._unit_regular_polygons[numVertices] = path return path unit_regular_polygon = classmethod(unit_regular_polygon) _unit_regular_stars = WeakValueDictionary() #@classmethod def unit_regular_star(cls, numVertices, innerCircle=0.5): """ (staticmethod) Returns a :class:`Path` for a unit regular star with the given numVertices and radius of 1.0, centered at (0, 0). """ if numVertices <= 16: path = cls._unit_regular_stars.get((numVertices, innerCircle)) else: path = None if path is None: ns2 = numVertices * 2 theta = (2*np.pi/ns2 * np.arange(ns2 + 1)) # This initial rotation is to make sure the polygon always # "points-up" theta += np.pi / 2.0 r = np.ones(ns2 + 1) r[1::2] = innerCircle verts = np.vstack((r*np.cos(theta), r*np.sin(theta))).transpose() path = Path(verts) cls._unit_regular_polygons[(numVertices, innerCircle)] = path return path unit_regular_star = classmethod(unit_regular_star) #@classmethod def unit_regular_asterisk(cls, numVertices): """ (staticmethod) Returns a :class:`Path` for a unit regular asterisk with the given numVertices and radius of 1.0, centered at (0, 0). """ return cls.unit_regular_star(numVertices, 0.0) unit_regular_asterisk = classmethod(unit_regular_asterisk) _unit_circle = None #@classmethod def unit_circle(cls): """ (staticmethod) Returns a :class:`Path` of the unit circle. The circle is approximated using cubic Bezier curves. This uses 8 splines around the circle using the approach presented here: Lancaster, Don. `Approximating a Circle or an Ellipse Using Four Bezier Cubic Splines <http://www.tinaja.com/glib/ellipse4.pdf>`_. """ if cls._unit_circle is None: MAGIC = 0.2652031 SQRTHALF = np.sqrt(0.5) MAGIC45 = np.sqrt((MAGIC*MAGIC) / 2.0) vertices = np.array( [[0.0, -1.0], [MAGIC, -1.0], [SQRTHALF-MAGIC45, -SQRTHALF-MAGIC45], [SQRTHALF, -SQRTHALF], [SQRTHALF+MAGIC45, -SQRTHALF+MAGIC45], [1.0, -MAGIC], [1.0, 0.0], [1.0, MAGIC], [SQRTHALF+MAGIC45, SQRTHALF-MAGIC45], [SQRTHALF, SQRTHALF], [SQRTHALF-MAGIC45, SQRTHALF+MAGIC45], [MAGIC, 1.0], [0.0, 1.0], [-MAGIC, 1.0], [-SQRTHALF+MAGIC45, SQRTHALF+MAGIC45], [-SQRTHALF, SQRTHALF], [-SQRTHALF-MAGIC45, SQRTHALF-MAGIC45], [-1.0, MAGIC], [-1.0, 0.0], [-1.0, -MAGIC], [-SQRTHALF-MAGIC45, -SQRTHALF+MAGIC45], [-SQRTHALF, -SQRTHALF], [-SQRTHALF+MAGIC45, -SQRTHALF-MAGIC45], [-MAGIC, -1.0], [0.0, -1.0], [0.0, -1.0]], np.float_) codes = cls.CURVE4 * np.ones(26) codes[0] = cls.MOVETO codes[-1] = cls.CLOSEPOLY cls._unit_circle = Path(vertices, codes) return cls._unit_circle unit_circle = classmethod(unit_circle) #@classmethod def arc(cls, theta1, theta2, n=None, is_wedge=False): """ (staticmethod) Returns an arc on the unit circle from angle *theta1* to angle *theta2* (in degrees). If *n* is provided, it is the number of spline segments to make. If *n* is not provided, the number of spline segments is determined based on the delta between *theta1* and *theta2*. Masionobe, L. 2003. `Drawing an elliptical arc using polylines, quadratic or cubic Bezier curves <http://www.spaceroots.org/documents/ellipse/index.html>`_. """ # degrees to radians theta1 *= np.pi / 180.0 theta2 *= np.pi / 180.0 twopi = np.pi * 2.0 halfpi = np.pi * 0.5 eta1 = np.arctan2(np.sin(theta1), np.cos(theta1)) eta2 = np.arctan2(np.sin(theta2), np.cos(theta2)) eta2 -= twopi * np.floor((eta2 - eta1) / twopi) if (theta2 - theta1 > np.pi) and (eta2 - eta1 < np.pi): eta2 += twopi # number of curve segments to make if n is None: n = int(2 ** np.ceil((eta2 - eta1) / halfpi)) if n < 1: raise ValueError("n must be >= 1 or None") deta = (eta2 - eta1) / n t = np.tan(0.5 * deta) alpha = np.sin(deta) * (np.sqrt(4.0 + 3.0 * t * t) - 1) / 3.0 steps = np.linspace(eta1, eta2, n + 1, True) cos_eta = np.cos(steps) sin_eta = np.sin(steps) xA = cos_eta[:-1] yA = sin_eta[:-1] xA_dot = -yA yA_dot = xA xB = cos_eta[1:] yB = sin_eta[1:] xB_dot = -yB yB_dot = xB if is_wedge: length = n * 3 + 4 vertices = np.zeros((length, 2), np.float_) codes = Path.CURVE4 * np.ones((length, ), Path.code_type) vertices[1] = [xA[0], yA[0]] codes[0:2] = [Path.MOVETO, Path.LINETO] codes[-2:] = [Path.LINETO, Path.CLOSEPOLY] vertex_offset = 2 end = length - 2 else: length = n * 3 + 1 vertices = np.zeros((length, 2), np.float_) codes = Path.CURVE4 * np.ones((length, ), Path.code_type) vertices[0] = [xA[0], yA[0]] codes[0] = Path.MOVETO vertex_offset = 1 end = length vertices[vertex_offset :end:3, 0] = xA + alpha * xA_dot vertices[vertex_offset :end:3, 1] = yA + alpha * yA_dot vertices[vertex_offset+1:end:3, 0] = xB - alpha * xB_dot vertices[vertex_offset+1:end:3, 1] = yB - alpha * yB_dot vertices[vertex_offset+2:end:3, 0] = xB vertices[vertex_offset+2:end:3, 1] = yB return Path(vertices, codes) arc = classmethod(arc) #@classmethod def wedge(cls, theta1, theta2, n=None): """ (staticmethod) Returns a wedge of the unit circle from angle *theta1* to angle *theta2* (in degrees). If *n* is provided, it is the number of spline segments to make. If *n* is not provided, the number of spline segments is determined based on the delta between *theta1* and *theta2*. """ return cls.arc(theta1, theta2, n, True) wedge = classmethod(wedge) _get_path_collection_extents = get_path_collection_extents def get_path_collection_extents(*args): """ Given a sequence of :class:`Path` objects, returns the bounding box that encapsulates all of them. """ from transforms import Bbox if len(args[1]) == 0: raise ValueError("No paths provided") return Bbox.from_extents(*_get_path_collection_extents(*args))
agpl-3.0
giorgiop/scikit-learn
sklearn/linear_model/__init__.py
83
3139
""" The :mod:`sklearn.linear_model` module implements generalized linear models. It includes Ridge regression, Bayesian Regression, Lasso and Elastic Net estimators computed with Least Angle Regression and coordinate descent. It also implements Stochastic Gradient Descent related algorithms. """ # See http://scikit-learn.sourceforge.net/modules/sgd.html and # http://scikit-learn.sourceforge.net/modules/linear_model.html for # complete documentation. from .base import LinearRegression from .bayes import BayesianRidge, ARDRegression from .least_angle import (Lars, LassoLars, lars_path, LarsCV, LassoLarsCV, LassoLarsIC) from .coordinate_descent import (Lasso, ElasticNet, LassoCV, ElasticNetCV, lasso_path, enet_path, MultiTaskLasso, MultiTaskElasticNet, MultiTaskElasticNetCV, MultiTaskLassoCV) from .huber import HuberRegressor from .sgd_fast import Hinge, Log, ModifiedHuber, SquaredLoss, Huber from .stochastic_gradient import SGDClassifier, SGDRegressor from .ridge import (Ridge, RidgeCV, RidgeClassifier, RidgeClassifierCV, ridge_regression) from .logistic import (LogisticRegression, LogisticRegressionCV, logistic_regression_path) from .omp import (orthogonal_mp, orthogonal_mp_gram, OrthogonalMatchingPursuit, OrthogonalMatchingPursuitCV) from .passive_aggressive import PassiveAggressiveClassifier from .passive_aggressive import PassiveAggressiveRegressor from .perceptron import Perceptron from .randomized_l1 import (RandomizedLasso, RandomizedLogisticRegression, lasso_stability_path) from .ransac import RANSACRegressor from .theil_sen import TheilSenRegressor __all__ = ['ARDRegression', 'BayesianRidge', 'ElasticNet', 'ElasticNetCV', 'Hinge', 'HuberRegressor', 'Lars', 'LarsCV', 'Lasso', 'LassoCV', 'LassoLars', 'LassoLarsCV', 'LassoLarsIC', 'LinearRegression', 'Log', 'LogisticRegression', 'LogisticRegressionCV', 'ModifiedHuber', 'MultiTaskElasticNet', 'MultiTaskElasticNetCV', 'MultiTaskLasso', 'MultiTaskLassoCV', 'OrthogonalMatchingPursuit', 'OrthogonalMatchingPursuitCV', 'PassiveAggressiveClassifier', 'PassiveAggressiveRegressor', 'Perceptron', 'RandomizedLasso', 'RandomizedLogisticRegression', 'Ridge', 'RidgeCV', 'RidgeClassifier', 'RidgeClassifierCV', 'SGDClassifier', 'SGDRegressor', 'SquaredLoss', 'TheilSenRegressor', 'enet_path', 'lars_path', 'lasso_path', 'lasso_stability_path', 'logistic_regression_path', 'orthogonal_mp', 'orthogonal_mp_gram', 'ridge_regression', 'RANSACRegressor']
bsd-3-clause
nik-hil/fastai
deeplearning2/rossman_exp.py
10
5451
train_ratio=0.9 use_dict=True use_scaler=False init_emb=False split_contins=True samp_size = 100000 #samp_size = 0 import math, keras, datetime, pandas as pd, numpy as np, keras.backend as K import matplotlib.pyplot as plt, xgboost, operator, random, pickle, os from sklearn_pandas import DataFrameMapper from sklearn.preprocessing import LabelEncoder, Imputer, StandardScaler from keras.models import Model from keras.layers import merge, Input from keras.layers.core import Dense, Activation, Reshape, Flatten, Dropout from keras.layers.embeddings import Embedding from keras.optimizers import Adam from keras.layers.normalization import BatchNormalization from keras.regularizers import l2 from keras import initializations np.set_printoptions(4) cfg = K.tf.ConfigProto() cfg.gpu_options.allow_growth = True K.set_session(K.tf.Session(config=cfg)) os.chdir('data/rossman') cat_var_dict = {'Store': 50, 'DayOfWeek': 6, 'Year': 2, 'Month': 6, 'Day': 10, 'StateHoliday': 3, 'CompetitionMonthsOpen': 2, 'Promo2Weeks': 1, 'StoreType': 2, 'Assortment': 3, 'PromoInterval': 3, 'CompetitionOpenSinceYear': 4, 'Promo2SinceYear': 4, 'State': 6, 'Week': 2, 'Events': 4, 'Promo_fw': 1, 'Promo_bw': 1, 'StateHoliday_fw': 1, 'StateHoliday_bw': 1, 'SchoolHoliday_fw': 1, 'SchoolHoliday_bw': 1} cats, contins= [o for n,o in np.load('vars.npz').items()] y = np.load('deps.npz').items()[0][1] if samp_size != 0: np.random.seed(42) idxs = sorted(np.random.choice(len(y), samp_size, replace=False)) cats= cats[idxs] contins= contins[idxs] y= y[idxs] n=len(y) train_size = int(n*train_ratio) contins_trn_orig, contins_val_orig = contins[:train_size], contins[train_size:] cats_trn, cats_val = cats[:train_size], cats[train_size:] y_trn, y_val = y[:train_size], y[train_size:] contin_map_fit = pickle.load(open('contin_maps.pickle', 'rb')) cat_map_fit = pickle.load(open('cat_maps.pickle', 'rb')) def cat_map_info(feat): return feat[0], len(feat[1].classes_) co_enc = StandardScaler().fit(contins_trn_orig) tf_contins_trn = co_enc.transform(contins_trn_orig) tf_contins_val = co_enc.transform(contins_val_orig) """ def rmspe(y_pred, targ = y_valid_orig): return math.sqrt(np.square((targ - y_pred)/targ).mean()) def log_max_inv(preds, mx = max_log_y): return np.exp(preds * mx) def normalize_inv(preds): return preds * ystd + ymean """ def split_cols(arr): return np.hsplit(arr,arr.shape[1]) def emb_init(shape, name=None): return initializations.uniform(shape, scale=0.6/shape[1], name=name) def get_emb(feat): name, c = cat_map_info(feat) if use_dict: c2 = cat_var_dict[name] else: c2 = (c+2)//3 if c2>50: c2=50 inp = Input((1,), dtype='int64', name=name+'_in') if init_emb: u = Flatten(name=name+'_flt')(Embedding(c, c2, input_length=1)(inp)) else: u = Flatten(name=name+'_flt')(Embedding(c, c2, input_length=1, init=emb_init)(inp)) return inp,u def get_contin(feat): name = feat[0][0] inp = Input((1,), name=name+'_in') return inp, Dense(1, name=name+'_d')(inp) def split_data(): if split_contins: map_train = split_cols(cats_trn) + split_cols(contins_trn) map_valid = split_cols(cats_val) + split_cols(contins_val) else: map_train = split_cols(cats_trn) + [contins_trn] map_valid = split_cols(cats_val) + [contins_val] return (map_train, map_valid) def get_contin_one(): n_contin = contins_trn.shape[1] contin_inp = Input((n_contin,), name='contin') contin_out = BatchNormalization()(contin_inp) return contin_inp, contin_out def train(model, map_train, map_valid, bs=128, ne=10): return model.fit(map_train, y_trn, batch_size=bs, nb_epoch=ne, verbose=0, validation_data=(map_valid, y_val)) def get_model(): if split_contins: conts = [get_contin(feat) for feat in contin_map_fit.features] cont_out = [d for inp,d in conts] cont_inp = [inp for inp,d in conts] else: contin_inp, contin_out = get_contin_one() cont_out = [contin_out] cont_inp = [contin_inp] embs = [get_emb(feat) for feat in cat_map_fit.features] x = merge([emb for inp,emb in embs] + cont_out, mode='concat') x = Dropout(0.02)(x) x = Dense(1000, activation='relu', init='uniform')(x) x = Dense(500, activation='relu', init='uniform')(x) x = Dense(1, activation='sigmoid')(x) model = Model([inp for inp,emb in embs] + cont_inp, x) model.compile('adam', 'mean_absolute_error') #model.compile(Adam(), 'mse') return model for split_contins in [True, False]: for use_dict in [True, False]: for use_scaler in [True, False]: for init_emb in [True, False]: print ({'split_contins':split_contins, 'use_dict':use_dict, 'use_scaler':use_scaler, 'init_emb':init_emb}) if use_scaler: contins_trn = tf_contins_trn contins_val = tf_contins_val else: contins_trn = contins_trn_orig contins_val = contins_val_orig map_train, map_valid = split_data() model = get_model() hist = np.array(train(model, map_train, map_valid, 128, 10) .history['val_loss']) print(hist) print(hist.min())
apache-2.0
adiIspas/Machine-Learning_A-Z
Machine Learning A-Z/Part 7 - Natural Language Processing/Section 36 - Natural Language Processing/natural_language_processing.py
3
1452
# Natural Language Processing # Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset dataset = pd.read_csv('Restaurant_Reviews.tsv', delimiter = '\t', quoting = 3) # Cleaning the texts import re import nltk nltk.download('stopwords') from nltk.corpus import stopwords from nltk.stem.porter import PorterStemmer corpus = [] for i in range(0, 1000): review = re.sub('[^a-zA-Z]', ' ', dataset['Review'][i]) review = review.lower() review = review.split() ps = PorterStemmer() review = [ps.stem(word) for word in review if not word in set(stopwords.words('english'))] review = ' '.join(review) corpus.append(review) # Creating the Bag of Words model from sklearn.feature_extraction.text import CountVectorizer cv = CountVectorizer(max_features = 1500) X = cv.fit_transform(corpus).toarray() y = dataset.iloc[:, 1].values # Splitting the dataset into the Training set and Test set from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 0) # Fitting Naive Bayes to the Training set from sklearn.naive_bayes import GaussianNB classifier = GaussianNB() classifier.fit(X_train, y_train) # Predicting the Test set results y_pred = classifier.predict(X_test) # Making the Confusion Matrix from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test, y_pred)
mit
hsuantien/scikit-learn
sklearn/metrics/regression.py
23
16771
"""Metrics to assess performance on regression task Functions named as ``*_score`` return a scalar value to maximize: the higher the better Function named as ``*_error`` or ``*_loss`` return a scalar value to minimize: the lower the better """ # Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr> # Mathieu Blondel <mathieu@mblondel.org> # Olivier Grisel <olivier.grisel@ensta.org> # Arnaud Joly <a.joly@ulg.ac.be> # Jochen Wersdorfer <jochen@wersdoerfer.de> # Lars Buitinck <L.J.Buitinck@uva.nl> # Joel Nothman <joel.nothman@gmail.com> # Noel Dawe <noel@dawe.me> # Manoj Kumar <manojkumarsivaraj334@gmail.com> # Michael Eickenberg <michael.eickenberg@gmail.com> # Konstantin Shmelkov <konstantin.shmelkov@polytechnique.edu> # License: BSD 3 clause from __future__ import division import numpy as np from ..utils.validation import check_array, check_consistent_length from ..utils.validation import column_or_1d import warnings __ALL__ = [ "mean_absolute_error", "mean_squared_error", "median_absolute_error", "r2_score", "explained_variance_score" ] def _check_reg_targets(y_true, y_pred, multioutput): """Check that y_true and y_pred belong to the same regression task Parameters ---------- y_true : array-like, y_pred : array-like, multioutput : array-like or string in ['raw_values', uniform_average', 'variance_weighted'] or None None is accepted due to backward compatibility of r2_score(). Returns ------- type_true : one of {'continuous', continuous-multioutput'} The type of the true target data, as output by 'utils.multiclass.type_of_target' y_true : array-like of shape = (n_samples, n_outputs) Ground truth (correct) target values. y_pred : array-like of shape = (n_samples, n_outputs) Estimated target values. multioutput : array-like of shape = (n_outputs) or string in ['raw_values', uniform_average', 'variance_weighted'] or None Custom output weights if ``multioutput`` is array-like or just the corresponding argument if ``multioutput`` is a correct keyword. """ check_consistent_length(y_true, y_pred) y_true = check_array(y_true, ensure_2d=False) y_pred = check_array(y_pred, ensure_2d=False) if y_true.ndim == 1: y_true = y_true.reshape((-1, 1)) if y_pred.ndim == 1: y_pred = y_pred.reshape((-1, 1)) if y_true.shape[1] != y_pred.shape[1]: raise ValueError("y_true and y_pred have different number of output " "({0}!={1})".format(y_true.shape[1], y_pred.shape[1])) n_outputs = y_true.shape[1] multioutput_options = (None, 'raw_values', 'uniform_average', 'variance_weighted') if multioutput not in multioutput_options: multioutput = check_array(multioutput, ensure_2d=False) if n_outputs == 1: raise ValueError("Custom weights are useful only in " "multi-output cases.") elif n_outputs != len(multioutput): raise ValueError(("There must be equally many custom weights " "(%d) as outputs (%d).") % (len(multioutput), n_outputs)) y_type = 'continuous' if n_outputs == 1 else 'continuous-multioutput' return y_type, y_true, y_pred, multioutput def mean_absolute_error(y_true, y_pred, sample_weight=None, multioutput='uniform_average'): """Mean absolute error regression loss Read more in the :ref:`User Guide <mean_absolute_error>`. Parameters ---------- y_true : array-like of shape = (n_samples) or (n_samples, n_outputs) Ground truth (correct) target values. y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs) Estimated target values. sample_weight : array-like of shape = (n_samples), optional Sample weights. multioutput : string in ['raw_values', 'uniform_average'] or array-like of shape (n_outputs) Defines aggregating of multiple output values. Array-like value defines weights used to average errors. 'raw_values' : Returns a full set of errors in case of multioutput input. 'uniform_average' : Errors of all outputs are averaged with uniform weight. Returns ------- loss : float or ndarray of floats If multioutput is 'raw_values', then mean absolute error is returned for each output separately. If multioutput is 'uniform_average' or an ndarray of weights, then the weighted average of all output errors is returned. MAE output is non-negative floating point. The best value is 0.0. Examples -------- >>> from sklearn.metrics import mean_absolute_error >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> mean_absolute_error(y_true, y_pred) 0.5 >>> y_true = [[0.5, 1], [-1, 1], [7, -6]] >>> y_pred = [[0, 2], [-1, 2], [8, -5]] >>> mean_absolute_error(y_true, y_pred) 0.75 >>> mean_absolute_error(y_true, y_pred, multioutput='raw_values') array([ 0.5, 1. ]) >>> mean_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7]) ... # doctest: +ELLIPSIS 0.849... """ y_type, y_true, y_pred, multioutput = _check_reg_targets( y_true, y_pred, multioutput) output_errors = np.average(np.abs(y_pred - y_true), weights=sample_weight, axis=0) if multioutput == 'raw_values': return output_errors elif multioutput == 'uniform_average': # pass None as weights to np.average: uniform mean multioutput = None return np.average(output_errors, weights=multioutput) def mean_squared_error(y_true, y_pred, sample_weight=None, multioutput='uniform_average'): """Mean squared error regression loss Read more in the :ref:`User Guide <mean_squared_error>`. Parameters ---------- y_true : array-like of shape = (n_samples) or (n_samples, n_outputs) Ground truth (correct) target values. y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs) Estimated target values. sample_weight : array-like of shape = (n_samples), optional Sample weights. multioutput : string in ['raw_values', 'uniform_average'] or array-like of shape (n_outputs) Defines aggregating of multiple output values. Array-like value defines weights used to average errors. 'raw_values' : Returns a full set of errors in case of multioutput input. 'uniform_average' : Errors of all outputs are averaged with uniform weight. Returns ------- loss : float or ndarray of floats A non-negative floating point value (the best value is 0.0), or an array of floating point values, one for each individual target. Examples -------- >>> from sklearn.metrics import mean_squared_error >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> mean_squared_error(y_true, y_pred) 0.375 >>> y_true = [[0.5, 1],[-1, 1],[7, -6]] >>> y_pred = [[0, 2],[-1, 2],[8, -5]] >>> mean_squared_error(y_true, y_pred) # doctest: +ELLIPSIS 0.708... >>> mean_squared_error(y_true, y_pred, multioutput='raw_values') ... # doctest: +ELLIPSIS array([ 0.416..., 1. ]) >>> mean_squared_error(y_true, y_pred, multioutput=[0.3, 0.7]) ... # doctest: +ELLIPSIS 0.824... """ y_type, y_true, y_pred, multioutput = _check_reg_targets( y_true, y_pred, multioutput) output_errors = np.average((y_true - y_pred) ** 2, axis=0, weights=sample_weight) if multioutput == 'raw_values': return output_errors elif multioutput == 'uniform_average': # pass None as weights to np.average: uniform mean multioutput = None return np.average(output_errors, weights=multioutput) def median_absolute_error(y_true, y_pred): """Median absolute error regression loss Read more in the :ref:`User Guide <median_absolute_error>`. Parameters ---------- y_true : array-like of shape = (n_samples) Ground truth (correct) target values. y_pred : array-like of shape = (n_samples) Estimated target values. Returns ------- loss : float A positive floating point value (the best value is 0.0). Examples -------- >>> from sklearn.metrics import median_absolute_error >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> median_absolute_error(y_true, y_pred) 0.5 """ y_type, y_true, y_pred, _ = _check_reg_targets(y_true, y_pred, 'uniform_average') if y_type == 'continuous-multioutput': raise ValueError("Multioutput not supported in median_absolute_error") return np.median(np.abs(y_pred - y_true)) def explained_variance_score(y_true, y_pred, sample_weight=None, multioutput='uniform_average'): """Explained variance regression score function Best possible score is 1.0, lower values are worse. Read more in the :ref:`User Guide <explained_variance_score>`. Parameters ---------- y_true : array-like of shape = (n_samples) or (n_samples, n_outputs) Ground truth (correct) target values. y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs) Estimated target values. sample_weight : array-like of shape = (n_samples), optional Sample weights. multioutput : string in ['raw_values', 'uniform_average', \ 'variance_weighted'] or array-like of shape (n_outputs) Defines aggregating of multiple output scores. Array-like value defines weights used to average scores. 'raw_values' : Returns a full set of scores in case of multioutput input. 'uniform_average' : Scores of all outputs are averaged with uniform weight. 'variance_weighted' : Scores of all outputs are averaged, weighted by the variances of each individual output. Returns ------- score : float or ndarray of floats The explained variance or ndarray if 'multioutput' is 'raw_values'. Notes ----- This is not a symmetric function. Examples -------- >>> from sklearn.metrics import explained_variance_score >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> explained_variance_score(y_true, y_pred) # doctest: +ELLIPSIS 0.957... >>> y_true = [[0.5, 1], [-1, 1], [7, -6]] >>> y_pred = [[0, 2], [-1, 2], [8, -5]] >>> explained_variance_score(y_true, y_pred, multioutput='uniform_average') ... # doctest: +ELLIPSIS 0.983... """ y_type, y_true, y_pred, multioutput = _check_reg_targets( y_true, y_pred, multioutput) y_diff_avg = np.average(y_true - y_pred, weights=sample_weight, axis=0) numerator = np.average((y_true - y_pred - y_diff_avg) ** 2, weights=sample_weight, axis=0) y_true_avg = np.average(y_true, weights=sample_weight, axis=0) denominator = np.average((y_true - y_true_avg) ** 2, weights=sample_weight, axis=0) nonzero_numerator = numerator != 0 nonzero_denominator = denominator != 0 valid_score = nonzero_numerator & nonzero_denominator output_scores = np.ones(y_true.shape[1]) output_scores[valid_score] = 1 - (numerator[valid_score] / denominator[valid_score]) output_scores[nonzero_numerator & ~nonzero_denominator] = 0. if multioutput == 'raw_values': # return scores individually return output_scores elif multioutput == 'uniform_average': # passing to np.average() None as weights results is uniform mean avg_weights = None elif multioutput == 'variance_weighted': avg_weights = denominator else: avg_weights = multioutput return np.average(output_scores, weights=avg_weights) def r2_score(y_true, y_pred, sample_weight=None, multioutput=None): """R^2 (coefficient of determination) regression score function. Best possible score is 1.0, lower values are worse. Read more in the :ref:`User Guide <r2_score>`. Parameters ---------- y_true : array-like of shape = (n_samples) or (n_samples, n_outputs) Ground truth (correct) target values. y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs) Estimated target values. sample_weight : array-like of shape = (n_samples), optional Sample weights. multioutput : string in ['raw_values', 'uniform_average', 'variance_weighted'] or None or array-like of shape (n_outputs) Defines aggregating of multiple output scores. Array-like value defines weights used to average scores. Default value correponds to 'variance_weighted', but will be changed to 'uniform_average' in next versions. 'raw_values' : Returns a full set of scores in case of multioutput input. 'uniform_average' : Scores of all outputs are averaged with uniform weight. 'variance_weighted' : Scores of all outputs are averaged, weighted by the variances of each individual output. Returns ------- z : float or ndarray of floats The R^2 score or ndarray of scores if 'multioutput' is 'raw_values'. Notes ----- This is not a symmetric function. Unlike most other scores, R^2 score may be negative (it need not actually be the square of a quantity R). References ---------- .. [1] `Wikipedia entry on the Coefficient of determination <http://en.wikipedia.org/wiki/Coefficient_of_determination>`_ Examples -------- >>> from sklearn.metrics import r2_score >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> r2_score(y_true, y_pred) # doctest: +ELLIPSIS 0.948... >>> y_true = [[0.5, 1], [-1, 1], [7, -6]] >>> y_pred = [[0, 2], [-1, 2], [8, -5]] >>> r2_score(y_true, y_pred, multioutput='variance_weighted') # doctest: +ELLIPSIS 0.938... """ y_type, y_true, y_pred, multioutput = _check_reg_targets( y_true, y_pred, multioutput) if sample_weight is not None: sample_weight = column_or_1d(sample_weight) weight = sample_weight[:, np.newaxis] else: weight = 1. numerator = (weight * (y_true - y_pred) ** 2).sum(axis=0, dtype=np.float64) denominator = (weight * (y_true - np.average( y_true, axis=0, weights=sample_weight)) ** 2).sum(axis=0, dtype=np.float64) nonzero_denominator = denominator != 0 nonzero_numerator = numerator != 0 valid_score = nonzero_denominator & nonzero_numerator output_scores = np.ones([y_true.shape[1]]) output_scores[valid_score] = 1 - (numerator[valid_score] / denominator[valid_score]) # arbitrary set to zero to avoid -inf scores, having a constant # y_true is not interesting for scoring a regression anyway output_scores[nonzero_numerator & ~nonzero_denominator] = 0. if multioutput is None and y_true.shape[1] != 1: # @FIXME change in 0.18 warnings.warn("Default 'multioutput' behavior now corresponds to " "'variance_weighted' value, it will be changed " "to 'uniform_average' in 0.18.", DeprecationWarning) multioutput = 'variance_weighted' if multioutput == 'raw_values': # return scores individually return output_scores elif multioutput == 'uniform_average': # passing None as weights results is uniform mean avg_weights = None elif multioutput == 'variance_weighted': avg_weights = denominator # avoid fail on constant y or one-element arrays if not np.any(nonzero_denominator): if not np.any(nonzero_numerator): return 1.0 else: return 0.0 else: avg_weights = multioutput return np.average(output_scores, weights=avg_weights)
bsd-3-clause
yunfeilu/scikit-learn
sklearn/decomposition/tests/test_sparse_pca.py
160
6028
# Author: Vlad Niculae # License: BSD 3 clause import sys import numpy as np from sklearn.utils.testing import assert_array_almost_equal from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_array_equal from sklearn.utils.testing import SkipTest from sklearn.utils.testing import assert_true from sklearn.utils.testing import assert_false from sklearn.utils.testing import if_safe_multiprocessing_with_blas from sklearn.decomposition import SparsePCA, MiniBatchSparsePCA from sklearn.utils import check_random_state def generate_toy_data(n_components, n_samples, image_size, random_state=None): n_features = image_size[0] * image_size[1] rng = check_random_state(random_state) U = rng.randn(n_samples, n_components) V = rng.randn(n_components, n_features) centers = [(3, 3), (6, 7), (8, 1)] sz = [1, 2, 1] for k in range(n_components): img = np.zeros(image_size) xmin, xmax = centers[k][0] - sz[k], centers[k][0] + sz[k] ymin, ymax = centers[k][1] - sz[k], centers[k][1] + sz[k] img[xmin:xmax][:, ymin:ymax] = 1.0 V[k, :] = img.ravel() # Y is defined by : Y = UV + noise Y = np.dot(U, V) Y += 0.1 * rng.randn(Y.shape[0], Y.shape[1]) # Add noise return Y, U, V # SparsePCA can be a bit slow. To avoid having test times go up, we # test different aspects of the code in the same test def test_correct_shapes(): rng = np.random.RandomState(0) X = rng.randn(12, 10) spca = SparsePCA(n_components=8, random_state=rng) U = spca.fit_transform(X) assert_equal(spca.components_.shape, (8, 10)) assert_equal(U.shape, (12, 8)) # test overcomplete decomposition spca = SparsePCA(n_components=13, random_state=rng) U = spca.fit_transform(X) assert_equal(spca.components_.shape, (13, 10)) assert_equal(U.shape, (12, 13)) def test_fit_transform(): alpha = 1 rng = np.random.RandomState(0) Y, _, _ = generate_toy_data(3, 10, (8, 8), random_state=rng) # wide array spca_lars = SparsePCA(n_components=3, method='lars', alpha=alpha, random_state=0) spca_lars.fit(Y) # Test that CD gives similar results spca_lasso = SparsePCA(n_components=3, method='cd', random_state=0, alpha=alpha) spca_lasso.fit(Y) assert_array_almost_equal(spca_lasso.components_, spca_lars.components_) @if_safe_multiprocessing_with_blas def test_fit_transform_parallel(): alpha = 1 rng = np.random.RandomState(0) Y, _, _ = generate_toy_data(3, 10, (8, 8), random_state=rng) # wide array spca_lars = SparsePCA(n_components=3, method='lars', alpha=alpha, random_state=0) spca_lars.fit(Y) U1 = spca_lars.transform(Y) # Test multiple CPUs spca = SparsePCA(n_components=3, n_jobs=2, method='lars', alpha=alpha, random_state=0).fit(Y) U2 = spca.transform(Y) assert_true(not np.all(spca_lars.components_ == 0)) assert_array_almost_equal(U1, U2) def test_transform_nan(): # Test that SparsePCA won't return NaN when there is 0 feature in all # samples. rng = np.random.RandomState(0) Y, _, _ = generate_toy_data(3, 10, (8, 8), random_state=rng) # wide array Y[:, 0] = 0 estimator = SparsePCA(n_components=8) assert_false(np.any(np.isnan(estimator.fit_transform(Y)))) def test_fit_transform_tall(): rng = np.random.RandomState(0) Y, _, _ = generate_toy_data(3, 65, (8, 8), random_state=rng) # tall array spca_lars = SparsePCA(n_components=3, method='lars', random_state=rng) U1 = spca_lars.fit_transform(Y) spca_lasso = SparsePCA(n_components=3, method='cd', random_state=rng) U2 = spca_lasso.fit(Y).transform(Y) assert_array_almost_equal(U1, U2) def test_initialization(): rng = np.random.RandomState(0) U_init = rng.randn(5, 3) V_init = rng.randn(3, 4) model = SparsePCA(n_components=3, U_init=U_init, V_init=V_init, max_iter=0, random_state=rng) model.fit(rng.randn(5, 4)) assert_array_equal(model.components_, V_init) def test_mini_batch_correct_shapes(): rng = np.random.RandomState(0) X = rng.randn(12, 10) pca = MiniBatchSparsePCA(n_components=8, random_state=rng) U = pca.fit_transform(X) assert_equal(pca.components_.shape, (8, 10)) assert_equal(U.shape, (12, 8)) # test overcomplete decomposition pca = MiniBatchSparsePCA(n_components=13, random_state=rng) U = pca.fit_transform(X) assert_equal(pca.components_.shape, (13, 10)) assert_equal(U.shape, (12, 13)) def test_mini_batch_fit_transform(): raise SkipTest("skipping mini_batch_fit_transform.") alpha = 1 rng = np.random.RandomState(0) Y, _, _ = generate_toy_data(3, 10, (8, 8), random_state=rng) # wide array spca_lars = MiniBatchSparsePCA(n_components=3, random_state=0, alpha=alpha).fit(Y) U1 = spca_lars.transform(Y) # Test multiple CPUs if sys.platform == 'win32': # fake parallelism for win32 import sklearn.externals.joblib.parallel as joblib_par _mp = joblib_par.multiprocessing joblib_par.multiprocessing = None try: U2 = MiniBatchSparsePCA(n_components=3, n_jobs=2, alpha=alpha, random_state=0).fit(Y).transform(Y) finally: joblib_par.multiprocessing = _mp else: # we can efficiently use parallelism U2 = MiniBatchSparsePCA(n_components=3, n_jobs=2, alpha=alpha, random_state=0).fit(Y).transform(Y) assert_true(not np.all(spca_lars.components_ == 0)) assert_array_almost_equal(U1, U2) # Test that CD gives similar results spca_lasso = MiniBatchSparsePCA(n_components=3, method='cd', alpha=alpha, random_state=0).fit(Y) assert_array_almost_equal(spca_lasso.components_, spca_lars.components_)
bsd-3-clause
winklerand/pandas
pandas/tests/reshape/test_merge_ordered.py
2
2966
import pandas as pd from pandas import DataFrame, merge_ordered from pandas.util import testing as tm from pandas.util.testing import assert_frame_equal from numpy import nan class TestMergeOrdered(object): def setup_method(self, method): self.left = DataFrame({'key': ['a', 'c', 'e'], 'lvalue': [1, 2., 3]}) self.right = DataFrame({'key': ['b', 'c', 'd', 'f'], 'rvalue': [1, 2, 3., 4]}) def test_basic(self): result = merge_ordered(self.left, self.right, on='key') expected = DataFrame({'key': ['a', 'b', 'c', 'd', 'e', 'f'], 'lvalue': [1, nan, 2, nan, 3, nan], 'rvalue': [nan, 1, 2, 3, nan, 4]}) assert_frame_equal(result, expected) def test_ffill(self): result = merge_ordered( self.left, self.right, on='key', fill_method='ffill') expected = DataFrame({'key': ['a', 'b', 'c', 'd', 'e', 'f'], 'lvalue': [1., 1, 2, 2, 3, 3.], 'rvalue': [nan, 1, 2, 3, 3, 4]}) assert_frame_equal(result, expected) def test_multigroup(self): left = pd.concat([self.left, self.left], ignore_index=True) left['group'] = ['a'] * 3 + ['b'] * 3 result = merge_ordered(left, self.right, on='key', left_by='group', fill_method='ffill') expected = DataFrame({'key': ['a', 'b', 'c', 'd', 'e', 'f'] * 2, 'lvalue': [1., 1, 2, 2, 3, 3.] * 2, 'rvalue': [nan, 1, 2, 3, 3, 4] * 2}) expected['group'] = ['a'] * 6 + ['b'] * 6 assert_frame_equal(result, expected.loc[:, result.columns]) result2 = merge_ordered(self.right, left, on='key', right_by='group', fill_method='ffill') assert_frame_equal(result, result2.loc[:, result.columns]) result = merge_ordered(left, self.right, on='key', left_by='group') assert result['group'].notna().all() def test_merge_type(self): class NotADataFrame(DataFrame): @property def _constructor(self): return NotADataFrame nad = NotADataFrame(self.left) result = nad.merge(self.right, on='key') assert isinstance(result, NotADataFrame) def test_empty_sequence_concat(self): # GH 9157 empty_pat = "[Nn]o objects" none_pat = "objects.*None" test_cases = [ ((), empty_pat), ([], empty_pat), ({}, empty_pat), ([None], none_pat), ([None, None], none_pat) ] for df_seq, pattern in test_cases: tm.assert_raises_regex(ValueError, pattern, pd.concat, df_seq) pd.concat([pd.DataFrame()]) pd.concat([None, pd.DataFrame()]) pd.concat([pd.DataFrame(), None])
bsd-3-clause
nomadcube/scikit-learn
examples/bicluster/plot_spectral_coclustering.py
276
1736
""" ============================================== A demo of the Spectral Co-Clustering algorithm ============================================== This example demonstrates how to generate a dataset and bicluster it using the the Spectral Co-Clustering algorithm. The dataset is generated using the ``make_biclusters`` function, which creates a matrix of small values and implants bicluster with large values. The rows and columns are then shuffled and passed to the Spectral Co-Clustering algorithm. Rearranging the shuffled matrix to make biclusters contiguous shows how accurately the algorithm found the biclusters. """ print(__doc__) # Author: Kemal Eren <kemal@kemaleren.com> # License: BSD 3 clause import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import make_biclusters from sklearn.datasets import samples_generator as sg from sklearn.cluster.bicluster import SpectralCoclustering from sklearn.metrics import consensus_score data, rows, columns = make_biclusters( shape=(300, 300), n_clusters=5, noise=5, shuffle=False, random_state=0) plt.matshow(data, cmap=plt.cm.Blues) plt.title("Original dataset") data, row_idx, col_idx = sg._shuffle(data, random_state=0) plt.matshow(data, cmap=plt.cm.Blues) plt.title("Shuffled dataset") model = SpectralCoclustering(n_clusters=5, random_state=0) model.fit(data) score = consensus_score(model.biclusters_, (rows[:, row_idx], columns[:, col_idx])) print("consensus score: {:.3f}".format(score)) fit_data = data[np.argsort(model.row_labels_)] fit_data = fit_data[:, np.argsort(model.column_labels_)] plt.matshow(fit_data, cmap=plt.cm.Blues) plt.title("After biclustering; rearranged to show biclusters") plt.show()
bsd-3-clause
tayebzaidi/snova_analysis
Miscellaneous/typ1a_features.py
1
2252
import matplotlib.pyplot as plt import scipy.interpolate as scinterp import numpy as np import peakfinding import peak_original import smoothing import plotter import random import readin import sys import os if __name__== '__main__': Mbdata = [] delM15data = [] path = "/Users/zaidi/Documents/REU/restframe/" filenames = os.listdir(path) random.shuffle(filenames) for filename in filenames: current_file = os.path.join(path, filename) data= readin.readin_SNrest(filename) indB = np.where((data.band == 'B')) Bdata = data[indB] Bdata = np.sort(Bdata) if len(Bdata.phase) > 3: spl = scinterp.UnivariateSpline(Bdata.phase, Bdata.mag) spl.set_smoothing_factor(2./len(Bdata.phase)) phase_new = np.arange(Bdata.phase[0], Bdata.phase[-1], 1) mag_new = spl(phase_new) maxp, minp = peak_original.peakdet(mag_new, 0.5, phase_new) if len(minp) > 0 and minp[0][0] < 5 and minp[0][0] > -5: Mb = minp[0][1] delM15 = minp[0][1] - spl(minp[0][0]+15) Mbdata.append(Mb) delM15data.append(delM15) if delM15 > 0 or delM15 < -5: print minp print filename print spl(minp[0][0] + 15) fig = plt.figure(1) ax = fig.add_subplot(1,1,1) ax.plot(phase_new, mag_new) ax.plot(Bdata.phase, Bdata.mag) if len(minp) > 0: ax.scatter(minp[:,0],minp[:,1]) plt.show(fig) ''' maxp, minp = peakfinding.peakdetect(mag_new, phase_new, 200, 1.5) if len(minp) > 0: print minp print filename fig = plt.figure(1) ax = fig.add_subplot(1,1,1) #ax.scatter(minp[:,0], minp[:,1],'bo') #ax.plot(Bdata.phase, Bdata.mag) #plt.show(fig) ''' #interp = smoothing.Interpolate1D(data.phase print Mbdata print delM15data fig = plt.figure(2) ax = fig.add_subplot(1,1,1) ax.scatter(Mbdata, delM15data) plt.show(fig)
gpl-3.0
billbrod/spatial-frequency-preferences
sfp/image_computable.py
1
6815
#!/usr/bin/python """code to help run the image-computable version of the model we're using this primarily to check the effect of vignetting, but this does make our project image-computable (though it's a linear model and so will fail in some trivial cases) """ import itertools import argparse import numpy as np import pandas as pd import pyrtools as pt from scipy import interpolate def upsample(signal, target_shape): """upsample a signal to target_shape this uses scipy's interpolate.interp2d (and so will end up with a smoothed signal) """ x = np.linspace(-(signal.shape[0]-1)/2, (signal.shape[0]-1)/2, num=signal.shape[0]) y = np.linspace(-(signal.shape[1]-1)/2, (signal.shape[1]-1)/2, num=signal.shape[1]) f = interpolate.interp2d(x, y, signal) x = np.linspace(-(signal.shape[0]-1)/2, (signal.shape[0]-1)/2, num=target_shape[0]) y = np.linspace(-(signal.shape[1]-1)/2, (signal.shape[1]-1)/2, num=target_shape[1]) return f(x,y) def calc_energy_and_filters(stim, stim_df, n_orientations=6, save_path_template=None): """this creates the energy and filter arrays We assume the stimuli have natural groups, here indexed by the "class_idx" column in stim_df, and all stimuli within these groups should be considered the same stimuli, that is, we sum the energy across all of them. for the spatial frequency project, these are the different phases of the gratings (because of how we structure our experiment, we estimate a response amplitude to all phases together). Note that this will take a while to run (~10 or 20 minutes). Since it only needs to run once per experiment, didn't bother to make it efficient at all. The outputs will also be very large, totalling about 11GB Parameters ---------- stim : np.ndarray The stimuli to produce energy for. Should have shape (n, *img_size), where n is the number of total stimuli. stim_df : pd.DataFrame The DataFrame describing the stimuli. Must contain the column "class_idx", which indexes the different stimulus classes (see above) n_orientations : int the number of orientations in the steerable pyramid. 6 is the number used to model fMRI voxels in Roth, Z. N., Heeger, D., & Merriam, E. (2018). Stimulus vignetting and orientation selectivity in human visual cortex. bioRxiv. save_path_template : str or None the template string for the save path we'll use for energy and filters. should end in .npy and contain one %s, which we'll replace with "energy" and "filters". Returns ------- energy : np.ndarray energy has shape (stim_df.class_idx.nunique(), max_ht, n_orientations, *img_size) and contains the energy (square and absolute value the complex valued output of SteerablePyramidFreq; equivalently, square and sum the output of the quadrature pair of filters that make up the pyramid) for each image, at each scale and orientation. the energy has all been upsampled to the size of the initial image. filters : np.ndarray filters has shape (max_ht, n_orientations, *img_size) and is the fourier transform of the filters at each scale and orientation, zero-padded so they all have the same size. we only have one set of filters (instead of one per stimulus class) because the same pyramid was used for each of them; we ensure this by getting the filters for each stimulus class and checking that they're individually equal to the average across classes. """ img_size = stim.shape[1:] # this computation comes from the SteerablePyramidFreq code max_ht = int(np.floor(np.log2(min(img_size))) - 2) energy = np.zeros((stim_df.class_idx.nunique(), max_ht, n_orientations, *img_size), dtype=np.float32) filters = np.zeros_like(energy) for i, g in stim_df.groupby('class_idx'): idx = g.index filled_filters = False for j in idx: pyr = pt.pyramids.SteerablePyramidFreq(stim[j], order=n_orientations-1, is_complex=True) for k, l in itertools.product(range(max_ht), range(n_orientations)): energy[int(i), k, l, :, :] += upsample(np.abs(pyr.pyr_coeffs[(k, l)])**2, img_size) # we only want to run this once per stimulus class if not filled_filters: if k > 0: lomask = pyr._lomasks[k-1] else: lomask = pyr._lo0mask filt = pyr._anglemasks[k][l] * pyr._himasks[k] * lomask pad_num = [] for m in range(2): pad_num.append([(img_size[m] - filt.shape[m])//2, (img_size[m] - filt.shape[m])//2]) if filt.shape[m] + 2*pad_num[m][0] != img_size[m]: pad_num[m][0] += img_size[m] - (filt.shape[m] + 2*pad_num[m][0]) filters[int(i), k, l, :, :] = np.pad(filt, pad_num, 'constant', constant_values=0) filled_filters = True filter_mean = np.mean(filters, 0) for i in range(filters.shape[0]): if not(np.allclose(filter_mean, filters[i,:,:,:,:])): raise Exception("Something has gone terribly wrong, the filters for stim class %d are different than the rest!" % i) filters = filter_mean if save_path_template is not None: np.save(save_path_template % "energy", energy) np.save(save_path_template % "filters", filters) return energy, filters if __name__ == '__main__': parser = argparse.ArgumentParser( description=("Calculate and save the energy for each stimulus class, as well as the Fourier" " transform of the filters of the steerable pyramid we use to get this. For " "use with image-computable version of this model"), formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("stimuli", help=("Path to the stimulus .npy file.")) parser.add_argument("stimuli_description_df", help=("Path to the stimulus description dataframe .csv file.")) parser.add_argument("save_path_template", help=("Path template (with .npy extension) where we'll save the results. " "Should contain one %s.")) parser.add_argument('--n_orientations', '-n', default=6, type=int, help=("The number of orientations in the steerable pyramid used here.")) args = vars(parser.parse_args()) stim = np.load(args.pop('stimuli')) stim_df = pd.read_csv(args.pop('stimuli_description_df')) calc_energy_and_filters(stim, stim_df, **args)
mit
IshankGulati/scikit-learn
sklearn/feature_selection/variance_threshold.py
123
2572
# Author: Lars Buitinck # License: 3-clause BSD import numpy as np from ..base import BaseEstimator from .base import SelectorMixin from ..utils import check_array from ..utils.sparsefuncs import mean_variance_axis from ..utils.validation import check_is_fitted class VarianceThreshold(BaseEstimator, SelectorMixin): """Feature selector that removes all low-variance features. This feature selection algorithm looks only at the features (X), not the desired outputs (y), and can thus be used for unsupervised learning. Read more in the :ref:`User Guide <variance_threshold>`. Parameters ---------- threshold : float, optional Features with a training-set variance lower than this threshold will be removed. The default is to keep all features with non-zero variance, i.e. remove the features that have the same value in all samples. Attributes ---------- variances_ : array, shape (n_features,) Variances of individual features. Examples -------- The following dataset has integer features, two of which are the same in every sample. These are removed with the default setting for threshold:: >>> X = [[0, 2, 0, 3], [0, 1, 4, 3], [0, 1, 1, 3]] >>> selector = VarianceThreshold() >>> selector.fit_transform(X) array([[2, 0], [1, 4], [1, 1]]) """ def __init__(self, threshold=0.): self.threshold = threshold def fit(self, X, y=None): """Learn empirical variances from X. Parameters ---------- X : {array-like, sparse matrix}, shape (n_samples, n_features) Sample vectors from which to compute variances. y : any Ignored. This parameter exists only for compatibility with sklearn.pipeline.Pipeline. Returns ------- self """ X = check_array(X, ('csr', 'csc'), dtype=np.float64) if hasattr(X, "toarray"): # sparse matrix _, self.variances_ = mean_variance_axis(X, axis=0) else: self.variances_ = np.var(X, axis=0) if np.all(self.variances_ <= self.threshold): msg = "No feature in X meets the variance threshold {0:.5f}" if X.shape[0] == 1: msg += " (X contains only one sample)" raise ValueError(msg.format(self.threshold)) return self def _get_support_mask(self): check_is_fitted(self, 'variances_') return self.variances_ > self.threshold
bsd-3-clause
Winand/pandas
pandas/tests/io/formats/test_eng_formatting.py
22
8085
import numpy as np import pandas as pd from pandas import DataFrame from pandas.compat import u import pandas.io.formats.format as fmt from pandas.util import testing as tm class TestEngFormatter(object): def test_eng_float_formatter(self): df = DataFrame({'A': [1.41, 141., 14100, 1410000.]}) fmt.set_eng_float_format() result = df.to_string() expected = (' A\n' '0 1.410E+00\n' '1 141.000E+00\n' '2 14.100E+03\n' '3 1.410E+06') assert result == expected fmt.set_eng_float_format(use_eng_prefix=True) result = df.to_string() expected = (' A\n' '0 1.410\n' '1 141.000\n' '2 14.100k\n' '3 1.410M') assert result == expected fmt.set_eng_float_format(accuracy=0) result = df.to_string() expected = (' A\n' '0 1E+00\n' '1 141E+00\n' '2 14E+03\n' '3 1E+06') assert result == expected tm.reset_display_options() def compare(self, formatter, input, output): formatted_input = formatter(input) assert formatted_input == output def compare_all(self, formatter, in_out): """ Parameters: ----------- formatter: EngFormatter under test in_out: list of tuples. Each tuple = (number, expected_formatting) It is tested if 'formatter(number) == expected_formatting'. *number* should be >= 0 because formatter(-number) == fmt is also tested. *fmt* is derived from *expected_formatting* """ for input, output in in_out: self.compare(formatter, input, output) self.compare(formatter, -input, "-" + output[1:]) def test_exponents_with_eng_prefix(self): formatter = fmt.EngFormatter(accuracy=3, use_eng_prefix=True) f = np.sqrt(2) in_out = [ (f * 10 ** -24, " 1.414y"), (f * 10 ** -23, " 14.142y"), (f * 10 ** -22, " 141.421y"), (f * 10 ** -21, " 1.414z"), (f * 10 ** -20, " 14.142z"), (f * 10 ** -19, " 141.421z"), (f * 10 ** -18, " 1.414a"), (f * 10 ** -17, " 14.142a"), (f * 10 ** -16, " 141.421a"), (f * 10 ** -15, " 1.414f"), (f * 10 ** -14, " 14.142f"), (f * 10 ** -13, " 141.421f"), (f * 10 ** -12, " 1.414p"), (f * 10 ** -11, " 14.142p"), (f * 10 ** -10, " 141.421p"), (f * 10 ** -9, " 1.414n"), (f * 10 ** -8, " 14.142n"), (f * 10 ** -7, " 141.421n"), (f * 10 ** -6, " 1.414u"), (f * 10 ** -5, " 14.142u"), (f * 10 ** -4, " 141.421u"), (f * 10 ** -3, " 1.414m"), (f * 10 ** -2, " 14.142m"), (f * 10 ** -1, " 141.421m"), (f * 10 ** 0, " 1.414"), (f * 10 ** 1, " 14.142"), (f * 10 ** 2, " 141.421"), (f * 10 ** 3, " 1.414k"), (f * 10 ** 4, " 14.142k"), (f * 10 ** 5, " 141.421k"), (f * 10 ** 6, " 1.414M"), (f * 10 ** 7, " 14.142M"), (f * 10 ** 8, " 141.421M"), (f * 10 ** 9, " 1.414G"), (f * 10 ** 10, " 14.142G"), (f * 10 ** 11, " 141.421G"), (f * 10 ** 12, " 1.414T"), (f * 10 ** 13, " 14.142T"), (f * 10 ** 14, " 141.421T"), (f * 10 ** 15, " 1.414P"), (f * 10 ** 16, " 14.142P"), (f * 10 ** 17, " 141.421P"), (f * 10 ** 18, " 1.414E"), (f * 10 ** 19, " 14.142E"), (f * 10 ** 20, " 141.421E"), (f * 10 ** 21, " 1.414Z"), (f * 10 ** 22, " 14.142Z"), (f * 10 ** 23, " 141.421Z"), (f * 10 ** 24, " 1.414Y"), (f * 10 ** 25, " 14.142Y"), (f * 10 ** 26, " 141.421Y")] self.compare_all(formatter, in_out) def test_exponents_without_eng_prefix(self): formatter = fmt.EngFormatter(accuracy=4, use_eng_prefix=False) f = np.pi in_out = [ (f * 10 ** -24, " 3.1416E-24"), (f * 10 ** -23, " 31.4159E-24"), (f * 10 ** -22, " 314.1593E-24"), (f * 10 ** -21, " 3.1416E-21"), (f * 10 ** -20, " 31.4159E-21"), (f * 10 ** -19, " 314.1593E-21"), (f * 10 ** -18, " 3.1416E-18"), (f * 10 ** -17, " 31.4159E-18"), (f * 10 ** -16, " 314.1593E-18"), (f * 10 ** -15, " 3.1416E-15"), (f * 10 ** -14, " 31.4159E-15"), (f * 10 ** -13, " 314.1593E-15"), (f * 10 ** -12, " 3.1416E-12"), (f * 10 ** -11, " 31.4159E-12"), (f * 10 ** -10, " 314.1593E-12"), (f * 10 ** -9, " 3.1416E-09"), (f * 10 ** -8, " 31.4159E-09"), (f * 10 ** -7, " 314.1593E-09"), (f * 10 ** -6, " 3.1416E-06"), (f * 10 ** -5, " 31.4159E-06"), (f * 10 ** -4, " 314.1593E-06"), (f * 10 ** -3, " 3.1416E-03"), (f * 10 ** -2, " 31.4159E-03"), (f * 10 ** -1, " 314.1593E-03"), (f * 10 ** 0, " 3.1416E+00"), (f * 10 ** 1, " 31.4159E+00"), (f * 10 ** 2, " 314.1593E+00"), (f * 10 ** 3, " 3.1416E+03"), (f * 10 ** 4, " 31.4159E+03"), (f * 10 ** 5, " 314.1593E+03"), (f * 10 ** 6, " 3.1416E+06"), (f * 10 ** 7, " 31.4159E+06"), (f * 10 ** 8, " 314.1593E+06"), (f * 10 ** 9, " 3.1416E+09"), (f * 10 ** 10, " 31.4159E+09"), (f * 10 ** 11, " 314.1593E+09"), (f * 10 ** 12, " 3.1416E+12"), (f * 10 ** 13, " 31.4159E+12"), (f * 10 ** 14, " 314.1593E+12"), (f * 10 ** 15, " 3.1416E+15"), (f * 10 ** 16, " 31.4159E+15"), (f * 10 ** 17, " 314.1593E+15"), (f * 10 ** 18, " 3.1416E+18"), (f * 10 ** 19, " 31.4159E+18"), (f * 10 ** 20, " 314.1593E+18"), (f * 10 ** 21, " 3.1416E+21"), (f * 10 ** 22, " 31.4159E+21"), (f * 10 ** 23, " 314.1593E+21"), (f * 10 ** 24, " 3.1416E+24"), (f * 10 ** 25, " 31.4159E+24"), (f * 10 ** 26, " 314.1593E+24")] self.compare_all(formatter, in_out) def test_rounding(self): formatter = fmt.EngFormatter(accuracy=3, use_eng_prefix=True) in_out = [(5.55555, ' 5.556'), (55.5555, ' 55.556'), (555.555, ' 555.555'), (5555.55, ' 5.556k'), (55555.5, ' 55.556k'), (555555, ' 555.555k')] self.compare_all(formatter, in_out) formatter = fmt.EngFormatter(accuracy=1, use_eng_prefix=True) in_out = [(5.55555, ' 5.6'), (55.5555, ' 55.6'), (555.555, ' 555.6'), (5555.55, ' 5.6k'), (55555.5, ' 55.6k'), (555555, ' 555.6k')] self.compare_all(formatter, in_out) formatter = fmt.EngFormatter(accuracy=0, use_eng_prefix=True) in_out = [(5.55555, ' 6'), (55.5555, ' 56'), (555.555, ' 556'), (5555.55, ' 6k'), (55555.5, ' 56k'), (555555, ' 556k')] self.compare_all(formatter, in_out) formatter = fmt.EngFormatter(accuracy=3, use_eng_prefix=True) result = formatter(0) assert result == u(' 0.000') def test_nan(self): # Issue #11981 formatter = fmt.EngFormatter(accuracy=1, use_eng_prefix=True) result = formatter(np.nan) assert result == u('NaN') df = pd.DataFrame({'a': [1.5, 10.3, 20.5], 'b': [50.3, 60.67, 70.12], 'c': [100.2, 101.33, 120.33]}) pt = df.pivot_table(values='a', index='b', columns='c') fmt.set_eng_float_format(accuracy=1) result = pt.to_string() assert 'NaN' in result tm.reset_display_options() def test_inf(self): # Issue #11981 formatter = fmt.EngFormatter(accuracy=1, use_eng_prefix=True) result = formatter(np.inf) assert result == u('inf')
bsd-3-clause
gmatteo/pymatgen
pymatgen/io/gaussian.py
2
59623
# coding: utf-8 # Copyright (c) Pymatgen Development Team. # Distributed under the terms of the MIT License. """ This module implements input and output processing from Gaussian. """ import re import warnings import numpy as np import scipy.constants as cst from monty.io import zopen from pymatgen.core.composition import Composition from pymatgen.core.periodic_table import Element from pymatgen.core.structure import Molecule from pymatgen.core.operations import SymmOp from pymatgen.core.units import Ha_to_eV from pymatgen.electronic_structure.core import Spin from pymatgen.util.coord import get_angle __author__ = "Shyue Ping Ong, Germain Salvato-Vallverdu, Xin Chen" __copyright__ = "Copyright 2013, The Materials Virtual Lab" __version__ = "0.1" __maintainer__ = "Shyue Ping Ong" __email__ = "ongsp@ucsd.edu" __date__ = "8/1/15" float_patt = re.compile(r"\s*([+-]?\d+\.\d+)") def read_route_line(route): """ read route line in gaussian input/output and return functional basis_set and a dictionary of other route parameters Args: route (str) : the route line return functional (str) : the method (HF, PBE ...) basis_set (str) : the basis set route (dict) : dictionary of parameters """ scrf_patt = re.compile(r"^([sS][cC][rR][fF])\s*=\s*(.+)") multi_params_patt = re.compile(r"^([A-z]+[0-9]*)[\s=]+\((.*)\)$") functional = None basis_set = None route_params = {} dieze_tag = None if route: if "/" in route: tok = route.split("/") functional = tok[0].split()[-1] basis_set = tok[1].split()[0] for tok in [functional, basis_set, "/"]: route = route.replace(tok, "") for tok in route.split(): if scrf_patt.match(tok): m = scrf_patt.match(tok) route_params[m.group(1)] = m.group(2) elif tok.upper() in ["#", "#N", "#P", "#T"]: # does not store # in route to avoid error in input if tok == "#": dieze_tag = "#N" else: dieze_tag = tok continue else: m = re.match(multi_params_patt, tok.strip("#")) if m: pars = {} for par in m.group(2).split(","): p = par.split("=") pars[p[0]] = None if len(p) == 1 else p[1] route_params[m.group(1)] = pars else: d = tok.strip("#").split("=") route_params[d[0]] = None if len(d) == 1 else d[1] return functional, basis_set, route_params, dieze_tag class GaussianInput: """ An object representing a Gaussian input file. """ # Commonly used regex patterns _zmat_patt = re.compile(r"^(\w+)*([\s,]+(\w+)[\s,]+(\w+))*[\-\.\s,\w]*$") _xyz_patt = re.compile(r"^(\w+)[\s,]+([\d\.eE\-]+)[\s,]+([\d\.eE\-]+)[\s,]+" r"([\d\.eE\-]+)[\-\.\s,\w.]*$") def __init__( self, mol, charge=None, spin_multiplicity=None, title=None, functional="HF", basis_set="6-31G(d)", route_parameters=None, input_parameters=None, link0_parameters=None, dieze_tag="#P", gen_basis=None, ): """ Args: mol: Input molecule. It can either be a Molecule object, a string giving the geometry in a format supported by Guassian, or ``None``. If the molecule is ``None``, you will need to use read it in from a checkpoint. Consider adding ``CHK`` to the ``link0_parameters``. charge: Charge of the molecule. If None, charge on molecule is used. Defaults to None. This allows the input file to be set a charge independently from the molecule itself. If ``mol`` is not a Molecule object, then you must specify a charge. spin_multiplicity: Spin multiplicity of molecule. Defaults to None, which means that the spin multiplicity is set to 1 if the molecule has no unpaired electrons and to 2 if there are unpaired electrons. If ``mol`` is not a Molecule object, then you must specify the multiplicity title: Title for run. Defaults to formula of molecule if None. functional: Functional for run. basis_set: Basis set for run. route_parameters: Additional route parameters as a dict. For example, {'SP':"", "SCF":"Tight"} input_parameters: Additional input parameters for run as a dict. Used for example, in PCM calculations. E.g., {"EPS":12} link0_parameters: Link0 parameters as a dict. E.g., {"%mem": "1000MW"} dieze_tag: # preceding the route line. E.g. "#p" gen_basis: allows a user-specified basis set to be used in a Gaussian calculation. If this is not None, the attribute ``basis_set`` will be set to "Gen". """ self._mol = mol # Determine multiplicity and charge settings if isinstance(mol, Molecule): self.charge = charge if charge is not None else mol.charge nelectrons = mol.charge + mol.nelectrons - self.charge if spin_multiplicity is not None: self.spin_multiplicity = spin_multiplicity if (nelectrons + spin_multiplicity) % 2 != 1: raise ValueError( "Charge of {} and spin multiplicity of {} is" " not possible for this molecule".format(self.charge, spin_multiplicity) ) else: self.spin_multiplicity = 1 if nelectrons % 2 == 0 else 2 # Get a title from the molecule name self.title = title if title else self._mol.composition.formula else: self.charge = charge self.spin_multiplicity = spin_multiplicity # Set a title self.title = title if title else "Restart" # Store the remaining settings self.functional = functional self.basis_set = basis_set self.link0_parameters = link0_parameters if link0_parameters else {} self.route_parameters = route_parameters if route_parameters else {} self.input_parameters = input_parameters if input_parameters else {} self.dieze_tag = dieze_tag if dieze_tag[0] == "#" else "#" + dieze_tag self.gen_basis = gen_basis if gen_basis is not None: self.basis_set = "Gen" @property def molecule(self): """ Returns molecule associated with this GaussianInput. """ return self._mol @staticmethod def _parse_coords(coord_lines): """ Helper method to parse coordinates. """ paras = {} var_pattern = re.compile(r"^([A-Za-z]+\S*)[\s=,]+([\d\-\.]+)$") for l in coord_lines: m = var_pattern.match(l.strip()) if m: paras[m.group(1).strip("=")] = float(m.group(2)) species = [] coords = [] # Stores whether a Zmatrix format is detected. Once a zmatrix format # is detected, it is assumed for the remaining of the parsing. zmode = False for l in coord_lines: l = l.strip() if not l: break if (not zmode) and GaussianInput._xyz_patt.match(l): m = GaussianInput._xyz_patt.match(l) species.append(m.group(1)) toks = re.split(r"[,\s]+", l.strip()) if len(toks) > 4: coords.append([float(i) for i in toks[2:5]]) else: coords.append([float(i) for i in toks[1:4]]) elif GaussianInput._zmat_patt.match(l): zmode = True toks = re.split(r"[,\s]+", l.strip()) species.append(toks[0]) toks.pop(0) if len(toks) == 0: coords.append(np.array([0, 0, 0])) else: nn = [] parameters = [] while len(toks) > 1: ind = toks.pop(0) data = toks.pop(0) try: nn.append(int(ind)) except ValueError: nn.append(species.index(ind) + 1) try: val = float(data) parameters.append(val) except ValueError: if data.startswith("-"): parameters.append(-paras[data[1:]]) else: parameters.append(paras[data]) if len(nn) == 1: coords.append(np.array([0, 0, parameters[0]])) elif len(nn) == 2: coords1 = coords[nn[0] - 1] coords2 = coords[nn[1] - 1] bl = parameters[0] angle = parameters[1] axis = [0, 1, 0] op = SymmOp.from_origin_axis_angle(coords1, axis, angle, False) coord = op.operate(coords2) vec = coord - coords1 coord = vec * bl / np.linalg.norm(vec) + coords1 coords.append(coord) elif len(nn) == 3: coords1 = coords[nn[0] - 1] coords2 = coords[nn[1] - 1] coords3 = coords[nn[2] - 1] bl = parameters[0] angle = parameters[1] dih = parameters[2] v1 = coords3 - coords2 v2 = coords1 - coords2 axis = np.cross(v1, v2) op = SymmOp.from_origin_axis_angle(coords1, axis, angle, False) coord = op.operate(coords2) v1 = coord - coords1 v2 = coords1 - coords2 v3 = np.cross(v1, v2) adj = get_angle(v3, axis) axis = coords1 - coords2 op = SymmOp.from_origin_axis_angle(coords1, axis, dih - adj, False) coord = op.operate(coord) vec = coord - coords1 coord = vec * bl / np.linalg.norm(vec) + coords1 coords.append(coord) def _parse_species(sp_str): """ The species specification can take many forms. E.g., simple integers representing atomic numbers ("8"), actual species string ("C") or a labelled species ("C1"). Sometimes, the species string is also not properly capitalized, e.g, ("c1"). This method should take care of these known formats. """ try: return int(sp_str) except ValueError: sp = re.sub(r"\d", "", sp_str) return sp.capitalize() species = [_parse_species(sp) for sp in species] return Molecule(species, coords) @staticmethod def from_string(contents): """ Creates GaussianInput from a string. Args: contents: String representing an Gaussian input file. Returns: GaussianInput object """ lines = [l.strip() for l in contents.split("\n")] link0_patt = re.compile(r"^(%.+)\s*=\s*(.+)") link0_dict = {} for i, l in enumerate(lines): if link0_patt.match(l): m = link0_patt.match(l) link0_dict[m.group(1).strip("=")] = m.group(2) route_patt = re.compile(r"^#[sSpPnN]*.*") route = "" route_index = None for i, l in enumerate(lines): if route_patt.match(l): route += " " + l route_index = i # This condition allows for route cards spanning multiple lines elif (l == "" or l.isspace()) and route_index: break functional, basis_set, route_paras, dieze_tag = read_route_line(route) ind = 2 title = [] while lines[route_index + ind].strip(): title.append(lines[route_index + ind].strip()) ind += 1 title = " ".join(title) ind += 1 toks = re.split(r"[,\s]+", lines[route_index + ind]) charge = int(float(toks[0])) spin_mult = int(toks[1]) coord_lines = [] spaces = 0 input_paras = {} ind += 1 for i in range(route_index + ind, len(lines)): if lines[i].strip() == "": spaces += 1 if spaces >= 2: d = lines[i].split("=") if len(d) == 2: input_paras[d[0]] = d[1] else: coord_lines.append(lines[i].strip()) mol = GaussianInput._parse_coords(coord_lines) mol.set_charge_and_spin(charge, spin_mult) return GaussianInput( mol, charge=charge, spin_multiplicity=spin_mult, title=title, functional=functional, basis_set=basis_set, route_parameters=route_paras, input_parameters=input_paras, link0_parameters=link0_dict, dieze_tag=dieze_tag, ) @staticmethod def from_file(filename): """ Creates GaussianInput from a file. Args: filename: Gaussian input filename Returns: GaussianInput object """ with zopen(filename, "r") as f: return GaussianInput.from_string(f.read()) def _find_nn_pos_before_site(self, siteindex): """ Returns index of nearest neighbor atoms. """ alldist = [(self._mol.get_distance(siteindex, i), i) for i in range(siteindex)] alldist = sorted(alldist, key=lambda x: x[0]) return [d[1] for d in alldist] def get_zmatrix(self): """ Returns a z-matrix representation of the molecule. """ output = [] outputvar = [] for i, site in enumerate(self._mol): if i == 0: output.append("{}".format(site.specie)) elif i == 1: nn = self._find_nn_pos_before_site(i) bondlength = self._mol.get_distance(i, nn[0]) output.append("{} {} B{}".format(self._mol[i].specie, nn[0] + 1, i)) outputvar.append("B{}={:.6f}".format(i, bondlength)) elif i == 2: nn = self._find_nn_pos_before_site(i) bondlength = self._mol.get_distance(i, nn[0]) angle = self._mol.get_angle(i, nn[0], nn[1]) output.append("{} {} B{} {} A{}".format(self._mol[i].specie, nn[0] + 1, i, nn[1] + 1, i)) outputvar.append("B{}={:.6f}".format(i, bondlength)) outputvar.append("A{}={:.6f}".format(i, angle)) else: nn = self._find_nn_pos_before_site(i) bondlength = self._mol.get_distance(i, nn[0]) angle = self._mol.get_angle(i, nn[0], nn[1]) dih = self._mol.get_dihedral(i, nn[0], nn[1], nn[2]) output.append( "{} {} B{} {} A{} {} D{}".format(self._mol[i].specie, nn[0] + 1, i, nn[1] + 1, i, nn[2] + 1, i) ) outputvar.append("B{}={:.6f}".format(i, bondlength)) outputvar.append("A{}={:.6f}".format(i, angle)) outputvar.append("D{}={:.6f}".format(i, dih)) return "\n".join(output) + "\n\n" + "\n".join(outputvar) def get_cart_coords(self): """ Return the cartesian coordinates of the molecule """ def to_s(x): return "%0.6f" % x outs = [] for i, site in enumerate(self._mol): outs.append(" ".join([site.species_string, " ".join([to_s(j) for j in site.coords])])) return "\n".join(outs) def __str__(self): return self.to_string() def to_string(self, cart_coords=False): """ Return GaussianInput string Option: whe cart_coords sets to True return the cartesian coordinates instead of the z-matrix """ def para_dict_to_string(para, joiner=" "): para_str = [] # sorted is only done to make unittests work reliably for par, val in sorted(para.items()): if val is None or val == "": para_str.append(par) elif isinstance(val, dict): val_str = para_dict_to_string(val, joiner=",") para_str.append("{}=({})".format(par, val_str)) else: para_str.append("{}={}".format(par, val)) return joiner.join(para_str) output = [] if self.link0_parameters: output.append(para_dict_to_string(self.link0_parameters, "\n")) # Handle functional or basis set set to None, empty string or whitespace func_str = "" if self.functional is None else self.functional.strip() bset_str = "" if self.basis_set is None else self.basis_set.strip() if func_str != "" and bset_str != "": func_bset_str = " {}/{}".format(func_str, bset_str) else: # don't use the slash if either or both are set as empty func_bset_str = " {}{}".format(func_str, bset_str).rstrip() output.append( "{diez}{func_bset} {route}".format( diez=self.dieze_tag, func_bset=func_bset_str, route=para_dict_to_string(self.route_parameters), ) ) output.append("") output.append(self.title) output.append("") charge_str = "" if self.charge is None else "%d" % self.charge multip_str = "" if self.spin_multiplicity is None else " %d" % self.spin_multiplicity output.append("{}{}".format(charge_str, multip_str)) if isinstance(self._mol, Molecule): if cart_coords is True: output.append(self.get_cart_coords()) else: output.append(self.get_zmatrix()) elif self._mol is not None: output.append(str(self._mol)) output.append("") if self.gen_basis is not None: output.append("{:s}\n".format(self.gen_basis)) output.append(para_dict_to_string(self.input_parameters, "\n")) output.append("\n") return "\n".join(output) def write_file(self, filename, cart_coords=False): """ Write the input string into a file Option: see __str__ method """ with zopen(filename, "w") as f: f.write(self.to_string(cart_coords)) def as_dict(self): """ :return: MSONable dict """ return { "@module": self.__class__.__module__, "@class": self.__class__.__name__, "molecule": self.molecule.as_dict(), "functional": self.functional, "basis_set": self.basis_set, "route_parameters": self.route_parameters, "title": self.title, "charge": self.charge, "spin_multiplicity": self.spin_multiplicity, "input_parameters": self.input_parameters, "link0_parameters": self.link0_parameters, "dieze_tag": self.dieze_tag, } @classmethod def from_dict(cls, d): """ :param d: dict :return: GaussianInput """ return GaussianInput( mol=Molecule.from_dict(d["molecule"]), functional=d["functional"], basis_set=d["basis_set"], route_parameters=d["route_parameters"], title=d["title"], charge=d["charge"], spin_multiplicity=d["spin_multiplicity"], input_parameters=d["input_parameters"], link0_parameters=d["link0_parameters"], ) class GaussianOutput: """ Parser for Gaussian output files. .. note:: Still in early beta. Attributes: .. attribute:: structures All structures from the calculation in the standard orientation. If the symmetry is not considered, the standard orientation is not printed out and the input orientation is used instead. Check the `standard_orientation` attribute. .. attribute:: structures_input_orientation All structures from the calculation in the input orientation or the Z-matrix orientation (if an opt=z-matrix was requested). .. attribute:: opt_structures All optimized structures from the calculation in the standard orientation, if the attribute 'standard_orientation' is True, otherwise in the input or the Z-matrix orientation. .. attribute:: energies All energies from the calculation. .. attribute:: eigenvalues List of eigenvalues for the last geometry .. attribute:: MO_coefficients Matrix of MO coefficients for the last geometry .. attribute:: cart_forces All cartesian forces from the calculation. .. attribute:: frequencies A list for each freq calculation and for each mode of a dict with { "frequency": freq in cm-1, "symmetry": symmetry tag "r_mass": Reduce mass, "f_constant": force constant, "IR_intensity": IR Intensity, "mode": normal mode } The normal mode is a 1D vector of dx, dy dz of each atom. .. attribute:: hessian Matrix of second derivatives of the energy with respect to cartesian coordinates in the **input orientation** frame. Need #P in the route section in order to be in the output. .. attribute:: properly_terminated True if run has properly terminated .. attribute:: is_pcm True if run is a PCM run. .. attribute:: is_spin True if it is an unrestricted run .. attribute:: stationary_type If it is a relaxation run, indicates whether it is a minimum (Minimum) or a saddle point ("Saddle"). .. attribute:: corrections Thermochemical corrections if this run is a Freq run as a dict. Keys are "Zero-point", "Thermal", "Enthalpy" and "Gibbs Free Energy" .. attribute:: functional Functional used in the run. .. attribute:: basis_set Basis set used in the run .. attribute:: route Additional route parameters as a dict. For example, {'SP':"", "SCF":"Tight"} .. attribute:: dieze_tag # preceding the route line, e.g. "#P" .. attribute:: link0 Link0 parameters as a dict. E.g., {"%mem": "1000MW"} .. attribute:: charge Charge for structure .. attribute:: spin_multiplicity Spin multiplicity for structure .. attribute:: num_basis_func Number of basis functions in the run. .. attribute:: electrons number of alpha and beta electrons as (N alpha, N beta) .. attribute:: pcm PCM parameters and output if available. .. attribute:: errors error if not properly terminated (list to be completed in error_defs) .. attribute:: Mulliken_charges Mulliken atomic charges .. attribute:: eigenvectors Matrix of shape (num_basis_func, num_basis_func). Each column is an eigenvectors and contains AO coefficients of an MO. eigenvectors[Spin] = mat(num_basis_func, num_basis_func) .. attribute:: molecular_orbital MO development coefficients on AO in a more convenient array dict for each atom and basis set label. mo[Spin][OM j][atom i] = {AO_k: coeff, AO_k: coeff ... } .. attribute:: atom_basis_labels Labels of AO for each atoms. These labels are those used in the output of molecular orbital coefficients (POP=Full) and in the molecular_orbital array dict. atom_basis_labels[iatom] = [AO_k, AO_k, ...] .. attribute:: resumes List of gaussian data resume given at the end of the output file before the quotation. The resumes are given as string. .. attribute:: title Title of the gaussian run. .. attribute:: standard_orientation If True, the geometries stored in the structures are in the standard orientation. Else, the geometries are in the input orientation. .. attribute:: bond_orders Dict of bond order values read in the output file such as: {(0, 1): 0.8709, (1, 6): 1.234, ...} The keys are the atom indexes and the values are the Wiberg bond indexes that are printed using `pop=NBOREAD` and `$nbo bndidx $end`. Methods: .. method:: to_input() Return a GaussianInput object using the last geometry and the same calculation parameters. .. method:: read_scan() Read a potential energy surface from a gaussian scan calculation. .. method:: get_scan_plot() Get a matplotlib plot of the potential energy surface .. method:: save_scan_plot() Save a matplotlib plot of the potential energy surface to a file """ def __init__(self, filename): """ Args: filename: Filename of Gaussian output file. """ self.filename = filename self._parse(filename) @property def final_energy(self): """ :return: Final energy in Gaussian output. """ return self.energies[-1] @property def final_structure(self): """ :return: Final structure in Gaussian output. """ return self.structures[-1] def _parse(self, filename): start_patt = re.compile(r" \(Enter \S+l101\.exe\)") route_patt = re.compile(r" #[pPnNtT]*.*") link0_patt = re.compile(r"^\s(%.+)\s*=\s*(.+)") charge_mul_patt = re.compile(r"Charge\s+=\s*([-\d]+)\s+" r"Multiplicity\s+=\s*(\d+)") num_basis_func_patt = re.compile(r"([0-9]+)\s+basis functions") num_elec_patt = re.compile(r"(\d+)\s+alpha electrons\s+(\d+)\s+beta electrons") pcm_patt = re.compile(r"Polarizable Continuum Model") stat_type_patt = re.compile(r"imaginary frequencies") scf_patt = re.compile(r"E\(.*\)\s*=\s*([-\.\d]+)\s+") mp2_patt = re.compile(r"EUMP2\s*=\s*(.*)") oniom_patt = re.compile(r"ONIOM:\s+extrapolated energy\s*=\s*(.*)") termination_patt = re.compile(r"(Normal|Error) termination") error_patt = re.compile(r"(! Non-Optimized Parameters !|Convergence failure)") mulliken_patt = re.compile(r"^\s*(Mulliken charges|Mulliken atomic charges)") mulliken_charge_patt = re.compile(r"^\s+(\d+)\s+([A-Z][a-z]?)\s*(\S*)") end_mulliken_patt = re.compile(r"(Sum of Mulliken )(.*)(charges)\s*=\s*(\D)") std_orientation_patt = re.compile(r"Standard orientation") input_orientation_patt = re.compile(r"Input orientation|Z-Matrix orientation") orbital_patt = re.compile(r"(Alpha|Beta)\s*\S+\s*eigenvalues --(.*)") thermo_patt = re.compile(r"(Zero-point|Thermal) correction(.*)=" r"\s+([\d\.-]+)") forces_on_patt = re.compile(r"Center\s+Atomic\s+Forces\s+\(Hartrees/Bohr\)") forces_off_patt = re.compile(r"Cartesian\s+Forces:\s+Max.*RMS.*") forces_patt = re.compile(r"\s+(\d+)\s+(\d+)\s+([0-9\.-]+)\s+([0-9\.-]+)\s+([0-9\.-]+)") freq_on_patt = re.compile(r"Harmonic\sfrequencies\s+\(cm\*\*-1\),\sIR\sintensities.*Raman.*") normal_mode_patt = re.compile(r"\s+(\d+)\s+(\d+)\s+([0-9\.-]{4,5})\s+([0-9\.-]{4,5}).*") mo_coeff_patt = re.compile(r"Molecular Orbital Coefficients:") mo_coeff_name_patt = re.compile(r"\d+\s((\d+|\s+)\s+([a-zA-Z]{1,2}|\s+))\s+(\d+\S+)") hessian_patt = re.compile(r"Force constants in Cartesian coordinates:") resume_patt = re.compile(r"^\s1\\1\\GINC-\S*") resume_end_patt = re.compile(r"^\s.*\\\\@") bond_order_patt = re.compile(r"Wiberg bond index matrix in the NAO basis:") self.properly_terminated = False self.is_pcm = False self.stationary_type = "Minimum" self.corrections = {} self.energies = [] self.pcm = None self.errors = [] self.Mulliken_charges = {} self.link0 = {} self.cart_forces = [] self.frequencies = [] self.eigenvalues = [] self.is_spin = False self.hessian = None self.resumes = [] self.title = None self.bond_orders = {} read_coord = 0 read_mulliken = False read_eigen = False eigen_txt = [] parse_stage = 0 num_basis_found = False terminated = False parse_forces = False forces = [] parse_freq = False frequencies = [] read_mo = False parse_hessian = False routeline = "" standard_orientation = False parse_bond_order = False input_structures = list() std_structures = list() geom_orientation = None opt_structures = list() with zopen(filename) as f: for line in f: if parse_stage == 0: if start_patt.search(line): parse_stage = 1 elif link0_patt.match(line): m = link0_patt.match(line) self.link0[m.group(1)] = m.group(2) elif route_patt.search(line) or routeline != "": if set(line.strip()) == {"-"}: params = read_route_line(routeline) self.functional = params[0] self.basis_set = params[1] self.route_parameters = params[2] route_lower = {k.lower(): v for k, v in self.route_parameters.items()} self.dieze_tag = params[3] parse_stage = 1 else: routeline += line.strip() elif parse_stage == 1: if set(line.strip()) == {"-"} and self.title is None: self.title = "" elif self.title == "": self.title = line.strip() elif charge_mul_patt.search(line): m = charge_mul_patt.search(line) self.charge = int(m.group(1)) self.spin_multiplicity = int(m.group(2)) parse_stage = 2 elif parse_stage == 2: if self.is_pcm: self._check_pcm(line) if "freq" in route_lower and thermo_patt.search(line): m = thermo_patt.search(line) if m.group(1) == "Zero-point": self.corrections["Zero-point"] = float(m.group(3)) else: key = m.group(2).strip(" to ") self.corrections[key] = float(m.group(3)) if read_coord: [f.readline() for i in range(3)] line = f.readline() sp = [] coords = [] while set(line.strip()) != {"-"}: toks = line.split() sp.append(Element.from_Z(int(toks[1]))) coords.append([float(x) for x in toks[3:6]]) line = f.readline() read_coord = False if geom_orientation == "input": input_structures.append(Molecule(sp, coords)) elif geom_orientation == "standard": std_structures.append(Molecule(sp, coords)) if parse_forces: m = forces_patt.search(line) if m: forces.extend([float(_v) for _v in m.groups()[2:5]]) elif forces_off_patt.search(line): self.cart_forces.append(forces) forces = [] parse_forces = False # read molecular orbital eigenvalues if read_eigen: m = orbital_patt.search(line) if m: eigen_txt.append(line) else: read_eigen = False self.eigenvalues = {Spin.up: []} for eigenline in eigen_txt: if "Alpha" in eigenline: self.eigenvalues[Spin.up] += [float(e) for e in float_patt.findall(eigenline)] elif "Beta" in eigenline: if Spin.down not in self.eigenvalues: self.eigenvalues[Spin.down] = [] self.eigenvalues[Spin.down] += [float(e) for e in float_patt.findall(eigenline)] eigen_txt = [] # read molecular orbital coefficients if (not num_basis_found) and num_basis_func_patt.search(line): m = num_basis_func_patt.search(line) self.num_basis_func = int(m.group(1)) num_basis_found = True elif read_mo: # build a matrix with all coefficients all_spin = [Spin.up] if self.is_spin: all_spin.append(Spin.down) mat_mo = {} for spin in all_spin: mat_mo[spin] = np.zeros((self.num_basis_func, self.num_basis_func)) nMO = 0 end_mo = False while nMO < self.num_basis_func and not end_mo: f.readline() f.readline() self.atom_basis_labels = [] for i in range(self.num_basis_func): line = f.readline() # identify atom and OA labels m = mo_coeff_name_patt.search(line) if m.group(1).strip() != "": iat = int(m.group(2)) - 1 # atname = m.group(3) self.atom_basis_labels.append([m.group(4)]) else: self.atom_basis_labels[iat].append(m.group(4)) # MO coefficients coeffs = [float(c) for c in float_patt.findall(line)] for j, c in enumerate(coeffs): mat_mo[spin][i, nMO + j] = c nMO += len(coeffs) line = f.readline() # manage pop=regular case (not all MO) if nMO < self.num_basis_func and ( "Density Matrix:" in line or mo_coeff_patt.search(line) ): end_mo = True warnings.warn("POP=regular case, matrix " "coefficients not complete") f.readline() self.eigenvectors = mat_mo read_mo = False # build a more convenient array dict with MO # coefficient of each atom in each MO. # mo[Spin][OM j][atom i] = # {AO_k: coeff, AO_k: coeff ... } mo = {} for spin in all_spin: mo[spin] = [ [{} for iat in range(len(self.atom_basis_labels))] for j in range(self.num_basis_func) ] for j in range(self.num_basis_func): i = 0 for iat in range(len(self.atom_basis_labels)): for label in self.atom_basis_labels[iat]: mo[spin][j][iat][label] = self.eigenvectors[spin][i, j] i += 1 self.molecular_orbital = mo elif parse_freq: while line.strip() != "": #  blank line ifreqs = [int(val) - 1 for val in line.split()] for ifreq in ifreqs: frequencies.append( { "frequency": None, "r_mass": None, "f_constant": None, "IR_intensity": None, "symmetry": None, "mode": [], } ) # read freq, intensity, masses, symmetry ... while "Atom AN" not in line: if "Frequencies --" in line: freqs = map(float, float_patt.findall(line)) for ifreq, freq in zip(ifreqs, freqs): frequencies[ifreq]["frequency"] = freq elif "Red. masses --" in line: r_masses = map(float, float_patt.findall(line)) for ifreq, r_mass in zip(ifreqs, r_masses): frequencies[ifreq]["r_mass"] = r_mass elif "Frc consts --" in line: f_consts = map(float, float_patt.findall(line)) for ifreq, f_const in zip(ifreqs, f_consts): frequencies[ifreq]["f_constant"] = f_const elif "IR Inten --" in line: IR_intens = map(float, float_patt.findall(line)) for ifreq, intens in zip(ifreqs, IR_intens): frequencies[ifreq]["IR_intensity"] = intens else: syms = line.split()[:3] for ifreq, sym in zip(ifreqs, syms): frequencies[ifreq]["symmetry"] = sym line = f.readline() #  read normal modes line = f.readline() while normal_mode_patt.search(line): values = list(map(float, float_patt.findall(line))) for i, ifreq in zip(range(0, len(values), 3), ifreqs): frequencies[ifreq]["mode"].extend(values[i : i + 3]) line = f.readline() parse_freq = False self.frequencies.append(frequencies) frequencies = [] elif parse_hessian: #  read Hessian matrix under "Force constants in Cartesian coordinates" #  Hessian matrix is in the input orientation framework # WARNING : need #P in the route line parse_hessian = False ndf = 3 * len(input_structures[0]) self.hessian = np.zeros((ndf, ndf)) j_indices = range(5) jndf = 0 while jndf < ndf: for i in range(jndf, ndf): line = f.readline() vals = re.findall(r"\s*([+-]?\d+\.\d+[eEdD]?[+-]\d+)", line) vals = [float(val.replace("D", "E")) for val in vals] for jval, val in enumerate(vals): j = j_indices[jval] self.hessian[i, j] = val self.hessian[j, i] = val jndf += len(vals) line = f.readline() j_indices = [j + 5 for j in j_indices] elif parse_bond_order: # parse Wiberg bond order line = f.readline() line = f.readline() nat = len(input_structures[0]) matrix = list() for iat in range(nat): line = f.readline() matrix.append([float(v) for v in line.split()[2:]]) self.bond_orders = dict() for iat in range(nat): for jat in range(iat + 1, nat): self.bond_orders[(iat, jat)] = matrix[iat][jat] parse_bond_order = False elif termination_patt.search(line): m = termination_patt.search(line) if m.group(1) == "Normal": self.properly_terminated = True terminated = True elif error_patt.search(line): error_defs = { "! Non-Optimized Parameters !": "Optimization " "error", "Convergence failure": "SCF convergence error", } m = error_patt.search(line) self.errors.append(error_defs[m.group(1)]) elif num_elec_patt.search(line): m = num_elec_patt.search(line) self.electrons = (int(m.group(1)), int(m.group(2))) elif (not self.is_pcm) and pcm_patt.search(line): self.is_pcm = True self.pcm = {} elif "freq" in route_lower and "opt" in route_lower and stat_type_patt.search(line): self.stationary_type = "Saddle" elif mp2_patt.search(line): m = mp2_patt.search(line) self.energies.append(float(m.group(1).replace("D", "E"))) elif oniom_patt.search(line): m = oniom_patt.matcher(line) self.energies.append(float(m.group(1))) elif scf_patt.search(line): m = scf_patt.search(line) self.energies.append(float(m.group(1))) elif std_orientation_patt.search(line): standard_orientation = True geom_orientation = "standard" read_coord = True elif input_orientation_patt.search(line): geom_orientation = "input" read_coord = True elif "Optimization completed." in line: line = f.readline() if " -- Stationary point found." not in line: warnings.warn( "\n" + self.filename + ": Optimization complete but this is not a stationary point" ) if standard_orientation: opt_structures.append(std_structures[-1]) else: opt_structures.append(input_structures[-1]) elif not read_eigen and orbital_patt.search(line): eigen_txt.append(line) read_eigen = True elif mulliken_patt.search(line): mulliken_txt = [] read_mulliken = True elif not parse_forces and forces_on_patt.search(line): parse_forces = True elif freq_on_patt.search(line): parse_freq = True [f.readline() for i in range(3)] elif mo_coeff_patt.search(line): if "Alpha" in line: self.is_spin = True read_mo = True elif hessian_patt.search(line): parse_hessian = True elif resume_patt.search(line): resume = [] while not resume_end_patt.search(line): resume.append(line) line = f.readline() #  security if \\@ not in one line ! if line == "\n": break resume.append(line) resume = "".join([r.strip() for r in resume]) self.resumes.append(resume) elif bond_order_patt.search(line): parse_bond_order = True if read_mulliken: if not end_mulliken_patt.search(line): mulliken_txt.append(line) else: m = end_mulliken_patt.search(line) mulliken_charges = {} for line in mulliken_txt: if mulliken_charge_patt.search(line): m = mulliken_charge_patt.search(line) dic = {int(m.group(1)): [m.group(2), float(m.group(3))]} mulliken_charges.update(dic) read_mulliken = False self.Mulliken_charges = mulliken_charges # store the structures. If symmetry is considered, the standard orientation # is used. Else the input orientation is used. if standard_orientation: self.structures = std_structures self.structures_input_orientation = input_structures else: self.structures = input_structures self.structures_input_orientation = input_structures # store optimized structure in input orientation self.opt_structures = opt_structures if not terminated: warnings.warn("\n" + self.filename + ": Termination error or bad Gaussian output file !") def _check_pcm(self, line): energy_patt = re.compile(r"(Dispersion|Cavitation|Repulsion) energy" r"\s+\S+\s+=\s+(\S*)") total_patt = re.compile(r"with all non electrostatic terms\s+\S+\s+" r"=\s+(\S*)") parameter_patt = re.compile(r"(Eps|Numeral density|RSolv|Eps" r"\(inf[inity]*\))\s+=\s*(\S*)") if energy_patt.search(line): m = energy_patt.search(line) self.pcm["{} energy".format(m.group(1))] = float(m.group(2)) elif total_patt.search(line): m = total_patt.search(line) self.pcm["Total energy"] = float(m.group(1)) elif parameter_patt.search(line): m = parameter_patt.search(line) self.pcm[m.group(1)] = float(m.group(2)) def as_dict(self): """ Json-serializable dict representation. """ structure = self.final_structure d = { "has_gaussian_completed": self.properly_terminated, "nsites": len(structure), } comp = structure.composition d["unit_cell_formula"] = comp.as_dict() d["reduced_cell_formula"] = Composition(comp.reduced_formula).as_dict() d["pretty_formula"] = comp.reduced_formula d["is_pcm"] = self.is_pcm d["errors"] = self.errors d["Mulliken_charges"] = self.Mulliken_charges unique_symbols = sorted(list(d["unit_cell_formula"].keys())) d["elements"] = unique_symbols d["nelements"] = len(unique_symbols) d["charge"] = self.charge d["spin_multiplicity"] = self.spin_multiplicity vin = { "route": self.route_parameters, "functional": self.functional, "basis_set": self.basis_set, "nbasisfunctions": self.num_basis_func, "pcm_parameters": self.pcm, } d["input"] = vin nsites = len(self.final_structure) vout = { "energies": self.energies, "final_energy": self.final_energy, "final_energy_per_atom": self.final_energy / nsites, "molecule": structure.as_dict(), "stationary_type": self.stationary_type, "corrections": self.corrections, } d["output"] = vout d["@module"] = self.__class__.__module__ d["@class"] = self.__class__.__name__ return d def read_scan(self): """ Read a potential energy surface from a gaussian scan calculation. Returns: A dict: {"energies": [ values ], "coords": {"d1": [ values ], "A2", [ values ], ... }} "energies" are the energies of all points of the potential energy surface. "coords" are the internal coordinates used to compute the potential energy surface and the internal coordinates optimized, labelled by their name as defined in the calculation. """ def floatList(l): """return a list of float from a list of string""" return [float(v) for v in l] scan_patt = re.compile(r"^\sSummary of the potential surface scan:") optscan_patt = re.compile(r"^\sSummary of Optimized Potential Surface Scan") coord_patt = re.compile(r"^\s*(\w+)((\s*[+-]?\d+\.\d+)+)") # data dict return data = {"energies": list(), "coords": dict()} # read in file with zopen(self.filename, "r") as f: line = f.readline() while line != "": if optscan_patt.match(line): f.readline() line = f.readline() endScan = False while not endScan: data["energies"] += floatList(float_patt.findall(line)) line = f.readline() while coord_patt.match(line): icname = line.split()[0].strip() if icname in data["coords"]: data["coords"][icname] += floatList(float_patt.findall(line)) else: data["coords"][icname] = floatList(float_patt.findall(line)) line = f.readline() if not re.search(r"^\s+((\s*\d+)+)", line): endScan = True else: line = f.readline() elif scan_patt.match(line): line = f.readline() data["coords"] = {icname: list() for icname in line.split()[1:-1]} f.readline() line = f.readline() while not re.search(r"^\s-+", line): values = floatList(line.split()) data["energies"].append(values[-1]) for i, icname in enumerate(data["coords"]): data["coords"][icname].append(values[i + 1]) line = f.readline() else: line = f.readline() return data def get_scan_plot(self, coords=None): """ Get a matplotlib plot of the potential energy surface. Args: coords: internal coordinate name to use as abcissa. """ from pymatgen.util.plotting import pretty_plot plt = pretty_plot(12, 8) d = self.read_scan() if coords and coords in d["coords"]: x = d["coords"][coords] plt.xlabel(coords) else: x = range(len(d["energies"])) plt.xlabel("points") plt.ylabel("Energy (eV)") e_min = min(d["energies"]) y = [(e - e_min) * Ha_to_eV for e in d["energies"]] plt.plot(x, y, "ro--") return plt def save_scan_plot(self, filename="scan.pdf", img_format="pdf", coords=None): """ Save matplotlib plot of the potential energy surface to a file. Args: filename: Filename to write to. img_format: Image format to use. Defaults to EPS. coords: internal coordinate name to use as abcissa. """ plt = self.get_scan_plot(coords) plt.savefig(filename, format=img_format) def read_excitation_energies(self): """ Read a excitation energies after a TD-DFT calculation. Returns: A list: A list of tuple for each transition such as [(energie (eV), lambda (nm), oscillatory strength), ... ] """ transitions = list() # read in file with zopen(self.filename, "r") as f: line = f.readline() td = False while line != "": if re.search(r"^\sExcitation energies and oscillator strengths:", line): td = True if td: if re.search(r"^\sExcited State\s*\d", line): val = [float(v) for v in float_patt.findall(line)] transitions.append(tuple(val[0:3])) line = f.readline() return transitions def get_spectre_plot(self, sigma=0.05, step=0.01): """ Get a matplotlib plot of the UV-visible xas. Transition are plotted as vertical lines and as a sum of normal functions with sigma with. The broadening is applied in energy and the xas is plotted as a function of the wavelength. Args: sigma: Full width at half maximum in eV for normal functions. step: bin interval in eV Returns: A dict: {"energies": values, "lambda": values, "xas": values} where values are lists of abscissa (energies, lamba) and the sum of gaussian functions (xas). A matplotlib plot. """ from scipy.stats import norm from pymatgen.util.plotting import pretty_plot plt = pretty_plot(12, 8) transitions = self.read_excitation_energies() minval = min([val[0] for val in transitions]) - 5.0 * sigma maxval = max([val[0] for val in transitions]) + 5.0 * sigma npts = int((maxval - minval) / step) + 1 eneval = np.linspace(minval, maxval, npts) # in eV lambdaval = [cst.h * cst.c / (val * cst.e) * 1.0e9 for val in eneval] # in nm # sum of gaussian functions spectre = np.zeros(npts) for trans in transitions: spectre += trans[2] * norm(eneval, trans[0], sigma) spectre /= spectre.max() plt.plot(lambdaval, spectre, "r-", label="spectre") data = {"energies": eneval, "lambda": lambdaval, "xas": spectre} # plot transitions as vlines plt.vlines( [val[1] for val in transitions], 0.0, [val[2] for val in transitions], color="blue", label="transitions", linewidth=2, ) plt.xlabel("$\\lambda$ (nm)") plt.ylabel("Arbitrary unit") plt.legend() return data, plt def save_spectre_plot(self, filename="spectre.pdf", img_format="pdf", sigma=0.05, step=0.01): """ Save matplotlib plot of the spectre to a file. Args: filename: Filename to write to. img_format: Image format to use. Defaults to EPS. sigma: Full width at half maximum in eV for normal functions. step: bin interval in eV """ d, plt = self.get_spectre_plot(sigma, step) plt.savefig(filename, format=img_format) def to_input( self, mol=None, charge=None, spin_multiplicity=None, title=None, functional=None, basis_set=None, route_parameters=None, input_parameters=None, link0_parameters=None, dieze_tag=None, cart_coords=False, ): """ Create a new input object using by default the last geometry read in the output file and with the same calculation parameters. Arguments are the same as GaussianInput class. Returns gaunip (GaussianInput) : the gaussian input object """ if not mol: mol = self.final_structure if charge is None: charge = self.charge if spin_multiplicity is None: spin_multiplicity = self.spin_multiplicity if not title: title = self.title if not functional: functional = self.functional if not basis_set: basis_set = self.basis_set if not route_parameters: route_parameters = self.route_parameters if not link0_parameters: link0_parameters = self.link0 if not dieze_tag: dieze_tag = self.dieze_tag return GaussianInput( mol=mol, charge=charge, spin_multiplicity=spin_multiplicity, title=title, functional=functional, basis_set=basis_set, route_parameters=route_parameters, input_parameters=input_parameters, link0_parameters=link0_parameters, dieze_tag=dieze_tag, )
mit
CalvinNeo/EasyMLPlatform
py/graphic/tree.py
1
4067
#coding:utf8 import numpy as np import math import pylab as pl import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import json class GraphTree: def __init__(self): self.jsonobj = {} self.leafNode = dict(boxstyle = 'round4',fc = '0.8') self.branchNode = dict(boxstyle = 'sawtooth',fc = '0.8') self.arrow = dict(arrowstyle = '<-') self.depth = 0 self.leafcount = 0 def get_depth_leafcount(self,root): current_node = root.keys()[0] #name of choice node(string) branch_dict = root[current_node] maxdepth, thisdepth, thisleafcount = 0,0,0 for current_node in branch_dict.keys(): # print current_node,type(branch_dict[current_node]).__name__ if type(branch_dict[current_node]).__name__ == 'dict': temp = self.get_depth_leafcount(branch_dict[current_node]) thisdepth = 1 + temp[0] thisleafcount += temp[1] else: thisdepth = 1 thisleafcount += 1 if thisdepth > maxdepth: maxdepth = thisdepth return maxdepth,thisleafcount def load(self,strjson): self.jsonobj = dict(strjson) self.depth,self.leafcount = self.get_depth_leafcount(self.jsonobj) def plotMidText(self, cntrPt, parentPt, txtString): xMid = (parentPt[0] - cntrPt[0]) / 2.0 + cntrPt[0] yMid = (parentPt[1] - cntrPt[1]) / 2.0 + cntrPt[1] self.ax1.text(xMid, yMid, txtString) def plotNode(self, nodeTxt, cntrPt, parentPt, nodeType): self.ax1.annotate(nodeTxt, xy = parentPt, xycoords = 'axes fraction', xytext = cntrPt, \ textcoords = 'axes fraction', va = 'center', ha = 'center', bbox = nodeType, arrowprops = self.arrow) def plotTree(self, myTree, parentPt, nodeTxt): depth, leaves = self.get_depth_leafcount(myTree) current_node = myTree.keys()[0] cntrPt = (self.xOff + (1.0 + leaves) / 2.0 / self.leafcount, self.yOff) self.plotMidText(cntrPt, parentPt, nodeTxt) self.plotNode(current_node, cntrPt, parentPt, self.branchNode) branch_dict = myTree[current_node] self.yOff -= 1.0 / self.depth for current_node in branch_dict.keys(): if type(branch_dict[current_node]).__name__ == 'dict': self.plotTree(branch_dict[current_node], cntrPt, str(current_node)) else: self.xOff += 1.0 / self.leafcount self.plotNode(branch_dict[current_node], (self.xOff, self.yOff), cntrPt, self.leafNode) self.plotMidText((self.xOff, self.yOff), cntrPt, str(current_node)) self.yOff += 1.0 / self.depth def createPlot(self, show = True, save = ''): fig = plt.figure(1, facecolor = 'white') fig.clf() axprops = dict(xticks = [], yticks = []) self.ax1 = plt.subplot(111,frameon = False, **axprops) self.xOff, self.yOff = -0.5 / self.leafcount, 1.0 self.plotTree(self.jsonobj, (0.5,1.0), '') import StringIO, urllib, base64 if show: plt.show() else: imgdata = StringIO.StringIO() fig.savefig(imgdata, format='png') imgdata.seek(0) # rewind the data uri = 'data:image/png;base64,' + urllib.quote(base64.b64encode(imgdata.buf)) imgdata.close() return uri def showPlot(self): plt.show() if __name__ == '__main__': tr = GraphTree() # aa = '{"no surfacing":{"0":"no","1":{"flippers":{"0":"no","1":"yes"}}}}' # tr.load(json.loads(aa)) #JSON can't have non-string key aa = {"aged":{"0":"no","1":{"male":{"0":"no","1":"yes"}}}} # aa = {'water': {0: 1, 1: {'foot': {0: "'no'", 1: "'yes'"}}}} print dict(aa) # aa = {"no surfacing":{0:"no",1:{"flippers":{0:"no",1:"yes"}}}} # print dict(aa) tr.load(aa) print tr.leafcount,tr.depth tr.createPlot(show=True)
apache-2.0
khiner/aubio
python/demos/demo_waveform_plot.py
10
2099
#! /usr/bin/env python import sys from aubio import pvoc, source from numpy import zeros, hstack def get_waveform_plot(filename, samplerate = 0, block_size = 4096, ax = None, downsample = 2**4): import matplotlib.pyplot as plt if not ax: fig = plt.figure() ax = fig.add_subplot(111) hop_s = block_size allsamples_max = zeros(0,) downsample = downsample # to plot n samples / hop_s a = source(filename, samplerate, hop_s) # source file if samplerate == 0: samplerate = a.samplerate total_frames = 0 while True: samples, read = a() # keep some data to plot it later new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0) allsamples_max = hstack([allsamples_max, new_maxes]) total_frames += read if read < hop_s: break allsamples_max = (allsamples_max > 0) * allsamples_max allsamples_max_times = [ ( float (t) / downsample ) * hop_s for t in range(len(allsamples_max)) ] ax.plot(allsamples_max_times, allsamples_max, '-b') ax.plot(allsamples_max_times, -allsamples_max, '-b') ax.axis(xmin = allsamples_max_times[0], xmax = allsamples_max_times[-1]) set_xlabels_sample2time(ax, allsamples_max_times[-1], samplerate) return ax def set_xlabels_sample2time(ax, latest_sample, samplerate): ax.axis(xmin = 0, xmax = latest_sample) if latest_sample / float(samplerate) > 60: ax.set_xlabel('time (mm:ss)') ax.set_xticklabels([ "%02d:%02d" % (t/float(samplerate)/60, (t/float(samplerate))%60) for t in ax.get_xticks()[:-1]], rotation = 50) else: ax.set_xlabel('time (ss.mm)') ax.set_xticklabels([ "%02d.%02d" % (t/float(samplerate), 100*((t/float(samplerate))%1) ) for t in ax.get_xticks()[:-1]], rotation = 50) if __name__ == '__main__': import matplotlib.pyplot as plt if len(sys.argv) < 2: print "Usage: %s <filename>" % sys.argv[0] else: for soundfile in sys.argv[1:]: get_waveform_plot(soundfile) # display graph plt.show()
gpl-3.0
Ziqi-Li/bknqgis
pandas/pandas/tests/io/test_packers.py
7
31902
import pytest from warnings import catch_warnings import os import datetime import numpy as np import sys from distutils.version import LooseVersion from pandas import compat from pandas.compat import u, PY3 from pandas import (Series, DataFrame, Panel, MultiIndex, bdate_range, date_range, period_range, Index, Categorical) from pandas.errors import PerformanceWarning from pandas.io.packers import to_msgpack, read_msgpack import pandas.util.testing as tm from pandas.util.testing import (ensure_clean, assert_categorical_equal, assert_frame_equal, assert_index_equal, assert_series_equal, patch) from pandas.tests.test_panel import assert_panel_equal import pandas from pandas import Timestamp, NaT from pandas._libs.tslib import iNaT nan = np.nan try: import blosc # NOQA except ImportError: _BLOSC_INSTALLED = False else: _BLOSC_INSTALLED = True try: import zlib # NOQA except ImportError: _ZLIB_INSTALLED = False else: _ZLIB_INSTALLED = True @pytest.fixture(scope='module') def current_packers_data(): # our current version packers data from pandas.tests.io.generate_legacy_storage_files import ( create_msgpack_data) return create_msgpack_data() @pytest.fixture(scope='module') def all_packers_data(): # our all of our current version packers data from pandas.tests.io.generate_legacy_storage_files import ( create_data) return create_data() def check_arbitrary(a, b): if isinstance(a, (list, tuple)) and isinstance(b, (list, tuple)): assert(len(a) == len(b)) for a_, b_ in zip(a, b): check_arbitrary(a_, b_) elif isinstance(a, Panel): assert_panel_equal(a, b) elif isinstance(a, DataFrame): assert_frame_equal(a, b) elif isinstance(a, Series): assert_series_equal(a, b) elif isinstance(a, Index): assert_index_equal(a, b) elif isinstance(a, Categorical): # Temp, # Categorical.categories is changed from str to bytes in PY3 # maybe the same as GH 13591 if PY3 and b.categories.inferred_type == 'string': pass else: tm.assert_categorical_equal(a, b) elif a is NaT: assert b is NaT elif isinstance(a, Timestamp): assert a == b assert a.freq == b.freq else: assert(a == b) class TestPackers(object): def setup_method(self, method): self.path = '__%s__.msg' % tm.rands(10) def teardown_method(self, method): pass def encode_decode(self, x, compress=None, **kwargs): with ensure_clean(self.path) as p: to_msgpack(p, x, compress=compress, **kwargs) return read_msgpack(p, **kwargs) class TestAPI(TestPackers): def test_string_io(self): df = DataFrame(np.random.randn(10, 2)) s = df.to_msgpack(None) result = read_msgpack(s) tm.assert_frame_equal(result, df) s = df.to_msgpack() result = read_msgpack(s) tm.assert_frame_equal(result, df) s = df.to_msgpack() result = read_msgpack(compat.BytesIO(s)) tm.assert_frame_equal(result, df) s = to_msgpack(None, df) result = read_msgpack(s) tm.assert_frame_equal(result, df) with ensure_clean(self.path) as p: s = df.to_msgpack() fh = open(p, 'wb') fh.write(s) fh.close() result = read_msgpack(p) tm.assert_frame_equal(result, df) def test_path_pathlib(self): df = tm.makeDataFrame() result = tm.round_trip_pathlib(df.to_msgpack, read_msgpack) tm.assert_frame_equal(df, result) def test_path_localpath(self): df = tm.makeDataFrame() result = tm.round_trip_localpath(df.to_msgpack, read_msgpack) tm.assert_frame_equal(df, result) def test_iterator_with_string_io(self): dfs = [DataFrame(np.random.randn(10, 2)) for i in range(5)] s = to_msgpack(None, *dfs) for i, result in enumerate(read_msgpack(s, iterator=True)): tm.assert_frame_equal(result, dfs[i]) def test_invalid_arg(self): # GH10369 class A(object): def __init__(self): self.read = 0 pytest.raises(ValueError, read_msgpack, path_or_buf=None) pytest.raises(ValueError, read_msgpack, path_or_buf={}) pytest.raises(ValueError, read_msgpack, path_or_buf=A()) class TestNumpy(TestPackers): def test_numpy_scalar_float(self): x = np.float32(np.random.rand()) x_rec = self.encode_decode(x) tm.assert_almost_equal(x, x_rec) def test_numpy_scalar_complex(self): x = np.complex64(np.random.rand() + 1j * np.random.rand()) x_rec = self.encode_decode(x) assert np.allclose(x, x_rec) def test_scalar_float(self): x = np.random.rand() x_rec = self.encode_decode(x) tm.assert_almost_equal(x, x_rec) def test_scalar_complex(self): x = np.random.rand() + 1j * np.random.rand() x_rec = self.encode_decode(x) assert np.allclose(x, x_rec) def test_list_numpy_float(self): x = [np.float32(np.random.rand()) for i in range(5)] x_rec = self.encode_decode(x) # current msgpack cannot distinguish list/tuple tm.assert_almost_equal(tuple(x), x_rec) x_rec = self.encode_decode(tuple(x)) tm.assert_almost_equal(tuple(x), x_rec) def test_list_numpy_float_complex(self): if not hasattr(np, 'complex128'): pytest.skip('numpy cant handle complex128') x = [np.float32(np.random.rand()) for i in range(5)] + \ [np.complex128(np.random.rand() + 1j * np.random.rand()) for i in range(5)] x_rec = self.encode_decode(x) assert np.allclose(x, x_rec) def test_list_float(self): x = [np.random.rand() for i in range(5)] x_rec = self.encode_decode(x) # current msgpack cannot distinguish list/tuple tm.assert_almost_equal(tuple(x), x_rec) x_rec = self.encode_decode(tuple(x)) tm.assert_almost_equal(tuple(x), x_rec) def test_list_float_complex(self): x = [np.random.rand() for i in range(5)] + \ [(np.random.rand() + 1j * np.random.rand()) for i in range(5)] x_rec = self.encode_decode(x) assert np.allclose(x, x_rec) def test_dict_float(self): x = {'foo': 1.0, 'bar': 2.0} x_rec = self.encode_decode(x) tm.assert_almost_equal(x, x_rec) def test_dict_complex(self): x = {'foo': 1.0 + 1.0j, 'bar': 2.0 + 2.0j} x_rec = self.encode_decode(x) tm.assert_dict_equal(x, x_rec) for key in x: tm.assert_class_equal(x[key], x_rec[key], obj="complex value") def test_dict_numpy_float(self): x = {'foo': np.float32(1.0), 'bar': np.float32(2.0)} x_rec = self.encode_decode(x) tm.assert_almost_equal(x, x_rec) def test_dict_numpy_complex(self): x = {'foo': np.complex128(1.0 + 1.0j), 'bar': np.complex128(2.0 + 2.0j)} x_rec = self.encode_decode(x) tm.assert_dict_equal(x, x_rec) for key in x: tm.assert_class_equal(x[key], x_rec[key], obj="numpy complex128") def test_numpy_array_float(self): # run multiple times for n in range(10): x = np.random.rand(10) for dtype in ['float32', 'float64']: x = x.astype(dtype) x_rec = self.encode_decode(x) tm.assert_almost_equal(x, x_rec) def test_numpy_array_complex(self): x = (np.random.rand(5) + 1j * np.random.rand(5)).astype(np.complex128) x_rec = self.encode_decode(x) assert (all(map(lambda x, y: x == y, x, x_rec)) and x.dtype == x_rec.dtype) def test_list_mixed(self): x = [1.0, np.float32(3.5), np.complex128(4.25), u('foo')] x_rec = self.encode_decode(x) # current msgpack cannot distinguish list/tuple tm.assert_almost_equal(tuple(x), x_rec) x_rec = self.encode_decode(tuple(x)) tm.assert_almost_equal(tuple(x), x_rec) class TestBasic(TestPackers): def test_timestamp(self): for i in [Timestamp( '20130101'), Timestamp('20130101', tz='US/Eastern'), Timestamp('201301010501')]: i_rec = self.encode_decode(i) assert i == i_rec def test_nat(self): nat_rec = self.encode_decode(NaT) assert NaT is nat_rec def test_datetimes(self): # fails under 2.6/win32 (np.datetime64 seems broken) if LooseVersion(sys.version) < '2.7': pytest.skip('2.6 with np.datetime64 is broken') for i in [datetime.datetime(2013, 1, 1), datetime.datetime(2013, 1, 1, 5, 1), datetime.date(2013, 1, 1), np.datetime64(datetime.datetime(2013, 1, 5, 2, 15))]: i_rec = self.encode_decode(i) assert i == i_rec def test_timedeltas(self): for i in [datetime.timedelta(days=1), datetime.timedelta(days=1, seconds=10), np.timedelta64(1000000)]: i_rec = self.encode_decode(i) assert i == i_rec class TestIndex(TestPackers): def setup_method(self, method): super(TestIndex, self).setup_method(method) self.d = { 'string': tm.makeStringIndex(100), 'date': tm.makeDateIndex(100), 'int': tm.makeIntIndex(100), 'rng': tm.makeRangeIndex(100), 'float': tm.makeFloatIndex(100), 'empty': Index([]), 'tuple': Index(zip(['foo', 'bar', 'baz'], [1, 2, 3])), 'period': Index(period_range('2012-1-1', freq='M', periods=3)), 'date2': Index(date_range('2013-01-1', periods=10)), 'bdate': Index(bdate_range('2013-01-02', periods=10)), 'cat': tm.makeCategoricalIndex(100) } self.mi = { 'reg': MultiIndex.from_tuples([('bar', 'one'), ('baz', 'two'), ('foo', 'two'), ('qux', 'one'), ('qux', 'two')], names=['first', 'second']), } def test_basic_index(self): for s, i in self.d.items(): i_rec = self.encode_decode(i) tm.assert_index_equal(i, i_rec) # datetime with no freq (GH5506) i = Index([Timestamp('20130101'), Timestamp('20130103')]) i_rec = self.encode_decode(i) tm.assert_index_equal(i, i_rec) # datetime with timezone i = Index([Timestamp('20130101 9:00:00'), Timestamp( '20130103 11:00:00')]).tz_localize('US/Eastern') i_rec = self.encode_decode(i) tm.assert_index_equal(i, i_rec) def test_multi_index(self): for s, i in self.mi.items(): i_rec = self.encode_decode(i) tm.assert_index_equal(i, i_rec) def test_unicode(self): i = tm.makeUnicodeIndex(100) i_rec = self.encode_decode(i) tm.assert_index_equal(i, i_rec) def categorical_index(self): # GH15487 df = DataFrame(np.random.randn(10, 2)) df = df.astype({0: 'category'}).set_index(0) result = self.encode_decode(df) tm.assert_frame_equal(result, df) class TestSeries(TestPackers): def setup_method(self, method): super(TestSeries, self).setup_method(method) self.d = {} s = tm.makeStringSeries() s.name = 'string' self.d['string'] = s s = tm.makeObjectSeries() s.name = 'object' self.d['object'] = s s = Series(iNaT, dtype='M8[ns]', index=range(5)) self.d['date'] = s data = { 'A': [0., 1., 2., 3., np.nan], 'B': [0, 1, 0, 1, 0], 'C': ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'], 'D': date_range('1/1/2009', periods=5), 'E': [0., 1, Timestamp('20100101'), 'foo', 2.], 'F': [Timestamp('20130102', tz='US/Eastern')] * 2 + [Timestamp('20130603', tz='CET')] * 3, 'G': [Timestamp('20130102', tz='US/Eastern')] * 5, 'H': Categorical([1, 2, 3, 4, 5]), 'I': Categorical([1, 2, 3, 4, 5], ordered=True), } self.d['float'] = Series(data['A']) self.d['int'] = Series(data['B']) self.d['mixed'] = Series(data['E']) self.d['dt_tz_mixed'] = Series(data['F']) self.d['dt_tz'] = Series(data['G']) self.d['cat_ordered'] = Series(data['H']) self.d['cat_unordered'] = Series(data['I']) def test_basic(self): # run multiple times here for n in range(10): for s, i in self.d.items(): i_rec = self.encode_decode(i) assert_series_equal(i, i_rec) class TestCategorical(TestPackers): def setup_method(self, method): super(TestCategorical, self).setup_method(method) self.d = {} self.d['plain_str'] = Categorical(['a', 'b', 'c', 'd', 'e']) self.d['plain_str_ordered'] = Categorical(['a', 'b', 'c', 'd', 'e'], ordered=True) self.d['plain_int'] = Categorical([5, 6, 7, 8]) self.d['plain_int_ordered'] = Categorical([5, 6, 7, 8], ordered=True) def test_basic(self): # run multiple times here for n in range(10): for s, i in self.d.items(): i_rec = self.encode_decode(i) assert_categorical_equal(i, i_rec) class TestNDFrame(TestPackers): def setup_method(self, method): super(TestNDFrame, self).setup_method(method) data = { 'A': [0., 1., 2., 3., np.nan], 'B': [0, 1, 0, 1, 0], 'C': ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'], 'D': date_range('1/1/2009', periods=5), 'E': [0., 1, Timestamp('20100101'), 'foo', 2.], 'F': [Timestamp('20130102', tz='US/Eastern')] * 5, 'G': [Timestamp('20130603', tz='CET')] * 5, 'H': Categorical(['a', 'b', 'c', 'd', 'e']), 'I': Categorical(['a', 'b', 'c', 'd', 'e'], ordered=True), } self.frame = { 'float': DataFrame(dict(A=data['A'], B=Series(data['A']) + 1)), 'int': DataFrame(dict(A=data['B'], B=Series(data['B']) + 1)), 'mixed': DataFrame(data)} with catch_warnings(record=True): self.panel = { 'float': Panel(dict(ItemA=self.frame['float'], ItemB=self.frame['float'] + 1))} def test_basic_frame(self): for s, i in self.frame.items(): i_rec = self.encode_decode(i) assert_frame_equal(i, i_rec) def test_basic_panel(self): with catch_warnings(record=True): for s, i in self.panel.items(): i_rec = self.encode_decode(i) assert_panel_equal(i, i_rec) def test_multi(self): i_rec = self.encode_decode(self.frame) for k in self.frame.keys(): assert_frame_equal(self.frame[k], i_rec[k]) l = tuple([self.frame['float'], self.frame['float'].A, self.frame['float'].B, None]) l_rec = self.encode_decode(l) check_arbitrary(l, l_rec) # this is an oddity in that packed lists will be returned as tuples l = [self.frame['float'], self.frame['float'] .A, self.frame['float'].B, None] l_rec = self.encode_decode(l) assert isinstance(l_rec, tuple) check_arbitrary(l, l_rec) def test_iterator(self): l = [self.frame['float'], self.frame['float'] .A, self.frame['float'].B, None] with ensure_clean(self.path) as path: to_msgpack(path, *l) for i, packed in enumerate(read_msgpack(path, iterator=True)): check_arbitrary(packed, l[i]) def tests_datetimeindex_freq_issue(self): # GH 5947 # inferring freq on the datetimeindex df = DataFrame([1, 2, 3], index=date_range('1/1/2013', '1/3/2013')) result = self.encode_decode(df) assert_frame_equal(result, df) df = DataFrame([1, 2], index=date_range('1/1/2013', '1/2/2013')) result = self.encode_decode(df) assert_frame_equal(result, df) def test_dataframe_duplicate_column_names(self): # GH 9618 expected_1 = DataFrame(columns=['a', 'a']) expected_2 = DataFrame(columns=[1] * 100) expected_2.loc[0] = np.random.randn(100) expected_3 = DataFrame(columns=[1, 1]) expected_3.loc[0] = ['abc', np.nan] result_1 = self.encode_decode(expected_1) result_2 = self.encode_decode(expected_2) result_3 = self.encode_decode(expected_3) assert_frame_equal(result_1, expected_1) assert_frame_equal(result_2, expected_2) assert_frame_equal(result_3, expected_3) class TestSparse(TestPackers): def _check_roundtrip(self, obj, comparator, **kwargs): # currently these are not implemetned # i_rec = self.encode_decode(obj) # comparator(obj, i_rec, **kwargs) pytest.raises(NotImplementedError, self.encode_decode, obj) def test_sparse_series(self): s = tm.makeStringSeries() s[3:5] = np.nan ss = s.to_sparse() self._check_roundtrip(ss, tm.assert_series_equal, check_series_type=True) ss2 = s.to_sparse(kind='integer') self._check_roundtrip(ss2, tm.assert_series_equal, check_series_type=True) ss3 = s.to_sparse(fill_value=0) self._check_roundtrip(ss3, tm.assert_series_equal, check_series_type=True) def test_sparse_frame(self): s = tm.makeDataFrame() s.loc[3:5, 1:3] = np.nan s.loc[8:10, -2] = np.nan ss = s.to_sparse() self._check_roundtrip(ss, tm.assert_frame_equal, check_frame_type=True) ss2 = s.to_sparse(kind='integer') self._check_roundtrip(ss2, tm.assert_frame_equal, check_frame_type=True) ss3 = s.to_sparse(fill_value=0) self._check_roundtrip(ss3, tm.assert_frame_equal, check_frame_type=True) class TestCompression(TestPackers): """See https://github.com/pandas-dev/pandas/pull/9783 """ def setup_method(self, method): try: from sqlalchemy import create_engine self._create_sql_engine = create_engine except ImportError: self._SQLALCHEMY_INSTALLED = False else: self._SQLALCHEMY_INSTALLED = True super(TestCompression, self).setup_method(method) data = { 'A': np.arange(1000, dtype=np.float64), 'B': np.arange(1000, dtype=np.int32), 'C': list(100 * 'abcdefghij'), 'D': date_range(datetime.datetime(2015, 4, 1), periods=1000), 'E': [datetime.timedelta(days=x) for x in range(1000)], } self.frame = { 'float': DataFrame(dict((k, data[k]) for k in ['A', 'A'])), 'int': DataFrame(dict((k, data[k]) for k in ['B', 'B'])), 'mixed': DataFrame(data), } def test_plain(self): i_rec = self.encode_decode(self.frame) for k in self.frame.keys(): assert_frame_equal(self.frame[k], i_rec[k]) def _test_compression(self, compress): i_rec = self.encode_decode(self.frame, compress=compress) for k in self.frame.keys(): value = i_rec[k] expected = self.frame[k] assert_frame_equal(value, expected) # make sure that we can write to the new frames for block in value._data.blocks: assert block.values.flags.writeable def test_compression_zlib(self): if not _ZLIB_INSTALLED: pytest.skip('no zlib') self._test_compression('zlib') def test_compression_blosc(self): if not _BLOSC_INSTALLED: pytest.skip('no blosc') self._test_compression('blosc') def _test_compression_warns_when_decompress_caches(self, compress): not_garbage = [] control = [] # copied data compress_module = globals()[compress] real_decompress = compress_module.decompress def decompress(ob): """mock decompress function that delegates to the real decompress but caches the result and a copy of the result. """ res = real_decompress(ob) not_garbage.append(res) # hold a reference to this bytes object control.append(bytearray(res)) # copy the data here to check later return res # types mapped to values to add in place. rhs = { np.dtype('float64'): 1.0, np.dtype('int32'): 1, np.dtype('object'): 'a', np.dtype('datetime64[ns]'): np.timedelta64(1, 'ns'), np.dtype('timedelta64[ns]'): np.timedelta64(1, 'ns'), } with patch(compress_module, 'decompress', decompress), \ tm.assert_produces_warning(PerformanceWarning) as ws: i_rec = self.encode_decode(self.frame, compress=compress) for k in self.frame.keys(): value = i_rec[k] expected = self.frame[k] assert_frame_equal(value, expected) # make sure that we can write to the new frames even though # we needed to copy the data for block in value._data.blocks: assert block.values.flags.writeable # mutate the data in some way block.values[0] += rhs[block.dtype] for w in ws: # check the messages from our warnings assert str(w.message) == ('copying data after decompressing; ' 'this may mean that decompress is ' 'caching its result') for buf, control_buf in zip(not_garbage, control): # make sure none of our mutations above affected the # original buffers assert buf == control_buf def test_compression_warns_when_decompress_caches_zlib(self): if not _ZLIB_INSTALLED: pytest.skip('no zlib') self._test_compression_warns_when_decompress_caches('zlib') def test_compression_warns_when_decompress_caches_blosc(self): if not _BLOSC_INSTALLED: pytest.skip('no blosc') self._test_compression_warns_when_decompress_caches('blosc') def _test_small_strings_no_warn(self, compress): empty = np.array([], dtype='uint8') with tm.assert_produces_warning(None): empty_unpacked = self.encode_decode(empty, compress=compress) tm.assert_numpy_array_equal(empty_unpacked, empty) assert empty_unpacked.flags.writeable char = np.array([ord(b'a')], dtype='uint8') with tm.assert_produces_warning(None): char_unpacked = self.encode_decode(char, compress=compress) tm.assert_numpy_array_equal(char_unpacked, char) assert char_unpacked.flags.writeable # if this test fails I am sorry because the interpreter is now in a # bad state where b'a' points to 98 == ord(b'b'). char_unpacked[0] = ord(b'b') # we compare the ord of bytes b'a' with unicode u'a' because the should # always be the same (unless we were able to mutate the shared # character singleton in which case ord(b'a') == ord(b'b'). assert ord(b'a') == ord(u'a') tm.assert_numpy_array_equal( char_unpacked, np.array([ord(b'b')], dtype='uint8'), ) def test_small_strings_no_warn_zlib(self): if not _ZLIB_INSTALLED: pytest.skip('no zlib') self._test_small_strings_no_warn('zlib') def test_small_strings_no_warn_blosc(self): if not _BLOSC_INSTALLED: pytest.skip('no blosc') self._test_small_strings_no_warn('blosc') def test_readonly_axis_blosc(self): # GH11880 if not _BLOSC_INSTALLED: pytest.skip('no blosc') df1 = DataFrame({'A': list('abcd')}) df2 = DataFrame(df1, index=[1., 2., 3., 4.]) assert 1 in self.encode_decode(df1['A'], compress='blosc') assert 1. in self.encode_decode(df2['A'], compress='blosc') def test_readonly_axis_zlib(self): # GH11880 df1 = DataFrame({'A': list('abcd')}) df2 = DataFrame(df1, index=[1., 2., 3., 4.]) assert 1 in self.encode_decode(df1['A'], compress='zlib') assert 1. in self.encode_decode(df2['A'], compress='zlib') def test_readonly_axis_blosc_to_sql(self): # GH11880 if not _BLOSC_INSTALLED: pytest.skip('no blosc') if not self._SQLALCHEMY_INSTALLED: pytest.skip('no sqlalchemy') expected = DataFrame({'A': list('abcd')}) df = self.encode_decode(expected, compress='blosc') eng = self._create_sql_engine("sqlite:///:memory:") df.to_sql('test', eng, if_exists='append') result = pandas.read_sql_table('test', eng, index_col='index') result.index.names = [None] assert_frame_equal(expected, result) def test_readonly_axis_zlib_to_sql(self): # GH11880 if not _ZLIB_INSTALLED: pytest.skip('no zlib') if not self._SQLALCHEMY_INSTALLED: pytest.skip('no sqlalchemy') expected = DataFrame({'A': list('abcd')}) df = self.encode_decode(expected, compress='zlib') eng = self._create_sql_engine("sqlite:///:memory:") df.to_sql('test', eng, if_exists='append') result = pandas.read_sql_table('test', eng, index_col='index') result.index.names = [None] assert_frame_equal(expected, result) class TestEncoding(TestPackers): def setup_method(self, method): super(TestEncoding, self).setup_method(method) data = { 'A': [compat.u('\u2019')] * 1000, 'B': np.arange(1000, dtype=np.int32), 'C': list(100 * 'abcdefghij'), 'D': date_range(datetime.datetime(2015, 4, 1), periods=1000), 'E': [datetime.timedelta(days=x) for x in range(1000)], 'G': [400] * 1000 } self.frame = { 'float': DataFrame(dict((k, data[k]) for k in ['A', 'A'])), 'int': DataFrame(dict((k, data[k]) for k in ['B', 'B'])), 'mixed': DataFrame(data), } self.utf_encodings = ['utf8', 'utf16', 'utf32'] def test_utf(self): # GH10581 for encoding in self.utf_encodings: for frame in compat.itervalues(self.frame): result = self.encode_decode(frame, encoding=encoding) assert_frame_equal(result, frame) def test_default_encoding(self): for frame in compat.itervalues(self.frame): result = frame.to_msgpack() expected = frame.to_msgpack(encoding='utf8') assert result == expected result = self.encode_decode(frame) assert_frame_equal(result, frame) def legacy_packers_versions(): # yield the packers versions path = tm.get_data_path('legacy_msgpack') for v in os.listdir(path): p = os.path.join(path, v) if os.path.isdir(p): yield v class TestMsgpack(object): """ How to add msgpack tests: 1. Install pandas version intended to output the msgpack. TestPackers 2. Execute "generate_legacy_storage_files.py" to create the msgpack. $ python generate_legacy_storage_files.py <output_dir> msgpack 3. Move the created pickle to "data/legacy_msgpack/<version>" directory. """ minimum_structure = {'series': ['float', 'int', 'mixed', 'ts', 'mi', 'dup'], 'frame': ['float', 'int', 'mixed', 'mi'], 'panel': ['float'], 'index': ['int', 'date', 'period'], 'mi': ['reg2']} def check_min_structure(self, data, version): for typ, v in self.minimum_structure.items(): assert typ in data, '"{0}" not found in unpacked data'.format(typ) for kind in v: msg = '"{0}" not found in data["{1}"]'.format(kind, typ) assert kind in data[typ], msg def compare(self, current_data, all_data, vf, version): # GH12277 encoding default used to be latin-1, now utf-8 if LooseVersion(version) < '0.18.0': data = read_msgpack(vf, encoding='latin-1') else: data = read_msgpack(vf) self.check_min_structure(data, version) for typ, dv in data.items(): assert typ in all_data, ('unpacked data contains ' 'extra key "{0}"' .format(typ)) for dt, result in dv.items(): assert dt in current_data[typ], ('data["{0}"] contains extra ' 'key "{1}"'.format(typ, dt)) try: expected = current_data[typ][dt] except KeyError: continue # use a specific comparator # if available comp_method = "compare_{typ}_{dt}".format(typ=typ, dt=dt) comparator = getattr(self, comp_method, None) if comparator is not None: comparator(result, expected, typ, version) else: check_arbitrary(result, expected) return data def compare_series_dt_tz(self, result, expected, typ, version): # 8260 # dtype is object < 0.17.0 if LooseVersion(version) < '0.17.0': expected = expected.astype(object) tm.assert_series_equal(result, expected) else: tm.assert_series_equal(result, expected) def compare_frame_dt_mixed_tzs(self, result, expected, typ, version): # 8260 # dtype is object < 0.17.0 if LooseVersion(version) < '0.17.0': expected = expected.astype(object) tm.assert_frame_equal(result, expected) else: tm.assert_frame_equal(result, expected) @pytest.mark.parametrize('version', legacy_packers_versions()) def test_msgpacks_legacy(self, current_packers_data, all_packers_data, version): pth = tm.get_data_path('legacy_msgpack/{0}'.format(version)) n = 0 for f in os.listdir(pth): # GH12142 0.17 files packed in P2 can't be read in P3 if (compat.PY3 and version.startswith('0.17.') and f.split('.')[-4][-1] == '2'): continue vf = os.path.join(pth, f) try: with catch_warnings(record=True): self.compare(current_packers_data, all_packers_data, vf, version) except ImportError: # blosc not installed continue n += 1 assert n > 0, 'Msgpack files are not tested'
gpl-2.0
jeffzheng1/tensorflow
tensorflow/contrib/learn/python/learn/learn_io/pandas_io.py
14
3569
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Methods to allow pandas.DataFrame.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function try: # pylint: disable=g-import-not-at-top import pandas as pd HAS_PANDAS = True except ImportError: HAS_PANDAS = False PANDAS_DTYPES = { 'int8': 'int', 'int16': 'int', 'int32': 'int', 'int64': 'int', 'uint8': 'int', 'uint16': 'int', 'uint32': 'int', 'uint64': 'int', 'float16': 'float', 'float32': 'float', 'float64': 'float', 'bool': 'i' } def extract_pandas_data(data): """Extract data from pandas.DataFrame for predictors. Given a DataFrame, will extract the values and cast them to float. The DataFrame is expected to contain values of type int, float or bool. Args: data: `pandas.DataFrame` containing the data to be extracted. Returns: A numpy `ndarray` of the DataFrame's values as floats. Raises: ValueError: if data contains types other than int, float or bool. """ if not isinstance(data, pd.DataFrame): return data bad_data = [column for column in data if data[column].dtype.name not in PANDAS_DTYPES] if not bad_data: return data.values.astype('float') else: error_report = [("'" + str(column) + "' type='" + data[column].dtype.name + "'") for column in bad_data] raise ValueError('Data types for extracting pandas data must be int, ' 'float, or bool. Found: ' + ', '.join(error_report)) def extract_pandas_matrix(data): """Extracts numpy matrix from pandas DataFrame. Args: data: `pandas.DataFrame` containing the data to be extracted. Returns: A numpy `ndarray` of the DataFrame's values. """ if not isinstance(data, pd.DataFrame): return data return data.as_matrix() def extract_pandas_labels(labels): """Extract data from pandas.DataFrame for labels. Args: labels: `pandas.DataFrame` or `pandas.Series` containing one column of labels to be extracted. Returns: A numpy `ndarray` of labels from the DataFrame. Raises: ValueError: if more than one column is found or type is not int, float or bool. """ if isinstance(labels, pd.DataFrame): # pandas.Series also belongs to DataFrame if len(labels.columns) > 1: raise ValueError('Only one column for labels is allowed.') bad_data = [column for column in labels if labels[column].dtype.name not in PANDAS_DTYPES] if not bad_data: return labels.values else: error_report = ["'" + str(column) + "' type=" + str(labels[column].dtype.name) for column in bad_data] raise ValueError('Data types for extracting labels must be int, ' 'float, or bool. Found: ' + ', '.join(error_report)) else: return labels
apache-2.0