Lesson 10.1: 產生一維數據陣列
import numpy as np
npRange1 = np.arange(1, 10)
print('npRange1=')
print(npRange1);
Note and Comments
np.arange(m, n) 產生數列內容爲m到n-1的數列
result
Lesson 10.2: 產生固定間隔的一維數據陣列
Example Code
npRange2 = np.arange(1, 5, 0.5)
print('npRange2=')
print(npRange2);
print('npRange1[0] = '+str(npRange1[0])+', npRange1[2] = '+str(npRange1[2]));
print('npRange1[0:5] = '+str(npRange1[2:5]));
print('npRange1[2:5] = '+str(npRange1[2:5]));
Note and Comments
np.arange(m, n, l) 產生m到n-1的每個間隔為l的矩陣
npRange1[n]: npRange1矩陣的第n個元素
npRange1[n:m]: npRange1矩陣的第n到m個元素
str(): 將內容轉成字串
Lesson 10.3: 將python 的List轉成numpy array形式
Example Code 1
import numpy as np;
List1=[1,2,3,4,5,6,7,8,9,10]
npList=np.array(List1)
print("List1="+str(List1))
print("npList="+str(npList))
Note and Comments
我們可以利用numpy的array功能來將list轉換成ndarray(numpy 的矩陣型態)
Lesson 10.4: 串接數據陣列
Example Code 1
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
c= np.concatenate((a, b), axis=0)
print(c)
d=np.concatenate((a, b.T), axis=1)
print(d)
e=np.concatenate((a, b), axis=None)
print(e)
Note and Comments
result
[[1 2]
[3 4]
[5 6]]
[3 4 6]]
[1 2 3 4 5 6]
Lesson 10.5: 刪除或新增元素到陣列內
Example Code
- sequence2 = ["ABC","DEF","GHI"];
- print(sequence2);
- print(sequence2[1]);
- print(sequence2[1:3]);
- del sequence2[1]
- print(sequence2);
- sequence2[1:1]=["DEF"];
- print(sequence2);
- sequence2.append("JKL");
- print(sequence2);
Note and Comments
- del: 刪除元素, 也可以配合[n:m], 刪除n到m的元素
- append: 在squence的最後附加一個新元素
- sequence2[n:n]=["DEF"]: 新設定元素, 在第n-1的位置插入"DEF"