/ Python And R Data science skills: 33 sahpe attribute in python

Sunday 4 February 2018

33 sahpe attribute in python

33 max and min and reshape numpy
In [1]:
import numpy as np
In [2]:
from numpy.random import randint
In [3]:
za=np.arange(25)
In [4]:
za
Out[4]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24])
In [5]:
za.reshape(5,5)
Out[5]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
In [7]:
zb=za.reshape(5,5)
In [8]:
zb
Out[8]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
In [9]:
zb.max()
Out[9]:
24
In [10]:
za.max()
Out[10]:
24
In [11]:
za.min()
Out[11]:
0
In [12]:
za.argmax()
Out[12]:
24
In [13]:
zb.argmin()
Out[13]:
0
In [17]:
zc = np.random.randint(0,50,10)
In [19]:
zc
Out[19]:
array([ 5, 46, 26, 23, 23, 21, 40, 21,  3, 35])
In [18]:
zc.max()
Out[18]:
46
In [20]:
zc.min()
Out[20]:
3
In [21]:
zc.argmax()
Out[21]:
1
In [22]:
zc.argmin()
Out[22]:
8
In [25]:
zc.shape
Out[25]:
(10,)
In [26]:
zb.shape
Out[26]:
(5, 5)
In [27]:
zd=np.arange(100)
In [28]:
zd
Out[28]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
In [30]:
zd.shape
Out[30]:
(100,)
In [31]:
zd.reshape(5,5)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-31-2ace9a126e57> in <module>()
----> 1 zd.reshape(5,5)

ValueError: cannot reshape array of size 100 into shape (5,5)
In [32]:
zd.reshape(2,50)
Out[32]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
        34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
       [50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
        67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
        84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
In [33]:
zd.reshape(2,50).shape
Out[33]:
(2, 50)
In [34]:
zd.reshape(5,20)
Out[34]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19],
       [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
        37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
        57, 58, 59],
       [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
        77, 78, 79],
       [80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
        97, 98, 99]])
In [35]:
zd.reshape(5,20).shape
Out[35]:
(5, 20)
In [ ]:
zd.shape

No comments:

Post a Comment