<转载+随笔>机器学习实战笔记(Python实现)-03-朴素贝叶斯

本文仅作朴素贝叶斯算法参考笔记

  原文链接:机器学习实战笔记--03--朴素贝叶斯

0.理解朴素贝叶斯

  阮一峰大神的讲解

  张洋--算法杂货铺

1.集合交并

  createVocabList(dataSet)函数中的:

 

1 vocabSet = vocabSet | set(document) #创建两个集合的并集
View Code
 1 #示例:集合的并(和)
 2 >>> vocabSet = set([])
 3 >>> vocabSet = vocabSet | set(['aaa','vvv','sss'])
 4 >>> list(vocabSet)
 5 ['vvv', 'aaa', 'sss']
 6 >>> vocabSet = vocabSet | set(['aaa','ccc'])
 7 >>> list(vocabSet)
 8 ['ccc', 'vvv', 'aaa', 'sss']
 9 >>> vocabSet = vocabSet | set(['aaa','ccc'])
10 >>> vocabSet
11 {'ccc', 'vvv', 'aaa', 'sss'}
12 >>> list(vocabSet)
13 ['ccc', 'vvv', 'aaa', 'sss']
14 #示例:集合的交
15 >>> vocabSet = vocabSet & set(['aaa','ccc'])
16 >>> vocabSet
17 {'ccc', 'aaa'}
18 >>> list(vocabSet)
19 ['ccc', 'aaa']
20 #集合的差  与之类似

2.构造矩阵

  zeros(),ones(),eyes()

  

  1 >>> import numpy as np
  2 >>> p0Num = np.eye(3)
  3 >>> p0Num
  4 array([[ 1.,  0.,  0.],
  5        [ 0.,  1.,  0.],
  6        [ 0.,  0.,  1.]])
  7 >>> p0Num = np.zeros(3)
  8 >>> p0Num
  9 array([ 0.,  0.,  0.])
 10 >>> p0Num = np.ones(3)
 11 >>> p0Num
 12 array([ 1.,  1.,  1.])
 13 #附详细解释
 14 “““
 15 Help on function eye in numpy:
 16 
 17 numpy.eye = eye(N, M=None, k=0, dtype=<class 'float'>)
 18     Return a 2-D array with ones on the diagonal and zeros elsewhere.
 19     
 20     Parameters
 21     ----------
 22     N : int
 23       Number of rows in the output.
 24     M : int, optional
 25       Number of columns in the output. If None, defaults to `N`.
 26     k : int, optional
 27       Index of the diagonal: 0 (the default) refers to the main diagonal,
 28       a positive value refers to an upper diagonal, and a negative value
 29       to a lower diagonal.
 30     dtype : data-type, optional
 31       Data-type of the returned array.
 32     
 33     Returns
 34     -------
 35     I : ndarray of shape (N,M)
 36       An array where all elements are equal to zero, except for the `k`-th
 37       diagonal, whose values are equal to one.
 38     
 39     See Also
 40     --------
 41     identity : (almost) equivalent function
 42     diag : diagonal 2-D array from a 1-D array specified by the user.
 43     
 44     Examples
 45     --------
 46     >>> np.eye(2, dtype=int)
 47     array([[1, 0],
 48            [0, 1]])
 49     >>> np.eye(3, k=1)
 50     array([[ 0.,  1.,  0.],
 51            [ 0.,  0.,  1.],
 52            [ 0.,  0.,  0.]])
 53 
 54 
 55 numpy.zeros = zeros(...)
 56     zeros(shape, dtype=float, order='C')
 57     
 58     Return a new array of given shape and type, filled with zeros.
 59     
 60     Parameters
 61     ----------
 62     shape : int or sequence of ints
 63         Shape of the new array, e.g., ``(2, 3)`` or ``2``.
 64     dtype : data-type, optional
 65         The desired data-type for the array, e.g., `numpy.int8`.  Default is
 66         `numpy.float64`.
 67     order : {'C', 'F'}, optional
 68         Whether to store multidimensional data in C- or Fortran-contiguous
 69         (row- or column-wise) order in memory.
 70     
 71     Returns
 72     -------
 73     out : ndarray
 74         Array of zeros with the given shape, dtype, and order.
 75     
 76     See Also
 77     --------
 78     zeros_like : Return an array of zeros with shape and type of input.
 79     ones_like : Return an array of ones with shape and type of input.
 80     empty_like : Return an empty array with shape and type of input.
 81     ones : Return a new array setting values to one.
 82     empty : Return a new uninitialized array.
 83     
 84     Examples
 85     --------
 86     >>> np.zeros(5)
 87     array([ 0.,  0.,  0.,  0.,  0.])
 88     
 89     >>> np.zeros((5,), dtype=np.int)
 90     array([0, 0, 0, 0, 0])
 91     
 92     >>> np.zeros((2, 1))
 93     array([[ 0.],
 94            [ 0.]])
 95     
 96     >>> s = (2,2)
 97     >>> np.zeros(s)
 98     array([[ 0.,  0.],
 99            [ 0.,  0.]])
100     
101     >>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
102     array([(0, 0), (0, 0)],
103           dtype=[('x', '<i4'), ('y', '<i4')])
104 
105 
106 
107 ”””

3.range()函数

  创建列表,,参见:http://www.runoob.com/python/python-func-range.html

4.uniform()函数

  产生一个[a,b)之间的实数

 

posted on 2018-02-02 04:36  wastelands  阅读(87)  评论(0)    收藏  举报

导航