def test_masked_array_obj_dtype_pickleable(): marr = MaskedArray([1, None, 'a'], dtype=object) for mask in (True, False, [0, 1, 0]): marr.mask = mask marr_pickled = pickle.loads(pickle.dumps(marr)) assert_array_equal(marr.data, marr_pickled.data) assert_array_equal(marr.mask, marr_pickled.mask)
def create_cv_results(scores, candidate_params, n_splits, error_score, weights): if isinstance(scores[0], tuple): test_scores, train_scores = zip(*scores) else: test_scores = scores train_scores = None test_scores = [error_score if s is FIT_FAILURE else s for s in test_scores] if train_scores is not None: train_scores = [ error_score if s is FIT_FAILURE else s for s in train_scores ] # Construct the `cv_results_` dictionary results = {'params': candidate_params} n_candidates = len(candidate_params) if weights is not None: weights = np.broadcast_to(weights[None, :], (len(candidate_params), len(weights))) _store(results, 'test_score', test_scores, n_splits, n_candidates, splits=True, rank=True, weights=weights) if train_scores is not None: _store(results, 'train_score', train_scores, n_splits, n_candidates, splits=True) # Use one MaskedArray and mask all the places where the param is not # applicable for that candidate. Use defaultdict as each candidate may # not contain all the params param_results = defaultdict( lambda: MaskedArray(np.empty(n_candidates), mask=True, dtype=object)) for cand_i, params in enumerate(candidate_params): for name, value in params.items(): param_results["param_%s" % name][cand_i] = value results.update(param_results) return results
def create_cv_results(output, candidate_params, n_splits, error_score, iid, return_train_score): if return_train_score: train_scores, test_scores, test_sample_counts = unzip(output, 3) train_scores = [ error_score if s is FIT_FAILURE else s for s in train_scores ] else: test_scores, test_sample_counts = unzip(output, 2) test_scores = [error_score if s is FIT_FAILURE else s for s in test_scores] # Construct the `cv_results_` dictionary results = {'params': candidate_params} n_candidates = len(candidate_params) test_sample_counts = np.array(test_sample_counts[:n_splits], dtype=int) _store(results, 'test_score', test_scores, n_splits, n_candidates, splits=True, rank=True, weights=test_sample_counts if iid else None) if return_train_score: _store(results, 'train_score', train_scores, n_splits, n_candidates, splits=True) # Use one MaskedArray and mask all the places where the param is not # applicable for that candidate. Use defaultdict as each candidate may # not contain all the params param_results = defaultdict( lambda: MaskedArray(np.empty(n_candidates), mask=True, dtype=object)) for cand_i, params in enumerate(candidate_params): for name, value in params.items(): param_results["param_%s" % name][cand_i] = value results.update(param_results) return results
def test_masked_array_deprecated(): # TODO: remove in 0.25 with pytest.warns(FutureWarning, match='is deprecated'): MaskedArray()