python如何建立全零數(shù)組
語句格式:
numpy.zeros(shape, dtype=float, order='C')
參數(shù)說明:
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ù)組。
示例見下:
第四個(gè)例子看起來很方便。
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.
#指定長度的一維數(shù)組 >>> np.zeros(5) array([ 0., 0., 0., 0., 0.]) #指定數(shù)據(jù)類型,指定長度的一維數(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)置了從頭開始創(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)
本文主要介紹了Python列表中多元素刪除(移除)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03python3 簡單實(shí)現(xiàn)組合設(shè)計(jì)模式
這篇文章主要介紹了python3 簡單實(shí)現(xiàn)組合設(shè)計(jì)模式的方法,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07pandas 時(shí)間格式轉(zhuǎn)換的實(shí)現(xiàn)
這篇文章主要介紹了pandas 時(shí)間格式轉(zhuǎn)換的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07利用PyQt中的QThread類實(shí)現(xiàn)多線程
本文主要給大家分享的是python實(shí)現(xiàn)多線程及線程間通信的簡單方法,非常的實(shí)用,有需要的小伙伴可以參考下2020-02-02詳解Python中Pandas read_csv參數(shù)使用
在使用 Pandas 進(jìn)行數(shù)據(jù)分析和處理時(shí),read_csv 是一個(gè)非常常用的函數(shù),本文將詳細(xì)介紹 read_csv 函數(shù)的各個(gè)參數(shù)及其用法,希望對(duì)大家有所幫助2022-10-10