首页 > 其他分享 >第3章 Jupyter Notebook, numpy和matplotlib

第3章 Jupyter Notebook, numpy和matplotlib

时间:2022-10-29 20:55:51浏览次数:38  
标签:11 10 12 Jupyter 666 Notebook np array numpy

 

3-1 jupyter notebook基础

Notbook 示例

 

 

Notbook 源码

 1 [1]
 2 for x in range(5): 
 3     print('hello world')
 4 hello world
 5 hello world
 6 hello world
 7 hello world
 8 hello world
 9 
10 欢迎来到机器学习
11 阿济格的回复考核
12 
13 [2]
14  data = [ 2*i for i in range(100) ]
15 print(data)
16 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198]
17 
18 [3]
19 data[-1]
20 198
21 变量一旦在jupyer notebook 中创建即被保存
22 
23 [4]
24 len(data)
25 100
26 [8]
27 data[:10]
28 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
29 [6]
30 print(5464654)
31 5464654

 

3-2 jupyter notebook中的魔法命令

Notbook 示例

 

 

Notbook 源码

  1 [1]
  2 %run myscript/hello.py
  3 hello machine learning !
  4 
  5 [2]
  6 hello('what man')
  7 hello what man !
  8 
  9 [3]
 10 import mymodule.firstml
 11 8848
 12 能不能运行
 13 
 14 直接predict(8)不能运行
 15 
 16 通过 import 直接运行的文件,其文件中的函数不能为下节点直接调用
 17 
 18 [4]
 19 mymodule.firstml.predict(8)
 20 8
 21 
 22 对于上面 mymodule必不可少
 23 
 24 [5]
 25 %run mymodule/firstml.py
 26 8848
 27 能不能运行
 28 
 29 [6]
 30 import myscript.hello
 31 hello machine learning !
 32 
 33 [7]
 34 from mymodule import firstml
 35 [8]
 36 firstml.predict(9)
 37 9
 38 
 39 [9]
 40 %timeit L = [ i**2 for i in range(1000) ]
 41 497 µs ± 27.6 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
 42 
 43 [10]
 44 %timeit L = [ i**2 for i in range(1000000) ]
 45 638 ms ± 96.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
 46 
 47 [11]
 48 %timeit L = [ i**2 for i in range(10) ]
 49 5.21 µs ± 255 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
 50 
 51 [12]
 52 %%timeit 
 53 L = [ ]
 54 for n in range(1000):
 55     L.append( n ** 2)
 56 566 µs ± 28.5 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
 57 
 58 [13]
 59 %time L = [ i**2 for i in range(1000) ]
 60 CPU times: total: 0 ns
 61 Wall time: 0 ns
 62 
 63 [14]
 64 %%time 
 65 L = [ ]
 66 for n in range(1000):
 67     L.append( n ** 2)
 68 CPU times: total: 0 ns
 69 Wall time: 0 ns
 70 
 71 [15]
 72 import random
 73 L = [random.random()  for n in range(100000)]
 74 %timeit L.sort()
 75 2.48 ms ± 223 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
 76 
 77 random.random()方法返回一个随机数,其在0至1的范围之内,以下是其具体用法:   import random   print ("随机数: ", random.random())
 78 
 79 输出结果:0.22867521257116
 80 
 81 sort() 函数是序列的内部函数,用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。
 82 
 83 把L原地排序,也就是使用后并不是返回一个有序的序列副本,而是把当前序列变得有序。
 84 
 85 2、基本形式
 86 
 87 l.sort()(l是一种列表)
 88 
 89 [16]
 90 L = [random.random()  for n in range(100000)]
 91 %time L.sort()
 92 CPU times: total: 31.2 ms
 93 Wall time: 32.9 ms
 94 
 95 [17]
 96 %time L.sort()
 97 CPU times: total: 0 ns
 98 Wall time: 2 ms
 99 
100 [18]
101 %lsmagic
102 Available line magics:
103 %alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %colors  %conda  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %matplotlib  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode
104 
105 Available cell magics:
106 %%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile
107 
108 Automagic is ON, % prefix IS NOT needed for line magics.
109 [19]
110 %run?

 

3-3 Numpy数据基础and3-4 创建numpy数组和矩阵

 

Notbook 示例

 

 

Notbook 源码

  1 [1]
  2 import numpy
  3 [2]
  4 numpy.__version__
  5 '1.21.5'
  6 [3]
  7 import numpy as np
  8 [4]
  9 np.__version__
 10 '1.21.5'
 11 python list 的特点
 12 
 13 [5]
 14 L = [ i for i in range(10)]
 15 L
 16 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 17 [6]
 18 L[5]
 19 5
 20 [7]
 21 L[5] = 100
 22 L
 23 [0, 1, 2, 3, 4, 100, 6, 7, 8, 9]
 24 [8]
 25 L[5] = 'machine learning'
 26 L
 27 [0, 1, 2, 3, 4, 'machine learning', 6, 7, 8, 9]
 28 上面单双引号均可
 29 
 30 [9]
 31 import array
 32 [10]
 33 arr = array.array('i',[ i for i in range(10)])
 34 arr
 35 array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 36 [11]
 37 arr[5]
 38 5
 39 [12]
 40 arr[5] = 100
 41 arr[5]
 42 100
 43 arr[5] = 'machine learing '报错 array.array,限定数据类型。限制了灵活性,相对速度比较高;同时array只是将存储的数据看成数组或二维数组,而数组并没有看成矩阵,也没有配备向量或矩阵相关的运算; myArray = array.array('i', [i for i range(10)])
 44 
 45 numpy.array
 46 [13]
 47 nparr = np.array([i for i in range(10)])
 48 nparr
 49 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 50 [14]
 51 nparr[5]
 52 5
 53 [15]
 54 nparr[5] = 10
 55 nparr
 56 array([ 0,  1,  2,  3,  4, 10,  6,  7,  8,  9])
 57 nparry[5] = "machine learning" 将报错
 58 
 59 [16]
 60 nparr.dtype
 61 dtype('int32')
 62 [17]
 63 nparr[5] = 5.0
 64 [18]
 65 nparr
 66 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 67 [19]
 68 nparr.dtype
 69 dtype('int32')
 70 [20]
 71 nparr[5] = 3.14
 72 nparr
 73 array([0, 1, 2, 3, 4, 3, 6, 7, 8, 9])
 74 [21]
 75 nparr2 = np.array([1, 2, 3.0])
 76 nparr2.dtype
 77 dtype('float64')
 78 [22]
 79 np.zeros(10)
 80 array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
 81 [23]
 82 np.zeros(10).dtype
 83 dtype('float64')
 84 [24]
 85 np.zeros(10,dtype = int)
 86 array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
 87 [25]
 88 np.zeros((3,5))
 89 array([[0., 0., 0., 0., 0.],
 90        [0., 0., 0., 0., 0.],
 91        [0., 0., 0., 0., 0.]])
 92 [58]
 93 np.zeros(shape = (3,5),dtype = int)
 94 array([[0, 0, 0, 0, 0],
 95        [0, 0, 0, 0, 0],
 96        [0, 0, 0, 0, 0]])
 97 [27]
 98 np.ones(10)
 99 array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
