numpy---one
輸出:
<class 'numpy.ndarray'> (5,) [1 2 3 4 5]
<class 'numpy.ndarray'> (6,) [1 2 3 4 5 6]
<class 'numpy.ndarray'> (2, 5) [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
(2, 5) [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
(10, 1) [[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]]
(2, 3) [[1 2 3]
[4 5 6]]
[ 1 100 3 4 5 6] [[ 1 100 3]
[ 4 5 6]]
?
?
import numpy as np#創建數組(通過numpy函數) a = np.arange(0, 1, 0.1) #不包括終值 b = np.linspace(0, 1, 10) #包括終值,等差10個數 c = np.logspace(0, 2, 10) #從1到100,等比10個數 s = "abcdef" d = np.fromstring(s, dtype=np.int8) e = np.fromstring(s, dtype=np.int16) print(a,'\n',b,'\n',c,'\n',d,'\n',e,'\n')輸出:
[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
[ 0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
0.66666667 0.77777778 0.88888889 1. ]
[ 1. 1.66810054 2.7825594 4.64158883 7.74263683
12.91549665 21.5443469 35.93813664 59.94842503 100. ]
[ 97 98 99 100 101 102]
[25185 25699 26213]
?
import numpy as np#創建10個元素的一維數組 def func(i):return i%4+1print ( np.fromfunction(func,(10,)) )輸出:
[ 1. ?2. ?3. ?4. ?1. ?2. ?3. ?4. ?1. ?2.]
import numpy as npdef func(i,j):return (i + 1) * (j + 1)print(np.fromfunction(func, (9,9)))輸出:
[[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 2. 4. 6. 8. 10. 12. 14. 16. 18.]
[ 3. 6. 9. 12. 15. 18. 21. 24. 27.]
[ 4. 8. 12. 16. 20. 24. 28. 32. 36.]
[ 5. 10. 15. 20. 25. 30. 35. 40. 45.]
[ 6. 12. 18. 24. 30. 36. 42. 48. 54.]
[ 7. 14. 21. 28. 35. 42. 49. 56. 63.]
[ 8. 16. 24. 32. 40. 48. 56. 64. 72.]
[ 9. 18. 27. 36. 45. 54. 63. 72. 81.]]
?
ndim:維度,shape:(行數,列數),size:元素總個數 dtype:指定數據類型
?
# -*- coding: utf-8 -*- import numpy as npmatrix = np.array([[1,2,3], [4,5,6]]) #矩陣 print("dim; ",matrix.ndim) print("shape: ",matrix.shape) print("size: ",matrix.size)list1 = np.array([1,2,3,4],dtype=np.int32) print("list1 dtype: ",list1.dtype)list2 = np.array([1,2,3,4]) print("list2 dtype: ",list2.dtype)list3 = np.array([1,2,3,4],dtype=np.float) print("list3 dtype: ",list3.dtype)list4 = np.array([1,2,3,4],dtype=np.float32) print("list4 dtype: ",list4.dtype)list5 = np.ones((3,4),dtype=np.int) print("list5: ",list5)list6 = np.empty((3,4)) print("list6: ",list6)list7 = np.arange(5,15).reshape((2,5)) print("list7: ",list7)list8 = np.linspace(1,11,10) print("list8: ",list8)輸出;
dim; 2
shape: (2, 3)
size: 6
list1 dtype: int32
list2 dtype: int32
list3 dtype: float64
list4 dtype: float32
list5: [[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
list6: [[ 6.95332630e-310 1.69118108e-306 2.04722549e-306 1.29061142e-306]
[ 2.22522597e-306 1.33511969e-306 1.29061753e-306 1.11261027e-306]
[ 9.34609790e-307 1.11260619e-306 1.42410974e-306 8.34449381e-308]]
list7: [[ 5 6 7 8 9]
[10 11 12 13 14]]
list8: [ 1. 2.11111111 3.22222222 4.33333333 5.44444444
6.55555556 7.66666667 8.77777778 9.88888889 11. ]
?
# -*- coding: utf-8 -*- import numpy as npa = np.arange(5) b = np.array([1,2,3,4,5])print("a: ",a) print("b: ",b) addc = a + b print("add: ", addc)minusc = a -b print("minus: ",minusc)timec = a * b print("times: ",timec)squc = a**2 print("square: ",squc)sinc = 10 * np.sin(a) print("sin: ",sinc)print("compare: ",a<3)matrix1 = np.array([[1,2,3,4],[5,6,7,8]]) matrix2 = np.arange(8).reshape((4,2)) print("matrix *: ",np.dot(matrix1,matrix2)) print("matrix *",matrix1.dot(matrix2))suiji = np.random.random((2,4)) print("suiji: ",suiji) print("max: ",np.max(suiji)) print("min: ",np.min(suiji)) print("sum: ",np.sum(suiji)) print("col: ",np.min(suiji,axis=0)) print("row: ",np.max(suiji,axis=1))a: [0 1 2 3 4]
b: [1 2 3 4 5]
add: [1 3 5 7 9]
minus: [-1 -1 -1 -1 -1]
times: [ 0 2 6 12 20]
square: [ 0 1 4 9 16]
sin: [ 0. 8.41470985 9.09297427 1.41120008 -7.56802495]
compare: [ True True True False False]
matrix *: [[ 40 50]
[ 88 114]]
matrix * [[ 40 50]
[ 88 114]]
suiji: [[ 0.79302826 0.02704441 0.19401082 0.02216562]
[ 0.66149996 0.77353779 0.66565688 0.53205038]]
max: 0.793028259974
min: 0.0221656169264
sum: 3.66899411306
col: [ 0.66149996 0.02704441 0.19401082 0.02216562]
row: [ 0.79302826 0.77353779]
?
轉載于:https://www.cnblogs.com/crazybird123/p/7471956.html
總結
以上是生活随笔為你收集整理的numpy---one的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ1821 [JSOI2010]G
- 下一篇: DWR第六篇之文件下载