好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python机器学习库NumPy

在我们使用Python语言进行机器学习编程的时候,这是一个非常常用的基础库。本文针对Python 机器学习库 NumPy入门教程,感兴趣的朋友一起学习吧

a = np.array([1, 2, 3])
b = np.array([(1,2,3), (4,5,6)]) 
# create_array.py

import numpy as np

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

print('a=')
print(a)
print("a's ndim {}".format(a.ndim))
print("a's shape {}".format(a.shape))
print("a's size {}".format(a.size))
print("a's dtype {}".format(a.dtype))
print("a's itemsize {}".format(a.itemsize))
print('')
print('b=')
print(b)
print("b's ndim {}".format(b.ndim))
print("b's shape {}".format(b.shape))
print("b's size {}".format(b.size))
print("b's dtype {}".format(b.dtype))
print("b's itemsize {}".format(b.itemsize)) 
a=
[1 2 3]
a's ndim 1
a's shape (3,)
a's size 3
a's dtype int64
a's itemsize 8
b=
[[1 2 3]
 [4 5 6]]
b's ndim 2
b's shape (2, 3)
b's size 6
b's dtype int64
b's itemsize 8 
# create_specific_array.py

import numpy as np

a = np.zeros((2,3))
print('np.zeros((2,3)= \n{}\n'.format(a))

b = np.ones((2,3))
print('np.ones((2,3))= \n{}\n'.format(b))

c = np.empty((2,3))
print('np.empty((2,3))= \n{}\n'.format(c))

d = np.arange(1, 2, 0.3)
print('np.arange(1, 2, 0.3)= \n{}\n'.format(d))

e = np.linspace(1, 2, 7)
print('np.linspace(1, 2, 7)= \n{}\n'.format(e))

