第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何找到最小的 numpy dtype 來(lái)存儲(chǔ)最大整數(shù)值?

如何找到最小的 numpy dtype 來(lái)存儲(chǔ)最大整數(shù)值?

滄海一幻覺(jué) 2023-05-16 14:24:49
我需要?jiǎng)?chuàng)建一個(gè)非常大的numpy數(shù)組來(lái)保存非負(fù)整數(shù)值。我事先知道最大的整數(shù)是多少,所以我想盡可能使用最小的數(shù)據(jù)類(lèi)型。到目前為止,我有以下內(nèi)容:>>> import numpy as np>>> def minimal_type(max_val, types=[np.uint8,np.uint16,np.uint32,np.uint64]):    ''' finds the minimal data type needed to correctly store the given max_val        returns None if none of the provided types are sufficient    '''    for t in types:        if max_val <= np.iinfo(t).max:            return t    return None>>> print(minimal_type(42))<class 'numpy.uint8'>>>> print(minimal_type(255))<class 'numpy.uint8'>>>> print(minimal_type(256))<class 'numpy.uint16'>>>> print(minimal_type(4200000000))<class 'numpy.uint32'>>>> 有沒(méi)有numpy內(nèi)置的方法來(lái)實(shí)現(xiàn)這個(gè)功能?
查看完整描述

1 回答

?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

它是numpy.min_scalar_type。文檔中的示例:


>>> np.min_scalar_type(10)

dtype('uint8')

>>> np.min_scalar_type(-260)

dtype('int16')

>>> np.min_scalar_type(3.1)

dtype('float16')

>>> np.min_scalar_type(1e50)

dtype('float64')

>>> np.min_scalar_type(np.arange(4,dtype='f8'))

dtype('float64')

您可能對(duì)浮點(diǎn)數(shù)的行為不感興趣,但無(wú)論如何我都會(huì)為遇到這個(gè)問(wèn)題的其他人包括它,特別是因?yàn)槭褂?float16 并且缺少 float->int 降級(jí)可能會(huì)令人驚訝。


查看完整回答
反對(duì) 回復(fù) 2023-05-16
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)