100 [28]
101 np.ones((3,5))
102 array([[1., 1., 1., 1., 1.],
103        [1., 1., 1., 1., 1.],
104        [1., 1., 1., 1., 1.]])
105 [29]
106 np.full((3,6),666)
107 array([[666, 666, 666, 666, 666, 666],
108        [666, 666, 666, 666, 666, 666],
109        [666, 666, 666, 666, 666, 666]])
110 [30]
111 np.full(shape = (3,6),fill_value = 666)
112 array([[666, 666, 666, 666, 666, 666],
113        [666, 666, 666, 666, 666, 666],
114        [666, 666, 666, 666, 666, 666]])
115 fill_value 只有一个下划线
116 
117 [31]
118 np.full(fill_value = 666,shape = (3,6))
119 array([[666, 666, 666, 666, 666, 666],
120        [666, 666, 666, 666, 666, 666],
121        [666, 666, 666, 666, 666, 666]])
122 [32]
123 np.full(fill_value = 6,shape = (10))
124 array([6, 6, 6, 6, 6, 6, 6, 6, 6, 6])
125 [33]
126 [i for i in range(0,20,2)]
127 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
128 [34]
129 np.arange(0,20,2)
130 array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
131 [35]
132 np.arange(0,1,0.2)
133 array([0. , 0.2, 0.4, 0.6, 0.8])
134 在python中range步长只能为整数
135 
136 [36]
137 np.arange(0,10)
138 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
139 [37]
140 np.arange(10)
141 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
142 linspace
143 [38]
144 np.linspace(0,20,10)
145 array([ 0.        ,  2.22222222,  4.44444444,  6.66666667,  8.88888889,
146        11.11111111, 13.33333333, 15.55555556, 17.77777778, 20.        ])
147 [39]
148 np.linspace(0,20,11)
149 array([ 0.,  2.,  4.,  6.,  8., 10., 12., 14., 16., 18., 20.])
150 random
151 [40]
152 np.random.randint(0,10)
153 6
154 [41]
155 np.random.randint(0,10,10)
156 array([7, 0, 8, 7, 7, 4, 1, 4, 4, 8])
157 前闭后开,对于上面永远娶不到10
158 
159 [42]
160 np.random.randint(0,1,10)
161 array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
162 [43]
163 np.random.randint(4,8,size = 10)
164 array([7, 6, 6, 7, 5, 6, 4, 6, 5, 7])
165 [44]
166 np.random.randint(4,8,size = (3,5))
167 array([[4, 4, 5, 7, 6],
168        [7, 7, 4, 7, 7],
169        [6, 5, 5, 6, 7]])
170 [45]
171 np.random.randint(4,8,size = (3,5))
172 array([[7, 4, 6, 6, 6],
173        [7, 4, 5, 6, 5],
174        [4, 4, 6, 6, 7]])
175 [46]
176 np.random.seed(666)
177 [47]
178 np.random.randint(4,8,size = (3,5))
179 array([[4, 6, 5, 6, 6],
180        [6, 5, 6, 4, 5],
181        [7, 6, 7, 4, 7]])
182 [48]
183 np.random.seed(666)
184 np.random.randint(4,8,size = (3,5))
185 array([[4, 6, 5, 6, 6],
186        [6, 5, 6, 4, 5],
187        [7, 6, 7, 4, 7]])
188 [49]
189 np.random.random()
190 0.2811684913927954
191 最后一个random必须加括号
192 
193 [50]
194 np.random.random(10)
195 array([0.46284169, 0.23340091, 0.76706421, 0.81995656, 0.39747625,
196        0.31644109, 0.15551206, 0.73460987, 0.73159555, 0.8578588 ])
197 [51]
198 np.random.random((3,5))
199 array([[0.76741234, 0.95323137, 0.29097383, 0.84778197, 0.3497619 ],
200        [0.92389692, 0.29489453, 0.52438061, 0.94253896, 0.07473949],
201        [0.27646251, 0.4675855 , 0.31581532, 0.39016259, 0.26832981]])
202 [52]
203 np.random.normal()
204 0.7760516793129695
205 [53]
206 np.random.normal(10,100)
207 128.06359754812632
208 均值为10,方差为100
209 
210 [54]
211 np.random.normal(0,1,(3,5))
212 array([[ 0.06102404,  1.07856138, -0.79783572,  1.1701326 ,  0.1121217 ],
213        [ 0.03185388, -0.19206285,  0.78611284, -1.69046314, -0.98873907],
214        [ 0.31398563,  0.39638567,  0.57656584, -0.07019407,  0.91250436]])
215 [59]
216 np.random.normal?
217 [60]
218 np.random?
219 [57]
220 help(np.random.normal)
221 Help on built-in function normal:
222 
223 normal(...) method of numpy.random.mtrand.RandomState instance
224     normal(loc=0.0, scale=1.0, size=None)
225     
226     Draw random samples from a normal (Gaussian) distribution.
227     
228     The probability density function of the normal distribution, first
229     derived by De Moivre and 200 years later by both Gauss and Laplace
230     independently [2]_, is often called the bell curve because of
231     its characteristic shape (see the example below).
232     
233     The normal distributions occurs often in nature.  For example, it
234     describes the commonly occurring distribution of samples influenced
235     by a large number of tiny, random disturbances, each with its own
236     unique distribution [2]_.
237     
238     .. note::
239         New code should use the ``normal`` method of a ``default_rng()``
240         instance instead; please see the :ref:`random-quick-start`.
241     
242     Parameters
243     ----------
244     loc : float or array_like of floats
245         Mean ("centre") of the distribution.
246     scale : float or array_like of floats
247         Standard deviation (spread or "width") of the distribution. Must be
248         non-negative.
249     size : int or tuple of ints, optional
250         Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
251         ``m * n * k`` samples are drawn.  If size is ``None`` (default),
252         a single value is returned if ``loc`` and ``scale`` are both scalars.
253         Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
254     
255     Returns
256     -------
257     out : ndarray or scalar
258         Drawn samples from the parameterized normal distribution.
259     
260     See Also
261     --------
262     scipy.stats.norm : probability density function, distribution or
263         cumulative density function, etc.
264     Generator.normal: which should be used for new code.
265     
266     Notes
267     -----
268     The probability density for the Gaussian distribution is
269     
270     .. math:: p(x) = \frac{1}{\sqrt{ 2 \pi \sigma^2 }}
271                      e^{ - \frac{ (x - \mu)^2 } {2 \sigma^2} },
272     
273     where :math:`\mu` is the mean and :math:`\sigma` the standard
274     deviation. The square of the standard deviation, :math:`\sigma^2`,
275     is called the variance.
276     
277     The function has its peak at the mean, and its "spread" increases with
278     the standard deviation (the function reaches 0.607 times its maximum at
279     :math:`x + \sigma` and :math:`x - \sigma` [2]_).  This implies that
280     normal is more likely to return samples lying close to the mean, rather
281     than those far away.
282     
283     References
284     ----------
285     .. [1] Wikipedia, "Normal distribution",
286            https://en.wikipedia.org/wiki/Normal_distribution
287     .. [2] P. R. Peebles Jr., "Central Limit Theorem" in "Probability,
288            Random Variables and Random Signal Principles", 4th ed., 2001,
289            pp. 51, 51, 125.
290     
291     Examples
292     --------
293     Draw samples from the distribution:
294     
295     >>> mu, sigma = 0, 0.1 # mean and standard deviation
296     >>> s = np.random.normal(mu, sigma, 1000)
297     
298     Verify the mean and the variance:
299     
300     >>> abs(mu - np.mean(s))
301     0.0  # may vary
302     
303     >>> abs(sigma - np.std(s, ddof=1))
304     0.1  # may vary
305     
306     Display the histogram of the samples, along with
307     the probability density function:
308     
309     >>> import matplotlib.pyplot as plt
310     >>> count, bins, ignored = plt.hist(s, 30, density=True)
311     >>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
312     ...                np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
313     ...          linewidth=2, color='r')
314     >>> plt.show()
315     
316     Two-by-four array of samples from N(3, 6.25):
317     
318     >>> np.random.normal(3, 2.5, size=(2, 4))
319     array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],   # random
320            [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]])  # random

 

3-5 Numpy数组的基本操作and3-6 Numpy数组的合并与分割

Notbook 示例

 

 

 

 

 

Notbook 源码

  1 [1]
  2 import numpy as np
  3 [2]
  4 x = np.arange(10)
  5 x
  6 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  7 [3]
  8 X = np.arange(15).reshape(3,5)
  9 X
 10 array([[ 0,  1,  2,  3,  4],
 11        [ 5,  6,  7,  8,  9],
 12        [10, 11, 12, 13, 14]])
 13 基本属性
 14 [4]
 15 x.ndim
 16 1
 17 [5]
 18 X.ndim
 19 2
 20 [6]
 21 x.shape
 22 (10,)
 23 [7]
 24 X.shape
 25 (3, 5)
 26 [8]
 27 x.size
 28 10
 29 [9]
 30 X.size
 31 15
 32 Numpy.array的数据访问
 33 [10]
 34 x
 35 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 36 [11]
 37 x[0]
 38 0
 39 [12]
 40 x[-1]
 41 9
 42 [13]
 43 X
 44 array([[ 0,  1,  2,  3,  4],
 45        [ 5,  6,  7,  8,  9],
 46        [10, 11, 12, 13, 14]])
 47 [14]
 48 X[0][0]
 49 0
 50 [15]
 51 X[(2,2)]
 52 12
 53 [16]
 54 X[2,2]
 55 12
 56 [17]
 57 X[2][2]
 58 12
 59 [18]
 60 X[:2][:2]
 61 array([[0, 1, 2, 3, 4],
 62        [5, 6, 7, 8, 9]])
 63 注意与切片方式区分
 64 
 65 [19]
 66 x[0:5]
 67 array([0, 1, 2, 3, 4])
 68 [20]
 69 x[:5]
 70 array([0, 1, 2, 3, 4])
 71 [21]
 72 x[5:]
 73 array([5, 6, 7, 8, 9])
 74 [22]
 75 x[::2]
 76 array([0, 2, 4, 6, 8])
 77 [23]
 78 x[::-1]
 79 array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
 80 [24]
 81 X
 82 array([[ 0,  1,  2,  3,  4],
 83        [ 5,  6,  7,  8,  9],
 84        [10, 11, 12, 13, 14]])
 85 [25]
 86 X[:2,:3]
 87 array([[0, 1, 2],
 88        [5, 6, 7]])
 89 [26]
 90 x
 91 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 92 [27]
 93 x[:2][:3]
 94 array([0, 1])
 95 [28]
 96 X[:2][:3]
 97 array([[0, 1, 2, 3, 4],
 98        [5, 6, 7, 8, 9]])
 99 [29]
