python學習-numpy.array
2019-12-29 學習numpy中的array函式用法
一.在命令列中輸入help(numpy.array)得到以下輸出
help(numpy.array)
Help on built-in function array in module numpy.core.multiarray:
array(…)
array(object, dtype=None, copy=True, order=‘K’, subok=False, ndmin=0)
Create an array.
Parameters
----------
-
object : array_like
An array, any object exposing the array interface陣列介面, an object whose array -
dtype : data-type, optional(可選可不選的)
The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence如果不給定data-type,則data-type將會被預設為是能夠在序列sequence中容納該object的最小型別. This argument can only be used to ‘upcast’ the array但是這隻能處理向上陣列的情況. For downcasting, use the .astype(t) method對於downcasting的型別,則使用.astype(t) 方式來處理. -
copy : bool, optional
If true (default), then the object is copied. Otherwise, a copy will only be made if array returns a copy, if obj is a nested sequence巢狀序列,or if a copy is needed to satisfy any of the other requirements
(dtype
,order
, etc.). -
order : {‘K’, ‘A’, ‘C’, ‘F’}, optional
Specify the memory layout儲存配置 of the array.
4.1 If object is not
4.2 If object is an array the following holds.
When
copy=False
and a copy is made for other reasons, the result is the same as ifcopy=True
, with some exceptions forA
, see theNotes section. The default order is ‘K’. -
subok : bool, optional
If True, then sub-classes子類 will be passed-through, otherwise the returned array will be forced to be a base-class array預設為基礎型別陣列 (default). -
ndmin : int, optional
Specifies the minimum number of dimensions that the resulting array should have. ndmin引數是用於定義結果陣列應該擁有的最低維數。 Ones will be pre-pended 預先考慮to the shape as needed to meet this requirement.
二.返回結果
Returns
-------
out : ndarray
An array object satisfying the specified requirements.
See Also
--------
empty_like : Return an empty array with shape and type of input.
輸出一個和input的shape和type一致的空array
ones_like : Return an array of ones with shape and type of input.
按input的shape和type輸出一個全1陣列
zeros_like : Return an array of zeros with shape and type of input.
按input的shape、type輸出一個全零的陣列
full_like : Return a new array with shape of input filled with value.
按input的shape和type輸出一個new array
empty : Return a new uninitialized array.
返回一個未初始化的陣列
ones : Return a new array setting values to one.
返回一個new array,setting values to one
zeros : Return a new array setting values to zero.
返回一個已經設定了值為0的新陣列
full : Return a new array of given shape filled with value.
返回一個給定shape的新陣列,filled with value
Notes:
When order is 'A' and `object` is an array in neither 'C' nor 'F' order,
and a copy is forced by a change in dtype, then the order of the result is
not necessarily 'C' as expected. This is likely a bug.
三. Examples
--------
>>> np.array([1, 2, 3])
array([1, 2, 3])
Upcasting:
>>> np.array([1, 2, 3.0])
array([ 1., 2., 3.])
More than one dimension:
>>> np.array([[1, 2], [3, 4]]) #必須使用[ ]將不同行括起來,否則會報錯!
array([[1, 2],
[3, 4]])
Minimum dimensions 2:
>>> np.array([1, 2, 3], ndmin=2) #ndmin用於指定需要的最小維數
array([[1, 2, 3]])
>>>np.array([[1,2,3],[4,5,6]],dtype=complex)
array([[1.+0.j, 2.+0.j, 3.+0.j],
[4.+0.j, 5.+0.j, 6.+0.j]])
Type provided:
>>> np.array([1, 2, 3], dtype=complex) #python中complex()用於建立一個複數or將一個字串或數轉化為複數。
array([ 1.+0.j, 2.+0.j, 3.+0.j])
Data-type consisting of more than one element:
>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])
Creating an array from sub-classes:
>>> np.array(np.mat('1 2; 3 4'))
array([[1, 2],
[3, 4]])
>>> np.array(np.mat('1 2; 3 4'), subok=True)
#subok為TRUE則返回子類sub-class值,為false則返回一個base-class array
matrix([[1, 2],
[3, 4]])