numpy.fromstring

numpy.fromstring

numpy.fromstring(string, dtype=float, count=-1, sep='')

A new 1-D array initialized from text data in a string.
從字符串中的文本數據初始化的新一維數組。

1. Parameters:

string : str
A string containing the data.
包含數據的字符串。

dtype : data-type, optional
The data type of the array; default: float. For binary input data, the data must be in exactly this format.
數組的數據類型。default: float。對於二進制輸入數據,數據必須完全採用這種格式。

count : int, optional
Read this number of dtype elements from the data. If this is negative (the default), the count will be determined from the length of the data.
從數據中讀取此數量的 dtype 元素。如果爲負 (默認值),則將根據數據長度確定計數。

sep : str, optional
The string separating numbers in the data; extra whitespace between elements is also ignored.
數據中分隔數字的字符串,元素之間的多餘空格也將被忽略。

2. Returns

arr : ndarray
The constructed array.
構造的數組。

3. Raises

ValueError
If the string is not the correct size to satisfy the requested dtype and count.

4. example

(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$ python
Python 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
>>> np.fromstring('1 2', dtype=int, sep=' ')
array([1, 2])
>>>
>>> np.fromstring('1, 2', dtype=int, sep=',')
array([1, 2])
>>>
>>> np.fromstring('1234', dtype=np.uint8)
array([49, 50, 51, 52], dtype=uint8)
>>>
>>> np.fromstring('1234', np.uint8)
array([49, 50, 51, 52], dtype=uint8)
>>>
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$

5. little endian (低位字節在前)

Python 的字符串實際上是字節序列,每個字符佔一個字節,因此如果從字符串 s 創建一個 8bit 的整數數組的話,所得到的數組正好就是字符串中每個字符的 ASCII 編碼。

>>> s = "abcdefgh"
>>> np.fromstring(s, dtype=np.int8)
array([ 97,  98,  99, 100, 101, 102, 103, 104], dtype=int8)
>>>

如果從字符串 s 創建 16bit 的整數數組,那麼兩個相鄰的字節就表示一個整數,把字節 98 和字節 97 當作一個16 位的整數,它的值就是 98 * 256 + 97 = 25185。可以看出內存中是以 little endian (低位字節在前) 方式保存數據的。

>>> s = "abcdefgh"
>>> np.fromstring(s, dtype=np.int8)
array([ 97,  98,  99, 100, 101, 102, 103, 104], dtype=int8)
>>>
>>> np.fromstring(s, dtype=np.int16)
array([25185, 25699, 26213, 26727], dtype=int16)
>>>

如果把整個字符串轉換爲一個 64 位的雙精度浮點數數組,那麼它的值是:

>>> np.fromstring(s, dtype=np.float)
array([8.54088322e+194])

顯然這個例子沒有什麼意義,但是可以想象如果我們用 C 語言的二進制方式寫了一組 double 類型的數值到某個文件中,那們可以從此文件讀取相應的數據,並通過 fromstring 函數將其轉換爲 float64 類型的數組。

(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$ python
Python 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
>>> np.fromstring('12345678', np.uint8)
array([49, 50, 51, 52, 53, 54, 55, 56], dtype=uint8)
>>>
>>> np.fromstring('12345678', np.int16)
array([12849, 13363, 13877, 14391], dtype=int16)
>>>
50 * 256 + 49 = 12849
52 * 256 + 51 = 13363
54 * 256 + 53 = 13877
56 * 256 + 55 = 14391
little endian (低位字節在前)

>>> np.fromstring('12345678', np.int32)
array([875770417, 943142453], dtype=int32)
>>>
52 * 16777216 + 51 * 65536 + 50 * 256 + 49 = 872415232 + 3342336 + 12800 + 49 = 875770417
56 * 16777216 + 55 * 65536 + 54 * 256 + 53 = 939524096 + 3604480 + 13824 + 53 = 943142453
little endian (低位字節在前)

>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章