f = np.random.random((2,3))
print('np.random.random((2,3))= \n{}\n'.format(f)) 
np.zeros((2,3)= 
[[ 0. 0. 0.]
 [ 0. 0. 0.]]
np.ones((2,3))= 
[[ 1. 1. 1.]
 [ 1. 1. 1.]]
np.empty((2,3))= 
[[ 1. 1. 1.]
 [ 1. 1. 1.]]
np.arange(1, 2, 0.3)= 
[ 1. 1.3 1.6 1.9]
np.linspace(1, 2, 7)= 
[ 1.  1.16666667 1.33333333 1.5  1.66666667 1.83333333
 2. ]
np.random.random((2,3))= 
[[ 0.5744616 0.58700653 0.59609648]
 [ 0.0417809 0.23810732 0.38372978]] 
# shape_manipulation.py
zero_line = np.zeros((1,3))
one_column = np.ones((3,1))
print("zero_line = \n{}\n".format(zero_line))
print("one_column = \n{}\n".format(one_column))
a = np.array([(1,2,3), (4,5,6)])
b = np.arange(11, 20)
print("a = \n{}\n".format(a))
print("b = \n{}\n".format(b)) 
zero_line = 
[[ 0. 0. 0.]]
one_column = 
[[ 1.]
 [ 1.]
 [ 1.]]
a = 
[[1 2 3]
 [4 5 6]]
b = 
[11 12 13 14 15 16 17 18 19] 
# shape_manipulation.py
b = b.reshape(3, -1)
print("b.reshape(3, -1) = \n{}\n".format(b)) 
b.reshape(3, -1) = 
[[11 12 13]
 [14 15 16]
 [17 18 19]] 
# shape_manipulation.py
c = np.vstack((a, b, zero_line))
print("c = np.vstack((a,b, zero_line)) = \n{}\n".format(c)) 
c = np.vstack((a,b, zero_line)) = 
[[ 1. 2. 3.]
 [ 4. 5. 6.]
 [ 11. 12. 13.]
 [ 14. 15. 16.]
 [ 17. 18. 19.]
 [ 0. 0. 0.]] 
# shape_manipulation.py
a = a.reshape(3, 2)
print("a.reshape(3, 2) = \n{}\n".format(a))
d = np.hstack((a, b, one_column))
print("d = np.hstack((a,b, one_column)) = \n{}\n".format(d)) 
a.reshape(3, 2) = 
[[1 2]
 [3 4]
 [5 6]]
d = np.hstack((a,b, one_column)) = 
[[ 1.  2. 11. 12. 13.  1.]
 [ 3.  4. 14. 15. 16.  1.]
 [ 5.  6. 17. 18. 19.  1.]] 
# shape_manipulation.py
# np.vstack((a,b)) # ValueError: dimensions not match 
# shape_manipulation.py
e = np.hsplit(d, 3) # Split a into 3
print("e = np.hsplit(d, 3) = \n{}\n".format(e))
print("e[1] = \n{}\n".format(e[1])) 
e = np.hsplit(d, 3) = 
[array([[ 1., 2.],
    [ 3., 4.],
    [ 5., 6.]]), array([[ 11., 12.],
    [ 14., 15.],
    [ 17., 18.]]), array([[ 13.,  1.],
    [ 16.,  1.],
    [ 19.,  1.]])]
e[1] = 
[[ 11. 12.]
 [ 14. 15.]
 [ 17. 18.]] 
# shape_manipulation.py
f = np.hsplit(d, (1, 3)) # # Split a after the 1st and the 3rd column
print("f = np.hsplit(d, (1, 3)) = \n{}\n".format(f)) 
f = np.hsplit(d, (1, 3)) = 
[array([[ 1.],
    [ 3.],
    [ 5.]]), array([[ 2., 11.],
    [ 4., 14.],
    [ 6., 17.]]), array([[ 12., 13.,  1.],
    [ 15., 16.,  1.],
    [ 18., 19.,  1.]])] 
# shape_manipulation.py
g = np.vsplit(d, 3)
print("np.hsplit(d, 2) = \n{}\n".format(g))
# np.vsplit(d, 2) # ValueError: array split does not result in an equal pision
np.vsplit(d, 3)将产生三个一维数组:
np.vsplit(d, 3) = 
[array([[ 1.,  2., 11., 12., 13.,  1.]]), array([[ 3.,  4., 14., 15., 16.,  1.]]), array([[ 5.,  6., 17., 18., 19.,  1.]])] 
# array_index.py
import numpy as np
base_data = np.arange(100, 200)
print("base_data\n={}\n".format(base_data))
print("base_data[10] = {}\n".format(base_data[10])) 
base_data
=[100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
 190 191 192 193 194 195 196 197 198 199]
base_data[10] = 110 
# array_index.py
every_five = np.arange(0, 100, 5)
print("base_data[every_five] = \n{}\n".format(
  base_data[every_five])) 
base_data[every_five] = 
[100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185
 190 195] 
# array_index.py
a = np.array([(1,2), (10,20)])
print("a = \n{}\n".format(a))
print("base_data[a] = \n{}\n".format(base_data[a])) 
a = 
[[ 1 2]
 [10 20]]
base_data[a] = 
[[101 102]
 [110 120]] 
# array_index.py
base_data2 = base_data.reshape(10, -1)
print("base_data2 = np.reshape(base_data, (10, -1)) = \n{}\n".format(base_data2)) 
base_data2 = np.reshape(base_data, (10, -1)) = 
[[100 101 102 103 104 105 106 107 108 109]
 [110 111 112 113 114 115 116 117 118 119]
 [120 121 122 123 124 125 126 127 128 129]
 [130 131 132 133 134 135 136 137 138 139]
 [140 141 142 143 144 145 146 147 148 149]
 [150 151 152 153 154 155 156 157 158 159]
 [160 161 162 163 164 165 166 167 168 169]
 [170 171 172 173 174 175 176 177 178 179]
 [180 181 182 183 184 185 186 187 188 189]
 [190 191 192 193 194 195 196 197 198 199]] 
# array_index.py
print("base_data2[2] = \n{}\n".format(base_data2[2]))
print("base_data2[2, 3] = \n{}\n".format(base_data2[2, 3]))
print("base_data2[-1, -1] = \n{}\n".format(base_data2[-1, -1])) 
base_data2[2] = 
[120 121 122 123 124 125 126 127 128 129]
base_data2[2, 3] = 
123
base_data2[-1, -1] = 
199 
# array_index.py
print("base_data2[2, :]] = \n{}\n".format(base_data2[2, :]))
print("base_data2[:, 3]] = \n{}\n".format(base_data2[:, 3]))
print("base_data2[2:5, 2:4]] = \n{}\n".format(base_data2[2:5, 2:4])) 
base_data2[2, :]] = 
[120 121 122 123 124 125 126 127 128 129]
base_data2[:, 3]] = 
[103 113 123 133 143 153 163 173 183 193]
base_data2[2:5, 2:4]] = 
[[122 123]
 [132 133]
 [142 143]] 
