In [2]:
#numpy package import
import numpy as np

print(np.__version__)


1.12.1

In [7]:
c=np.zeros(5)
print(c)
print("%d bytes" %(c.size*c.itemsize))


[ 0.  0.  0.  0.  0.]
40 bytes

In [8]:
# komut satırından çalıştırma
# %run `python -c "import numpy; numpy.info(numpy.add)"`


ERROR:root:File `'`python.py'` not found.

In [11]:
f=np.arange(4,55)
print(f)


[ 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]

In [22]:
f=np.arange(2,55)
f=f[::-1]
print(f)
f=f[:-12]
print(f)
f=f[3::]
print(f)


[54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30
 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5
  4  3  2]
[54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30
 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14]
[51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27
 26 25 24 23 22 21 20 19 18 17 16 15 14]

In [23]:
Z = np.random.random((3,3,3))  #https://docs.scipy.org/doc/numpy/reference/routines.random.html
print(Z)


[[[ 0.18197647  0.10872416  0.91952692]
  [ 0.31244338  0.74038438  0.39251707]
  [ 0.27063429  0.74072815  0.98128016]]

 [[ 0.99745947  0.32953037  0.33451794]
  [ 0.52348407  0.26196534  0.32515756]
  [ 0.15319897  0.35093717  0.19609089]]

 [[ 0.97864139  0.13860047  0.61486517]
  [ 0.20443755  0.6476063   0.18840467]
  [ 0.50370159  0.74723545  0.92383174]]]

In [24]:
Z = np.random.random((10,10))  
Zmin, Zmax = Z.min(), Z.max()  
print(Zmin, Zmax)


0.00138337819513 0.996666543002

In [26]:
Z = np.ones((10,10))
Z[1:-1,1:-1] = 0 #sonra sınır değerler 1, iç değerler 0 olmak üzere matrisi değiştirelim
print(Z[0,0]) # Birinci satır birinci sütun elemanı
print(Z[1,1]) # İkinci satır ikinci sütun elemanı
print(Z[1]) # Bize 1. satırı gösterecek.
print(Z[-1,0])
print(Z)


1.0
0.0
[ 1.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
1.0
[[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]]

In [27]:
print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(0.3 == 3 * 0.1)


nan
False
False
nan
False

In [31]:
Z = np.diag(np.arange(4))
print(Z)
Z = np.diag(1+np.arange(4))
print(Z)
Z = np.diag(1+np.arange(4),k=-1)
print(Z)


[[0 0 0 0]
 [0 1 0 0]
 [0 0 2 0]
 [0 0 0 3]]
[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]
[[0 0 0 0 0]
 [1 0 0 0 0]
 [0 2 0 0 0]
 [0 0 3 0 0]
 [0 0 0 4 0]]

In [38]:
# Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
print(np.unravel_index(100,(13,14,15)))
print(np.unravel_index(100,(6,7,8)))


(0, 6, 10)
(1, 5, 4)

In [39]:
#Normalize a 5x5 random matrix 
Z = np.random.random((5,5))
Zmax, Zmin = Z.max(), Z.min()
Z = (Z - Zmin)/(Zmax - Zmin)
print(Z)


[[ 0.91648208  0.89517511  0.15549135  0.          0.24793119]
 [ 0.33507094  0.86217324  0.94270943  0.87699576  0.98145786]
 [ 0.99452702  0.89472857  0.28126343  0.88111716  1.        ]
 [ 0.31456781  0.93989073  0.39667802  0.28703851  0.17042215]
 [ 0.95002028  0.91888238  0.79686321  0.7193106   0.95558612]]

In [40]:
Z = np.dot(np.ones((5,3)), np.ones((3,2)))
print(Z)


[[ 3.  3.]
 [ 3.  3.]
 [ 3.  3.]
 [ 3.  3.]
 [ 3.  3.]]

In [41]:
#Given a 1D array, negate all elements which are between 3 and 8, in place.
Z = np.arange(11)
Z[(3 < Z) & (Z <= 8)] *= -1
print(Z)


[ 0  1  2  3 -4 -5 -6 -7 -8  9 10]

In [42]:
print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))


9
10

In [53]:
Z=1

Z=Z**Z
print(Z)
Z=2 << Z >> 2
print(Z)
Z=Z <- Z
print(Z)
Z=1j*Z
print(Z)
Z=Z/1/1
print(Z)
Z=1
Z=Z<Z>Z
print(Z)


1
1
False
0j
0j
False

In [61]:
Z = np.random.uniform(-10,+10,10)
print (np.copysign(np.ceil(np.abs(Z)), Z))


[-2.  7. -7. -3.  6.  9. -7.  3. -8.  2.]

In [74]:
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
today     = np.datetime64('today', 'D')
tomorrow  = np.datetime64('today', 'D') + np.timedelta64(1, 'D')

In [73]:
Z = np.arange('2017-07', '2017-08', dtype='datetime64[D]')
print(Z)


['2017-07-01' '2017-07-02' '2017-07-03' '2017-07-04' '2017-07-05'
 '2017-07-06' '2017-07-07' '2017-07-08' '2017-07-09' '2017-07-10'
 '2017-07-11' '2017-07-12' '2017-07-13' '2017-07-14' '2017-07-15'
 '2017-07-16' '2017-07-17' '2017-07-18' '2017-07-19' '2017-07-20'
 '2017-07-21' '2017-07-22' '2017-07-23' '2017-07-24' '2017-07-25'
 '2017-07-26' '2017-07-27' '2017-07-28' '2017-07-29' '2017-07-30'
 '2017-07-31']

In [81]:
A = np.ones(3)*1
B = np.ones(3)*2
C = np.ones(3)*3
print(A,B,C)
print(np.add(A,B,out=B))
print(np.divide(A,2,out=A))
print(np.negative(A,out=A))
print(np.multiply(A,B,out=A))


[ 1.  1.  1.] [ 2.  2.  2.] [ 3.  3.  3.]
[ 3.  3.  3.]
[ 0.5  0.5  0.5]
[-0.5 -0.5 -0.5]
[-1.5 -1.5 -1.5]

In [82]:
Z = np.random.uniform(0,10,10)

print (Z - Z%1)
print (np.floor(Z))
print (np.ceil(Z)-1)
print (Z.astype(int))
print (np.trunc(Z))


[ 8.  5.  4.  9.  0.  4.  6.  0.  4.  6.]
[ 8.  5.  4.  9.  0.  4.  6.  0.  4.  6.]
[ 8.  5.  4.  9.  0.  4.  6.  0.  4.  6.]
[8 5 4 9 0 4 6 0 4 6]
[ 8.  5.  4.  9.  0.  4.  6.  0.  4.  6.]

In [83]:
Z = np.linspace(0,1,12,endpoint=True)[1:-1]
print(Z)


[ 0.09090909  0.18181818  0.27272727  0.36363636  0.45454545  0.54545455
  0.63636364  0.72727273  0.81818182  0.90909091]

In [1]:




In [ ]: