1. 程式人生 > >Python-Numpy多維陣列--位操作, 字串函式, 算術函式

Python-Numpy多維陣列--位操作, 字串函式, 算術函式

一.位操作

1.bitwise_and

通過np.bitwise_and()函式對輸入陣列中的整數的二進位制表示的相應位執行位與運算。

例子

import numpy as np
print '13 和 17 的二進位制形式:'
a,b = 13,17
print bin(a), bin(b)
print '13 和 17 的位與:'
print np.bitwise_and(13, 17)
輸出如下:
13 和 17 的二進位制形式:0b1101 0b10001
13 和 17 的位與:1

你可以使用下表驗證此輸出。 考慮下面的位與真值表。

A B AND
1 1 1
1 0 0
0 1 0
0 0 0

2.bitwise_or

通過np.bitwise_or()函式對輸入陣列中的整數的二進位制表示的相應位執行位或運算。

例子

import numpy as np
a,b = 13,17
print '13 和 17 的二進位制形式:'
print bin(a), bin(b)
print '13 和 17 的位或:'
print np.bitwise_or(13, 17)
輸出如下:
13 和 17 的二進位制形式:0b1101 0b10001
13 和 17 的位或:29

你可以使用下表驗證此輸出。 考慮下面的位或真值表。

A B OR
1 1 1
1 0 1
0 1 1
0 0 0

3.invert

此函式計算輸入陣列中整數的位非結果。 對於有符號整數,返回補碼。

例子

import numpy as np
print '13 的位反轉,其中 ndarray 的 dtype 是 uint8:'
print np.invert(np.array([13], dtype = np.uint8))
# 比較 13 和 242 的二進位制表示,我們發現了位的反轉
print '13 的二進位制表示:'
print np.binary_repr(13, width = 8)
print '242 的二進位制表示:'
print np.binary_repr(242, width = 8)
輸出如下:
13 的位反轉,其中 ndarray 的 dtype 是 uint8:[242]
13 的二進位制表示:00001101
242 的二進位制表示:11110010
請注意,np.binary_repr()函式返回給定寬度中十進位制數的二進位制表示。

4.left_shift

numpy.left shift()函式將陣列元素的二進位制表示中的位向左移動到指定位置,右側附加相等數量的 0。

import numpy as np
print '將 10 左移兩位:'
print np.left_shift(10,2)
print '10 的二進位制表示:'
print np.binary_repr(10, width = 8)
print '40 的二進位制表示:'
print np.binary_repr(40, width = 8)
# '00001010' 中的兩位移動到了左邊,並在右邊添加了兩個 0。
輸出如下:
將 10 左移兩位:40
10 的二進位制表示:00001010
40 的二進位制表示:00101000

5.right_shift

 

numpy.right_shift()函式將陣列元素的二進位制表示中的位向右移動到指定位置,左側附加相等數量的 0。

import numpy as np
print '將 40 右移兩位:'
print np.right_shift(40,2)
print '40 的二進位制表示:'
print np.binary_repr(40, width = 8)
print '10 的二進位制表示:'
print np.binary_repr(10, width = 8)
# '00001010' 中的兩位移動到了右邊,並在左邊添加了兩個 0。
輸出如下:
將 40 右移兩位:10
40 的二進位制表示:00101000
10 的二進位制表示:00001010
  1.  

二.Numpy - 字串函式

1.numpy.char.add()函式執行按元素的字串連線。

import numpy as np
print '連線兩個字串:'
print np.char.add(['hello'],[' xyz'])
print '連線示例:'
print np.char.add(['hello', 'hi'],[' abc', ' xyz'])
輸出如下:
連線兩個字串:
['hello xyz']
連線示例:['hello abc' 'hi xyz']

2.numpy.char.multiply()這個函式執行多重連線。

import numpy as np
print np.char.multiply('Hello ',3)
輸出如下:Hello Hello Hello 

3.numpy.char.center()此函式返回所需寬度的陣列,以便輸入字串位於中心,並使用fillchar在左側和右側進行填充。

import numpy as np
# np.char.center(arr, width,fillchar)
print np.char.center('hello', 20,fillchar = '*')
輸出如下:
*******hello********

4.numpy.char.capitalize()函式返回字串的副本,其中第一個字母大寫

import numpy as np
print np.char.capitalize('hello world')
輸出如下:
Hello world 

5.numpy.char.title()返回輸入字串的按元素標題轉換版本,其中每個單詞的首字母都大寫。

import numpy as np
print np.char.title('hello how are you?')\
輸出如下:
Hello How Are You?

6.numpy.char.lower()函式返回一個數組,其元素轉換為小寫。它對每個元素呼叫str.lower

import numpy as np
print np.char.lower(['HELLO','WORLD'])
print np.char.lower('HELLO')
輸出如下:
['hello' 'world']
hello

7.numpy.char.upper()函式返回一個數組,其元素轉換為大寫。它對每個元素呼叫str.upper

 

import numpy as np
print np.char.upper('hello')
print np.char.upper(['hello','world'])
輸出如下:
HELLO
['HELLO' 'WORLD']

8.numpy.char.split()此函式返回輸入字串中的單詞列表。 預設情況下,空格用作分隔符。 否則,指定的分隔符字元用於分割字串。

import numpy as np
print np.char.split ('hello how are you?')
print np.char.split ('TutorialsPoint,Hyderabad,Telangana', sep = ',')
輸出如下:
['hello', 'how', 'are', 'you?']
['TutorialsPoint', 'Hyderabad', 'Telangana']

9.numpy.char.splitlines()函式返回陣列中元素的單詞列表,以換行符分割。

