Python: Working with numpy arrays

What numpy arrays are?

Python: Working with numpy arrays
Photo by Markus Krisetya on Unsplash

What numpy arrays are?

Numpy arrays is a collection of numbers, it can be either single dimension or multidimension.

To work with numpy arrays we need first to import numpy to our script, commonly we import numpy in a short formimport numpy as np

Creating an 1D numpy array with integers from 1 to 10

To create an one dimension array with integers from 1 to 10 create the following script and execute it#!/usr/bin/env python3
import numpy as npif __name__ == '__main__':
   arr_integer = np.arange(10)
   print(arr_integer.dtype)
   print(arr_integer)

Output

  • The first print statement returns the data type of the numpy array, we can see that is a 64 bit integer.
  • The second print statement simply prints the array.int64
    [0 1 2 3 4 5 6 7 8 9]

Creating an 1D numpy array with floats from 1 to 10arr_float = np.arange(10,dtype=float)

  • We can use the dtype parameter in the arange function to define the type of the array, in this case is float.

Converting a Python list to a numpy array

To convert a python list we use the numpy array function and we assign the result to a variable.my_list = [1,2,3,4,5,6]
my_list_np = np.array(my_list)

Creating a numpy array from a CSV

Very often the numpy arrays we want to create need to be imported from existing data, a very common method is to import CSV files.

Create file test.csv with the following content32323,4353423,4354653,424242345,64642,42343645,3232,342342,32323,4424,3232

Create the following script in the same directory as test.csv and execute it#!/usr/bin/env python3
import numpy as npif __name__ == '__main__':
   my_arr = np.genfromtxt('test.csv',delimiter=',',dtype=int)
   print(my_arr)
   print(my_arr.size)

  • np.genfromtxt imports data from a csv file
  • the first parameter is the name of the file, in this case is test.csv
  • delimiter defines the csv elements delimiter, in this case is comma.
  • dtype the type of the numpy array to be created, in this case is integer

The last print returns the size of the array, in this case is 11 elements.

Output[ 32323 4353423 4354653 424242345 64642 42343645 3232 342342 32323 4424 3232]
11

How to slice a numpy array

To slice a numpy array is very easy, is the same way as we do in Python lists. Note that numbering also starts from 0.

The following will return the sixth, seventh and eight element of the arrayprint(my_arr[5:8])

Output[42343645 3232 342342]

How to apply a mathematical operation to the array elements

To apply a mathematical operation to a numpy array is very easy! The following will add “1” to sixth, seventh and eight element of the array. Note that any applied operation does not change the array elements in-place. If you want the results to be kept you need to assign them to another variableprint(my_arr[5:8]+1)
print(my_arr[5:8])

Output[42343646 3233 342343]
[42343645 3232 342342]

I hope you find this article useful :)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…