Numpy-array-attributes

提供:Dev Guides
移動先:案内検索

NumPy-配列属性

この章では、NumPyのさまざまな配列属性について説明します。

ndarray.shape

この配列属性は、配列の次元で構成されるタプルを返します。 配列のサイズ変更にも使用できます。

例1

import numpy as np
a = np.array([[print a.shape

出力は次のとおりです-

(2, 3)

例2

# this resizes the ndarray
import numpy as np

a = np.array([[a.shape = (3,2)
print a

出力は次のとおりです-

[[Example 3

NumPy also provides a reshape function to resize an array.

[source,prettyprint,notranslate]

numpy as np a = np.array([[b = a.reshape(3,2)print bとしてインポート

The output is as follows −

[source,result,notranslate]

[[ndarray.ndim

この配列属性は、配列の次元数を返します。

例1

# an array of evenly spaced numbers
import numpy as np
a = np.arange(24)
print a

出力は次のとおりです-

[0 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 17 18 19 20 21 22 23]

例2

# this is one dimensional array
import numpy as np
a = np.arange(24)
a.ndim

# now reshape it
b = a.reshape(2,4,3)
print b
# b is having three dimensions

出力は次のとおりです-

[[numpy.itemsize

This array attribute returns the length of each element of array in bytes.

==== Example 1

[source,prettyprint,notranslate]

#配列のdtypeはint8(1バイト)numpyをインポートnp x = np.array([1,2,3,4,5]、dtype = np.int8)print x.itemsize

The output is as follows −

[source,result,notranslate]

1

==== Example 2

[source,prettyprint,notranslate]

#配列のdtypeはfloat32(4バイト)になりましたnpとしてインポートnumpy = np.array([1,2,3,4,5]、dtype = np.float32)print x.itemsize

The output is as follows −

[source,result,notranslate]

4

=== numpy.flags

The ndarray object has the following attributes. Its current values are returned by this function.

[width="100%",cols="50%,50%",options="header",]
|===
|Sr.No. |Attribute & Description
|1 a|
*C_CONTIGUOUS (C)*

The data is in a single, C-style contiguous segment

|2 a|
*F_CONTIGUOUS (F)*

The data is in a single, Fortran-style contiguous segment

|3 a|
*OWNDATA (O)*

The array owns the memory it uses or borrows it from another object

|4 a|
*WRITEABLE (W)*

The data area can be written to. Setting this to False locks the data, making it read-only

|5 a|
*ALIGNED (A)*

The data and all elements are aligned appropriately for the hardware

|6 a|
*UPDATEIFCOPY (U)*

This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array

|===

==== Example

The following example shows the current values of flags.

[source,prettyprint,notranslate]

numpyをnp x = np.array([1,2,3,4,5])としてインポートx.flagsを出力

The output is as follows −

[source,result,notranslate]

C_CONTIGUOUS:True F_CONTIGUOUS:True OWNDATA:True WRITEABLE:True ALIGNED:True UPDATEIFCOPY:False