1. 程式人生 > 其它 >Python資料分析(3)-numpy中nd陣列的建立

Python資料分析(3)-numpy中nd陣列的建立

  1. ndarray的記憶體結構

和其他的庫一樣,每個庫都可能有自己獨特的資料結構,例如OpenCV,numpy庫的多維陣列叫做ndarray( N dimensionality array ),它的記憶體結構如下圖: ndarray的記憶體結構 在這個結構體中有兩個物件,一個是用來描述元素型別的頭部區域,一個是用來儲存資料的資料區域。(事實上大多數資料型別的資料都是這麼儲存的)。

2 ndarray物件的建立

2.1 ndarray多維陣列的建立常規方法 建立一個3*3的陣列並在螢幕列印它以及它的型別和維數:

   import numpy as np

x = np.array([[0,1,2],[3,4,5],[6,7,8]],dtype = np.int32)
print('這個陣列是:',x)
print('這個陣列的資料型別是:',x.dtype)
print('這個陣列的大小:',x.shape)
[/code]

螢幕輸出結果:
![這裡寫圖片描述](https://img-blog.csdn.net/20161122145927579)

我們也可以採用更加直接的辦法:

```code
import numpy as np
x = np.arange(0,9).reshape(3,3)
print('這個陣列是:',x)
print('這個陣列的資料型別是:',x.dtype)
print('這個陣列的大小:',x.shape)
[/code]

螢幕上列印輸出的結果和前一種的結果是一樣的。

2.2 ndarray多維陣列的建立其他方法
除了常規方法,numpy還提供了一些其他的建立方法:
2.2.1 建立全0或者全1的陣列

![這裡寫圖片描述](https://img-blog.csdn.net/20161122150127017)
例如:
![這裡寫圖片描述](https://img-blog.csdn.net/20161122150240674)

```code
import numpy as np

x = np.ones([3,3])
print('這個陣列是:',x)
print('這個陣列的資料型別是:',x.dtype)
print('這個陣列的大小:',x.shape)
[/code]

```code
>>> x = np.arange(6)
>>> x = x.reshape(2,3)
>>> x
array([[0, 1, 2], [3, 4, 5]])
>>> np.ones_like(x)
array([[1, 1, 1], [1, 1, 1]])
>>> y = np.arange(3, dtype=np.float)
>>> y
array([ 0., 1., 2.])
>>> np.ones_like(y)
array([ 1., 1., 1.])
[/code]

當然也可以填充其他的數:

```code
import numpy as np
x = np.full([3,3],np.inf)
print('這個陣列是:',x)
print('這個陣列的資料型別是:',x.dtype)
print('這個陣列的大小:',x.shape)
[/code]

列印輸出: ![這裡寫圖片描述](https://img-blog.csdn.net/20161122150223908)

2.2.2 從已存在的資料中建立陣列
![這裡寫圖片描述](https://img-blog.csdn.net/20161122150510183)

```code
>>> np.array([1, 2, 3])
array([1, 2, 3])

>>> np.array([1, 2, 3.0])
array([ 1., 2., 3.])

>>> np.array([[1, 2], [3, 4]])
array([[1, 2], [3, 4]])

>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])

>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j, 2.+0.j, 3.+0.j])

>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])
[/code]

2.2.3 建立記錄陣列(record array,可能翻譯不準):建立一個陣列,將其他陣列集中在一起管理,並可以命名:
![這裡寫圖片描述](https://img-blog.csdn.net/20161122150534527)
例如:

```code
import numpy as np

x1 = np.arange(0,3)
x2 = np.array(['ff','hh','yy'])
x3 = ([1,2,3])
r = np.core.records.fromarrays([x1,x2,x3],names='a,b,c')
print(r[2])
print(r.a)
[/code]

2.2.4 建立字元陣列
numpy提供了專門的函式建立字元陣列:np.chararray()
首先看看它的引數:
Parameters
| ———-
| shape : tuple
| Shape of the array.
| itemsize : int, optional
| Length of each array element, in number of characters. Default is 1.
| unicode : bool, optional
| Are the array elements of type unicode (True) or string (False).
| Default is False.
| buffer : int, optional
| Memory address of the start of the array data. Default is None,
| in which case a new array is created.
| offset : int, optional
| Fixed stride displacement from the beginning of an axis?
| Default is 0. Needs to be >=0.
| strides : array_like of ints, optional
| Strides for the array (see ndarray.strides for full description).
| Default is None.
| order : {‘C’, ‘F’}, optional
| The order in which the array data is stored in memory: ‘C’ ->
| “row major” order (the default), ‘F’ -> “column major”
| (Fortran) order.

```code
Examples
| --------
| >>> charar = np.chararray((3, 3))
| >>> charar[:] = 'a'
| >>> charar
| chararray([['a', 'a', 'a'],
| ['a', 'a', 'a'],
| ['a', 'a', 'a']],
| dtype='|S1')
|
| >>> charar = np.chararray(charar.shape, itemsize=5)
| >>> charar[:] = 'abc'
| >>> charar
| chararray([['abc', 'abc', 'abc'],
| ['abc', 'abc', 'abc'],
| ['abc', 'abc', 'abc']],
| dtype='|S5')
[/code]

2.2.5 建立數值陣列
![這裡寫圖片描述](https://img-blog.csdn.net/20161122150625644)

```code
import numpy as np

x1 = np.arange(0,3,1)
x2 = np.linspace(0,3,4)
x3 = np.logspace(1,8,3)
x4 = np.mgrid[0:3,0:3]
x5 = np.ogrid[0:3,0:3]
print(x1,x2,x3,x4,x5)
[/code]

2.2.6 建立矩陣
![這裡寫圖片描述](https://img-blog.csdn.net/20161122150654778)

2.2.7 矩陣類(The matrix class)
![這裡寫圖片描述](https://img-blog.csdn.net/20161122150707872)


![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20210608151750993.gif)