/ Python And R Data science skills: 31 rand and randn eye zero methods in numpy

Sunday 4 February 2018

31 rand and randn eye zero methods in numpy

31 rand and randn eye zero methods in numpy
In [1]:
l1=[1,2,3,4]
In [1]:
import numpy as np
In [3]:
np.array(l1)
Out[3]:
array([1, 2, 3, 4])
In [4]:
l1
Out[4]:
[1, 2, 3, 4]
In [5]:
a1=np.array(l1)
In [6]:
a1
Out[6]:
array([1, 2, 3, 4])
In [7]:
l2=[[1,2,3],[4,5,6],[7,8,9]]
In [8]:
l2
Out[8]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [9]:
np.array(l2)
Out[9]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
In [10]:
l2
Out[10]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [11]:
l2[1]
Out[11]:
[4, 5, 6]
In [12]:
l2[1][0]
Out[12]:
4
In [13]:
l2[1,0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-a77be5f80f06> in <module>()
----> 1 l2[1,0]

TypeError: list indices must be integers or slices, not tuple
In [14]:
l2[2][2]
Out[14]:
9
In [17]:
a2=np.arange(10)
In [18]:
a2
Out[18]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [19]:
np.arange(2,5)
Out[19]:
array([2, 3, 4])
In [20]:
np.arange(0,11,2)
Out[20]:
array([ 0,  2,  4,  6,  8, 10])
In [2]:
np.zeros(5)
Out[2]:
array([ 0.,  0.,  0.,  0.,  0.])
In [7]:
np.zeros((4,3))
Out[7]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
In [8]:
np.ones(5)
Out[8]:
array([ 1.,  1.,  1.,  1.,  1.])
In [9]:
np.ones((5,2))
Out[9]:
array([[ 1.,  1.],
       [ 1.,  1.],
       [ 1.,  1.],
       [ 1.,  1.],
       [ 1.,  1.]])
In [12]:
 
Out[12]:
array([ 5.        ,  4.57142857,  4.14285714,  3.71428571,  3.28571429,
        2.85714286,  2.42857143,  2.        ])
In [15]:
np.linspace(1,100,80)
Out[15]:
array([   1.        ,    2.25316456,    3.50632911,    4.75949367,
          6.01265823,    7.26582278,    8.51898734,    9.7721519 ,
         11.02531646,   12.27848101,   13.53164557,   14.78481013,
         16.03797468,   17.29113924,   18.5443038 ,   19.79746835,
         21.05063291,   22.30379747,   23.55696203,   24.81012658,
         26.06329114,   27.3164557 ,   28.56962025,   29.82278481,
         31.07594937,   32.32911392,   33.58227848,   34.83544304,
         36.08860759,   37.34177215,   38.59493671,   39.84810127,
         41.10126582,   42.35443038,   43.60759494,   44.86075949,
         46.11392405,   47.36708861,   48.62025316,   49.87341772,
         51.12658228,   52.37974684,   53.63291139,   54.88607595,
         56.13924051,   57.39240506,   58.64556962,   59.89873418,
         61.15189873,   62.40506329,   63.65822785,   64.91139241,
         66.16455696,   67.41772152,   68.67088608,   69.92405063,
         71.17721519,   72.43037975,   73.6835443 ,   74.93670886,
         76.18987342,   77.44303797,   78.69620253,   79.94936709,
         81.20253165,   82.4556962 ,   83.70886076,   84.96202532,
         86.21518987,   87.46835443,   88.72151899,   89.97468354,
         91.2278481 ,   92.48101266,   93.73417722,   94.98734177,
         96.24050633,   97.49367089,   98.74683544,  100.        ])
In [16]:
np.eye(5)
Out[16]:
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  1.]])
In [18]:
np.random.rand(20)
Out[18]:
array([ 0.42039041,  0.91689467,  0.71850737,  0.28853198,  0.38260585,
        0.57254852,  0.29418118,  0.62436421,  0.11520722,  0.88731887,
        0.77221706,  0.18521335,  0.66264796,  0.92085166,  0.97325057,
        0.03007708,  0.42582891,  0.18248894,  0.45193976,  0.90704535])
In [19]:
np.random.rand(20,5)
Out[19]:
array([[ 0.8151788 ,  0.0176552 ,  0.91069417,  0.77172085,  0.45164062],
       [ 0.48825795,  0.41132032,  0.68917722,  0.09522061,  0.31135766],
       [ 0.99304626,  0.53070425,  0.99717552,  0.1976177 ,  0.72322989],
       [ 0.56295759,  0.76214179,  0.88227052,  0.35064428,  0.07992261],
       [ 0.98452153,  0.30045827,  0.56466011,  0.80091889,  0.85467807],
       [ 0.94689203,  0.71984116,  0.19719474,  0.36652226,  0.52813274],
       [ 0.84643381,  0.90316294,  0.66109455,  0.19076905,  0.91395746],
       [ 0.18044391,  0.03805482,  0.33402462,  0.79990986,  0.06873059],
       [ 0.03017411,  0.49410954,  0.84260582,  0.40812369,  0.72439137],
       [ 0.20786893,  0.99158763,  0.30003636,  0.37594669,  0.40337209],
       [ 0.71863534,  0.01960622,  0.67704154,  0.44057418,  0.09612367],
       [ 0.36504173,  0.23326889,  0.01711857,  0.37437881,  0.81654706],
       [ 0.92235793,  0.14497873,  0.8105022 ,  0.85079971,  0.98683516],
       [ 0.0015839 ,  0.99748329,  0.13846055,  0.08826409,  0.75998661],
       [ 0.49975192,  0.11191943,  0.5262975 ,  0.82156488,  0.75139777],
       [ 0.99410028,  0.14077209,  0.59287492,  0.45642444,  0.17802495],
       [ 0.3817506 ,  0.29579249,  0.21863691,  0.97706359,  0.78987163],
       [ 0.62385743,  0.40997337,  0.65007241,  0.73711668,  0.36401135],
       [ 0.67409558,  0.02919653,  0.06636092,  0.10497256,  0.02807239],
       [ 0.60050105,  0.21958149,  0.95765177,  0.10923112,  0.18236915]])
In [20]:
np.random.randn(2)
Out[20]:
array([ 0.05239124, -1.2307284 ])
In [21]:
np.random.randn(5)
Out[21]:
array([ 0.32459575,  0.67876918,  0.12072715, -0.17283291,  1.51803263])
In [22]:
np.random.randn(6,6)
Out[22]:
array([[-0.63571156,  0.82442586,  0.32061848, -0.59794406, -1.30155446,
        -0.14910293],
       [ 0.73885141, -0.54021506, -0.08949553, -0.46118507, -1.84069217,
        -0.17311084],
       [-1.2776191 ,  0.67025986, -0.19367932,  0.54164649,  0.40594966,
        -0.27650702],
       [ 1.50316574,  1.25877457,  0.77894425, -1.19285835, -0.82526142,
        -0.86325063],
       [ 0.22753929,  0.08361835,  1.09778614, -0.69328163,  0.25053842,
         0.38241235],
       [-1.15853879,  0.97019706,  0.16549956, -1.34027372,  0.17914922,
        -1.11488693]])

No comments:

Post a Comment