# NumPy array

Let x be a numpy array with 4 columns and 4 rows.

X=numpy.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]).

## Summary:

Here the topic is NumPy array and we have to answer the questions given below.

## Question(a):

a=x[:,2]

In the above question, every third element is accessed in the given arrays.

That is

[1,2,3,4] -> 3

[5,6,7,8] -> 7

[9,10,11,12] -> 11

[13,14,15,16] -> 15

So the output will be [3,7,11,15]

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
a=x[:,2]
print(a)

## Question(b):

b=x[-1,:2]

-1 represents the last row in the array  [13,14,15,16]. In this question, the slice operation is performed. Here the output is [13,14].

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
b=x[-1,:2]
print(b)

## Question (C):

c=x[:,[True,False,False,True]]

As here there are no particular dimensions mentioned, this operation will be applied on all dimensions of the array.

Only the elements at the same position as True will be printed.
[1,2,3,4] ->[ 2 1 1 2]

[5,6,7,8] -> [ 6 5 5 6]

[9,10,11,12] -> [10 9 9 10]

[13,14,15,16] -> [14 13 13 14].

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
c=x[:,(True,False,False,True)]
print(c)

## Question(d):

d=x[0:2,0:2]

Here the first 0:2 is the dimension range. And the second 0:2 is the elements in each dimension to be considered.

[1,2,3,4] -> [1,2]

[5,6,7,8] -> [5,6]

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
d=x[0:2,0:2]
print(d)

## Question(e):

e=x[[0,1,2],[0,1,2]]

Here, the first list [0,1,2] represents the dimensions (first, second and third). And the second list [0,1,2] is the elements to be accessed.

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
e=x[[0,1,2],[0,1,2]]
print(e)

## Question(f):

f=x[0]**2

Here x[0] represents the first row in the array. And **2 represents the Square of the elements in the first row of the array.

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
f=x[0]**2
print(f)

## Question(g):

g=x.max(axis=1)

Max() returns the maximum element in the axis.

Axis=1 represents the rows.

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
g=x.max(axis=1)
print(g)

## Question(h):

h=x[:2,:2]+x[:2,2:]

Here, x[:2,:2] represents the first two elements in each of the first two dimensions.

And x[:2,2:] represents the elements from the second index to the end.

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
h=x[:2,:2]+x[:2,2:]
print(h)

## Question(i):

i=x[:2,:3].T

Numpy.T returns the transpose of the given array.

The rows become columns and the columns become rows. And:2 represents the first two dimensions and :3 represents the first three elements in each dimension.

So, the transpose of [[1,2,3],[5,6,7]] will be [[1,5],[2,6],[3,7]].

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
i=x[:2,:3].T
print(i)

## Question(j):

j=x[:2,:3].reshape((3,2))

The reshape() method converts an array into an array with the given number of dimensions and columns.

Here, (3,2) means that the number of dimensions of resulting array should be 3 and the number of columns should be 2.

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
j=x[:2,:3].reshape((3,2))
print(j)

## Question(k):

k=x[:,:2].dot([1,1])

The dot() method returns the dot product(matrix multiplication) of the given two arrays. Here the two arrays are x[:,:2] and [1,1]. X[:,:2] represents the first two elements in all the dimensions of the array.

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
k=x[:,:2].dot([1,1])
print(k)

## Question(l):

l=x[:,:2].dot([[3,0],[0,2]])

The dot() method returns the dot product(matrix multiplication) of the given two arrays.

Here the two arrays are x[:,:2] and [[3,0],[0,2]]

X[:,:2] represents the first two elements in all the dimensions of the array.

## Source code:

import numpy as np
x=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
l=x[:,:2].dot([[3,0],[0,2]])
print(l)