python类number()的实例源码

basic.py 文件源码 项目:Theano-Deep-learning 作者: GeekLiB 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def infer_shape(self, node, shapes):

        if isinstance(node.inputs[1], TensorVariable):
            # We have padded node.inputs[0] to the right number of
            # dimensions for the output
            l = []
            for sh1, sh2, b1 in zip(shapes[0],
                                    shapes[1][1:],
                                    node.inputs[0].broadcastable):
                if b1:
                    l.append(sh2)
                else:
                    l.append(sh1)
            return [tuple(l)]
        else:
            import theano.typed_list
            assert isinstance(node.inputs[1],
                              theano.typed_list.TypedListVariable)
            raise ShapeError("Case not implemented")
            shape = shapes[0]
            for i in xrange(len(shapes[0]) - 1):
                shape[i] = shapes[1][i]
            return [(shape)]
opt.py 文件源码 项目:Theano-Deep-learning 作者: GeekLiB 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def check_for_x_over_absX(numerators, denominators):
    """Convert x/abs(x) into sign(x). """
    # TODO: this function should dig/search through dimshuffles
    # This won't catch a dimshuffled absolute value
    for den in list(denominators):
        if (den.owner and den.owner.op == T.abs_ and
                den.owner.inputs[0] in numerators):
            if den.owner.inputs[0].type.dtype.startswith('complex'):
                # TODO: Make an Op that projects a complex number to
                #      have unit length but projects 0 to 0.  That
                #      would be a weird Op, but consistent with the
                #      special case below.  I heard there's some
                #      convention in Matlab that is similar to
                #      this... but not sure.
                pass
            else:
                denominators.remove(den)
                numerators.remove(den.owner.inputs[0])
                numerators.append(T.sgn(den.owner.inputs[0]))
    return numerators, denominators
test_postprocessing.py 文件源码 项目:adel 作者: openalea-incubator 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_axis_statistics():
    adel_output_df = pd.read_csv(INPUTS_DIRPATH/ADEL_OUTPUT_FILENAME)
    adel_output_df['species'] = '0'
    axis_statistics_df, intermediate_df = pp.axis_statistics(adel_output_df, domain_area=1)
    axis_statistics_df.drop('species', 1, inplace=True)
    intermediate_df.drop('species', 1, inplace=True)
    axis_statistics_df.to_csv(OUTPUTS_DIRPATH/'actual_axis_statistics.csv', index=False, na_rep='NA')
    intermediate_df.to_csv(OUTPUTS_DIRPATH/'actual_intermediate.csv', index=False, na_rep='NA')

    desired_axis_statistics_df = pd.read_csv(OUTPUTS_DIRPATH/'desired_axis_statistics.csv')
    desired_axis_statistics_df.drop('has_ear', 1, inplace=True)
    axis_statistics_df = axis_statistics_df.select_dtypes(include=[np.number])
    desired_axis_statistics_df = desired_axis_statistics_df.select_dtypes(include=[np.number])
    np.testing.assert_allclose(axis_statistics_df.values, desired_axis_statistics_df.values, RELATIVE_TOLERANCE, ABSOLUTE_TOLERANCE)

    desired_intermediate_df = pd.read_csv(OUTPUTS_DIRPATH/'desired_intermediate.csv')
    desired_intermediate_df.drop('has_ear', 1, inplace=True)
    intermediate_df = intermediate_df.select_dtypes(include=[np.number])
    desired_intermediate_df = desired_intermediate_df.select_dtypes(include=[np.number])
    np.testing.assert_allclose(intermediate_df.values, desired_intermediate_df.values, RELATIVE_TOLERANCE, ABSOLUTE_TOLERANCE)
views.py 文件源码 项目:kanjitester 作者: larsyencken 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def _chart_csv_response(chart, name, data_set_name=None):
    "Respond with the data from a chart."
    if not data_set_name:
        data_set_name = name.split('_')[2]
    if not settings.DEBUG:
        response = HttpResponse(mimetype='text/csv')
        response['Content-Disposition'] = \
                'attachment; filename=%s.csv' % name
    else:
        response = HttpResponse(mimetype='text/html')
    writer = csv.writer(response)
    for row in chart.get_data(data_set_name):
        if isinstance(row, (float, int, numpy.number)):
            writer.writerow([row])
        else:
            writer.writerow(row)

    return response