100 X[:2]
101 array([[0, 1, 2, 3, 4],
102        [5, 6, 7, 8, 9]])
103 [30]
104 X[:2][:3]
105 array([[0, 1, 2, 3, 4],
106        [5, 6, 7, 8, 9]])
107 X[:2][:3]在X[:2]的基础上取值,还是按行取,欲取三个只有两个,故全部取出
108 
109 [31]
110 X
111 array([[ 0,  1,  2,  3,  4],
112        [ 5,  6,  7,  8,  9],
113        [10, 11, 12, 13, 14]])
114 [32]
115 X[:2,::2] #::2
116 array([[0, 2, 4],
117        [5, 7, 9]])
118 [33]
119 X[::-1,::-1]
120 array([[14, 13, 12, 11, 10],
121        [ 9,  8,  7,  6,  5],
122        [ 4,  3,  2,  1,  0]])
123 [34]
124 X[0]
125 array([0, 1, 2, 3, 4])
126 [35]
127 X[0,::] #对于此行及以下三行,:与::效果相同
128 array([0, 1, 2, 3, 4])
129 [36]
130 X[::,0]
131 array([ 0,  5, 10])
132 [37]
133 X[0,::].ndim
134 1
135 [38]
136 X[::,0]
137 array([ 0,  5, 10])
138 [39]
139 X[:,0].ndim
140 1
141 [40]
142 subx = X[:2,:3]
143 subx
144 array([[0, 1, 2],
145        [5, 6, 7]])
146 [41]
147 subx[0][0]=100
148 subx
149 array([[100,   1,   2],
150        [  5,   6,   7]])
151 [42]
152 X
153 array([[100,   1,   2,   3,   4],
154        [  5,   6,   7,   8,   9],
155        [ 10,  11,  12,  13,  14]])
156 相对于python 创建一个子矩阵,numpy则直接引用
157 
158 [43]
159 X[0][0] = 0
160 X
161 array([[ 0,  1,  2,  3,  4],
162        [ 5,  6,  7,  8,  9],
163        [10, 11, 12, 13, 14]])
164 [44]
165 subx
166 array([[0, 1, 2],
167        [5, 6, 7]])
168 [45]
169 subx = X[:2,:3].copy()
170 subx
171 array([[0, 1, 2],
172        [5, 6, 7]])
173 [46]
174 subx[0][0] = 100
175 subx
176 array([[100,   1,   2],
177        [  5,   6,   7]])
178 [47]
179 X
180 array([[ 0,  1,  2,  3,  4],
181        [ 5,  6,  7,  8,  9],
182        [10, 11, 12, 13, 14]])
183 reshape
184 [48]
185 x.shape
186 (10,)
187 [49]
188 x.ndim
189 1
190 [50]
191 x.reshape(2,5)
192 array([[0, 1, 2, 3, 4],
193        [5, 6, 7, 8, 9]])
194 [51]
195 x
196 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
197 [52]
198 A = x.reshape(2,5)
199 A
200 array([[0, 1, 2, 3, 4],
201        [5, 6, 7, 8, 9]])
202 [53]
203 x
204 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
205 [54]
206 B = x.reshape(1,10)
207 B
208 array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
209 [55]
210 B.ndim
211 2
212 [56]
213 B.shape
214 (1, 10)
215 [57]
216 x.shape
217 (10,)
218 [58]
219 x.reshape(10,-1)
220 array([[0],
221        [1],
222        [2],
223        [3],
224        [4],
225        [5],
226        [6],
227        [7],
228        [8],
229        [9]])
230 [59]
231 x.reshape(-1,10)
232 array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
233 [60]
234 x.reshape(2,-1)
235 array([[0, 1, 2, 3, 4],
236        [5, 6, 7, 8, 9]])
237 合并操作
238 [61]
239 x = np.array([1,2,3])
240 y = np.array([3,2,1])
241 [62]
242 x
243 array([1, 2, 3])
244 [63]
245 y
246 array([3, 2, 1])
247 [64]
248 np.concatenate([x,y])
249 array([1, 2, 3, 3, 2, 1])
250 [65]
251 z = np.array([666,666,666])
252 [66]
253 np.concatenate([x,y,z])
254 array([  1,   2,   3,   3,   2,   1, 666, 666, 666])
255 [67]
256 A = np.array([[1,2,3],[4,5,6]])
257 A
258 array([[1, 2, 3],
259        [4, 5, 6]])
260 [68]
261 np.concatenate([A,A])
262 array([[1, 2, 3],
263        [4, 5, 6],
264        [1, 2, 3],
265        [4, 5, 6]])
266 [69]
267 np.concatenate([A,A],axis = 1)
268 array([[1, 2, 3, 1, 2, 3],
269        [4, 5, 6, 4, 5, 6]])
270 [70]
271 np.concatenate([A,A],axis = 0)
272 array([[1, 2, 3],
273        [4, 5, 6],
274        [1, 2, 3],
275        [4, 5, 6]])
276 [71]
277 z
278 array([666, 666, 666])
279 [72]
280 A
281 array([[1, 2, 3],
282        [4, 5, 6]])
283 [73]
284 # np.vstack([A,z])  vstack 为操作向量提供方便
285 np.concatenate([A,z],axis = 0) 将报错,因为维度不一样
286 
287 [74]
288 np.concatenate([ A, z.reshape(1,-1)])
289 array([[  1,   2,   3],
290        [  4,   5,   6],
291        [666, 666, 666]])
292 这里的z只是一维的向量,不能直接和A进行concatenate操作, 需要先转换为1 * 3 的矩阵
293 
294 [75]
295 A
296 array([[1, 2, 3],
297        [4, 5, 6]])
298 [76]
299 A2 = np.concatenate([A,z.reshape([1,-1])])
300 A2
301 array([[  1,   2,   3],
302        [  4,   5,   6],
303        [666, 666, 666]])
304 [77]
305 np.vstack([A,z])
306 array([[  1,   2,   3],
307        [  4,   5,   6],
308        [666, 666, 666]])
309 [78]
310 B = np.full((2,2),100)
311 B
312 array([[100, 100],
313        [100, 100]])
314 [79]
315 np.hstack([A,B])
316 array([[  1,   2,   3, 100, 100],
317        [  4,   5,   6, 100, 100]])
318 分割操作
319 [80]
320 x = np.arange(10)
321 x
322 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
323 [81]
324 x1,x2,x3 = np.split(x,[3,7])
325 [82]
326 x1
327 array([0, 1, 2])
328 [83]
329 x2
330 array([3, 4, 5, 6])
331 [84]
332 x3
333 array([7, 8, 9])
334 [85]
335 x1,x2 = np.split(x,[5])
336 [86]
337 x1
338 array([0, 1, 2, 3, 4])
339 [87]
340 x2
341 array([5, 6, 7, 8, 9])
342 [116]
343 A = np.arange(16).reshape(4,4)
344 A
345 array([[ 0,  1,  2,  3],
346        [ 4,  5,  6,  7],
347        [ 8,  9, 10, 11],
348        [12, 13, 14, 15]])
349 [115]
350 A.shape
351 (2, 8)
352 [89]
353 A1,A2 = np.split(A,[2])
354 [90]
355 A1
356 array([[0, 1, 2, 3],
357        [4, 5, 6, 7]])
358 如果A1 = np.split(A,[2]),少一个A2参数则 [array([[0, 1, 2, 3], [4, 5, 6, 7]]), array([[ 8, 9, 10, 11], [12, 13, 14, 15]])]
359 
360 [91]
361 A2
362 array([[ 8,  9, 10, 11],
363        [12, 13, 14, 15]])
364 [92]
365 A1,A2 = np.split(A,[2],axis = 1)
366 [93]
367 A1
368 array([[ 0,  1],
369        [ 4,  5],
370        [ 8,  9],
371        [12, 13]])
372 [94]
373 A2
374 array([[ 2,  3],
375        [ 6,  7],
376        [10, 11],
377        [14, 15]])
378 [95]
379 A
380 array([[ 0,  1,  2,  3],
381        [ 4,  5,  6,  7],
382        [ 8,  9, 10, 11],
383        [12, 13, 14, 15]])
384 [96]
385 upper,lower = np.vsplit(A,[2])
386 [97]
387 upper
388 array([[0, 1, 2, 3],
389        [4, 5, 6, 7]])
390 [98]
391 lower
392 array([[ 8,  9, 10, 11],
393        [12, 13, 14, 15]])
394 [99]
395 left,right = np.hsplit(A,[2])
396 [100]
397 left
398 array([[ 0,  1],
399        [ 4,  5],
400        [ 8,  9],
401        [12, 13]])
402 [101]
403 right
404 array([[ 2,  3],
405        [ 6,  7],
406        [10, 11],
407        [14, 15]])
408 [102]
409 data = np.arange(16).reshape(4,4)
410 data
411 array([[ 0,  1,  2,  3],
412        [ 4,  5,  6,  7],
413        [ 8,  9, 10, 11],
414        [12, 13, 14, 15]])
415 [103]
416 x,y = np.hsplit(data,[-1])
417 [104]
418 x
419 array([[ 0,  1,  2],
420        [ 4,  5,  6],
421        [ 8,  9, 10],
422        [12, 13, 14]])
423 [105]
424 y
425 array([[ 3],
426        [ 7],
427        [11],
428        [15]])
429 [106]
430 y[:,0]
431 array([ 3,  7, 11, 15])

 

3-7 Numpy中的矩阵运算

Notbook 示例

 

 

