Read More “How to rotate a matrix?” »

The post How to rotate a matrix? appeared first on Pythoneo.

]]>With Numpy it is very easy to rotate matrix 90 degrees. There is dedicated rot90 Numpy method.

import numpy as np my_array = np.array([[11, 12, 13], [21, 22, 23], [31, 32, 33]]) print(f"This is my array: \n{my_array}") rotate_array = np.rot90(my_array) print(f"Array rotated: \n{rotate_array}")

To rotate 270 degrees just add 3 as a parameter to rot90 function. This parameter makes 3 rounds of 90 degrees rotation (counter clockwise).

import numpy as np my_array = np.array([[11, 12, 13], [21, 22, 23], [31, 32, 33]]) print(f"This is my array: \n{my_array}") rotate_array = np.rot90(my_array, 3) print(f"Array rotated 270 degrees: \n{rotate_array}")

This is 270 degrees rotation but we can also say that this is left rotation because this is how to rotate an matrix left direction.

To rotate 180 put np.rot90(my_array, 2). Having np.rot90(my_array, 4) will not change the array at all.

You can also rotate an array over the axes.

import numpy as np my_array = np.array([[11, 12, 13], [21, 22, 23], [31, 32, 33]]) print(f"This is my array: \n{my_array}") rotate_array = np.rot90(my_array, axes=(1, 0)) print(f"Array rotated over axes=(1, 0): \n{rotate_array}")

I used np.rot90(my_array, axes=(1, 0)) and this is how it got rotated.

The post How to rotate a matrix? appeared first on Pythoneo.

]]>Read More “Count how many zeros you have in array” »

The post Count how many zeros you have in array appeared first on Pythoneo.

]]>For sure you will be surprised. To count a zeros frequency in your matrix you need to use count_nonzero function. Then as a parameter you should use your array == 0. Believe me it works.

import numpy as np my_array = np.array([[1, 0, 5], [5, 3, 0], [0, 0, 2]]) zeros_frequency = np.count_nonzero(my_array == 0) print(f"Count of zeroes in my array: \n{zeros_frequency}")

As you can see count_nonzero function counted zeros.

The post Count how many zeros you have in array appeared first on Pythoneo.

]]>Read More “How to empty an array in Numpy?” »

The post How to empty an array in Numpy? appeared first on Pythoneo.

]]>There are 2 different ways to empty an array in Numpy.

The first method is to use Numpy empty function.

import numpy as np my_array = np.empty(shape=(0, 0)) print(f"My empty array: \n{my_array}")

Empty function created an empty matrix. Thanks to shape parameters you can define a shape of an array. Of course you can change it in the future using reshape function.

Another way is just to create an array but not setting any elements to that. Python allows us to do that.

import numpy as np my_array = np.array([]) print(f"My empty array: \n{my_array}")

The result is exactly the same. Your choise which way you would prefer to choose to create an empty matrix in Numpy.

The post How to empty an array in Numpy? appeared first on Pythoneo.

]]>Read More “How to copy array with Numpy?” »

The post How to copy array with Numpy? appeared first on Pythoneo.

]]>To create a copy of array you just need to use Numpy copy function. As an argument of copy function put your array.

import numpy as np my_array = np.array([1, 3, 5]) print(f"This is my array: \n{my_array}") array_copy = np.copy(my_array) print(f"This is copy of my array: \n{array_copy}")

This is how to create copy. Alternatively you can assign an array to another (array_copy = my_array) or create a view of an array.

The post How to copy array with Numpy? appeared first on Pythoneo.

]]>Read More “How to convert array to binary?” »

The post How to convert array to binary? appeared first on Pythoneo.

]]>Having an array created you can save it as an binary file using tofile setting bin as your file extension.

import numpy as np my_array = np.array([1, 3, 5]) my_binary = my_array.tofile("my_binary.bin")

Python created my_binary.bin file in the default directory. You set your own as you want of course

The post How to convert array to binary? appeared first on Pythoneo.

]]>Read More “How to normalize array in Numpy?” »

The post How to normalize array in Numpy? appeared first on Pythoneo.

]]>To normalize an array in Numpy you need to divide your array by np.linalg.norm of your array. Just take a look at below example or normalization.

import numpy as np my_array = np.array([[1, 3, 5], [7, 9, 11], [13, 15, 17]]) print(f"My array: \n{my_array}") my_normalized_array = my_array / np.linalg.norm(my_array) print(f"My normalized array: \n{my_normalized_array}")

Python returned normalized array as an output.

The post How to normalize array in Numpy? appeared first on Pythoneo.

]]>Read More “How to permute in Numpy?” »

The post How to permute in Numpy? appeared first on Pythoneo.

]]>There are two different use cases of permutations in Python you should bew aware of.

1. Permutation of random generated array.

Use random permutation Numpy function and use a number of elements as an arguments. I need 10 elements array to randomly generate.

import numpy as np for i in range(5): my_array = np.random.permutation(10) print(f"My generated permuted array: \n {my_array}")

Python returned five different arrays 10 items each.

2. Permutation of existing array.

Put your array as an argument of random permutation function.

import numpy as np my_array = np.array([1, 3, 5, 7, 9]) for i in range(5): permuted_array = np.random.permutation(my_array) print(f"My permuted array: \n {permuted_array}")

Your array has been permuted five times.

The post How to permute in Numpy? appeared first on Pythoneo.

]]>Read More “How to print full array in Numpy?” »

The post How to print full array in Numpy? appeared first on Pythoneo.

]]>By default Python will not print the whole array.

There is a clever way to print full array. To do that you need to increase np.set_printoptions threshold. To be sure threshold will be enough just set is as the size of your array. This is the most optimal solution.

import numpy as np my_array = np.arange(10000).reshape(10, -1) np.set_printoptions(threshold=np.size(my_array)) print(f"My fully printed array: \n {my_array}")

The other method is to set np.set_printoptions(threshold=np.inf) which is the positive infinity.

import numpy as np my_array = np.arange(10000).reshape(10, -1) np.set_printoptions(threshold=np.inf) print(f"My fully printed array: \n {my_array}")

The post How to print full array in Numpy? appeared first on Pythoneo.

]]>Read More “How to check for nan in array?” »

The post How to check for nan in array? appeared first on Pythoneo.

]]>To check if there any NaN (Not a Number) values in the array you need to learn how to use Numpy nan and isnan functions.

First I created example array which does contain nan value.

Next I used a trick. NaN element will be the lowest. So it is enough for me to check if the lowest value is nan. That’s why I implemented np.isnan(np.min(my_array)) which finds me if min value is nan.

import numpy as np my_array = np.array([1, 2, 4, np.nan]) is_nan = np.isnan(np.min(my_array)) print(f"Is there NaN element is my array? \n {is_nan}")

Python printed out boolean if nan exists or not.

The post How to check for nan in array? appeared first on Pythoneo.

]]>Read More “How many distinct values in array?” »

The post How many distinct values in array? appeared first on Pythoneo.

]]>To calculate frequency of values use Numpy unique function. As arguments use your array and return_counts=True.

Then I created variable unique_values and using asarray function created new array with the values and corresponding frequencies.

import numpy as np my_array = np.array([1, 1, 2, 2, 2, 3, 3, 3, 4, 77]) unique, counts = np.unique(my_array, return_counts=True) unique_values = np.asarray((unique, counts)).T print(f"This is the frequency of values" f" in my array: \n {unique_values}")

Python printed out frequency of values. As you can see in my array I had 1, 2, 3, 4 and 77. In second column Python returned the number of these values.

The post How many distinct values in array? appeared first on Pythoneo.

]]>