Wednesday, December 16, 2015

array course notes pfda 2

lst = range(1000)
array11 = np.arange(1000)

# Here's what the array looks like
array11[:10]





type(array11)



%timeit [i**2 for i in lst]

%timeit arr**2



arr[-1]



#The first difference to note between lists and arrays is that arrays are homogeneous



lst[0] = 'a string inside a list' lst[:10]

array11[0] = 'a string inside an array'   











#Once an array has been created, its dtype is fixed and it can only store elements of the same type. 



np.zeros(5, float)


np.zeros(3, int)



np.zeros(3, complex)





np.linspace(0, 1, num=5)



np.logspace(1, 4, num=4)



>>> np.logspace(2.0, 3.0, num=4)
    array([  100.        ,   215.443469  ,   464.15888336,  1000.        ])
>>> np.logspace(2.0, 3.0, num=4, endpoint=False)
    array([ 100.        ,  177.827941  ,  316.22776602,  562.34132519])
>>> np.logspace(2.0, 3.0, num=4, base=2.0)
    array([ 4.        ,  5.0396842 ,  6.34960421,  8.        ])







 >>> import matplotlib.pyplot as plt
>>> N = 10
>>> x1 = np.logspace(0.1, 1, N, endpoint=True)
>>> x2 = np.logspace(0.1, 1, N, endpoint=False)
>>> y = np.zeros(N)
>>> plt.plot(x1, y, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(x2, y + 0.5, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.ylim([-0.5, 1])
(-0.5, 1)
>>> plt.show()



#for example this will produce an array of 5 random samples taken from a standard normal distribution (0 mean and variance 1):

np.random.randn(5)



# this will also give 5 samples, but from a normal distribution with a mean of 10 and a standard deviation of 3:

norm10 = np.random.normal(loc=10, scale=3, size=10)



norm10

mask = norm10 > 9 mask 

mask

norm10[mask]



norm10[mask] = 0 print norm10



print 'Data type :', arr.dtype

 print 'Total number of elements :', arr.size 

print 'Number of dimensions :', arr.ndim 

print 'Shape (dimensionality) :', arr.shape

print 'Memory used (in bytes) :', arr.nbytes



print 'Minimum and maximum :', arr.min(), arr.max()

print 'Sum and product of all elements :', arr.sum(), arr.prod()

 print 'Mean and standard deviation :', arr.mean(), arr.std()



#Interactive help:

  np.array?



#Looking for something:
>>>

 np.lookfor('create array')



 np.con*?


%matplotlib inline







No comments:

Post a Comment