Notbook 源码

  1 [1]
  2 n = 10
  3 L = [ i for i in range(n)]
  4 [2]
  5 2 * L
  6 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  7 [3]
  8 A = []
  9 for e in L:
 10     A.append(2*e)
 11 A
 12 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
 13 [4]
 14 n = 1000000
 15 L = [ i for i in range(n)]
 16 [5]
 17 %%time
 18 A = []
 19 for e in L:
 20     A.append(2*e)
 21 CPU times: total: 375 ms
 22 Wall time: 449 ms
 23 
 24 [6]
 25 %%time
 26 A = [e*2 for e in L]
 27 CPU times: total: 172 ms
 28 Wall time: 209 ms
 29 
 30 [7]
 31 import numpy as np
 32 L = np.arange(n)
 33 %%time A = [e*2 for e in L] 把numpy L = np.arange(n) CPU times: total: 453 ms Wall time: 475 ms %%time百分号与time之间不能有空格
 34 
 35 [8]
 36 %%time
 37 A = np.array(2*e for e in L)
 38 CPU times: total: 31.2 ms
 39 Wall time: 22.9 ms
 40 
 41 %%time百分号与time之间不能有空格
 42 
 43 [9]
 44 %time
 45 A = 2*L
 46 CPU times: total: 0 ns
 47 Wall time: 0 ns
 48 
 49 [10]
 50 A
 51 array([      0,       2,       4, ..., 1999994, 1999996, 1999998])
 52 [11]
 53 n = 10
 54 L = np.arange(n)
 55 2*L
 56 array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
 57 universal function
 58 [12]
 59 x = np.arange(1,16).reshape(3,5)
 60 x
 61 array([[ 1,  2,  3,  4,  5],
 62        [ 6,  7,  8,  9, 10],
 63        [11, 12, 13, 14, 15]])
 64 [13]
 65 x+1
 66 array([[ 2,  3,  4,  5,  6],
 67        [ 7,  8,  9, 10, 11],
 68        [12, 13, 14, 15, 16]])
 69 [14]
 70 x-1
 71 array([[ 0,  1,  2,  3,  4],
 72        [ 5,  6,  7,  8,  9],
 73        [10, 11, 12, 13, 14]])
 74 [15]
 75 x*2
 76 array([[ 2,  4,  6,  8, 10],
 77        [12, 14, 16, 18, 20],
 78        [22, 24, 26, 28, 30]])
 79 [16]
 80 x/2
 81 array([[0.5, 1. , 1.5, 2. , 2.5],
 82        [3. , 3.5, 4. , 4.5, 5. ],
 83        [5.5, 6. , 6.5, 7. , 7.5]])
 84 [17]
 85 x//2
 86 array([[0, 1, 1, 2, 2],
 87        [3, 3, 4, 4, 5],
 88        [5, 6, 6, 7, 7]], dtype=int32)
 89 [18]
 90 x ** 2
 91 array([[  1,   4,   9,  16,  25],
 92        [ 36,  49,  64,  81, 100],
 93        [121, 144, 169, 196, 225]], dtype=int32)
 94 [19]
 95 x % 2 
 96 array([[1, 0, 1, 0, 1],
 97        [0, 1, 0, 1, 0],
 98        [1, 0, 1, 0, 1]], dtype=int32)
 99 [20]
100 1 / x
101 array([[1.        , 0.5       , 0.33333333, 0.25      , 0.2       ],
102        [0.16666667, 0.14285714, 0.125     , 0.11111111, 0.1       ],
103        [0.09090909, 0.08333333, 0.07692308, 0.07142857, 0.06666667]])
104 [21]
105 np.abs(x)
106 array([[ 1,  2,  3,  4,  5],
107        [ 6,  7,  8,  9, 10],
108        [11, 12, 13, 14, 15]])
109 [22]
110 np.sin(x)
111 array([[ 0.84147098,  0.90929743,  0.14112001, -0.7568025 , -0.95892427],
112        [-0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849, -0.54402111],
113        [-0.99999021, -0.53657292,  0.42016704,  0.99060736,  0.65028784]])
114 [23]
115 np.cos(x)
116 array([[ 0.54030231, -0.41614684, -0.9899925 , -0.65364362,  0.28366219],
117        [ 0.96017029,  0.75390225, -0.14550003, -0.91113026, -0.83907153],
118        [ 0.0044257 ,  0.84385396,  0.90744678,  0.13673722, -0.75968791]])
119 [24]
120 np.tan(x)
121 array([[ 1.55740772e+00, -2.18503986e+00, -1.42546543e-01,
122          1.15782128e+00, -3.38051501e+00],
123        [-2.91006191e-01,  8.71447983e-01, -6.79971146e+00,
124         -4.52315659e-01,  6.48360827e-01],
125        [-2.25950846e+02, -6.35859929e-01,  4.63021133e-01,
126          7.24460662e+00, -8.55993401e-01]])
127 [25]
128 np.exp(x)
129 array([[2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01,
130         1.48413159e+02],
131        [4.03428793e+02, 1.09663316e+03, 2.98095799e+03, 8.10308393e+03,
132         2.20264658e+04],
133        [5.98741417e+04, 1.62754791e+05, 4.42413392e+05, 1.20260428e+06,
134         3.26901737e+06]])
135 e的x次方
136 
137 [26]
138 np.power(3,x)
139 array([[       3,        9,       27,       81,      243],
140        [     729,     2187,     6561,    19683,    59049],
141        [  177147,   531441,  1594323,  4782969, 14348907]], dtype=int32)
142 [27]
143 3 ** x
144 array([[       3,        9,       27,       81,      243],
145        [     729,     2187,     6561,    19683,    59049],
146        [  177147,   531441,  1594323,  4782969, 14348907]], dtype=int32)
147 [28]
148 np.log(x)
149 array([[0.        , 0.69314718, 1.09861229, 1.38629436, 1.60943791],
150        [1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509],
151        [2.39789527, 2.48490665, 2.56494936, 2.63905733, 2.7080502 ]])
152 直接调用log相对于以e为底
153 
154 [29]
155 np.log2(x)
156 array([[0.        , 1.        , 1.5849625 , 2.        , 2.32192809],
157        [2.5849625 , 2.80735492, 3.        , 3.169925  , 3.32192809],
158        [3.45943162, 3.5849625 , 3.70043972, 3.80735492, 3.9068906 ]])
159 [30]
160 np.log10(x)
161 array([[0.        , 0.30103   , 0.47712125, 0.60205999, 0.69897   ],
162        [0.77815125, 0.84509804, 0.90308999, 0.95424251, 1.        ],
163        [1.04139269, 1.07918125, 1.11394335, 1.14612804, 1.17609126]])
164 矩阵运算
165 [31]
166 A = np.arange(4).reshape(2,2)
167 A
168 array([[0, 1],
169        [2, 3]])
170 如果 A = np.arange(2,2) A 则有 array([], dtype=int32)
171 
172 [32]
173 B = np.full((2,2),10)
174 B
175 array([[10, 10],
176        [10, 10]])
177 [33]
178 A + B
179 array([[10, 11],
180        [12, 13]])
181 [34]
182 A - B
183 array([[-10,  -9],
184        [ -8,  -7]])
185 [35]
186 A * B
187 array([[ 0, 10],
188        [20, 30]])
189 [36]
190 A / B
191 array([[0. , 0.1],
192        [0.2, 0.3]])
193 [37]
194 A.dot(B)
195 array([[10, 10],
196        [50, 50]])
197 [38]
198 A
199 array([[0, 1],
200        [2, 3]])
201 [39]
202 A.T
203 array([[0, 2],
204        [1, 3]])
205 C = np.full((3,3),666) A + C A.dot(C) 对于上代码后两个等式将不能运行,需要符合矩阵运算规则
206 
207 [40]
208 V = np.array([1,2])
209 V
210 array([1, 2])
211 V = np.array((1,2)) V 运行结果相同
212 
213 [41]
214 A
215 array([[0, 1],
216        [2, 3]])
217 [42]
218 V + A
219 array([[1, 3],
220        [3, 5]])
221 若A = np.arange(6).reshape(3,2) A array([[0, 1], [2, 3], [4, 5]]) 则np.vstack([v] * A.shape[0]) array([[1, 2], [1, 2], [1, 2]])
222 
223 [43]
224 np.vstack([V] * A.shape[0])
225 array([[1, 2],
226        [1, 2]])
227 A.shape[0]表示以A的基本单元为基础的高度
228 
229 [44]
230 np.vstack([V] * A.shape[0])+A
231 array([[1, 3],
232        [3, 5]])
233 [45]
234 V
235 array([1, 2])
236 [46]
237 np.tile(V,(2,1)) + A # 横向重复两次,纵向重复一次
238 array([[1, 3],
239        [3, 5]])
240 [47]
241 V
242 array([1, 2])
243 [48]
244 A
245 array([[0, 1],
246        [2, 3]])
247 [49]
248 V * A
249 array([[0, 2],
250        [2, 6]])
251 [50]
252 V.dot(A)
253 array([4, 7])
254 [51]
255 A.dot(V)
256 array([2, 8])
257 对于一个矩阵和一个向量的乘法,numpy会自动将向量转成合适的形式
258 
259 矩阵的逆
260 [52]
261 np.linalg.inv(A)
262 array([[-1.5,  0.5],
263        [ 1. ,  0. ]])
264 [53]
265 invA = np.linalg.inv(A)
266 [54]
267 A.dot(invA)
268 array([[1., 0.],
269        [0., 1.]])
270 [55]
271 invA.dot(A)
272 array([[1., 0.],
273        [0., 1.]])
274 [56]
275 X = np.arange(16).reshape(2,8)
276 X
277 array([[ 0,  1,  2,  3,  4,  5,  6,  7],
278        [ 8,  9, 10, 11, 12, 13, 14, 15]])
279 np.linalg.inv(A)将报错
280 
281 [57]
282 pinvX  = np.linalg.pinv(X)
283 [58]
284 pinvX
285 array([[-1.35416667e-01,  5.20833333e-02],
286        [-1.01190476e-01,  4.16666667e-02],
287        [-6.69642857e-02,  3.12500000e-02],
288        [-3.27380952e-02,  2.08333333e-02],
289        [ 1.48809524e-03,  1.04166667e-02],
290        [ 3.57142857e-02, -3.46944695e-18],
291        [ 6.99404762e-02, -1.04166667e-02],
292        [ 1.04166667e-01, -2.08333333e-02]])
293 [59]
294 pinvX.shape
295 (8, 2)
296 [60]
297 X.dot(pinvX)
298 array([[ 1.00000000e+00, -1.94289029e-16],
299        [ 6.66133815e-16,  1.00000000e+00]])

 

 

