06 ,dtype : 元素類型,類型檢查 dtype ,類型轉換 astype

1 ,numpy 的數據類型 : 比 python 多

  1. numpy 支持的數據類型 : 比 Python 內置的類型要多很多
  2. 基本上 :對應 C 語言的數據類型
  3. 部分類型 :對應 Python 內置類型。

2 ,常見的數據類型 :

  1. 基本數據類型 :
2.	bool_    存儲爲一個字節的布爾值(真或假)
3.	int_     默認整數,相當於 C 的long,通常爲int32或int64
4.	intc     相當於 C 的int,通常爲int32或int64
5.	intp     用於索引的整數,相當於 C 的size_t,通常爲int32或int64
6.	int8     字節(-128 ~ 127)
7.	int16    16位整數(-32768 ~ 32767)
8.	int32    32位整數(-2147483648 ~ 2147483647)
9.	int64    64位整數(-9223372036854775808 ~ 9223372036854775807)
10.	uint8    8 位無符號整數(0 ~ 255)
11.	uint16   16 位無符號整數(0 ~ 65535)
12.	uint32   32 位無符號整數(0 ~ 4294967295)
13.	uint64   64 位無符號整數(0 ~ 18446744073709551615)
14.	float_   float64的簡寫
15.	float16  半精度浮點:符號位,5 位指數,10 位尾數
16.	float32  單精度浮點:符號位,8 位指數,23 位尾數
17.	float64  雙精度浮點:符號位,11 位指數,52 位尾數
18.	complex_    complex128的簡寫
19.	complex64   複數,由兩個 32 位浮點表示(實部和虛部)
20.	complex128  複數,由兩個 64 位浮點表示(實部和虛部)
  1. 對象類型 : 對象,字符串
1. object    python的對象類型
2. S         固定長度的字符串類型,每個字符 1 個字節
   S1        字符串,長度爲 1
3. U         固定長度的 unicode 類型
   U2        字符串,長度爲 2

3 ,檢查 nd 的元素類型 : nd01.dtype

  1. 代碼 :
if __name__ == '__main__':
    nd01 = np.array(["1",2,3,4])
    print(nd01.dtype)
==================================
<U1

4 ,類型轉換 : nd01.astype(np.int)

  1. 代碼 :
if __name__ == '__main__':
    nd01 = np.array(["1",2,3,4])
    nd02 = nd01.astype(np.int)
    nd03 = nd02.astype(np.unicode)
    print(nd01.dtype)
    print(nd02.dtype)
    print(nd03.dtype)
====================================
<U1
int32
<U11
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章