我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用numpy.round_()。
def test_2d(self): # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) m_1 = np.round_(np.random.rand(5, 5), 0) m_2 = np.round_(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns d = mr_['1', b_1, b_2] self.assertTrue(d.shape == (5, 10)) assert_array_equal(d[:, :5], b_1) assert_array_equal(d[:, 5:], b_2) assert_array_equal(d.mask, np.r_['1', m_1, m_2]) d = mr_[b_1, b_2] self.assertTrue(d.shape == (10, 5)) assert_array_equal(d[:5,:], b_1) assert_array_equal(d[5:,:], b_2) assert_array_equal(d.mask, np.r_[m_1, m_2])
def round_(a, decimals=0, out=None): """ Return a copy of a, rounded to 'decimals' places. When 'decimals' is negative, it specifies the number of positions to the left of the decimal point. The real and imaginary parts of complex numbers are rounded separately. Nothing is done if the array is not of float type and 'decimals' is greater than or equal to 0. Parameters ---------- decimals : int Number of decimals to round to. May be negative. out : array_like Existing array to use for output. If not given, returns a default copy of a. Notes ----- If out is given and does not have a mask attribute, the mask of a is lost! """ if out is None: return np.round_(a, decimals, out) else: np.round_(getdata(a), decimals, out) if hasattr(out, '_mask'): out._mask = getmask(a) return out
def std_score(a): return np.round_(50 + 10 * (a - np.average(a)) / np.std(a))
def test_round(self): param_str = "round(2.45, 1) -> float" test_param = TemplateParameter(parameter_str=param_str, type_converter=self.type_converter) result = test_param.render(df=test_df) self.assertEquals(result, np.round_(2.45, 1))
def _execute(self, value, decimals): return np.round_(value, decimals)
def _calculate_score(solution, prediction, task_type, metric=None): if task_type not in TASK_TYPES: raise NotImplementedError(task_type) solution = np.array(solution, dtype=np.float32) if task_type == MULTICLASS_CLASSIFICATION: # This used to crash on travis-ci; special treatment to find out why # it crashed! solution_binary = np.zeros(prediction.shape) for i in range(solution_binary.shape[0]): label = int(np.round_(solution[i])) solution_binary[i, label] = 1 solution = solution_binary elif task_type == BINARY_CLASSIFICATION: solution = solution.reshape(-1, 1) prediction = prediction[:, 1].reshape(-1, 1) if solution.shape != prediction.shape: raise ValueError("Solution shape %s != prediction shape %s" % (solution.shape, prediction.shape)) if metric is None: score = dict() if task_type in REGRESSION_TASKS: cprediction = sanitize_array(prediction) for metric_ in REGRESSION_METRICS: score[metric_] = regression_metrics.calculate_score(metric_, solution, cprediction) else: csolution, cprediction = normalize_array(solution, prediction) for metric_ in CLASSIFICATION_METRICS: score[metric_] = classification_metrics.calculate_score( metric_, csolution, cprediction, task_type) for metric_ in score: if np.isnan(score[metric_]): score[metric_] = 0 else: if task_type in REGRESSION_TASKS: cprediction = sanitize_array(prediction) score = regression_metrics.calculate_score(metric, solution, cprediction) else: csolution, cprediction = normalize_array(solution, prediction) score = classification_metrics.calculate_score(metric, csolution, cprediction, task=task_type) if np.isnan(score): score = 0 return score