3-8 Numpy中的聚合运算and3-9 Numpy中的arg运算

Notbook 示例

 

 

Notbook 源码

 

  1 [1]
  2 import numpy as np
  3 L = np.random.random(100)
  4 [2]
  5 L
  6 array([0.3516844 , 0.65757053, 0.92102817, 0.08232176, 0.29381881,
  7        0.65003014, 0.97971106, 0.5226598 , 0.30206059, 0.08943851,
  8        0.18012353, 0.73187738, 0.21161704, 0.99245898, 0.72893738,
  9        0.81803006, 0.46057643, 0.78305203, 0.21144964, 0.36252883,
 10        0.35272908, 0.25604162, 0.28917731, 0.67740027, 0.90426583,
 11        0.84544952, 0.40110828, 0.04247786, 0.01085229, 0.34014734,
 12        0.70602233, 0.51424636, 0.39786138, 0.36595197, 0.07393888,
 13        0.75163777, 0.45510184, 0.08657692, 0.67168446, 0.88948244,
 14        0.2225157 , 0.2878433 , 0.49303834, 0.81036448, 0.01278707,
 15        0.10601227, 0.89256786, 0.44614657, 0.43688635, 0.79290611,
 16        0.74071001, 0.88761407, 0.28543688, 0.29216674, 0.11519011,
 17        0.73617155, 0.3164547 , 0.37765957, 0.02654595, 0.54816316,
 18        0.81750059, 0.63684799, 0.27424713, 0.1936451 , 0.06800676,
 19        0.8512259 , 0.9296003 , 0.32355494, 0.85864702, 0.59670948,
 20        0.34124613, 0.31258873, 0.64220369, 0.14140289, 0.61121421,
 21        0.05605693, 0.79119813, 0.27604157, 0.74071167, 0.81422001,
 22        0.68846038, 0.56658745, 0.75986556, 0.84020345, 0.93567278,
 23        0.20082763, 0.77921029, 0.22234662, 0.63349144, 0.91545638,
 24        0.44481323, 0.89917595, 0.37389024, 0.68454439, 0.13811925,
 25        0.62240377, 0.96188648, 0.1028726 , 0.11962078, 0.91129131])
 26 [3]
 27 sum    (L)
 28 50.26791878652218
 29 [4]
 30 np.sum(L)
 31 50.267918786522195
 32 [5]
 33 big_array = np.random.rand(1000000)
 34 %timeit sum(big_array)
 35 %timeit np.sum(big_array)
 36 151 ms ± 2.28 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
 37 1.96 ms ± 239 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
 38 
 39 [59]
 40 big_array
 41 array([0.02665502, 0.79968067, 0.15228547, ..., 0.26373403, 0.7936674 ,
 42        0.82165722])
 43 [6]
 44 np.min(big_array)
 45 2.3131487036920362e-07
 46 [7]
 47 np.max(big_array)
 48 0.9999998477089909
 49 [8]
 50 big_array.min()
 51 2.3131487036920362e-07
 52 [9]
 53 big_array.max()
 54 0.9999998477089909
 55 [10]
 56 big_array.sum()
 57 499562.4579554482
 58 [11]
 59 X = np.arange(16).reshape(4,-1)
 60 X
 61 array([[ 0,  1,  2,  3],
 62        [ 4,  5,  6,  7],
 63        [ 8,  9, 10, 11],
 64        [12, 13, 14, 15]])
 65 [12]
 66 np.sum(X)
 67 120
 68 [13]
 69 np.sum(X,axis = 0)
 70 array([24, 28, 32, 36])
 71 [14]
 72 np.sum(X,axis = 1)
 73 array([ 6, 22, 38, 54])
 74 [15]
 75 np.prod(X) # 返回给定维度上各个元素的乘积
 76 0
 77 [16]
 78 np.prod(X + 1)
 79 2004189184
 80 [17]
 81 np.mean(X)
 82 7.5
 83 [18]
 84 np.median(X)
 85 7.5
 86 [19]
 87 v = np.array([1,1,2,2,10])
 88 np.mean(v)
 89 3.2
 90 [20]
 91 np.median(v)
 92 2.0
 93 [21]
 94 np.percentile(big_array,q = 50) # 有50%小于它的数
 95 0.49877343789608786
 96 [22]
 97 np.median(big_array)
 98 0.49877343789608786
 99 [23]
100 np.percentile(big_array,q = 100)
101 0.9999998477089909
102 [24]
103 np.max(big_array)
104 0.9999998477089909
105 [25]
106 for percent in [0,25,50,75,100]:
107     print(np.percentile(big_array,q = percent))
108 2.3131487036920362e-07
109 0.24949869046558878
110 0.49877343789608786
111 0.7495843236030313
112 0.9999998477089909
113 
114 [26]
115 np.var(big_array)# 方差
116 0.08337201925355782
117 [27]
118 np.std(big_array)
119 0.28874213279941985
120 [28]
121 x = np.random.normal(0,1,size = 1000000)
122 [29]
123 np.mean(x)
124 -0.0014305613307867217
125 [30]
126 np.std(x)
127 1.00021235447686
128 索引
129 [31]
130 np.min(x)
131 -4.691580943262158
132 [32]
133 np.argmin(x)
134 858651
135 [33]
136 x[989892]
137 -0.6379807273320142
138 [34]
139 np.argmax(x)
140 721252
141 [35]
142 x[215038]
143 -2.267033547724769
144 [36]
145 np.max(x)
146 4.710557431454966
147 排序和使用索引
148 [37]
149 x = np.arange(16)
150 x
151 array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
152 [38]
153 np.random.shuffle(x)
154 x
155 array([ 1, 13,  0,  2, 15,  7, 10,  5,  3, 11,  4, 12,  6,  9,  8, 14])
156 [39]
157 np.sort(x)
158 array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
159 [40]
160 x
161 array([ 1, 13,  0,  2, 15,  7, 10,  5,  3, 11,  4, 12,  6,  9,  8, 14])
162 [41]
163 x.sort()
164 [42]
165 x
166 array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
167 [43]
168 X = np.random.randint(10,size = (4,4))
169 X
170 array([[0, 0, 2, 9],
171        [0, 2, 3, 2],
172        [9, 1, 3, 4],
173        [6, 8, 0, 2]])
174 [44]
175 np.sort(X)
176 array([[0, 0, 2, 9],
177        [0, 2, 2, 3],
178        [1, 3, 4, 9],
179        [0, 2, 6, 8]])
180 [45]
181 np.sort(X,axis = 1)
182 array([[0, 0, 2, 9],
183        [0, 2, 2, 3],
184        [1, 3, 4, 9],
185        [0, 2, 6, 8]])
186 [46]
187 np.sort(X,axis = 0 )
188 array([[0, 0, 0, 2],
189        [0, 1, 2, 2],
190        [6, 2, 3, 4],
191        [9, 8, 3, 9]])
192 [47]
193 x
194 array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
195 [48]
196 np.random.shuffle(x)
197 [49]
198 x
199 array([12, 14,  5, 11,  4, 10,  7,  3,  2,  0, 15, 13,  6,  9,  1,  8])
200 [50]
201 np.argsort(x)
202 array([ 9, 14,  8,  7,  4,  2, 12,  6, 15, 13,  5,  3,  0, 11,  1, 10],
203       dtype=int64)
204 [51]
205 np.partition(x,3)
206 array([ 0,  1,  2,  3,  4,  6,  7,  5,  8, 14, 15, 13, 10,  9, 11, 12])
207 [52]
208 np.argpartition(x,3)
209 array([ 9, 14,  8,  7,  4, 12,  6,  2, 15,  1, 10, 11,  5, 13,  3,  0],
210       dtype=int64)
211 [53]
212 X
213 array([[0, 0, 2, 9],
214        [0, 2, 3, 2],
215        [9, 1, 3, 4],
216        [6, 8, 0, 2]])
217 [54]
218 np.argsort(X,axis = 1)
219 array([[0, 1, 2, 3],
220        [0, 1, 3, 2],
221        [1, 2, 3, 0],
222        [2, 3, 0, 1]], dtype=int64)
223 [55]
224 np.argsort(X,axis = 0)
225 array([[0, 0, 3, 1],
226        [1, 2, 0, 3],
227        [3, 1, 1, 2],
228        [2, 3, 2, 0]], dtype=int64)
229 [56]
230 np.argsort(X)
231 array([[0, 1, 2, 3],
232        [0, 1, 3, 2],
233        [1, 2, 3, 0],
234        [2, 3, 0, 1]], dtype=int64)
235 [61]
236 np.partition(X,2)
237 array([[0, 0, 2, 9],
238        [0, 2, 2, 3],
239        [1, 3, 4, 9],
240        [0, 2, 6, 8]])
241 [57]
242 np.argpartition(X,2,axis = 1)
243 array([[0, 1, 2, 3],
244        [0, 1, 3, 2],
245        [1, 2, 3, 0],
246        [2, 3, 0, 1]], dtype=int64)
247 [58]
248 np.argpartition(X,2,axis = 0)
249 array([[0, 0, 3, 1],
250        [1, 2, 0, 3],
251        [3, 1, 2, 2],
252        [2, 3, 1, 0]], dtype=int64)

 

 3-10 Numpy中的比较和FancyIndexing

 Notbook 示例

 

 

