參考鏈接: Python中的Array | 數組1(簡介和功能)
一、list和array的區別?
Python的數組通過Numpy包的array實現。 Python里二者最大的區別是,list可以存儲不同類型的數據,而array只能存儲相同類型的數據。?
import numpy
#直接定義
a = [1,2,3,4,'5']? ?#列表list,可以混合類型
b = numpy.array([1,2,3,4,5])? ? ? ? #數字數組array
c = numpy.array([1,2,3,4,'5'])? ? ? #字符數組array
?
#打印出來也有不同
print(a)? ? #[1, 2, 3, 4]
print(b)? ? #[1 2 3]
print(c)? ? #['1' '2' '3' '4' '5']
?
#生成值為連續數字的對象
a1 = list(range(5))
b1 = numpy.arange(5)
?
#打印結果
print(a1)? ?#[0, 1, 2, 3, 4]
print(b1)? ?#[0 1 2 3 4]
?
?
二、創建數組的方法?
(一) numpy.empty 創建未初始化的數組(非空,元素為隨機值)?
numpy.empty(shape, dtype = float, order = ‘C’) 參數列表分別為形狀,數據類型,在計算機中的存儲為行優先 ‘C’ 或者列優先 ‘F’。?
import numpy?
x = numpy.empty([3,2], dtype = int)?
print(x)
?
[[0 0]
?[0 0]
?[0 0]]
?
(二) numpy.zeros 創建元素為 0 的數組?
numpy.zeros(shape, dtype = float, order = ‘C’)?
import numpy
y = numpy.zeros((2,2), dtype=int)
print(y)
?
[[0 0]
?[0 0]]
?
(三) numpy.ones 創建元素為 1 的數組?
import numpy
z = numpy.ones((2,2))? ? #這幾個創建方式都需要注意第一個參數的形式
print(z)
?
[[1. 1.] [1. 1.]]?
(四) numpy.asarray 根據已有的元組或者列表創建數組?
numpy.asarray(a, dtype = None, order = None) 這是從列表轉換為數組的例子?
import numpy
x = [[1,2,3],[4,5,6]]? ?#需要注意原列表的形式
y = [[1,2,3],[4,5]]
z = [[1,2,3],[4,5,'6']]
q = [[1,2,3],[4,5,6]]
?
a = numpy.asarray(x)
b = numpy.asarray(y)
c = numpy.asarray(z)
d = numpy.asarray(q,dtype=float)
print(a)
print(b)
print(c)
print(d)
?
[[1 2 3]
?[4 5 6]]
[list([1, 2, 3]) list([4, 5])]
[['1' '2' '3']
?['4' '5' '6']]
[[1. 2. 3.]
?[4. 5. 6.]]
?
(五) numpy.frombuffer 流形式讀入轉換為數組?
numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0) 細節太多不討論了,需要的時候再看?
(六) numpy.fromiter 從可迭代對象創建數組,返回一維數組?
numpy.fromiter(iterable, dtype, count=-1) count為讀取的數據數量,默認為-1,讀取所有數據?
import numpy
x = range(5)
it = iter(x)
a = numpy.fromiter(x, dtype=float)
print(a)
?
[0. 1. 2. 3. 4.]
?
(七) numpy.arange 從數值范圍創建數組?
numpy.arange(start, stop, step, dtype)?
import numpy
a = numpy.arange(5)
print(a)
?
[0 1 2 3 4]
?
(八) numpy.linspace 創建等差數列一維數組?
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) num為數量,endpoint為真時stop被包含在數列中,retstep為真時顯示間距?
import numpy
a = numpy.linspace(0,100,11)
b = numpy.linspace(0,100,11,retstep =True, dtype=int)
print(a)
print(b)
?
[? 0.? 10.? 20.? 30.? 40.? 50.? 60.? 70.? 80.? 90. 100.]
(array([? 0,? 10,? 20,? 30,? 40,? 50,? 60,? 70,? 80,? 90, 100]), 10.0)
?
(九) numpy.logspace 創建等比數列一維數組?
numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None) 序列的起始值為:base**start (base為底的start次方) 序列的終止值為:base ** stop (base為底的stop次方), 如果endpoint為true,該值包含于數列中 base為log的底數?
import numpy
a = numpy.logspace(1, 4, 4, base=3, dtype = int)
b = numpy.logspace(1, 10, 10, base=2)
print(a)
print(b)
?
[ 3? 9 27 81]
[? ?2.? ? 4.? ? 8.? ?16.? ?32.? ?64.? 128.? 256.? 512. 1024.]
?
三、array的操作?
(一) reshape 整形?
import numpy
a = numpy.arange(6)
b = a.reshape(3,2)? ? #改變數組形狀,參數是行數和列數,需要匹配原數組的元素數量否則報錯
print(a)
print(b)
?
[0 1 2 3 4 5 6 7]
[[0 1]
?[2 3]
?[4 5]
?[6 7]]
?
(二) flat 數組迭代器?
import numpy
#一維數組可以直接for循環迭代
a = numpy.arange(6)
for x in a:
? ? print(x)
?
b = numpy.arange(6).reshape(3,2)? ? #二維數組
#flat迭代器
for x in b.flat:
? ? print(x)
?
#多重for循環,跟迭代器二者等效
for x in b:?
? ? for y in x:?
? ? ? ? print(y)
?
(三) flatten 深拷貝,同copy()?
ndarray.flatten(order=‘C’) order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原順序,‘K’ – 元素在內存中的出現順序。?
import numpy
a = numpy.arange(4)
b=a.copy()
c=a.flatten()
?
a[1] = 8
b[0] = 9
?
print(a)
print(b)
print(c)
?
[0 8 2 3]
[9 1 2 3]
[0 1 2 3]
?
(四) ravel 返回數組的視圖,修改會影響原數組?
numpy.ravel(a, order=‘C’) order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原順序,‘K’ – 元素在內存中的出現順序。?
import numpy
a = numpy.arange(8).reshape(2,4)
b = a.ravel()
c = a.ravel(order = 'F')? ? # 就這個修改不會影響其他的
d = a.ravel(order = 'A')
e = a.ravel(order = 'K')
print(a)
print(b)
print(c)
print(d)
print(e)
?
[[0 1 2 3]
?[4 5 6 7]]
[0 1 2 3 4 5 6 7]
[0 4 1 5 2 6 3 7]
[0 1 2 3 4 5 6 7]
[0 1 2 3 4 5 6 7]
?
注意,修改order='F’模式的時候不會影響其他模式的序列,沒有深究為何,用的時候再找。?
b[4] = 44
c[5] = 55
d[6] = 66
e[7] = 77
print(a)
print(b)
print(c)
print(d)
print(e)
?
[[ 0? 1? 2? 3]
?[44? 5 66 77]]
[ 0? 1? 2? 3 44? 5 66 77]
[ 0? 4? 1? 5? 2 55? 3? 7]
[ 0? 1? 2? 3 44? 5 66 77]
[ 0? 1? 2? 3 44? 5 66 77]
?
(五) transpose 等同于T,翻轉數組行和列?
numpy.transpose(arr, axes) arr:要操作的數組 axes:整數列表,對應維度,通常所有維度都會對換。?
import numpy
a = numpy.arange(6).reshape(2,3)
b = a.transpose()
c = a.T? ? ?#注意寫法
?
print(a)
print(b)
print(c)
?
[[0 1 2]
?[3 4 5]]
[[0 3]
?[1 4]
?[2 5]]
[[0 3]
?[1 4]
?[2 5]]
?
(六) 后面暫時略,以后補完