test_regression.py 文件源码 项目:Alfred 作者: jkachhadia 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_ticket_1539(self):
        dtypes = [x for x in np.typeDict.values()
                  if (issubclass(x, np.number)
                      and not issubclass(x, np.timedelta64))]
        a = np.array([], dtypes[0])
        failures = []
        # ignore complex warnings
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', np.ComplexWarning)
            for x in dtypes:
                b = a.astype(x)
                for y in dtypes:
                    c = a.astype(y)
                    try:
                        np.dot(b, c)
                    except TypeError:
                        failures.append((x, y))
        if failures:
            raise AssertionError("Failures: %r" % failures)
core.py 文件源码 项目:Alfred 作者: jkachhadia 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
def round(self, decimals=0, out=None):
        """
        Return an array rounded a to the given number of decimals.

        Refer to `numpy.around` for full documentation.

        See Also
        --------
        numpy.around : equivalent function

        """
        result = self._data.round(decimals=decimals, out=out).view(type(self))
        if result.ndim > 0:
            result._mask = self._mask
            result._update_from(self)
        elif self._mask:
            # Return masked when the scalar is masked
            result = masked
        # No explicit output: we're done
        if out is None:
            return result
        if isinstance(out, MaskedArray):
            out.__setmask__(self._mask)
        return out
wordgen_samples.py 文件源码 项目:saapy 作者: ashapochka 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def prefer_alignment(value_type):
    if np.issubdtype(value_type, np.number):
        return ALIGN.RIGHT
    else:
        return ALIGN.LEFT
rigid_transformations.py 文件源码 项目:autolab_core 作者: BerkeleyAutomation 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _check_valid_rotation(self, rotation):
        """Checks that the given rotation matrix is valid.
        """
        if not isinstance(rotation, np.ndarray) or not np.issubdtype(rotation.dtype, np.number):
            raise ValueError('Rotation must be specified as numeric numpy array')

        if len(rotation.shape) != 2 or rotation.shape[0] != 3 or rotation.shape[1] != 3:
            raise ValueError('Rotation must be specified as a 3x3 ndarray')

        if np.abs(np.linalg.det(rotation) - 1.0) > 1e-3:
            raise ValueError('Illegal rotation. Must have determinant == 1.0')
rigid_transformations.py 文件源码 项目:autolab_core 作者: BerkeleyAutomation 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def _check_valid_translation(self, translation):
        """Checks that the translation vector is valid.
        """
        if not isinstance(translation, np.ndarray) or not np.issubdtype(translation.dtype, np.number):
            raise ValueError('Translation must be specified as numeric numpy array')

        t = translation.squeeze()
        if len(t.shape) != 1 or t.shape[0] != 3:
            raise ValueError('Translation must be specified as a 3-vector, 3x1 ndarray, or 1x3 ndarray')
keras_nn.py 文件源码 项目:NN_with_Keras 作者: IBMPredictiveAnalytics 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def check(self,df):
        if self.objective == "regression" or self.objective == "classification":
            if self.input_type == "text":
                if not self.text_field:
                    raise Exception("Please specify a text field")
            else:
                if not self.target:
                    raise Exception("Please specify a target field")
                if len(self.fields) == 0:
                    raise Exception("Please specify at least one predictor field")
                numericTarget = False
                if df[self.target].dtype == np.number:
                    numericTarget = True
                if self.objective == "regression" and not numericTarget:
                    raise Exception("Please use a numeric target field for the regression objective")
                if self.objective == "classification" and numericTarget:
                    raise Exception("Please use a string target field for the classification objective")

        elif self.objective == "time_series":
            if not self.target:
                raise Exception("Please specify a target field")
            if not self.order_field:
                raise Exception("Please specify an index field")
            if df[self.target].dtype != np.number:
                raise Exception("Please use a numeric target field for the time series objective")
        else:
            if len(self.fields) == 0:
                raise Exception("Please specify at least one predictor field")
