參考資料:https://numpy.org/doc/stable/reference/ ## np.array([])
若要製造一個矩陣,可使用此函式。 ## array.ndim 可查看array是幾維矩陣。
## array.size 可查看有多少元素在array裡。 ## array.transpose() or
array.T 可將矩陣轉置,row變成column,column變成row。
1
2
3
4
5
6
7
8
9
10
11
12import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9])
print(arr)
#output
#[[1 2 3]
#[4 5 6]
#[7 8 9]]
print(arr.transpose())
#output
#[[1 4 7]
#[2 5 8]
#[3 6 9]]1
2
3
4
5
6import numpy as np
arr = np.array([1,2,3],[4,5,6],[7,8,9],dtype = float)
print(arr)
a = np.zeros((2,3),dtype = int)
print(a)
print(arr.dtype)#output:float641
2
3
4
5import numpy as np
a = np.linspace(1,10,5,endpoint = True)
b = np.linspace(1,10,5,endpoiont = False)
print(a)
print(b)1
2
3
4
5
6import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]],dtype = float)
min_idx = np.argmin(arr)
max_idx = np.argmax(arr)
print(min_idx)
print(max_idx)1
2
3
4import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
summ = np.cumsum(arr)
print(summ)#[ 1 3 6 10 15 21 28 36 45]1
2
3import numpy as np
x = np.linspace(-1,1,30)[:,np.newaxis]
print(x)1
2
3
4
5import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(a.itemsize)
print(a.nbytes)1
2
3
4
5
6
7
8
9
10
11import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.flatten()
print(b)
print('==============')
x = np.array([[1,2,3],[4,5,6]])
y = x.ravel()
print(y)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.flatten(order = 'C')
print(b)
c = a.flatten(order = 'F')
print(c)
print('==============')
x = np.array([[1,2,3],[4,5,6]])
y = x.ravel(order = 'C')
print(y)
z = x.ravel(order = 'F')
print(z)1
2
3
4
5
6import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([[10,11,12],[13,14,15],[16,17,18]])
print(np.concatenate((a,b)))1
2
3
4
5import numpy as np
a = np.random.randint(-50,50,50)
print(a)
print(np.where(a > 0,'T','F'))1
2
3
4
5
6
7
8import numpy as np
a = np.array([1,2,3,4,5])
b = np.array([5,10,15,20,25])
print(np.add(a,b))
print(np.subtract(a, b))
print(np.multiply(a,b))
print(np.divide(a,b))
print(np.mod(a,b))1
2
3
4
5
6
7
8
9import numpy as np
math = np.array([80,50,68,74,88])
print('Math:')
print('平均:',np.nanmean(math))
print('最高分:',np.nanmax(math))
print('最低分:',np.nanmin(math))
print('標準差:',np.nanstd(math))
print('變異數:',np.nanvar(math))
print('相關係數:',np.corrcoef(math))1
2
3
4
5
6
7import numpy as np
ham = np.array([1,2,3])
egg = np.array([4,5,6])
print(np.dot(ham, egg))# output : 32
print(ham @ egg)# output : 321
2
3
4
5
6
7
8
9
10import numpy as np
a = 2.7
b = 1.3
c = -4
arr = np.array([a,b,c])
result = np.exp(arr)
print(result)
-
Next PostPandas
-
Previous PostScikit Learn