2020年2月29日 星期六

Numpy 資料類型 與 Python 的簡單對應

Numpy 資料類型 與 Python 的簡單對應

numpy 的 ndarray ,使用時都是從 python 現有的資料轉換過來的,在這之中numpy 做了一些轉換工作

In [1]:
# python data type
import numpy as np
a1 = np.array([2,3,4])
a1.dtype
Out[1]:
dtype('int32')
In [2]:
b = np.array([1.2, 3.5, 5.1])
b.dtype
Out[2]:
dtype('float64')

當 numpy 建立 ndarray 時,會自動將 python 的型別轉換成 numpy 的資料型別

因為numpy 存在很多 python 沒有的資料類型

In [3]:
a2 = np.array([2,3,4],dtype=np.uint8)
a2.dtype
Out[3]:
dtype('uint8')
In [4]:
a2[[2]]=257
a2
Out[4]:
array([2, 3, 1], dtype=uint8)
In [ ]:
 

Numpy 與 Python string

numpy 預設將 字串轉成 Unicode,顯示出來是大寫的 U

In [5]:
s1=np.array(['a','b','c'])
s1.dtype
Out[5]:
dtype('<U1')
In [6]:
s2 = np.array([1,2,3],dtype=np.dtype('U'))
s2.dtype
Out[6]:
dtype('<U1')

Numpy 與 None

None 是 python 用來代表 Null 的特殊物件,如果按照python 的規則轉成字串的話,會變成 "None" 四個字

在Numpy 中,如果用 dtype 來設定的話

In [7]:
s4=np.array([None,'b','c'],dtype="U")
s4
Out[7]:
array(['None', 'b', 'c'], dtype='<U4')

(Python) objects type

如果對 None 的dtype 沒有設定,那就會轉成 numpy 中的 objects

這個 objects 指的是 python 物件,也就是 python 原生的任意數值都可以

In [8]:
o1=np.array([None,'b','c',3])
o1.dtype
Out[8]:
dtype('O')
In [9]:
o1
Out[9]:
array([None, 'b', 'c', 3], dtype=object)
In [10]:
type(o1[3])
Out[10]:
int
In [11]:
type(o1[2])
Out[11]:
str
In [12]:
type(o1[0])
Out[12]:
NoneType
In [ ]:
 

使用LibreOffice製作代碼瀑布

Matrix_Digital_Rain_in_Excel_1 Matrix Digital Rain in LibreOffice 之前在facebook 看到一個沒有見過的效果,一時興起就研究了一下 如何使用LibreOffie...