NumPy Arrays( Part III )

Vivek Nikam and Abhay Chaudhari
3 min readJul 5, 2021

--

smart and simple

Photo by XPS on Unsplash

In this article, we will see some functions related to linear algebra and more.

eye: It creates an identity matrix.

diag: It creates a diagonal matrix.

dot: It is used to multiply two matrices.

here I have created two matrices randomly.

We can concatenate two matrices horizontally(hstack) as well as vertically(vstack). but for horizontal concatenation, the number of rows must be the same, similarly for verticle concatenation columns.

hstack:

vstack:

___________________________________________________________________

Relational operations on numpy:

Let’s suppose I have an array.

A = [-8 2 6 -10 -2]

Now I want to compare every value to check whether it is positive or not.

we know that broadcasting is possible in numpy similarly, we can check every value in a single statement.

A > 0

It returns a boolean array..

Returns True if the value is greater than 0 else False. (as per the condition we have mentioned)

let’s assign the above statement to a variable.

Using that variable we can get those actual values instead of True and False.

similarly,

we can directly get the result in a single statement.

___________________________________________________________________

Let’s take another example,

B = [ 2 3 4 6 7 8 9 10 11 12 14 18]

Now, I want those values which are divisible by 2 as well as 3.

res1 = B % 2 == 0
res2 = B % 3 == 0

Similarly, instead of res1 and res2:

Try some more examples then your concept will get more clear.

Thanks for reading!!

--

--

No responses yet