[云炬python3玩转机器学习笔记] 3-4创建Numpy数组和矩阵
生活随笔
收集整理的這篇文章主要介紹了
[云炬python3玩转机器学习笔记] 3-4创建Numpy数组和矩阵
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
?
?
?
其他創建 numpy.array的方法 np.zeros(10) array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) np.zeros(10).dtype dtype('float64') np.zeros(10,dtype=int) array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) np.zeros((3,5)) array([[0., 0., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 0., 0.]]) np.zeros(shape=(3,5), dtype=int) array([[0, 0, 0, 0, 0],[0, 0, 0, 0, 0],[0, 0, 0, 0, 0]]) np.ones(10) array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) np.ones((3,5)) array([[1., 1., 1., 1., 1.],[1., 1., 1., 1., 1.],[1., 1., 1., 1., 1.]]) np.full(shape=(3,5),fill_value=666.0) array([[666., 666., 666., 666., 666.],[666., 666., 666., 666., 666.],[666., 666., 666., 666., 666.]]) np.full(fill_value=666.0, shape=(3,5)) array([[666., 666., 666., 666., 666.],[666., 666., 666., 666., 666.],[666., 666., 666., 666., 666.]]) arange [i for i in range(0,20,2)] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] np.arange(0,20,2) array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18]) [i for i in range(0,1,0.2)] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-46-be9c9326671d> in <module> ----> 1 [i for i in range(0,1,0.2)]TypeError: 'float' object cannot be interpreted as an integer np.arange(0,1,0.2) array([0. , 0.2, 0.4, 0.6, 0.8]) np.arange(0,10) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) np.arange(10) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Linespace np.linspace(0,20,10) array([ 0. , 2.22222222, 4.44444444, 6.66666667, 8.88888889,11.11111111, 13.33333333, 15.55555556, 17.77777778, 20. ]) np.linspace(0,20,11) array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.]) random np.random.randint(0,10) 3 np.random.randint(0,10,10) array([0, 3, 2, 6, 4, 6, 4, 9, 5, 9]) np.random.randint(0,1,10) array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) np.random.randint(4,8,size=10) array([5, 4, 7, 7, 7, 5, 7, 7, 6, 5]) np.random.randint(4,8,size=(3,5)) array([[4, 5, 5, 5, 5],[7, 4, 6, 5, 6],[5, 6, 7, 6, 7]]) np.random.randint(4,8,size=(3,5)) array([[6, 4, 4, 4, 5],[6, 5, 7, 4, 4],[7, 6, 4, 6, 6]]) np.random.seed(666) np.random.randint(4,8,size=(3,5)) array([[4, 6, 5, 6, 6],[6, 5, 6, 4, 5],[7, 6, 7, 4, 7]]) np.random.seed(666) np.random.randint(4,8,size=(3,5)) array([[4, 6, 5, 6, 6],[6, 5, 6, 4, 5],[7, 6, 7, 4, 7]]) np.random.random() 0.2811684913927954 np.random.random(10) array([0.92389692, 0.29489453, 0.52438061, 0.94253896, 0.07473949,0.27646251, 0.4675855 , 0.31581532, 0.39016259, 0.26832981]) np.random.random((3,5)) array([[0.75366384, 0.66673747, 0.87287954, 0.52109719, 0.75020425],[0.32940234, 0.29130197, 0.00103619, 0.6361797 , 0.97933558],[0.91236279, 0.39925165, 0.40322917, 0.33454934, 0.72306649]]) np.random.normal() -0.27084406682175244 np.random.normal(10,100) 90.97649843106657 np.random.normal(0,1,(3,5)) array([[ 1.85205227, 1.67819021, -0.98076924, 0.47031082, 0.18226991],[-0.84388249, 0.20996833, 0.22958666, 0.26307642, 2.16633222],[-1.04887593, -1.84768442, 0.53401503, -1.19574802, -0.28915737]]) np.random.normal? np.random? help(np.random.normal) Help on built-in function normal:normal(...) method of numpy.random.mtrand.RandomState instancenormal(loc=0.0, scale=1.0, size=None)Draw random samples from a normal (Gaussian) distribution.The probability density function of the normal distribution, firstderived by De Moivre and 200 years later by both Gauss and Laplaceindependently [2]_, is often called the bell curve because ofits characteristic shape (see the example below).The normal distributions occurs often in nature. For example, itdescribes the commonly occurring distribution of samples influencedby a large number of tiny, random disturbances, each with its ownunique distribution [2]_... note::New code should use the ``normal`` method of a ``default_rng()``instance instead; please see the :ref:`random-quick-start`.Parameters----------loc : float or array_like of floatsMean ("centre") of the distribution.scale : float or array_like of floatsStandard deviation (spread or "width") of the distribution. Must benon-negative.size : int or tuple of ints, optionalOutput shape. If the given shape is, e.g., ``(m, n, k)``, then``m * n * k`` samples are drawn. If size is ``None`` (default),a single value is returned if ``loc`` and ``scale`` are both scalars.Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.Returns-------out : ndarray or scalarDrawn samples from the parameterized normal distribution.See Also--------scipy.stats.norm : probability density function, distribution orcumulative density function, etc.Generator.normal: which should be used for new code.Notes-----The probability density for the Gaussian distribution is.. math:: p(x) = \frac{1}{\sqrt{ 2 \pi \sigma^2 }}e^{ - \frac{ (x - \mu)^2 } {2 \sigma^2} },where :math:`\mu` is the mean and :math:`\sigma` the standarddeviation. The square of the standard deviation, :math:`\sigma^2`,is called the variance.The function has its peak at the mean, and its "spread" increases withthe standard deviation (the function reaches 0.607 times its maximum at:math:`x + \sigma` and :math:`x - \sigma` [2]_). This implies thatnormal is more likely to return samples lying close to the mean, ratherthan those far away.References----------.. [1] Wikipedia, "Normal distribution",https://en.wikipedia.org/wiki/Normal_distribution.. [2] P. R. Peebles Jr., "Central Limit Theorem" in "Probability,Random Variables and Random Signal Principles", 4th ed., 2001,pp. 51, 51, 125.Examples--------Draw samples from the distribution:>>> mu, sigma = 0, 0.1 # mean and standard deviation>>> s = np.random.normal(mu, sigma, 1000)Verify the mean and the variance:>>> abs(mu - np.mean(s))0.0 # may vary>>> abs(sigma - np.std(s, ddof=1))0.1 # may varyDisplay the histogram of the samples, along withthe probability density function:>>> import matplotlib.pyplot as plt>>> count, bins, ignored = plt.hist(s, 30, density=True)>>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *... np.exp( - (bins - mu)**2 / (2 * sigma**2) ),... linewidth=2, color='r')>>> plt.show()Two-by-four array of samples from N(3, 6.25):>>> np.random.normal(3, 2.5, size=(2, 4))array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random?
總結
以上是生活随笔為你收集整理的[云炬python3玩转机器学习笔记] 3-4创建Numpy数组和矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BAT的前端,不是技术牛就够了!还应该锻
- 下一篇: 我说程序员要测试自己的代码,结果被怼!