import numpy as np
print np.char.splitlines('hello\nhow are you?')
print np.char.splitlines('hello\rhow are you?')
輸出如下:
['hello', 'how are you?']
['hello', 'how are you?']
注意:'\n','\r','\r\n'都會用作換行符。

10.numpy.char.strip()函式返回陣列的副本,其中元素移除了開頭或結尾處的特定字元。

import numpy as np
print np.char.strip('ashok arora','a')
print np.char.strip(['arora','admin','java'],'a')
輸出如下:
shok aror
['ror' 'dmin' 'jav']

11.numpy.char.join()這個函式返回一個字串,其中單個字元由特定的分隔符連線

 

import numpy as np
print np.char.join(':','dmy')
print np.char.join([':','-'],['dmy','ymd'])
輸出如下:
d:m:y
['d:m:y' 'y-m-d']

12.numpy.char.replace()這個函式返回字串副本,其中所有字元序列的出現位置都被另一個給定的字元序列取代

 

import numpy as np
print np.char.replace ('He is a good boy', 'is', 'was')

13.numpy.char.decode()這個函式在給定的字串中使用特定編碼呼叫str.decode()

import numpy as np
a = np.char.encode('hello', 'cp500')
print a
print np.char.decode(a,'cp500')
輸出如下:
\x88\x85\x93\x93\x96
hello

14.numpy.char.encode()此函式對陣列中的每個元素呼叫str.encode函式。 預設編碼是utf_8,可以使用標準 Python 庫中的編解碼器。

 

import numpy as np
a = np.char.encode('hello', 'cp500')
print a
輸出如下:\x88\x85\x93\x93\x96

三.Numpy - 算數函式

1.三角函式

NumPy 擁有標準的三角函式,它為弧度制單位的給定角度返回三角函式比值。

示例

import numpy as np
a = np.array([0,30,45,60,90])
print '不同角度的正弦值:'
# 通過乘 pi/180 轉化為弧度
print np.sin(a*np.pi/180)
print '陣列中角度的餘弦值:'
print np.cos(a*np.pi/180)
print '陣列中角度的正切值:'
print np.tan(a*np.pi/180)
輸出如下:
不同角度的正弦值:[ 0. 0.5 0.70710678 0.8660254 1. ]
陣列中角度的餘弦值:[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01.12323400e-17]
陣列中角度的正切值:[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+001.63312394e+16]

arcsinarccos,和arctan函式返回給定角度的sincostan的反三角函式。 這些函式的結果可以通過numpy.degrees()函式通過將弧度制轉換為角度制來驗證。

示例

import numpy as np
a = np.array([0,30,45,60,90])
print '含有正弦值的陣列:'
sin = np.sin(a*np.pi/180)
print sin
print '計算角度的反正弦,返回值以弧度為單位:'
inv = np.arcsin(sin)
print inv
print '通過轉化為角度制來檢查結果:'
print np.degrees(inv)
print 'arccos 和 arctan 函式行為類似:'
cos = np.cos(a*np.pi/180)
print cos
print '反餘弦:'
inv = np.arccos(cos)
print inv
print '角度制單位:'
print np.degrees(inv)
print 'tan 函式:'
tan = np.tan(a*np.pi/180)
print tan
print '反正切:'
inv = np.arctan(tan)
print inv
print '角度制單位:'
print np.degrees(inv)
輸出如下:
含有正弦值的陣列:[ 0. 0.5 0.70710678 0.8660254 1. ]
計算角度的反正弦,返回值以弧度製為單位:[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]
通過轉化為角度制來檢查結果:[ 0. 30. 45. 60. 90.]
arccos 和 arctan 函式行為類似:[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-016.12323400e-17]
反餘弦:[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]
角度制單位:[ 0. 30. 45. 60. 90.]
tan 函式:[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+001.63312394e+16]
反正切:[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]
角度制單位:[ 0. 30. 45. 60. 90.]

2.舍入函式

 

(1)numpy.around()這個函式返回四捨五入到所需精度的值。 該函式接受以下引數。

numpy.around(a,decimals)

序號 引數及描述
1. a 輸入陣列
2. decimals 要舍入的小數位數。 預設值為0。 如果為負,整數將四捨五入到小數點左側的位置

示例

import numpy as np
a = np.array([1.0,5.55, 123, 0.567, 25.532])
print '原陣列:'
print a
print '舍入後:'
print np.around(a)
print np.around(a, decimals = 1)
print np.around(a, decimals = -1)
輸出如下:
原陣列:
[ 1. 5.55 123. 0.567 25.532]
舍入後:
[ 1. 6. 123. 1. 26. ]
[ 1. 5.6 123. 0.6 25.5]
[ 0. 10. 120. 0. 30. ]

​​​​​​​(2)numpy.floor()​​​​​​​此函式返回不大於輸入引數的最大整數。 即標量x 的下限是最大的整數i ,使得i <= x。 注意在Python中,向下取整總是從 0 舍入。

​​​​​​​示例

import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
print '提供的陣列:'
print a
print '修改後的陣列:'
print np.floor(a)
輸出如下:
提供的陣列:[ -1.7 1.5 -0.2 0.6 10. ]
修改後的陣列:[ -2. 1. -1. 0. 10.]

(3)numpy.ceil():函式返回輸入值的上限,即,標量x的上限是最小的整數i ,使得i> = x

 

示例

import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
print '提供的陣列:'
print a
print '修改後的陣列:'
print np.ceil(a)
輸出如下:
提供的陣列:[ -1.7 1.5 -0.2 0.6 10. ]
修改後的陣列:[ -1. 2. -0. 1. 10.]