最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Numpy基本操作

2022-08-15 09:57 作者:限量版范兒  | 我要投稿

?Numpy介紹

  Numpy(Numerical Python)是一個(gè)開源的Python科學(xué)計(jì)算庫(kù),用于快速處理任意維度的數(shù)組。

  Numpy支持常見的數(shù)組和矩陣操作。對(duì)于同樣的數(shù)值計(jì)算任務(wù),使用Numpy比直接使用Python要簡(jiǎn)潔的多。

  Numpy使用ndarray對(duì)象來(lái)處理多維數(shù)組,該對(duì)象是一個(gè)快速而靈活的大數(shù)據(jù)容器。

?

性能

  ndarray支持并行化運(yùn)算(向量化運(yùn)算)

  ndarray在存儲(chǔ)數(shù)據(jù)的時(shí)候,數(shù)據(jù)與數(shù)據(jù)的地址都是連續(xù)的,這樣就給使得批量操作數(shù)組元素時(shí)速度更快。

  

數(shù)據(jù)集合介紹

  NumPy提供了一個(gè)N維數(shù)組類型ndarray,它描述了相同類型的“items”的集合。

代碼展示:

這是一個(gè)二維數(shù)組

import numpy as np# 創(chuàng)建ndarrayscore = np.array([[80, 89, 86, 67, 79], [78, 97, 89, 67, 81], [90, 94, 78, 67, 74], [91, 91, 90, 67, 69], [76, 87, 75, 67, 86], [70, 79, 84, 67, 84], [94, 92, 93, 67, 64], [86, 85, 83, 67, 80]])

?

ndarray的屬性

數(shù)組屬性反映了數(shù)組本身固有的信息。

屬性名字 ? ?屬性解釋 ndarray.shape ? ?數(shù)組維度的元組 ndarray.ndim ? ?數(shù)組維數(shù) ndarray.size ? ?數(shù)組中的元素?cái)?shù)量 ndarray.itemsize ? ?一個(gè)數(shù)組元素的長(zhǎng)度(字節(jié)) ndarray.dtype ? ?數(shù)組元素的類型

?

ndarray的類型

>>> type(score.dtype)<type 'numpy.dtype'>名稱 ? ?描述 ? ?簡(jiǎn)寫 np.bool ? ?用一個(gè)字節(jié)存儲(chǔ)的布爾類型(True或False) ? ?'b'np.int8 ? ?一個(gè)字節(jié)大小,-128 至 127 ? ?'i'np.int16 ? ?整數(shù),-32768 至 32767 ? ?'i2'np.int32 ? ?整數(shù),-2 31 至 2 32 -1 ? ?'i4'np.int64 ? ?整數(shù),-2 63 至 2 63 - 1 ? ?'i8'np.uint8 ? ?無(wú)符號(hào)整數(shù),0 至 255 ? ?'u'np.uint16 ? ?無(wú)符號(hào)整數(shù),0 至 65535 ? ?'u2'np.uint32 ? ?無(wú)符號(hào)整數(shù),0 至 2 ** 32 - 1 ? ?'u4'np.uint64 ? ?無(wú)符號(hào)整數(shù),0 至 2 ** 64 - 1 ? ?'u8'np.float16 ? ?半精度浮點(diǎn)數(shù):16位,正負(fù)號(hào)1位,指數(shù)5位,精度10位 ? ?'f2'np.float32 ? ?單精度浮點(diǎn)數(shù):32位,正負(fù)號(hào)1位,指數(shù)8位,精度23位 ? ?'f4'np.float64 ? ?雙精度浮點(diǎn)數(shù):64位,正負(fù)號(hào)1位,指數(shù)11位,精度52位 ? ?'f8'np.complex64 ? ?復(fù)數(shù),分別用兩個(gè)32位浮點(diǎn)數(shù)表示實(shí)部和虛部 ? ?'c8'np.complex128 ? ?復(fù)數(shù),分別用兩個(gè)64位浮點(diǎn)數(shù)表示實(shí)部和虛部 ? ?'c16'np.object_ ? ?python對(duì)象 ? ?'O'np.string_ ? ?字符串 ? ?'S'np.unicode_ ? ?unicode類型 ? ?'U'

創(chuàng)建數(shù)組的時(shí)候指定類型

>>> a = np.array([[1, 2, 3],[4, 5, 6]], dtype=np.float32)>>> a.dtype dtype('float32')

生成0和1的數(shù)組

  • 方法

  • empty(shape[, dtype, order]) empty_like(a[, dtype, order, subok])
    eye(N[, M, k, dtype, order])

  • identity(n[, dtype])

  • ones(shape[, dtype, order])

  • ones_like(a[, dtype, order, subok])

  • zeros(shape[, dtype, order])?zeros_like(a[, dtype, order, subok])
    full(shape, fill_value[, dtype, order])

  • full_like(a, fill_value[, dtype, order, subok])


示例:
>>> zero = np.zeros([3, 4]) array([[ 0., ?0., ?0., ?0.], ? ? ? [ 0., ?0., ?0., ?0.], ? ? ? [ 0., ?0., ?0., ?0.]])

從現(xiàn)有數(shù)組生成

方法介紹
  array(object[, dtype, copy, order, subok, ndmin])   asarray(a[, dtype, order])   asanyarray(a[, dtype, order]) ascontiguousarray(a[, dtype])   asmatrix(data[, dtype])   copy(a[, order])