value_counter.py 文件源码 项目:Eskapade 作者: KaveIO 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def process_columns(self, df):
        """Process columns before histogram filling

        Specifically, convert timestamp columns to integers
        and numeric variables are converted to indices

        :param df: input (pandas) data frame
        :returns: output (pandas) data frame with converted timestamp columns
        :rtype: pandas DataFrame
        """

        # timestamp variables are converted to ns here
        # make temp df for value counting (used below)

        idf = df[self.str_cols].copy(deep=False)
        for col in self.dt_cols:
            self.log().debug('Converting column "%s" of type "%s" to nanosec', col, self.var_dtype[col])
            idf[col] = df[col].apply(hf.to_ns)

        # numerical variables are converted to indices here
        for col in self.num_cols + self.dt_cols:
            self.log().debug('Converting column "%s" of type "%s" to index', col, self.var_dtype[col])
            # find column specific bin_specs. if not found, use dict of default
            # values.
            dt = df[col].dtype
            is_number = isinstance(dt.type(), np.number)
            is_timestamp = isinstance(dt.type(), np.datetime64)
            sf = idf if is_timestamp else df
            bin_specs = self.bin_specs.get(col, self._unit_bin_specs if is_number else self._unit_timestamp_specs)
            idf[col] = sf[col].apply(hf.value_to_bin_index, **bin_specs)

        return idf
_visualizer.py 文件源码 项目:q2-diversity 作者: qiime2 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def bioenv(output_dir: str, distance_matrix: skbio.DistanceMatrix,
           metadata: qiime2.Metadata) -> None:
    # convert metadata to numeric values where applicable, drop the non-numeric
    # values, and then drop samples that contain NaNs
    df = metadata.to_dataframe()
    df = df.apply(lambda x: pd.to_numeric(x, errors='ignore'))

    # filter categorical columns
    pre_filtered_cols = set(df.columns)
    df = df.select_dtypes([numpy.number]).dropna()
    filtered_categorical_cols = pre_filtered_cols - set(df.columns)

    # filter 0 variance numerical columns
    pre_filtered_cols = set(df.columns)
    df = df.loc[:, df.var() != 0]
    filtered_zero_variance_cols = pre_filtered_cols - set(df.columns)

    # filter the distance matrix to exclude samples that were dropped from
    # the metadata, and keep track of how many samples survived the filtering
    # so that information can be presented to the user.
    initial_dm_length = distance_matrix.shape[0]
    distance_matrix = distance_matrix.filter(df.index, strict=False)
    filtered_dm_length = distance_matrix.shape[0]

    result = skbio.stats.distance.bioenv(distance_matrix, df)
    result = q2templates.df_to_html(result)

    index = os.path.join(TEMPLATES, 'bioenv_assets', 'index.html')
    q2templates.render(index, output_dir, context={
        'initial_dm_length': initial_dm_length,
        'filtered_dm_length': filtered_dm_length,
        'filtered_categorical_cols': ', '.join(filtered_categorical_cols),
        'filtered_zero_variance_cols': ', '.join(filtered_zero_variance_cols),
        'result': result})
util.py 文件源码 项目:allennlp 作者: allenai 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def sanitize(x: Any) -> Any:  # pylint: disable=invalid-name,too-many-return-statements
    """
    Sanitize turns PyTorch and Numpy types into basic Python types so they
    can be serialized into JSON.
    """
    if isinstance(x, (str, float, int, bool)):
        # x is already serializable
        return x
    elif isinstance(x, torch.autograd.Variable):
        return sanitize(x.data)
    elif isinstance(x, torch._TensorBase):  # pylint: disable=protected-access
        # tensor needs to be converted to a list (and moved to cpu if necessary)
        return x.cpu().tolist()
    elif isinstance(x, numpy.ndarray):
        # array needs to be converted to a list
        return x.tolist()
    elif isinstance(x, numpy.number):
        # NumPy numbers need to be converted to Python numbers
        return x.item()
    elif isinstance(x, dict):
        # Dicts need their values sanitized
        return {key: sanitize(value) for key, value in x.items()}
    elif isinstance(x, (list, tuple)):
        # Lists and Tuples need their values sanitized
        return [sanitize(x_i) for x_i in x]
    else:
        raise ValueError("cannot sanitize {} of type {}".format(x, type(x)))
test_regression.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_array_side_effect(self):
        # The second use of itemsize was throwing an exception because in
        # ctors.c, discover_itemsize was calling PyObject_Length without
        # checking the return code.  This failed to get the length of the
        # number 2, and the exception hung around until something checked
        # PyErr_Occurred() and returned an error.
        assert_equal(np.dtype('S10').itemsize, 10)
        np.array([['abc', 2], ['long   ', '0123456789']], dtype=np.string_)
        assert_equal(np.dtype('S10').itemsize, 10)
