In [2]:
import numpy as np

In [2]:
a = np.array([12,312] )

In [3]:
a


Out[3]:
array([ 12, 312])

In [5]:
np.arange(100)


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, 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 [6]:
a = np.array([[12,312],[1,22]] )

In [8]:
a.shape


Out[8]:
(2, 2)

In [9]:
a.size


Out[9]:
4

In [10]:
a.dtype


Out[10]:
dtype('int64')

In [11]:
a.strides


Out[11]:
(16, 8)

In [12]:
a[1,0]


Out[12]:
1

In [13]:
a.flags


Out[13]:
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

In [17]:
a


Out[17]:
array([[ 12, 312],
       [  1,  22]])

In [18]:
2*a


Out[18]:
array([[ 24, 624],
       [  2,  44]])

In [19]:
a*a


Out[19]:
array([[  144, 97344],
       [    1,   484]])

In [20]:
np.matrix(a)*np.matrix(a)


Out[20]:
matrix([[  456, 10608],
        [   34,   796]])

In [21]:
a.dot(a)


Out[21]:
array([[  456, 10608],
       [   34,   796]])

In [22]:
a = np.arange(1,4)

In [24]:
np.outer(a,a)


Out[24]:
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

In [39]:
x = np.linspace( 0.1,0.4,10)

In [40]:
x


Out[40]:
array([ 0.1       ,  0.13333333,  0.16666667,  0.2       ,  0.23333333,
        0.26666667,  0.3       ,  0.33333333,  0.36666667,  0.4       ])

In [27]:
np.sin(x)


Out[27]:
array([ 0.09983342,  0.13293862,  0.16589613,  0.19866933,  0.23122181,
        0.26351739,  0.29552021,  0.3271947 ,  0.35850567,  0.38941834])

In [33]:
from math import sin

In [36]:
x


Out[36]:
array([ 0.1       ,  0.13333333,  0.16666667,  0.2       ,  0.23333333,
        0.26666667,  0.3       ,  0.33333333,  0.36666667,  0.4       ])

In [37]:
np.sin(x,out=x)


Out[37]:
array([ 0.09983342,  0.13293862,  0.16589613,  0.19866933,  0.23122181,
        0.26351739,  0.29552021,  0.3271947 ,  0.35850567,  0.38941834])

In [38]:
x


Out[38]:
array([ 0.09983342,  0.13293862,  0.16589613,  0.19866933,  0.23122181,
        0.26351739,  0.29552021,  0.3271947 ,  0.35850567,  0.38941834])

In [42]:
list(map(sin,x))


Out[42]:
[0.09983341664682815,
 0.1329386226223141,
 0.16589613269341505,
 0.19866933079506122,
 0.2312218056342986,
 0.2635173911435351,
 0.2955202066613396,
 0.32719469679615226,
 0.35850567092861724,
 0.3894183423086505]

In [43]:
np.sum(np.sin(x))


Out[43]:
2.462715616230212

Wydajność a cache


In [3]:
x = np.zeros((20000,))

y = np.zeros((20000*67,))[::67]

x.shape, y.shape
((20000,), (20000,))

%timeit x.sum()


The slowest run took 5.64 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 11.3 µs per loop

In [4]:
%timeit y.sum()


The slowest run took 50.08 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 24.5 µs per loop

In [3]:
import numpy as np

In [4]:
a = np.arange(10,200)

In [6]:
a[::10]


Out[6]:
array([ 10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120, 130,
       140, 150, 160, 170, 180, 190])

In [7]:
x = np.linspace(0,10,15)

In [11]:
x[np.sin(x)<0]


Out[11]:
array([  3.57142857,   4.28571429,   5.        ,   5.71428571,  10.        ])

In [12]:
np.sin(x[np.sin(x)<0])


Out[12]:
array([-0.41672165, -0.91034694, -0.95892427, -0.53870529, -0.54402111])

In [13]:
np.nonzero(np.sin(x)<0)


Out[13]:
(array([ 5,  6,  7,  8, 14]),)

In [14]:
x[5]


Out[14]:
3.5714285714285716

In [ ]:


In [ ]:


In [ ]:


In [1]:
import numpy as np 
x = np.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]], dtype=np.int8)
y = np.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]], dtype=np.int8,order='F')

In [2]:
str(x.data)


Out[2]:
'<memory at 0x7fb440087cf0>'

In [3]:
str(y.data)


Out[3]:
'<memory at 0x7fb440087cf0>'

In [20]:
x.flags,y.flags


Out[20]:
(  C_CONTIGUOUS : True
   F_CONTIGUOUS : False
   OWNDATA : True
   WRITEABLE : True
   ALIGNED : True
   UPDATEIFCOPY : False,   C_CONTIGUOUS : False
   F_CONTIGUOUS : True
   OWNDATA : True
   WRITEABLE : True
   ALIGNED : True
   UPDATEIFCOPY : False)

In [2]:
x.data.f_contiguous,y.data.f_contiguous


Out[2]:
(False, True)

In [14]:
x.strides,y.strides


Out[14]:
((3, 1), (1, 3))

In [5]:
x.data.hex(),y.data.hex()


Out[5]:
('010203040506070809', '010203040506070809')

In [17]:
d1 = x.data

In [28]:
x.data.tobytes()


Out[28]:
b'\x01\x02\x03\x04\x05\x06\x07\x08\t'

In [31]:
y.data.tolist()


Out[31]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [43]:
from ctypes import c_int, addressof

In [46]:
x.strides,x.T.strides,x.T.copy().strides


Out[46]:
((3, 1), (1, 3), (3, 1))

In [45]:
x.data.hex(),x.transpose().data.hex(),x.transpose().copy().data.hex()


Out[45]:
('010203040506070809', '010407020508030609', '010407020508030609')

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: