title-img


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 →

Zeros and Ones python

zeros The zeros tool returns a new array with a given shape and type filled with 's. import numpy print numpy.zeros((1,2)) #Default type is float #Output : [[ 0. 0.]] print numpy.zeros((1,2), dtype = numpy.int) #Type changes to int #Output : [[0 0]] ones The ones tool returns a new array with a given shape and type filled with 's. import numpy print numpy.ones((1,2)) #Default type is float #Output : [[ 1. 1.]] print numpy.ones

View Solution →

Array Mathematics Python

Basic mathematical functions operate element-wise on arrays. They are available both as operator overloads and as functions in the NumPy module. import numpy a = numpy.array([1,2,3,4], float) b = numpy.array([5,6,7,8], float) print a + b #[ 6. 8. 10. 12.] print numpy.add(a, b) #[ 6. 8. 10. 12.] print a - b #[-4. -4. -4. -4.] print numpy.subtract(a, b) #[-4. -4. -4. -4.] print a * b #[ 5. 12.

View Solution →