title-img


Reduce Function python

Given a list of rational numbers,find their product. Concept The reduce() function applies a function of two arguments cumulatively on a list of objects in succession from left to right to reduce it to one value. Say you have a list, say [1,2,3] and you have to find its sum. >>> reduce(lambda x, y : x + y,[1,2,3]) 6 You can also define an initial value. If it is specified, the function will assume initial value as the value given, and then reduce. It is equivalent to adding the initia

View Solution →

Arrays python

The NumPy (Numeric Python) package helps us manipulate large arrays and matrices of numeric data. To use the NumPy module, we need to import it using: import numpy Arrays A NumPy array is a grid of values. They are similar to lists, except that every element of an array must be the same type. import numpy a = numpy.array([1,2,3,4,5]) print a[1] #2 b = numpy.array([1,2,3,4,5],float) print b[1] #2.0 In the above example, numpy.array() is used to convert a li

View Solution →

Shape and Reshape python

shape The shape tool gives a tuple of array dimensions and can be used to change the dimensions of an array. (a). Using shape to get array dimensions import numpy my__1D_array = numpy.array([1, 2, 3, 4, 5]) print my_1D_array.shape #(5,) -> 1 row and 5 columns my__2D_array = numpy.array([[1, 2],[3, 4],[6,5]]) print my_2D_array.shape #(3, 2) -> 3 rows and 2 columns (b). Using shape to change array dimensions import numpy change_array = numpy.array([1,2,3,4,5,6]) c

View Solution →

Transpose and Flatten python

Transpose We can generate the transposition of an array using the tool numpy.transpose. It will not affect the original array, but it will create a new array. import numpy my_array = numpy.array([[1,2,3], [4,5,6]]) print numpy.transpose(my_array) #Output [[1 4] [2 5] [3 6]] Flatten The tool flatten creates a copy of the input array flattened to one dimension. import numpy my_array = numpy.array([[1,2,3], [4,5,6]])

View Solution →

Concatenate python

Concatenate Two or more arrays can be concatenated together using the concatenate function with a tuple of the arrays to be joined: import numpy array_1 = numpy.array([1,2,3]) array_2 = numpy.array([4,5,6]) array_3 = numpy.array([7,8,9]) print numpy.concatenate((array_1, array_2, array_3)) #Output [1 2 3 4 5 6 7 8 9] If an array has more than one dimension, it is possible to specify the axis along which multiple arrays are concatenated. By default, it is along the first di

View Solution →