#!============================================================================ #! NumPy #!============================================================================ import numpy #! 1. Construct an array from the list: [1,2,3] print '================================================' print '#! 1. Construct an array from the list: [1,2,3]' print '================================================' a = numpy.array([1,2,3]) print a #! 2. Cast it into floats print '\n================================================' print '#! 2. Cast it into floats' print '================================================' b = a.astype(float) print b #! 3. Create an array of int ranging from 0-10 print '\n================================================' print '#! 3. Create an array of int ranging from 0-10' print '================================================' a = numpy.arange(10,dtype=int) print a #! 4. Create an array containing 7 evenly spaced numbers between 0 and 23 print '\n================================================' print '#! 4. Create an array containing 7 evenly spaced numbers between 0 and 23' print '================================================' a = numpy.linspace(0,23,7) #! 5. Create an array with shape (2,5,5,2) containing only the number 5 print '\n================================================' print '#! 5. Create an array with shape (2,5,5,2) containing only the number 5' print '================================================' a = numpy.ones((2,5,5,2))*5.0 #! 6. Reshape the resulting array from shape (2,5,5,2) to only 2D print '\n================================================' print '#! 6. Reshape the resulting array from shape (2,5,5,2) to only 2D' print '================================================' c = a.reshape((10,10)) #! 7. Starting from a=numpy.arange(10) and b=numpy.arange(5) # create the array [0,1,2,3,4,4,3,2,1,0] print '\n================================================' print '#! 7. Starting from a=numpy.arange(10) and b=numpy.arange(5) # create the array [0,1,2,3,4,4,3,2,1,0]' print '================================================' a=numpy.arange(10) b=numpy.arange(5) a[5:]=b[5::-1] print a #! 8. Calculate the mean, std, var, max, min, of an array with shape (15,15) containing floats drawn from a normal #distribution with mean 21 and sigma 4.5 (use numpy.random.normal()) print '\n================================================' print '#! 8. Calculate the mean, std, var, max, min, of an array with shape (15,15) containing floats drawn from a normal distribution with mean ' print '================================================' a = numpy.random.normal(loc=21.,scale=4.5,size=(15,15)) print a.mean() print a.std() print a.var() print a.max() print a.min() #! 9. Do the same but not on all elements but only on the 1nd dimension print '================================================' print '#! 9. Do the same but not on all elements but only on the 1nd dimension' print '================================================' a.mean(1) a.std(1) a.var(1) a.max(1) a.min(1) #! 10. Transform the array such that it is only 1d print '\n================================================' print '#! 10. Transform the array such that it is only 1d' print '================================================' a = a.flatten() #! 11. Remove all values smaller 15 and larger 26 print '\n================================================' print '#! 11. Remove all values smaller 15 and larger 26' print '================================================' b = a[(a>15)&(a<26)] #! 12. Clip all values smaller 15 and larger 26 to NaN (hint:clip ) print '\n================================================' print '#! 12. Clip all values smaller 15 and larger 26 to NaN (hint:clip )' print '================================================' b = a.clip(15,26) b[b==15] = numpy.NaN b[b==26] = numpy.NaN print b #! 13. Calculate the sum of the resulting array. It should not be NaN! (hint:nansum) print '\n================================================' print '#! 13. Calculate the sum of the resulting array. It should not be NaN! (hint:nansum)' print '================================================' print numpy.nansum(b) #! 14. Convert the NaN's to 0.0 (hint: nan_to_sum) print '\n================================================' print '#! 14. Convert the NaN\'s to 0.0 (hint: nan_to_sum)' print '================================================' c = numpy.nan_to_num(b) #! 15. Save the array and load it again print '\n================================================' print '#! 15. Save the array and load it again' print '================================================' numpy.savetxt('lastarray.gz',c) c_loaded = numpy.loadtxt('lastarray.gz')