示例:
a = np.array([[1,2,3],[4,5,6]])# 從現(xiàn)有的數(shù)組當(dāng)中創(chuàng)建 a1 = np.array(a)# 相當(dāng)于索引的形式,并沒(méi)有真正的創(chuàng)建一個(gè)新的 a2 = np.asarray(a)

生成固定范圍的數(shù)組

方法介紹  np.linspace (start, stop, num, endpoint, retstep, dtype)

  •   start 序列的起始值

  •   stop 序列的終止值,

  •   如果endpoint為true,該值包含于序列中

  •   num 要生成的等間隔樣例數(shù)量,默認(rèn)為50

  •   endpoint 序列中是否包含stop值,默認(rèn)為ture

  •   retstep 如果為true,返回樣例,

  •   以及連續(xù)數(shù)字之間的步長(zhǎng)

  •   dtype 輸出ndarray的數(shù)據(jù)類型

    示例:

# 生成等間隔的數(shù)組 np.linspace(0, 100, 10)

其它的還有

  • numpy.arange(start,stop, step, dtype)

  •   numpy.logspace(start,stop, num, endpoint, base, dtype)

示例:

np.arange(10, 50, 2)


生成隨機(jī)數(shù)組?np.random模塊

  • 均勻分布

    • np.random.rand(d0,?d1,?...,?dn)

      返回[0.0,1.0)內(nèi)的一組均勻分布的數(shù)。

    • np.random.uniform(low=0.0,?high=1.0,?size=None)

      功能:從一個(gè)均勻分布[low,high)中隨機(jī)采樣,注意定義域是左閉右開,即包含low,不包含high.

      參數(shù)介紹:

      low: 采樣下界,float類型,默認(rèn)值為0;

      high: 采樣上界,float類型,默認(rèn)值為1;

      size: 輸出樣本數(shù)目,為int或元組(tuple)類型,例如,size=(m,n,k), 則輸出mnk個(gè)樣本,缺省時(shí)輸出1個(gè)值。

      返回值:ndarray類型,其形狀和參數(shù)size中描述一致。

    • np.random.randint(low,?high=None,?size=None,?dtype='l')

      從一個(gè)均勻分布中隨機(jī)采樣,生成一個(gè)整數(shù)或N維整數(shù)數(shù)組,取數(shù)范圍:若high不為None時(shí),取[low,high)之間隨機(jī)整數(shù),否則取值[0,low)之間隨機(jī)整數(shù)。

      

  補(bǔ)充:均勻分布

    均勻分布(Uniform Distribution)是概率統(tǒng)計(jì)中的重要分布之一。顧名思義,均勻,表示可能性相等的含義。均勻分布在自然情況下極為罕見,而人工栽培的有一定株行距的植物群落即是均勻分布。

?

示例:

# 生成均勻分布的隨機(jī)數(shù) x1 = np.random.uniform(-1, 1, 100000000)

?

正態(tài)分布

  • np.random.randn(d0, d1, …, dn)

    功能:從標(biāo)準(zhǔn)正態(tài)分布中返回一個(gè)或多個(gè)樣本值

  • np.random.normal(loc=0.0,?scale=1.0,?size=None)

    loc:float

    此概率分布的均值(對(duì)應(yīng)著整個(gè)分布的中心centre)

    scale:float

    此概率分布的標(biāo)準(zhǔn)差(對(duì)應(yīng)于分布的寬度,scale越大越矮胖,scale越小,越瘦高)

    size:int or tuple of ints

    輸出的shape,默認(rèn)為None,只輸出一個(gè)值

  • np.random.standard_normal(size=None)

    返回指定形狀的標(biāo)準(zhǔn)正態(tài)分布的數(shù)組。

示例:

# 生成正態(tài)分布的隨機(jī)數(shù) x2 = np.random.normal(1.75, 1, 100000000)

數(shù)組的索引、切片

# 二維的數(shù)組,兩個(gè)維度 stock_change[0, 0:3]

形狀修改

ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape.

ndarray.T 數(shù)組的轉(zhuǎn)置

將數(shù)組的行、列進(jìn)行互換 stock_change.shape (8, 10) stock_change.T.shape (10, 8)


ndarray.resize(new_shape[, refcheck]) Change shape and size of array in-place.

類型修改

ndarray.astype(type)

ndarray.tostring([order])或者ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array.

數(shù)組的去重

temp = np.array([[1, 2, 3, 4],[3, 4, 5, 6]])>>> np.unique(temp) array([1, 2, 3, 4, 5, 6])

邏輯運(yùn)算

鏈接:https://www.dianjilingqu.com/484704.html

Numpy基本操作的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
拉萨市| 夏邑县| 张家口市| 洛南县| 来安县| 若尔盖县| 乌拉特中旗| 和平区| 博兴县| 灌云县| 十堰市| 县级市| 鹤庆县| 静海县| 固安县| 浑源县| 兴安盟| 巴彦县| 海盐县| 丹阳市| 林甸县| 图们市| 普兰店市| 扎鲁特旗| 汉沽区| 海城市| 五河县| 闻喜县| 雅安市| 安溪县| 枣强县| 左贡县| 息烽县| 招远市| 隆尧县| 从江县| 进贤县| 尚义县| 开阳县| 红安县| 天津市|