Notbook 源码

  1 fancy indexing
  2 [1]
  3 import numpy as np
  4 
  5 x = np.arange(16)
  6 x
  7 array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
  8 [2]
  9 x[3]
 10 3
 11 [3]
 12 x[3:9]
 13 array([3, 4, 5, 6, 7, 8])
 14 [4]
 15 x[3:9:2]
 16 array([3, 5, 7])
 17 [5]
 18 [x[3],x[5],x[8]]
 19 [3, 5, 8]
 20 [6]
 21 ind = [3,5,8]
 22 [7]
 23 x[ind]
 24 array([3, 5, 8])
 25 用inw也行,只要对上就OK
 26 
 27 [8]
 28 ind = np.array([[0,2],
 29                [1,3]])
 30 x[ind]
 31 array([[0, 2],
 32        [1, 3]])
 33 [9]
 34 X = x.reshape(4,-1)
 35 X
 36 array([[ 0,  1,  2,  3],
 37        [ 4,  5,  6,  7],
 38        [ 8,  9, 10, 11],
 39        [12, 13, 14, 15]])
 40 [10]
 41 row = np.array([0,1,2])
 42 col = np.array([1,2,3])
 43 X[row,col]
 44 array([ 1,  6, 11])
 45 [11]
 46 X[0,col]
 47 array([1, 2, 3])
 48 [12]
 49 X     [:2,col]
 50 array([[1, 2, 3],
 51        [5, 6, 7]])
 52 [13]
 53 col = [True ,False,True ,True]
 54 [43]
 55 X = x.reshape(4,-1)
 56 [46]
 57 X[1:3,col]
 58 array([[ 4,  6,  7],
 59        [ 8, 10, 11]])
 60 numpy array的比较
 61 [15]
 62 x
 63 array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
 64 [16]
 65 x < 3
 66 array([ True,  True,  True, False, False, False, False, False, False,
 67        False, False, False, False, False, False, False])
 68 [17]
 69 x > 3
 70 array([False, False, False, False,  True,  True,  True,  True,  True,
 71         True,  True,  True,  True,  True,  True,  True])
 72 [18]
 73 x <= 3
 74 array([ True,  True,  True,  True, False, False, False, False, False,
 75        False, False, False, False, False, False, False])
 76 [19]
 77 x >= 3
 78 array([False, False, False,  True,  True,  True,  True,  True,  True,
 79         True,  True,  True,  True,  True,  True,  True])
 80 [20]
 81 x == 3 
 82 array([False, False, False,  True, False, False, False, False, False,
 83        False, False, False, False, False, False, False])
 84 [21]
 85 x != 3
 86 array([ True,  True,  True, False,  True,  True,  True,  True,  True,
 87         True,  True,  True,  True,  True,  True,  True])
 88 [22]
 89 2 * x == 24 - 4*x
 90 array([False, False, False, False,  True, False, False, False, False,
 91        False, False, False, False, False, False, False])
 92 [23]
 93 X
 94 array([[ 0,  1,  2,  3],
 95        [ 4,  5,  6,  7],
 96        [ 8,  9, 10, 11],
 97        [12, 13, 14, 15]])
 98 [24]
 99 X < 6
100 array([[ True,  True,  True,  True],
101        [ True,  True, False, False],
102        [False, False, False, False],
103        [False, False, False, False]])
104 [25]
105 x
106 array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
107 [26]
108 np.sum(x <= 3)
109 4
110 [27]
111 np.count_nonzero(x <= 3)
112 4
113 [28]
114 np.any(x == 0) #只要有一个元素满足条件则True
115 True
116 [29]
117 np.any( x<0 )
118 False
119 [30]
120 np.all( x >= 0 )
121 True
122 [31]
123 np.all( x > 0)
124 False
125 [32]
126 X
127 array([[ 0,  1,  2,  3],
128        [ 4,  5,  6,  7],
129        [ 8,  9, 10, 11],
130        [12, 13, 14, 15]])
131 [33]
132 np.sum( X % 2 == 0)
133 8
134 [34]
135 np.sum( X % 2 == 0 , axis = 1)
136 array([2, 2, 2, 2])
137 [35]
138 np.sum( X % 2 == 0 , axis = 0)
139 array([4, 0, 4, 0])
140 [36]
141 np.all(X > 0 ,axis = 1)
142 array([False,  True,  True,  True])
143 [37]
144 np.sum((x > 3) & (x < 10)) #用&&则报错
145 6
146 [38]
147 np.sum((x%2==0) | (x>10))
148 11
149 [39]
150 np.sum(~(x==0))
151 15
152 [40]
153 x[x<5]
154 array([0, 1, 2, 3, 4])
155 [41]
156 x[x%2==0]
157 array([ 0,  2,  4,  6,  8, 10, 12, 14])
158 [47]
159 X[ X[:,3] % 3 == 0,:]
160 # 布尔值决定取舍
161 #即,X [[1,0,0,1],:]
162 array([[ 0,  1,  2,  3],
163        [12, 13, 14, 15]])

 

3-11 Matplotlib数据可视化基础

Notbook 示例

 

 

 

 

Notbook 源码

  1 [1]
  2  #import matplotlib as mpl
  3 [2]
  4 import matplotlib.pyplot as plt
  5 [3]
  6 import numpy as np
  7 [4]
  8 x = np.linspace(0,10,100)
  9 [5]
 10 x
 11 array([ 0.        ,  0.1010101 ,  0.2020202 ,  0.3030303 ,  0.4040404 ,
 12         0.50505051,  0.60606061,  0.70707071,  0.80808081,  0.90909091,
 13         1.01010101,  1.11111111,  1.21212121,  1.31313131,  1.41414141,
 14         1.51515152,  1.61616162,  1.71717172,  1.81818182,  1.91919192,
 15         2.02020202,  2.12121212,  2.22222222,  2.32323232,  2.42424242,
 16         2.52525253,  2.62626263,  2.72727273,  2.82828283,  2.92929293,
 17         3.03030303,  3.13131313,  3.23232323,  3.33333333,  3.43434343,
 18         3.53535354,  3.63636364,  3.73737374,  3.83838384,  3.93939394,
 19         4.04040404,  4.14141414,  4.24242424,  4.34343434,  4.44444444,
 20         4.54545455,  4.64646465,  4.74747475,  4.84848485,  4.94949495,
 21         5.05050505,  5.15151515,  5.25252525,  5.35353535,  5.45454545,
 22         5.55555556,  5.65656566,  5.75757576,  5.85858586,  5.95959596,
 23         6.06060606,  6.16161616,  6.26262626,  6.36363636,  6.46464646,
 24         6.56565657,  6.66666667,  6.76767677,  6.86868687,  6.96969697,
 25         7.07070707,  7.17171717,  7.27272727,  7.37373737,  7.47474747,
 26         7.57575758,  7.67676768,  7.77777778,  7.87878788,  7.97979798,
 27         8.08080808,  8.18181818,  8.28282828,  8.38383838,  8.48484848,
 28         8.58585859,  8.68686869,  8.78787879,  8.88888889,  8.98989899,
 29         9.09090909,  9.19191919,  9.29292929,  9.39393939,  9.49494949,
 30         9.5959596 ,  9.6969697 ,  9.7979798 ,  9.8989899 , 10.        ])
 31 [6]
 32 y = np.sin(x)
 33 [7]
 34 y
 35 array([ 0.        ,  0.10083842,  0.20064886,  0.2984138 ,  0.39313661,
 36         0.48385164,  0.56963411,  0.64960951,  0.72296256,  0.78894546,
 37         0.84688556,  0.8961922 ,  0.93636273,  0.96698762,  0.98775469,
 38         0.99845223,  0.99897117,  0.98930624,  0.96955595,  0.93992165,
 39         0.90070545,  0.85230712,  0.79522006,  0.73002623,  0.65739025,
 40         0.57805259,  0.49282204,  0.40256749,  0.30820902,  0.21070855,
 41         0.11106004,  0.01027934, -0.09060615, -0.19056796, -0.28858706,
 42        -0.38366419, -0.47483011, -0.56115544, -0.64176014, -0.7158225 ,
 43        -0.7825875 , -0.84137452, -0.89158426, -0.93270486, -0.96431712,
 44        -0.98609877, -0.99782778, -0.99938456, -0.99075324, -0.97202182,
 45        -0.94338126, -0.90512352, -0.85763861, -0.80141062, -0.73701276,
 46        -0.66510151, -0.58640998, -0.50174037, -0.41195583, -0.31797166,
 47        -0.22074597, -0.12126992, -0.0205576 ,  0.0803643 ,  0.18046693,
 48         0.27872982,  0.37415123,  0.46575841,  0.55261747,  0.63384295,
 49         0.7086068 ,  0.77614685,  0.83577457,  0.8868821 ,  0.92894843,
 50         0.96154471,  0.98433866,  0.99709789,  0.99969234,  0.99209556,
 51         0.97438499,  0.94674118,  0.90944594,  0.86287948,  0.8075165 ,
 52         0.74392141,  0.6727425 ,  0.59470541,  0.51060568,  0.42130064,
 53         0.32770071,  0.23076008,  0.13146699,  0.03083368, -0.07011396,
 54        -0.17034683, -0.26884313, -0.36459873, -0.45663749, -0.54402111])
 55 [8]
 56 plt.plot(x,y)# 不用plt.show()直接出图
 57 [<matplotlib.lines.Line2D at 0x1b74c435760>]
 58 
 59 [9]
 60 cosy = np.cos(x)
 61 [10]
 62 cosy.shape
 63 (100,)
 64 [11]
 65 siny = y.copy()
 66 [12]
 67 plt.plot(x,siny)
 68 plt.plot(x,cosy)
 69 [<matplotlib.lines.Line2D at 0x1b74c539b80>]
 70 
 71 [13]
 72 plt.plot(x,siny)
 73 plt.plot(x,cosy,color = 'red')
 74 [<matplotlib.lines.Line2D at 0x1b74c5b2700>]
 75 
 76 [14]
 77 plt.plot(x,siny)
 78 plt.plot(x,cosy,color = 'red',linestyle = '--')
 79 [<matplotlib.lines.Line2D at 0x1b74c61adf0>]
 80 
 81 [15]
 82 plt.plot(x,siny)
 83 plt.plot(x,cosy,color = 'red',linestyle = '--')
 84 plt.xlim(-5,15)
 85 (-5.0, 15.0)
 86 
 87 [16]
 88 plt.plot(x,siny)
 89 plt.plot(x,cosy,color = 'red',linestyle = '--')
 90 plt.xlim(-5,15)
 91 plt.ylim(0,1.5)
 92 (0.0, 1.5)
 93 
 94 [17]
 95 plt.plot(x,siny)
 96 plt.plot(x,cosy,color = 'red',linestyle = '--')
 97 plt.axis([-1,11,-2,2])
 98 (-1.0, 11.0, -2.0, 2.0)
 99 
