numpy.fromfunction()函數通過在每個坐標上執行一個函數來構造一個數組,因此,所得數組在坐標(x,y,z)處的值為fn(x,y,z)。
Syntax:numpy.fromfunction(function, shape, dtype)
Parameters:
function:[callable] The function is called with N parameters, where N is the rank of shape. Each parameter represents the coordinates of the array varying along a specific axis.
shape:[(N, ) tuple of ints] Shape of the output array, which also determines the shape of the coordinate arrays passed to function.
dtype:[data-type, optional] Data-type of the coordinate arrays passed to function.
Return:The output array.
代碼1:
Python3
# Python program explaining
# numpy.fromfunction() function
# importing numpy as geek
import numpy as geek
gfg = geek.fromfunction(lambda i, j:i * j, (4, 4), dtype = float)
print(gfg)
輸出:
[[0. 0. 0. 0.]
[0. 1. 2. 3.]
[0. 2. 4. 6.]
[0. 3. 6. 9.]]
代碼2:
Python3
# Python program explaining
# numpy.fromfunction() function
# importing numpy as geek
import numpy as geek
gfg = geek.fromfunction(lambda i, j:i == j, (3, 3), dtype = int)
print(gfg)
輸出:
[[ True False False]
[False True False]
[False False True]]
相關用法
- Python numpy.cov()用法及代碼示例
- Python numpy.ma.where()用法及代碼示例
- Python numpy.ix_()用法及代碼示例
- Python numpy.mintypecode()用法及代碼示例
- Python numpy.sctype2char()用法及代碼示例
- Python numpy.promote_types()用法及代碼示例
- Python numpy.matrix.A()用法及代碼示例
- Python numpy.maximum_sctype()用法及代碼示例
- Python numpy.correlate()用法及代碼示例
- Python numpy.nanstd()用法及代碼示例
- Python numpy.real()用法及代碼示例
- Python numpy.roots()用法及代碼示例
- Python numpy.broadcast_to()用法及代碼示例
- Python numpy.iinfo()用法及代碼示例
- Python Numpy MaskedArray.all()用法及代碼示例
- Python numpy.char.add()用法及代碼示例
- Python Numpy recarray.put()用法及代碼示例
- Python numpy.find_common_type()用法及代碼示例
注:本文由純淨天空篩選整理自sanjoy_62大神的英文原創作品 numpy.fromfunction() function – Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。