python 创建空的numpy数组_Numpy入门教程:01. 数组的创建与属性
背景
什么是 NumPy 呢?
NumPy 這個詞來源于兩個單詞 -- Numerical和Python。其是一個功能強(qiáng)大的 Python 庫,可以幫助程序員輕松地進(jìn)行數(shù)值計算,通常應(yīng)用于以下場景:
執(zhí)行各種數(shù)學(xué)任務(wù),如:數(shù)值積分、微分、內(nèi)插、外推等。因此,當(dāng)涉及到數(shù)學(xué)任務(wù)時,它形成了一種基于 Python 的 MATLAB 的快速替代。
計算機(jī)中的圖像表示為多維數(shù)字?jǐn)?shù)組。NumPy 提供了一些優(yōu)秀的庫函數(shù)來快速處理圖像。例如,鏡像圖像、按特定角度旋轉(zhuǎn)圖像等。
在編寫機(jī)器學(xué)習(xí)算法時,需要對矩陣進(jìn)行各種數(shù)值計算。如:矩陣乘法、求逆、換位、加法等。NumPy 數(shù)組用于存儲訓(xùn)練數(shù)據(jù)和機(jī)器學(xué)習(xí)模型的參數(shù)。
數(shù)據(jù)類型
Python只定義一種整數(shù)類型,一種浮點(diǎn)類型。這在不需要關(guān)心數(shù)據(jù)在計算機(jī)中表示的所有方式的應(yīng)用中是方便的。然而,對于科學(xué)計算,通常需要更多的控制。
數(shù)組的創(chuàng)建
導(dǎo)入 numpy。
import?numpy?numpy 提供的最重要的數(shù)據(jù)結(jié)構(gòu)是ndarray,它是 python 中l(wèi)ist的擴(kuò)展。
利用現(xiàn)有數(shù)據(jù)
None,?copy=【例】
import?numpy?array()和asarray()都可以將結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)化為 ndarray,但是主要區(qū)別就是當(dāng)數(shù)據(jù)源是 ndarray 時,array()仍然會 copy 出一個副本,占用新的內(nèi)存,但不改變 dtype 時 asarray()不會。
None,?order?=?【例】
import?numpy?【例】
import?numpy?【例】
import?numpy?利用 Ones 和 zeros 填充方式
在機(jī)器學(xué)習(xí)任務(wù)中經(jīng)常做的一件事就是初始化參數(shù),需要用常數(shù)值或者隨機(jī)值來創(chuàng)建一個固定大小的矩陣。
numpy.zeros(shape, dtype=float, order='C') 返回給定形狀和類型的新數(shù)組,并用零填充。
numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None)Return an array of zeros with the same shape and type as a given array.
【例】
import?numpy?- numpy.ones(shape, dtype=float, order='C') 返回給定形狀和類型的新數(shù)組,并填充為1。
numpy.ones_like(a, dtype=None, order='K', subok=True, shape=None) Return an array of ones with the same shape and type as a given array.
【例】
import?numpy?- numpy.empty(shape, dtype=float, order='C') 返回一個空數(shù)組,數(shù)組元素為隨機(jī)數(shù)。
numpy.empty_like(prototype, dtype=None, order='K', subok=True, shape=None) Return a new array with the same shape and type as a given array.
【例】
import?numpy?- numpy.full(shape, fill_value, dtype=None, order='C') 返回一個常數(shù)數(shù)組。
numpy.full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None)Return a full array with the same shape and type as a given array.
【例】
import?numpy?- numpy.eye(N, M=None, k=0, dtype=) 返回一個二維數(shù)組,對角線上為1,其他地方為零。
numpy.identity(n, dtype=None)[source]Return the identity array,The identity array is a square array with ones on the main diagonal.
【例】
import?numpy?利用數(shù)值范圍
numpy.arange([start, ]stop, [step, ]dtype=None) 返回給定間隔內(nèi)的均勻間隔的值。
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) 返回指定間隔內(nèi)的等間隔數(shù)字。
numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)返回數(shù)以對數(shù)刻度均勻分布。
numpy.random.random(size=None) 返回一個由[0,1)內(nèi)的隨機(jī)數(shù)組成的數(shù)組。
【例】
import?numpy?【例】
import?numpy?數(shù)組的屬性
在使用 numpy 時,你會想知道數(shù)組的某些信息。很幸運(yùn),在這個包里邊包含了很多便捷的方法,可以給你想要的信息。
numpy.ndarray.ndim用于返回數(shù)組的維數(shù)(軸的個數(shù))也稱為秩,一維數(shù)組的秩為 1,二維數(shù)組的秩為 2,以此類推。
numpy.ndarray.shape表示數(shù)組的維度,返回一個元組,這個元組的長度就是維度的數(shù)目,即 ndim 屬性(秩)。
numpy.ndarray.size數(shù)組中所有元素的總量,相當(dāng)于數(shù)組的shape中所有元素的乘積,例如矩陣的元素總量為行與列的乘積。
numpy.ndarray.dtype ndarray 對象的元素類型。
numpy.ndarray.itemsize以字節(jié)的形式返回數(shù)組中每一個元素的大小。
【例】
import?numpy?在ndarray中所有元素必須是同一類型,否則會自動向下轉(zhuǎn)換,int->float->str。
【例】
import?numpy?當(dāng)前活動
我是 終身學(xué)習(xí)者“老馬”,一個長期踐行“結(jié)伴式學(xué)習(xí)”理念的 中年大叔。
我崇尚分享,渴望成長,于2010年創(chuàng)立了“LSGO軟件技術(shù)團(tuán)隊”,并加入了國內(nèi)著名的開源組織“Datawhale”,也是“Dre@mtech”、“智能機(jī)器人研究中心”和“大數(shù)據(jù)與哲學(xué)社會科學(xué)實(shí)驗(yàn)室”的一員。
愿我們一起學(xué)習(xí),一起進(jìn)步,相互陪伴,共同成長。
后臺回復(fù)「搜搜搜」,隨機(jī)獲取電子資源!
歡迎關(guān)注,請掃描二維碼:
總結(jié)
以上是生活随笔為你收集整理的python 创建空的numpy数组_Numpy入门教程:01. 数组的创建与属性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c 函数多次声明_【C语言】- stat
- 下一篇: python跟谁学_Python 应该怎