test_item_selection.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_simple(self):
        a = [[1, 2], [3, 4]]
        a_str = [[b'1', b'2'], [b'3', b'4']]
        modes = ['raise', 'wrap', 'clip']
        indices = [-1, 4]
        index_arrays = [np.empty(0, dtype=np.intp),
                        np.empty(tuple(), dtype=np.intp),
                        np.empty((1, 1), dtype=np.intp)]
        real_indices = {'raise': {-1: 1, 4: IndexError},
                        'wrap': {-1: 1, 4: 0},
                        'clip': {-1: 0, 4: 1}}
        # Currently all types but object, use the same function generation.
        # So it should not be necessary to test all. However test also a non
        # refcounted struct on top of object.
        types = np.int, np.object, np.dtype([('', 'i', 2)])
        for t in types:
            # ta works, even if the array may be odd if buffer interface is used
            ta = np.array(a if np.issubdtype(t, np.number) else a_str, dtype=t)
            tresult = list(ta.T.copy())
            for index_array in index_arrays:
                if index_array.size != 0:
                    tresult[0].shape = (2,) + index_array.shape
                    tresult[1].shape = (2,) + index_array.shape
                for mode in modes:
                    for index in indices:
                        real_index = real_indices[mode][index]
                        if real_index is IndexError and index_array.size != 0:
                            index_array.put(0, index)
                            assert_raises(IndexError, ta.take, index_array,
                                          mode=mode, axis=1)
                        elif index_array.size != 0:
                            index_array.put(0, index)
                            res = ta.take(index_array, mode=mode, axis=1)
                            assert_array_equal(res, tresult[real_index])
                        else:
                            res = ta.take(index_array, mode=mode, axis=1)
                            assert_(res.shape == (2,) + index_array.shape)
core.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def _delegate_binop(self, other):
        # This emulates the logic in
        # multiarray/number.c:PyArray_GenericBinaryFunction
        if (not isinstance(other, np.ndarray)
                and not hasattr(other, "__numpy_ufunc__")):
            other_priority = getattr(other, "__array_priority__", -1000000)
            if self.__array_priority__ < other_priority:
                return True
        return False
dataframe_client.py 文件源码 项目:Dshield 作者: ywjt 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _convert_array(self, array):
        try:
            global np
            import numpy as np
        except ImportError as ex:
            raise ImportError('DataFrameClient requires Numpy, '
                              '"{ex}" problem importing'.format(ex=str(ex)))
        if self.ignore_nan:
            number_types = (int, float, np.number)
            condition = (all(isinstance(el, number_types) for el in array) and
                         np.isnan(array))
            return list(np.where(condition, None, array))
        else:
            return list(array)
util.py 文件源码 项目:singlecell-dash 作者: czbiohub 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def maybe_format(item):
    """Pretty-format a string, integer, float, or percent

    Parameters
    ----------
    item : pandas.Series
        A single-item series containing a .name attribute and a value in the
        first (0th) index
    """
    value = item[0]
    if pd.isnull(value):
        return 'N/A'
    elif isinstance(value, str):
        return value
    elif 'percent' in item.name.lower():
        return '{:.2f}%'.format(value)
    elif isinstance(value, pd.Timestamp):
        return str(np.datetime64(value, 'D'))
    elif (isinstance(value, float)  # this must go before ints!
          or np.issubdtype(value, np.number)):
        if value >= 1e3:
            return locale.format("%d", int(value), grouping=True)
        else:
            return locale.format("%.3g", value, grouping=True)
    elif (isinstance(value, int)
          or np.issubdtype(value, np.integer)):
        return locale.format("%d", value, grouping=True)
    else:
        raise TypeError
mca.py 文件源码 项目:prince 作者: MaxHalford 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def q(self):
        """The number of columns in the initial dataframe

        As opposed to `p` which is the number of columns in the indicator matrix of the initial
        dataframe.
        """
        return self.initial_dataframe.shape[1]
pca.py 文件源码 项目:prince 作者: MaxHalford 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def n_supplementary_rows(self):
        """The number of supplementary rows."""
        return self.supplementary_rows.shape[0]


问题


面经


文章

微信
公众号

扫码关注公众号