# operation.py
import numpy as np
base_data = (np.random.random((5, 5)) - 0.5) * 100
print("base_data = \n{}\n".format(base_data))
print("np.amin(base_data) = {}".format(np.amin(base_data)))
print("np.amax(base_data) = {}".format(np.amax(base_data)))
print("np.average(base_data) = {}".format(np.average(base_data)))
print("np.sum(base_data) = {}".format(np.sum(base_data)))
print("np.sin(base_data) = \n{}".format(np.sin(base_data))) 
base_data = 
[[ -9.63895991 6.9292461 -2.35654712 -48.45969283 13.56031937]
 [-39.75875796 -43.21031705 -49.27708561 6.80357128 33.71975059]
 [ 36.32228175 30.92546582 -41.63728955 28.68799187 6.44818484]
 [ 7.71568596 43.24884701 -14.90716555 -9.24092252 3.69738718]
 [-31.90994273 34.06067289 18.47830413 -16.02495202 -44.84625246]]

np.amin(base_data) = -49.277085606595726
np.amax(base_data) = 43.24884701268845
np.average(base_data) = -3.22680706079886
np.sum(base_data) = -80.6701765199715
np.sin(base_data) = 
[[ 0.21254814 0.60204578 -0.70685739 0.9725159 0.8381861 ]
 [-0.88287359 0.69755541 0.83514527 0.49721505 0.74315189]
 [-0.98124746 -0.47103234 0.7149727 -0.40196147 0.16425187]
 [ 0.99045239 -0.66943662 -0.71791164 -0.18282139 -0.5276184 ]
 [-0.4741657 0.47665553 -0.36278223 0.31170676 -0.76041722]] 
# matrix.py

import numpy as np

base_data = np.floor((np.random.random((5, 5)) - 0.5) * 100)
print("base_data = \n{}\n".format(base_data))

print("base_data.T = \n{}\n".format(base_data.T))
print("base_data.transpose() = \n{}\n".format(base_data.transpose()))

matrix_one = np.ones((5, 5))
print("matrix_one = \n{}\n".format(matrix_one))

minus_one = np.dot(matrix_one, -1)
print("minus_one = \n{}\n".format(minus_one))

print("np.dot(base_data, minus_one) = \n{}\n".format(
 np.dot(base_data, minus_one)))
这段代码 
输出如下: base_data = [[-49. -5. 11. -13. -41.] [ -6. -33. -33. -47. -4.] [-38. 26. 28. -18. 18.] [ -3. -19. -15. -39. 45.] [-43. 6. 18. -15. -21.]] base_data.T = [[-49. -6. -38. -3. -43.] [ -5. -33. 26. -19. 6.] [ 11. -33. 28. -15. 18.] [-13. -47. -18. -39. -15.] [-41. -4. 18. 45. -21.]] base_data.transpose() = [[-49. -6. -38. -3. -43.] [ -5. -33. 26. -19. 6.] [ 11. -33. 28. -15. 18.] [-13. -47. -18. -39. -15.] [-41. -4. 18. 45. -21.]] matrix_one = [[ 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1.]] minus_one = [[-1. -1. -1. -1. -1.] [-1. -1. -1. -1. -1.] [-1. -1. -1. -1. -1.] [-1. -1. -1. -1. -1.] [-1. -1. -1. -1. -1.]] np.dot(base_data, minus_one) = [[ 97. 97. 97. 97. 97.] [ 123. 123. 123. 123. 123.] [ -16. -16. -16. -16. -16.] [ 31. 31. 31. 31. 31.] [ 55. 55. 55. 55. 55.]]
# rand.py
import numpy as np
print("random: {}\n".format(np.random.random(20)));
print("rand: {}\n".format(np.random.rand(3, 4)));
print("randint: {}\n".format(np.random.randint(0, 100, 20)));
print("permutation: {}\n".format(np.random.permutation(np.arange(20)))); 
random: [0.62956026 0.56816277 0.30903156 0.50427765 0.92117724 0.43044905
 0.54591323 0.47286235 0.93241333 0.32636472 0.14692983 0.02163887
 0.85014782 0.20164791 0.76556972 0.15137427 0.14626625 0.60972522
 0.2995841 0.27569573]
rand: [[0.38629927 0.43779617 0.96276889 0.80018417]
 [0.67656892 0.97189483 0.13323458 0.90663724]
 [0.99440473 0.85197677 0.9420241 0.79598706]]
randint: [74 65 51 34 22 69 81 36 73 35 98 26 41 84 0 93 41 6 51 55]
permutation: [15 3 8 18 14 19 16 1 0 4 10 17 5 2 6 12 9 11 13 7] 

以上就是Python 机器学习库 NumPy的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python机器学习库NumPy的详细内容...

  阅读:39次