python如何建立全零數(shù)組
語(yǔ)句格式:
numpy.zeros(shape, dtype=float, order='C')
參數(shù)說(shuō)明:
shape:整型或元素為整型的序列,表示生成的新數(shù)組的shape,如(2,3)或 2。
dtype:生成數(shù)組的數(shù)據(jù)格式,如numpy.int8。默認(rèn)為numpy.float64。
order:{'C', 'F'}可選,是否將多維數(shù)據(jù)存儲(chǔ)為C-或Fortran-contiguous(按行或按列)順序。
返回值:ndarray,一個(gè)指定了shape, dtype, order的零數(shù)組。
示例見(jiàn)下:
第四個(gè)例子看起來(lái)很方便。
Numpy文檔原文:
numpy.zeros
numpy.zeros(shape, dtype=float, order='C')
Return a new array of given shape and type, filled with zeros.
Parameters:
shape : int or sequence of ints
Shape of the new array, e.g., (2, 3) or 2.
dtype : data-type, optional
The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
order : {‘C', ‘F'}, optional
Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory.
Returns:
out : ndarray
Array of zeros with the given shape, dtype, and order.
#指定長(zhǎng)度的一維數(shù)組
>>> np.zeros(5)
array([ 0., 0., 0., 0., 0.])
#指定數(shù)據(jù)類型,指定長(zhǎng)度的一維數(shù)組
>>> np.zeros((5,), dtype=int)
array([0, 0, 0, 0, 0])
#二維數(shù)組
>>> np.zeros((2, 1))
array([[ 0.],
[ 0.]])
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0., 0.],
[ 0., 0.]])
#指定dtype
>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
array([(0, 0), (0, 0)],
dtype=[('x', '<i4'), ('y', '<i4')])
內(nèi)容擴(kuò)展:
python創(chuàng)建數(shù)組的方法
直接定義法:
1.直接定義
matrix=[0,1,2,3]
2.間接定義
matrix=[0 for i in range(4)] print(matrix)
Numpy方法:
Numpy內(nèi)置了從頭開(kāi)始創(chuàng)建數(shù)組的函數(shù):
zeros(shape)將創(chuàng)建一個(gè)用指定形狀用0填充的數(shù)組。默認(rèn)的dtype是float64。
下面是幾種常用的創(chuàng)建方法:
#coding=utf-8 import numpy as np a = np.array([1,2,3,4,5]) print a b = np.zeros((2,3)) print b c = np.arange(10) print c d = np.arange(2,10,dtype=np.float) print d e = np.linspace(1.0,4.0,6) print e f = np.indices((3,3)) print f
到此這篇關(guān)于python如何建立全零數(shù)組的文章就介紹到這了,更多相關(guān)python建立全零數(shù)組的方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)輕松防止屏幕截圖的技巧分享
屏幕截圖是一種常見(jiàn)的用于記錄信息或者監(jiān)控用戶活動(dòng)的方法,為了保護(hù)隱私和數(shù)據(jù)安全,可以通過(guò)使用Python編寫(xiě)一些防護(hù)措施來(lái)防止他人截取我們的屏幕,下面我們就來(lái)學(xué)習(xí)一下有哪些具體操作吧2023-12-12
sublime3之內(nèi)網(wǎng)安裝python插件Anaconda的流程
這篇文章主要介紹了sublime3之內(nèi)網(wǎng)安裝python插件Anaconda的流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(四):域名系統(tǒng)
當(dāng)我們?cè)谏暇W(wǎng)的時(shí)候,通常輸入的是網(wǎng)址,其實(shí)這就是一個(gè)域名,而我們計(jì)算機(jī)網(wǎng)絡(luò)上的計(jì)算機(jī)彼此之間只能用IP地址才能相互識(shí)別2014-06-06
python虛擬環(huán)境virualenv的安裝與使用
virtualenv 是一個(gè)創(chuàng)建隔絕的Python環(huán)境的工具。virtualenv創(chuàng)建一個(gè)包含所有必要的可執(zhí)行文件的文件夾,用來(lái)使用Python工程所需的包。下面這篇文章就給大家介紹了python虛擬環(huán)境virualenv的安裝與使用,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-12-12
Python的子線程和子進(jìn)程是如何手動(dòng)結(jié)束的?
今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著如何手動(dòng)結(jié)束Python的子線程和子進(jìn)程展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
python通過(guò)郵件服務(wù)器端口發(fā)送郵件的方法
這篇文章主要介紹了python通過(guò)郵件服務(wù)器端口發(fā)送郵件的方法,涉及Python發(fā)送郵件的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Win10操作系統(tǒng)中PyTorch虛擬環(huán)境配置+PyCharm配置
本文主要介紹了Win10操作系統(tǒng)中PyTorch虛擬環(huán)境配置+PyCharm配置,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