100 [18]
101 plt.plot(x,siny)
102 plt.plot(x,cosy,color = 'red',linestyle = '--')
103 plt.xlabel("x label")
104 plt.ylabel('y value')
105 Text(0, 0.5, 'y value')
106 
107 [19]
108 plt.plot(x,siny,label= 'sin(x)')
109 plt.plot(x,cosy,color = 'red',linestyle = '--',label= 'cos(x)')
110 plt.xlabel("x label")
111 plt.ylabel('y value')
112 plt.legend()   # 无sin(x)与cos(x)下标,legend(),需要加括号
113 <matplotlib.legend.Legend at 0x1b74d815bb0>
114 
115 [20]
116 plt.plot(x,siny,label= 'sin(x)')
117 plt.plot(x,cosy,color = 'red',linestyle = '--',label= 'cos(x)')
118 plt.xlabel("x label")
119 plt.ylabel('y value')
120 plt.legend()
121 plt.title("welcom to ML world")
122 Text(0.5, 1.0, 'welcom to ML world')
123 
124 scatter plot
125 [21]
126 plt.scatter(x,siny)
127 <matplotlib.collections.PathCollection at 0x1b74d9064f0>
128 
129 [22]
130 plt.scatter(x,siny)
131 plt.scatter(x,cosy,color = "red")
132 <matplotlib.collections.PathCollection at 0x1b74c764a60>
133 
134 [23]
135 x = np.random.normal(0,1,10000)
136 y = np.random.normal(0,1,10000)
137 plt.scatter(x,y,alpha = 0.5)
138 <matplotlib.collections.PathCollection at 0x1b74c6eac10>

 

3-12 数据加载和简单的数据探索

Notbook 示例

 

 

 

 

