1. 程式人生 > 其它 >一道C語言簡單題

一道C語言簡單題

技術標籤:python

判斷array是多少維度(ndarray,n=?):數最前面的方括號有幾個
e.g. 【【【【 ----> 4darray

1. numpy的屬性

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

array.size #9
array.shape #(3, 3)
array.ndim #2
array.dtype #int32

2. numpy運算

  1. 按位運算
    operation between two arrays: element-wise operations
+ - * /
** 次方
% 取模
// 取整

e.g. arr1 + arr2

operation between an array and a number: apply the number to all the elements of the array

+ - * /
邏輯運算 e.g. arr1 > 3

3)矩陣運算

np.dot(arr1, arr2) or arr1.dot(arr2)

  1. 轉秩

arr1.T
np.transpose(arr1)

  1. 隨機矩陣

np.random.random((row, col)) #隨機數取值範圍[0,1)random floats in the half-open interval [0.0, 1.0).

np.random.normal(size=(row,col)) #隨機數符合標準正態分佈
np.random.randin(low, high, size=(row,col)) #隨機數取值範圍[low, high)的整數

  1. sum
  • Axis or axes along which a sum is performed. axis=0, 求和所有行,所以每一列得到一個值。axis=1, 求和所有列,所以每一行得到一個值
  • The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.
  • The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.

7)min,max
得到值:
np.min(arr)
np.max(arr)
得到索引:
np.argmin(arr)
np.argmax(arr)

  1. mean, median, sqrt, sort(對每一行進行從小到大排序), clip(小於lower_bound的數=lower_bound, 大於upper_bound的=upper_bound)
    np.mean(arr)
    arr.mean()

np.median(arr)

np.sqrt(arr)

np.sort(arr)

np.clip(arr, lower_bound, upper_bound)

3. numpy的索引

  1. 索引
    row,col索引都從0開始
    arr[1]: row 1
    arr[1][1]: element(1,1)
    arr[1,2]: element(1,2)
    arr[:,2]: col 2

2)迭代行:for迴圈用來迭代arr的每一行
arr: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(arr)

[[1 2 3]
[4 5 6]
[7 8 9]]

for i in arr:
print(i) #operations on each row

[1 2 3]
[4 5 6]
[7 8 9]

3)迭代列:for迴圈用來迭代arr的每一列
for i in arr.T:
print(i) #operations on each column
[1 4 7]
[2 5 8]
[3 6 9]

4)迭代元素
for i in arr.flat:
print(i) #operations on each element
1
2
3
4
5
6
7
8
9

4. array合併

1)垂直合併
np.vstack((arr1, arr2, arr3, …))
2)水平合併
np.hstack((arr1, arr2, arr3, …))

  1. np.concatenate((arr1, arr2, …), axis = 0/1/None)
  • axis: int, optional. The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.
  • The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).
  • Note: axis = r / c, the dimension of r / c 被改變
  1. reshape or add new axis
  • arr.reshape(int or tuple of ints) or numpy.reshape(arr, int or tuple of ints):
    如果newshape是int,那麼變成一個一維陣列;
    如果newshape是一個tuple,那麼這個tuple是返回陣列的shape,如(1,3,2);
    新的形狀應該與原來的形狀相容;
    一個形狀尺寸可以是-1。在本例中,該值是根據陣列的長度和其餘維推斷出來的,如:-1在下圖中代表3
    在這裡插入圖片描述
  • arr.reshape(1,-1):不論原來的shape是什麼樣,現在變成一個只有一行的2d array
    e.g. arr = np.array([[[1,2,3]],
    [[4,5,6]]])
    print(arr.shape)
    print(arr.reshape(1,-1))

(2, 1, 3)
[[1 2 3 4 5 6]]

  • arr.reshape(-1,1): 不論原來的shape是什麼樣,現在變成一個只有一列的2d array

  • np.newaxis: 在某一dim和某一dim之間新增一個dim=1
    e.g. arr = np.array([[[1,2,3]],
    [[4,5,6]]])
    arr1 = arr[:, np.newaxis, :, :] #arr1: (2, 1, 1, 3)

在最前面或最後面新增一個dim=1

arr2 = arr[np.newaxis, …] #arr2: (1, 2, 1, 3)
arr3 = arr[…, np.newaxis] #arr3: (2, 1, 3, 1)

print(arr1)
[[[[1 2 3]]]

[[[4 5 6]]]]

  1. np.atleast_2d(arr): 將小於2d的arr變為2d資料,2d以上的保持不變; np.atleast_3d(arr): 將小於3d的arr變為2d資料,3d以上的保持不變;

5. array分割

1)arr1, arr2 (分成幾份就有幾個arr)= np.split(arr, 分成的份數, axis=1)
Note: axis=0 會改變行數,axis=1會改變列數。
split只可以等分成幾份(equal division)

2)np.array_split(arr, 3, axis=1)
array_split可以不等分

3)np.vsplit(arr, 3) 相當於np.split(arr, 3, axis=0) 垂直分割
np.hsplit(arr, 3) 相當於np.split(arr, 3, axis=1) 水平分割

6. 深淺拷貝

1)淺拷貝:arr2 = arr1 共享一塊記憶體(兩個指標指向一個地址),改變一個arr,另一個也會隨之改變
2)深拷貝:arr2 = arr.copy() 另外開闢一個地址,兩個arr不會互相影響