Notbook 源码

  1 1]
  2 import numpy as np
  3 [2]
  4 # import matplotlib as mpl
  5 import matplotlib.pyplot as plt
  6 [3]
  7 from sklearn import datasets
  8 [4]
  9 iris = datasets.load_iris()
 10 [5]
 11 iris.keys()
 12 dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename', 'data_module'])
 13 [6]
 14 print(iris.DESCR)
 15 .. _iris_dataset:
 16 
 17 Iris plants dataset
 18 --------------------
 19 
 20 **Data Set Characteristics:**
 21 
 22     :Number of Instances: 150 (50 in each of three classes)
 23     :Number of Attributes: 4 numeric, predictive attributes and the class
 24     :Attribute Information:
 25         - sepal length in cm
 26         - sepal width in cm
 27         - petal length in cm
 28         - petal width in cm
 29         - class:
 30                 - Iris-Setosa
 31                 - Iris-Versicolour
 32                 - Iris-Virginica
 33                 
 34     :Summary Statistics:
 35 
 36     ============== ==== ==== ======= ===== ====================
 37                     Min  Max   Mean    SD   Class Correlation
 38     ============== ==== ==== ======= ===== ====================
 39     sepal length:   4.3  7.9   5.84   0.83    0.7826
 40     sepal width:    2.0  4.4   3.05   0.43   -0.4194
 41     petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)
 42     petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)
 43     ============== ==== ==== ======= ===== ====================
 44 
 45     :Missing Attribute Values: None
 46     :Class Distribution: 33.3% for each of 3 classes.
 47     :Creator: R.A. Fisher
 48     :Donor: Michael Marshall (MARSHALL%[email protected])
 49     :Date: July, 1988
 50 
 51 The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken
 52 from Fisher's paper. Note that it's the same as in R, but not as in the UCI
 53 Machine Learning Repository, which has two wrong data points.
 54 
 55 This is perhaps the best known database to be found in the
 56 pattern recognition literature.  Fisher's paper is a classic in the field and
 57 is referenced frequently to this day.  (See Duda & Hart, for example.)  The
 58 data set contains 3 classes of 50 instances each, where each class refers to a
 59 type of iris plant.  One class is linearly separable from the other 2; the
 60 latter are NOT linearly separable from each other.
 61 
 62 .. topic:: References
 63 
 64    - Fisher, R.A. "The use of multiple measurements in taxonomic problems"
 65      Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
 66      Mathematical Statistics" (John Wiley, NY, 1950).
 67    - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.
 68      (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.
 69    - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
 70      Structure and Classification Rule for Recognition in Partially Exposed
 71      Environments".  IEEE Transactions on Pattern Analysis and Machine
 72      Intelligence, Vol. PAMI-2, No. 1, 67-71.
 73    - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions
 74      on Information Theory, May 1972, 431-433.
 75    - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II
 76      conceptual clustering system finds 3 classes in the data.
 77    - Many, many more ...
 78 
 79 [7]
 80 iris.data
 81 array([[5.1, 3.5, 1.4, 0.2],
 82        [4.9, 3. , 1.4, 0.2],
 83        [4.7, 3.2, 1.3, 0.2],
 84        [4.6, 3.1, 1.5, 0.2],
 85        [5. , 3.6, 1.4, 0.2],
 86        [5.4, 3.9, 1.7, 0.4],
 87        [4.6, 3.4, 1.4, 0.3],
 88        [5. , 3.4, 1.5, 0.2],
 89        [4.4, 2.9, 1.4, 0.2],
 90        [4.9, 3.1, 1.5, 0.1],
 91        [5.4, 3.7, 1.5, 0.2],
 92        [4.8, 3.4, 1.6, 0.2],
 93        [4.8, 3. , 1.4, 0.1],
 94        [4.3, 3. , 1.1, 0.1],
 95        [5.8, 4. , 1.2, 0.2],
 96        [5.7, 4.4, 1.5, 0.4],
 97        [5.4, 3.9, 1.3, 0.4],
 98        [5.1, 3.5, 1.4, 0.3],
 99        [5.7, 3.8, 1.7, 0.3],
100        [5.1, 3.8, 1.5, 0.3],
101        [5.4, 3.4, 1.7, 0.2],
102        [5.1, 3.7, 1.5, 0.4],
103        [4.6, 3.6, 1. , 0.2],
104        [5.1, 3.3, 1.7, 0.5],
105        [4.8, 3.4, 1.9, 0.2],
106        [5. , 3. , 1.6, 0.2],
107        [5. , 3.4, 1.6, 0.4],
108        [5.2, 3.5, 1.5, 0.2],
109        [5.2, 3.4, 1.4, 0.2],
110        [4.7, 3.2, 1.6, 0.2],
111        [4.8, 3.1, 1.6, 0.2],
112        [5.4, 3.4, 1.5, 0.4],
113        [5.2, 4.1, 1.5, 0.1],
114        [5.5, 4.2, 1.4, 0.2],
115        [4.9, 3.1, 1.5, 0.2],
116        [5. , 3.2, 1.2, 0.2],
117        [5.5, 3.5, 1.3, 0.2],
118        [4.9, 3.6, 1.4, 0.1],
119        [4.4, 3. , 1.3, 0.2],
120        [5.1, 3.4, 1.5, 0.2],
121        [5. , 3.5, 1.3, 0.3],
122        [4.5, 2.3, 1.3, 0.3],
123        [4.4, 3.2, 1.3, 0.2],
124        [5. , 3.5, 1.6, 0.6],
125        [5.1, 3.8, 1.9, 0.4],
126        [4.8, 3. , 1.4, 0.3],
127        [5.1, 3.8, 1.6, 0.2],
128        [4.6, 3.2, 1.4, 0.2],
129        [5.3, 3.7, 1.5, 0.2],
130        [5. , 3.3, 1.4, 0.2],
131        [7. , 3.2, 4.7, 1.4],
132        [6.4, 3.2, 4.5, 1.5],
133        [6.9, 3.1, 4.9, 1.5],
134        [5.5, 2.3, 4. , 1.3],
135        [6.5, 2.8, 4.6, 1.5],
136        [5.7, 2.8, 4.5, 1.3],
137        [6.3, 3.3, 4.7, 1.6],
138        [4.9, 2.4, 3.3, 1. ],
139        [6.6, 2.9, 4.6, 1.3],
140        [5.2, 2.7, 3.9, 1.4],
141        [5. , 2. , 3.5, 1. ],
142        [5.9, 3. , 4.2, 1.5],
143        [6. , 2.2, 4. , 1. ],
144        [6.1, 2.9, 4.7, 1.4],
145        [5.6, 2.9, 3.6, 1.3],
146        [6.7, 3.1, 4.4, 1.4],
147        [5.6, 3. , 4.5, 1.5],
148        [5.8, 2.7, 4.1, 1. ],
149        [6.2, 2.2, 4.5, 1.5],
150        [5.6, 2.5, 3.9, 1.1],
151        [5.9, 3.2, 4.8, 1.8],
152        [6.1, 2.8, 4. , 1.3],
153        [6.3, 2.5, 4.9, 1.5],
154        [6.1, 2.8, 4.7, 1.2],
155        [6.4, 2.9, 4.3, 1.3],
156        [6.6, 3. , 4.4, 1.4],
157        [6.8, 2.8, 4.8, 1.4],
158        [6.7, 3. , 5. , 1.7],
159        [6. , 2.9, 4.5, 1.5],
160        [5.7, 2.6, 3.5, 1. ],
161        [5.5, 2.4, 3.8, 1.1],
162        [5.5, 2.4, 3.7, 1. ],
163        [5.8, 2.7, 3.9, 1.2],
164        [6. , 2.7, 5.1, 1.6],
165        [5.4, 3. , 4.5, 1.5],
166        [6. , 3.4, 4.5, 1.6],
167        [6.7, 3.1, 4.7, 1.5],
168        [6.3, 2.3, 4.4, 1.3],
169        [5.6, 3. , 4.1, 1.3],
170        [5.5, 2.5, 4. , 1.3],
171        [5.5, 2.6, 4.4, 1.2],
172        [6.1, 3. , 4.6, 1.4],
173        [5.8, 2.6, 4. , 1.2],
174        [5. , 2.3, 3.3, 1. ],
175        [5.6, 2.7, 4.2, 1.3],
176        [5.7, 3. , 4.2, 1.2],
177        [5.7, 2.9, 4.2, 1.3],
178        [6.2, 2.9, 4.3, 1.3],
179        [5.1, 2.5, 3. , 1.1],
180        [5.7, 2.8, 4.1, 1.3],
181        [6.3, 3.3, 6. , 2.5],
182        [5.8, 2.7, 5.1, 1.9],
183        [7.1, 3. , 5.9, 2.1],
184        [6.3, 2.9, 5.6, 1.8],
185        [6.5, 3. , 5.8, 2.2],
186        [7.6, 3. , 6.6, 2.1],
187        [4.9, 2.5, 4.5, 1.7],
188        [7.3, 2.9, 6.3, 1.8],
189        [6.7, 2.5, 5.8, 1.8],
190        [7.2, 3.6, 6.1, 2.5],
191        [6.5, 3.2, 5.1, 2. ],
192        [6.4, 2.7, 5.3, 1.9],
193        [6.8, 3. , 5.5, 2.1],
194        [5.7, 2.5, 5. , 2. ],
195        [5.8, 2.8, 5.1, 2.4],
196        [6.4, 3.2, 5.3, 2.3],
197        [6.5, 3. , 5.5, 1.8],
198        [7.7, 3.8, 6.7, 2.2],
199        [7.7, 2.6, 6.9, 2.3],
200        [6. , 2.2, 5. , 1.5],
201        [6.9, 3.2, 5.7, 2.3],
202        [5.6, 2.8, 4.9, 2. ],
203        [7.7, 2.8, 6.7, 2. ],
204        [6.3, 2.7, 4.9, 1.8],
205        [6.7, 3.3, 5.7, 2.1],
206        [7.2, 3.2, 6. , 1.8],
207        [6.2, 2.8, 4.8, 1.8],
208        [6.1, 3. , 4.9, 1.8],
209        [6.4, 2.8, 5.6, 2.1],
210        [7.2, 3. , 5.8, 1.6],
211        [7.4, 2.8, 6.1, 1.9],
212        [7.9, 3.8, 6.4, 2. ],
213        [6.4, 2.8, 5.6, 2.2],
214        [6.3, 2.8, 5.1, 1.5],
215        [6.1, 2.6, 5.6, 1.4],
216        [7.7, 3. , 6.1, 2.3],
217        [6.3, 3.4, 5.6, 2.4],
218        [6.4, 3.1, 5.5, 1.8],
219        [6. , 3. , 4.8, 1.8],
220        [6.9, 3.1, 5.4, 2.1],
221        [6.7, 3.1, 5.6, 2.4],
222        [6.9, 3.1, 5.1, 2.3],
223        [5.8, 2.7, 5.1, 1.9],
224        [6.8, 3.2, 5.9, 2.3],
225        [6.7, 3.3, 5.7, 2.5],
226        [6.7, 3. , 5.2, 2.3],
227        [6.3, 2.5, 5. , 1.9],
228        [6.5, 3. , 5.2, 2. ],
229        [6.2, 3.4, 5.4, 2.3],
230        [5.9, 3. , 5.1, 1.8]])
231 [8]
232 iris.data.shape
233 (150, 4)
234 [9]
235 iris.feature_names
236 ['sepal length (cm)',
237  'sepal width (cm)',
238  'petal length (cm)',
239  'petal width (cm)']
240 [10]
241 iris.target
242 array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
243        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
244        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
245        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
246        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
247        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
248        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
249 [11]
250 iris.target.shape
251 (150,)
252 [12]
253 iris.target_names
254 array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
255 [13]
256 X = iris.data[:,:2]
257 [14]
258 X.shape
259 (150, 2)
260 [15]
261 plt.scatter(X[:,0],X[:,1])
262 <matplotlib.collections.PathCollection at 0x1aa5b280280>
263 
264 [16]
265 y = iris.target
266 [17]
267 plt.scatter(X[y == 0,0],X[y == 0,1],color = 'red')
268 plt.scatter(X[y == 1,0],X[y == 1,1],color = 'blue')
269 plt.scatter(X[y == 2,0],X[y == 2,1],color = 'green')
270 <matplotlib.collections.PathCollection at 0x1aa5b386d30>
271 
272 [18]
273 plt.scatter(X[y == 0,0],X[y == 0,1],color = 'red',marker='o')
274 plt.scatter(X[y == 1,0],X[y == 1,1],color = 'blue',marker='+')
275 plt.scatter(X[y == 2,0],X[y == 2,1],color = 'green',marker='x')
276 <matplotlib.collections.PathCollection at 0x1aa5b4089d0>
277 
278 [19]
279 X = iris.data[:,2:]
280 [20]
281 plt.scatter(X[y == 0,0],X[y == 0,1],color = 'red',marker='o')
282 plt.scatter(X[y == 1,0],X[y == 1,1],color = 'blue',marker='+')
283 plt.scatter(X[y == 2,0],X[y == 2,1],color = 'green',marker='x')
284 <matplotlib.collections.PathCollection at 0x1aa5b46f640>

 

标签:11,10,12,Jupyter,666,Notebook,np,array,numpy
From: https://www.cnblogs.com/Cai-Gbro/p/16839828.html

相关文章

  • 通过SSH远程使用jupyter notebook
    1.背景一直苦恼于本地机器和服务器上都要配置一些机器学习方面的环境,今天花了点时间研究了下Jupternotebook远程访问服务器,所以记录一下。有些步骤非必须,这里尽量写清楚......
  • numpy(ndarray)和tensor(GPU上的numpy)速查
    类型(Types)NumpyPyTorchnp.ndarraytorch.Tensornp.float32torch.float32;torch.floatnp.float64torch.float64;torch.doublenp.floattorch.float1......
  • numpy和 pandas学习
    这是我自己的学习笔记,就不要看了。##技术篇###numpy基础-numpy生成随机数据np.random.normal(0,10,1024)标准正态分布平均数0,标准差10,1024个数据,正态分布也......
  • Numpy
    ndarray对象基本应用标准的Python中用列表(list)保存一组值,可以当作数组使用。但由于列表的元素可以是任何对象,因此列表中保存的是对象的指针。对于数值运算来说,这种结构......
  • 用numpy实现最简单的前馈神经网络——正向网络建立篇
    目录mnist分析(输入分析)下载简要说明加载显示输出分析拟合函数建立激活函数拟合函数正向计算梯度梯度下降最终代码根据上一篇文章,来构建神经网络吧明确输入和输出选择合......
  • 用numpy实现最简单的前馈神经网络——神经网络架构篇
    目录神经网络架构矩阵运算拟合——深度学习的目的最简单的拟合——线性回归深度学习中的拟合平均损失最小——梯度下降法反向传播和链式法则激活函数和损失函数的选择总结......
  • Numpy温习函数方法
    一、NumpyPandas1.1简介方便数组矩阵运算1.2优势运算速度快:numpy和pandas都是采用C语言编写,pandas又是基于numpy,是numpy的升级版本。消耗资源少:采用的是......
  • jupyter notebook修改默认目录
    1.打开cmd输入命令​​jupyternotebook--generate-config​​该操作会生成如下文件:C:\Users\你的用户名\.jupyter\jupyter_notebook_config.py2.编辑上述文件:大......
  • python numpy 基础科学计算包,数学函数库
    pipinstallnumpynumpy.array()函数,强大的N维数组对象ndarrayimportnumpyasnpa=np.array([1,2,3])print(a)[123]#多于一个维度importnumpyasnpa......
  • 【Python数据分析】数据的维度、Numpy
    数据的维度一维数据一维数据由对等关系的有序或无序数据构成,采用线性方式组织列表和数组二维数据是由多个一维数据组成,是一维数据的组合形式多维数据由一维或二维数......