Author name: Satyabrata Jena

Find the index of value in Numpy Array using numpy.where()

How to find the index of value in Numpy array using numpy.where() in Python ?

NumPy or Numerical Python which is a general purpose array processing package and it is used for working with arrays. In python there numpy.where( ) function which is used to select elements from a numpy array, based on a condition and if the condition is satisfied we perform some operations.

Syntax : numpy.where(condition[ , x, y ])

Where

  • condition is a conditional expression which returns the Numpy array of bool
  • x,y are two optional arrays i.e either both are passed or not passed

In this article we will discuss about how to get the index of an element in a Numpy array (both 1D & 2D) using this function. So, let’s explore the concept well.

1. Find index of a value in 1D Numpy array :

Suppose array_one is an 1D Numpy array, So it will look like

array_one = np.array([10, 15, 20, 25, 30, 35, 40, 45, 50, 30, 60, 65, 70, 75, 80, 85, 30, 95])

Now, let’s find the index number of 30 in this Numpy array.

Program :

import numpy as np
# numpy array created with a list of numbers
array_one = np.array([10, 15, 20, 25, 30, 35, 40, 45, 50, 30, 60, 65, 70, 75, 80, 85, 30, 95])
# Get the index of elements with value 30
output = np.where(array_one == 30)
#printing the indices where the element 30 is present
print('Tuple of arrays returned : ', output)
print("Elements with value 30 exists at following indices", output[0], sep='\n')
Output :
Tuple of arrays returned :  (array([4, 9, 16]), )
Elements with value 30 exists at following indices 
[4 9 16]

As result is a tuple of arrays which contains the indices where value 30 exists in array array_one i.e. so it returned

(array([4, 9, 16]), )

And as a flat 1D array it returned

[4 9 16]

And if the element is not present in the Numpy array then returned array of indices will be empty. So let’s check that with an example

Program :

import numpy as np
# numpy array created with a list of numbers
array_one = np.array([10, 15, 20, 25, 30, 35, 40, 45, 50, 30, 60, 65, 70, 75, 80, 85, 30, 95])
# Get the index of elements with value 100
output = np.where(array_one == 100)
print('Tuple of arrays returned : ', output)
#printing the indices where the element 100 is present
#As 100 is not present in Numpy array i.e array_one so returned array with indices is empty 
print("Elements with value 30 exists at following indices", output[0], sep='\n')
Output :

Tuple of arrays returned : (array([  ]), )
Elements with value 30 exists at following indices
[  ]

2. Find index of a value in 2D Numpy array :

Suppose array_two is an 2D Numpy array, So it will look like

array_two = np.array([ [10, 15, 20], [25, 30, 35], [40, 45, 50], [30, 60, 65], [70, 75, 80], [85, 30, 95] ])

Now, let’s find the index number of 30 in this Numpy array.

Program :

import numpy as np
# numpy array created with a list of numbers
array_two = np.array([ [10, 15, 20], [25, 30, 35], [40, 45, 50], [30, 60, 65], [70, 75, 80], [85, 30, 95] ])
# Get the index of elements with value 30
output = np.where(array_two == 30)
#printing the indices where the element 30 is present
print('Tuple of arrays returned: ', output)
print("Elements with value 30 first exists at index:", output[0][0])
Output :
Tuple of arrays returned: (array([1, 3, 5]), array([1, 0, 1])) 
Elements with value 30 first exists at index: 1

Where,

First array [1, 3, 5] represents row indices where the element 30 is present

And the second array [1, 0, 1] represents column indices where the element 30 is present.

3. Get indices of elements based on multiple conditions in Numpy :

Example : 1

Get the indices of elements with value less than 20 and greater than 10.

Program :

import numpy as np
# numpy array created with a list of numbers
example_array = np.array([11, 6, 13, 8, 15, 16, 7, 15, 1, 2, 14, 19, 4, 20])
# Get the index of elements of value less than 20 and greater than 10
output = np.where((example_array  > 10) & (example_array  < 20))
print("Elements which are less than 20 and greater than 10 exists at indices",output, sep='\n')
Output :
Elements which are less than 20 and greater than 10 exists at indices 
(array([ 0, 2, 4, 5, 7, 10, 11]),)

Example : 2

Get the first index of an element in numpy array.

Program :

import numpy as np
# numpy array created with a list of numbers
example_array = np.array([15, 6, 13, 8, 15, 16, 7, 15, 1, 2, 14, 19, 4, 20])
# Get the index of elements with value less than 20 and greater than 10
output = np.where(example_array == 15)
if len(output) > 0 and len(output[0]) > 0:
    print('First Index of element with value 15 is ', output[0][0])
Output :
First Index of element with value 15 is 0

Example : 3

Get the indices of the elements which is less than equal to 2

Program :

import numpy as np 
arr = np.arange(10)
#prints the array
print(arr)
#prints indices of the elements which is less than equal to 2
print(arr[arr <= 2])
Output :
[0 1 2 3 4 5 6 7 8 9] 
[0 1 2]

Find the index of value in Numpy Array using numpy.where() Read More »

Python Dictionary – Append / Add / Update key-value pairs

How to add or append new key value pairs to a dictionary or update existing keys’ values in python.

In this article we will discuss about how we can add or append new key value pairs to a dictionary or update existing keys’ values. As we know Dictionary in python is one of the important datatype which is used to store data values in key : value pair.

Syntax of dictionary :

dictionary_name = {key1: value1, key2: value2}

where,

  • key1, key2… represents keys in a dictionary. These keys can be a string, integer, tuple, etc. But keys needs to be unique.
  • value1, value2… represents values in dictionary. These values can be numbers, strings, list or list within a list etc.
  • key and value is separated by : symbol.
  • key-value pair symbol is separated by , symbol.

For example,

my_dictionary = {"Name":"Satya", "Address":"Bhubaneswar", "Age":20, }

So, now let’s explore the concept how to add a key:value pair to dictionary in Python.

Method #1 : Add/Update using update( ) function :

In python there is an inbuilt member function update( ) which can be used to add or update a new key-value pair to the dictionary.

dictionary_name.update(Iterable_Sequence of key: value)

Actually, this update( ) function adds the key-value pair in the dictionary and if the key exists in the dictionary then it updates the value of that key.

If the key does not exist in the dictionary then it will add the key-value pair in the dictionary. Like this

#Program

CovidCase = {"January": 100000, "February": 110000, "March": 120000, "April": 130000}
print("Before adding new value")
print(CovidCase)
#Adding new key-value
#As the key does not exist so the key May and it's value will be added
CovidCase.update({"May": "140000"})
print("After adding new value")
print(CovidCase)
Output :
Before adding new value
{"January": 100000, "February": 110000, "March": 120000, "April": 130000}
After adding new value
{"January": 100000, "February": 110000, "March": 120000, "April": 130000, "May": 1400000}

If the key exists in the dictionary then it will update the key-value pair in the dictionary. Like this

#Program 

CovidCase = {"January": 100000, "February": 110000, "March": 120000, "April": 130000} 
print("Before adding new value") 
print(CovidCase) 
#Adding new key-value 
#As the key exists,so key January value will be updated
CovidCase.update({"January": "140000"}) 
print("After adding new value") 
print(CovidCase)
Output : 
Before adding new value 
{"January": 100000, "February": 110000, "March": 120000, "April": 130000} 
After adding new value 
{"January": 140000, "February": 110000, "March": 120000, "April": 130000, "May": 1400000}

Method #2 : Add/Update using Subscript [ ] notation :

In python there is a subscript [] operator which can also be used to create a new key-value pair just by assigning a value to that key.

If the key does not exist in the dictionary then it will add the key-value pair in the dictionary. Like this

#Program

CovidCase = {"January": 100000, "February": 110000, "March": 120000, "April": 130000}
print("Before adding new value")
print(CovidCase)
#Adding new key-value
#As the key does not exist so the key May and it's value will be added
CovidCase["May"]=140000
print("After adding new value")
print(CovidCase)
Output : 
Before adding new value 
{"January": 100000, "February": 110000, "March": 120000, "April": 130000} 
After adding new value 
{"January": 100000, "February": 110000, "March": 120000, "April": 130000, "May": 1400000}

If the key exists in the dictionary then it will update the key-value pair in the dictionary. Like this

#Program

CovidCase = {"January": 100000, "February": 110000, "March": 120000, "April": 130000} 
print("Before adding new value") 
print(CovidCase) 
#Adding new key-value
#As the key exists,so key January value will be updated
CovidCase["January"]=140000 
print("After adding new value") 
print(CovidCase)
Output : 
Before adding new value 
{"January": 100000, "February": 110000, "March": 120000, "April": 130000} 
After adding new value 
{"January": 140000, "February": 110000, "March": 120000, "April": 130000, "May": 1400000}

Method #3 : Append using append( ) function:

In python there is an inbuilt function append( ) to add values to the keys of the dictionary. As we will add only value to the keys, so we need to find out the keys to which we want to append the values.

#Program

CovidCase = {"January": [], "February": [], "March": []} 
print("Before adding new value") 
print(CovidCase) 
#Adding new key-value
#As the key exists,so key January value will be updated
CovidCase["January"].append(140000)
CovidCase["February"].append(150000)
CovidCase["March"].append(160000)
print("After adding new value") 
print(CovidCase)
Output : 
Before adding new value 
{"January": [ ], "February": [ ], "March": [ ]} 
After adding new value 
{"January": 140000, "February": 150000, "March": 160000}

Method #4 : Adding values to an existing key-value pair

With the help of update( ) function and subscript [ ] operator the key-value pair is added in dictionary only if that key does not exist in dictionary and if the key exists in the dictionary then the respective value of the key in  dictionary is updated. But if we do not want to replace the value of an existing key dictionary but we want to add a new value to a current value then also we can do it by creating a function.

#Program

def add_value(dict_obj, key, value):
    # Checking the key exists or not
    if key in dict_obj:
        # Key exist in dict.
        # Checking type of value of key is list or not
        if not isinstance(dict_obj[key], list):
            # If type not in list, then make it list
            dict_obj[key] = [dict_obj[key]]
        # Adding the value in list
        dict_obj[key].append(value)
    else:
        # if key is not in dictionary then add key-value pair
        dict_obj[key] = value


CovidCase = {"January": 140000, "February": 150000, "March": 160000}
#Before adding
print(CovidCase)
#After adding
add_value(CovidCase, "February", 170000)
print(CovidCase)
Output : 
Before adding new value 
{"January": [ ], "February": [ ], "March": [ ]} 
After adding new value 
{"January": 140000, "February": [150000, 170000] "March": 160000}

Python Dictionary – Append / Add / Update key-value pairs Read More »

Python: Get file size in KB, MB, or GB – human-readable format

In this tutorial of getting the file size in KB, MB, or GB in Human-readable format in Python, we are going to see how we can get the sizes of different files in bytes, kilobytes, megabytes, gigabytes, etc.

Different ways to get file size in Bytes, Kilobytes, Megabytes, Gigabytes, etc

  1. Get file size using os.path.getsize()
  2. Get file size using os.stat().st_size
  3. Get size using pathlib.Path.stat().st_size
  4. Get file size in KiloBytes, MegaBytes, or GigaBytes

Method-1: Get file size in bytes using os.path.getsize()

In this function, we pass the path of the file into it and if the file exists it returns the file size in bytes. If the file does not exist it throws os.error.

Syntax:

Syntax : os.path.getsize(path)

Program to Get file size in bytes using os.path.getsize()

Source Code:

# Program to Get file size in bytes using os.path.getsize()

import os
def fileSizeInBytes(filePath):
    """Size of the file in bytes"""
    sizeOfFile = os.path.getsize(filePath)
    return sizeOfFile
filePath = 'movie.mkv'
#Getting the size of the file from by passing the path into the function
sizeOfFile = fileSizeInBytes(filePath)
print('Size of the file(In Bytes) - ', sizeOfFile)
Output :
Size of the file(In Bytes) -  349016027

Method-2: Get file size in bytes using os.stat().st_size

We can also find the size of the file using a stats( ) function available in Python’s OS module. It takes the file path as an argument and returns an object containing the file details.

Syntax:

os.stat(path, *, dir_fd=None, follow_symlinks=True)

Program to Get file size in bytes using os.stat().st_size

Source Code:

# Program to Get file size in bytes using os.stat().st_size

import os
def fileSizeInBytes(filePath):
    """Find the size of the file in bytes"""
    statOfFile = os.stat(filePath)
    #Size of file from the stats
    sizeOfFile = statOfFile.st_size
    return sizeOfFile
filePath = 'movie.mkv'
#Getting the size of the file from by passing the path into the function
sizeOfFile = fileSizeInBytes(filePath)
print('Size of the file(In Bytes) - ', sizeOfFile)
Output :
Size of the file(In Bytes) -  349016027

Method-3: Get file size in bytes using pathlib.Path.stat().st_size

 

# program to Get file size in bytes using pathlib.Path.stat().st_size

from pathlib import Path
def fileSizeInBytes(filePath):
    """Size of the file in bytes"""
    fileObj = Path(filePath)
    #Size of file from the stats
    sizeOfFile = fileObj.stat().st_size
    return sizeOfFile
filePath = 'movie.mkv'
#Getting the size of the file from by passing the path into the function
sizeOfFile = fileSizeInBytes(filePath)
print('Size of the file(In Bytes) - ', sizeOfFile)
Output :
Size of the file(In Bytes) -  349016027

Method-4: Get file size in human-readable units like kilobytes (KB), Megabytes (MB), or GigaBytes (GB)

We can get the file sizes using the above method but we can not convert them to KB, MB, GB, TB directly so we have to create a function. The function would convert the units and so us the size of the file in our desired unit.

Here is a conversion table to switch from bytes to another unit of measurement :

UnitsNumber of bytes
KiloBytes1024
MegaBytes1024*1024 = 1 048 576‬
GigaBytes1024*1024*1024 = 1 073 741 824

et file size in human-readable units like kilobytes (KB), Megabytes (MB), or GigaBytes (GB

#Program to Get file size in human-readable units like KB, MB, GB, TB

import enum
import os

class sizeUnit(enum.Enum):
    #class to store the various units
    BYTES = 1
    KB = 2
    MB = 3
    GB = 4

def unitConvertor(sizeInBytes, unit):
    #Cinverts the file unit
    if unit == sizeUnit.KB:
        return sizeInBytes/1024
    elif unit == sizeUnit.MB:
        return sizeInBytes/(1024*1024)
    elif unit == sizeUnit.GB:
        return sizeInBytes/(1024*1024*1024)
    else:
        return sizeInBytes

def fileSize(filePath, size_type):
    """File size in KB, MB and GB"""
    size = os.path.getsize(filePath)
    return unitConvertor(size, size_type)

filePath = 'movie.mkv'
sizeOfFile = fileSize(filePath,sizeUnit.BYTES)
print('Size of the file in - ', sizeOfFile)
#Printing the size of file in KB
sizeOfFile = fileSize(filePath,sizeUnit.KB)
print('Size of the file in KB - ', sizeOfFile)
#Printing the size of file in MB
sizeOfFile = fileSize(filePath,sizeUnit.MB)
print('Size of the file in MB - ', sizeOfFile)
#Printing the size of file in GB
sizeOfFile = fileSize(filePath,sizeUnit.GB)
print('Size of the file in GB - ', sizeOfFile)
Output :
Size of the file in -  349016027
Size of the file in KB -  340835.9638671875
Size of the file in MB -  332.8476209640503
Size of the file in GB -  0.32504650484770536

Check if the file exists before checking for the size of the file

In case the file does not exist it will throw an error disrupting our program’s execution. So to handle that we can check if the file exists or not and if not then handle it by using the if-else block.

# Program to check if the file exists before checking for the size of the file

import os 

def fileSizeInBytes(filePath):
    """ Size of the file in bytes"""
    sizeOfFile = os.path.getsize(filePath)
    return sizeOfFile

#Passing a file name that does not exist into the program
fileName = 'nonExistentFile.txt'
if os.path.exists(fileName):
    size = fileSizeInBytes(fileName)
    print('Size of file in Bytes : ', size)
else:
    print('File not there')
Output :
File not there

Python: Get file size in KB, MB, or GB – human-readable format Read More »

Python Numpy: flatten() vs ravel() | Difference between the ravel numpy and flatten numpy functions

In this tutorial, python beginners and experts can easily learn about the difference between numpy ravel and flatten functions. The main aim of both numpy.ravel() and numpy.flatten() functions are the same i.e. to flatten a numpy array of any shape. Before learning about flatten ravel differences, let’s take a look at the basic overview of both the numpy functions from here.

numpy.ravel()

ravel() is a built-in function in the module of numpy that returns a view of the original array and also returns a flattened 1D view of the numpy array object. Moreover, it accepts an array-like element as a parameter.

Syntax of the ravel() function in numpy:

numpy.ravel(a, order='C')

numpy.ndarray.flatten()

Flatten always returns a copy. The function of the flatten() is a faster member function of the numpy array where it returns a flattened 1D copy of the numpy array object.

Syntax of Numpy flatten() function:

ndarray.flatten(order='C')

Let’s discuss some of the differences between the numpy ravel & flatten function:

Differences between ravel() & flatten()

First of all, import the numpy module,

import numpy as np

A quick view about the difference between flatten and ravel functions in numpy is furnished below:

a.flatten()

(i) Return copy of the original array

(ii) If you modify any value of this array value of the original array is not affected.

(iii) Flatten() is comparatively slower than ravel() as it occupies memory.

(iv) Flatten is a method of a ndarray object.

a.ravel()

(i) Return only reference/view of original array

(ii) If you modify the array you would notice that the value of the original array also changes.

(iii) Ravel is faster than flatten() as it does not occupy any memory.

(iv) Ravel is a library-level function.

For in-depth knowledge on the difference between flatten and ravel numpy functions, kindly check out the further sections without any fail.

Difference 1: Performance: Copy vs view

Thenumpy.ndarray.flatten()function returns a flatten copy of numpy array object only, whereas numpy.ravel() returns a flattened view of the input array if possible.

Difference Between flatten and ravel numpy

import numpy as sc
# Creation of 2D Numpy array
arr_twoD = sc.array([[10, 20, 30],
                [40, 50, 60],
                [70, 85, 90]])
print('2D array')                
print(arr_twoD)
# Convert the 2D array to flattened 1d view
flatn_arr = arr_twoD.flatten()
print('Flattened 1D view')
flatn_arr[1] = 55
print(flatn_arr)
print(arr_twoD)
Output :
2D array
[[10 20 30]
 [40 50 60]
 [70 85 90]]
Flattened 1D view
[10 55 30 40 50 60 70 85 90]
[[10 20 30]
 [40 50 60]
 [70 85 90]]

Here we can successfully change 2nd element of the numpy array that is reflected in the 1D numpy array. But the original 2D array is unaffected which denotes that flatten() returns a copy of the input array.

Let’s try using numpy.ravel() to convert 2D numpy array to 1D array

ravel() returns a flattened view of the numpy array object if possible. When there is any change done in the view object it will also get reflected in the original numpy array.

convert 2D numpy array to 1D array using numpy.ravel()

import numpy as sc
# Creation of 2D Numpy array
arr_twoD = sc.array([[10, 20, 30],
                [40, 50, 60],
                [70, 85, 90]])
print('2D array')                
print(arr_twoD)
# Convert the 2D array to flattened 1d view
flatn_arr = sc.ravel(arr_twoD)
flatn_arr[1] = 55
print('Flattened 1D view:')
print(flatn_arr)
print(arr_twoD)
Output :
2D array
[[10 20 30]
 [40 50 60]
 [70 85 90]]
Flattened 1D view:
[10 55 30 40 50 60 70 85 90]
[[10 55 30]
 [40 50 60]
 [70 85 90]]

We can check of the ravel()function returns a view object or not by using the base attribute of the returned object. If it is None then it returns to the original numpy array i.e. it returns a flattened array which is a view only.

using numpy.ravel() to convert 2D numpy array to 1D array

import numpy as sc
# Creation of 2D Numpy array
arr_twoD = sc.array([[10, 20, 30],
                [40, 50, 60],
                [70, 85, 90]])
print('Original 2D array')                
print(arr_twoD)
# Convert the 2D array to flattened 1d view
flatn_arr = sc.ravel(arr_twoD)
if flatn_arr.base is not None:
    # Change the 2nd element of flat array
    flatn_arr[1] = 55
    # Modification will be reflected in 2D numpy array and flattened array
    print('Flattened 1D view:')
    print(flatn_arr)
    print(arr_twoD)
Output :
Original 2D array
[[10 20 30]
 [40 50 60]
 [70 85 90]]
Flattened 1D view:
[10 55 30 40 50 60 70 85 90]
[[10 55 30]
 [40 50 60]
 [70 85 90]]

So ravel() returns a view of the input array and hence here the performance of ravel() is better than flatten().

Difference 2: Compatibility with other array-like sequences (list etc)

The ndarray.flatten()function can be used to flatten a numpy array object only. In the case of a numpy.ravel()function being a built-in function, it accepts an array-like element, so we can even pass a list to it.

Program for Compatibility with other array like sequences

import numpy as sc
# Creation of list of lists
list_lists = [[10, 20,30],
[40, 50, 60],
[70, 85, 90]]
# Creation of flattened numpy array from list of lists
flatn_arr = sc.ravel(list_lists)
print('Flattened 1D Array:')
print(flatn_arr)
Output :
Flattened 1D Array:
[10 20 30 40 50 60 70 85 90]

While this is not feasible with ndarray.flatten() function.

Conclusion on A view of the original array using numpy ravel

Therefore, to conclude in the end there are 2 main differences between the ndarray.flatten() and numpy.ravel() function,

  1. ravel() function returns a view of the array if possible, where flatten() always returns a copy. Therefore the performance of ravel() is much better than flatten()
  2. ravel() function can accept other array-like elements like lists etc. Although, flatten() can work with numpy arrays only.

Python Numpy: flatten() vs ravel() | Difference between the ravel numpy and flatten numpy functions Read More »

np.delete(): Remove items/rows/columns from Numpy Array | How to Delete Rows/Columns in a Numpy Array?

In this tutorial, we will discuss about how we can remove items/rows/columns from the Numpy array. We can remove elements from a NumPy array by index position using numpy.delete( ). Get to know the syntax of numpy.delete() function and learn about all its parameters with a clear explanation. Refer to the further modules to be aware of procedures on how to remove or delete an element or multiple elements in rows/columns of 1D and 2D Arrays by its Index Position.

How to remove items/rows/columns from Numpy array using np.delete() in Python?

Python provides a method to delete elements from the numpy array depending on the index position and its syntax is provided below.

numpy.delete( )

Python’s NumPy library has a method to delete elements. It is done by using the np.delete( ) function.

Syntax:

numpy.delete(arr, obj, axis=None)

Where,

  • arr: Array from which elements are to be deleted
  • obj: Index position or positions from which elements are to be deleted
  • axis: The axis along which we want to delete (1 means delete along columns, 0 means delete along the rows, if None then the array is flattened out and then the elements are to be deleted)

It returns a copy of the array with the required elements deleted.

Delete an Element in 1D Numpy Array by its Index position

Let’s see the implementation of it.

Python Program to Delete an element in 1D Numpy Array by its Index position

#Program :

import numpy as np
#Numpy array
arr = np.array([10,20,30,40,50,60,70,80,90])
#deleting a=item at index 2
arr = np.delete(arr, 2)
print('Modified Numpy Array after deleting element at index 2')
print(arr)
Output :
Modified Numpy Array after deleting element at index 2
[10 20 40 50 60 70 80 90]

Delete multiple elements in 1D Numpy Array by its Index position

Let’s see the implementation of it.

Python Program to Delete Multiple Elements in 1D Numpy Array by its Index position

#Program : 

import numpy as np 
#Numpy array 
arr = np.array([10,20,30,40,50,60,70,80,90]) 
#deleting a=item at index 2,5,7 
arr = np.delete(arr, 2) 
print('Modified Numpy Array after deleting element at index 2,5,7') 
print(arr)
Output :
Modified Numpy Array after deleting element at index  2,5,7
[10 20 40 50 70 90]

Deleting rows & columns from a 2D Numpy Array

Delete a column in 2D Numpy Array by its column number

if we want to delete a column from a 2D numpy array using np.delete() then we have to pass the axis=1 along with numpy array and index of column.

Let’s see the implementation of it.

#program

import numpy as np

arr2d = np.array([[10,20,30],
[40,50,60],
[70,80,90]])
#deleting elements at column 1
arr = np.delete(arr2d, 1, axis=1)
print('Modified Numpy Array by deleting element at index position 2,5,7')
print(arr)
Output :
Modified Numpy Array by deleting element at index position 2,5,7
[[10 30]
[40 60]
[70 90]]

Delete multiple columns in 2D Numpy Array by its column number

To delete multiple columns pass axis=1 and list of column numbers to be deleted along with numpy array to the function.

Let’s see the implementation of it.

#Program :

import numpy as np

arr2D = np.array([[11 ,12, 13, 11],
                [21, 22, 23, 24],
                [31, 32, 33, 34]])
arr2D = np.delete(arr2D, [2,3], axis=1)
print('Modified 2D Numpy Array by removing columns at index 2 and 3')
print(arr2D)
Output :
Modified 2D Numpy Array by removing columns at index 2 and 3
[[11 12]
[21 22]
[31 32]]

Delete a row in 2D Numpy Array by its row number

To delete a row from a 2D numpy array using np.delete() we need to pass the axis=0 along with numpy array and the row index.

Let’s see the implementation of it.

Python Program to Delete a row in 2D Numpy Array by its row number

#Program : 

import numpy as np 
arr2D = np.array([[11 ,12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]]) 
arr2D = np.delete(arr2D, 0, axis=0) 
print('Modified 2D Numpy Array by removing rowss at index 0') 
print(arr2D)
Output :
Modified 2D Numpy Array by removing rowss at index 0
[[21 22 23 24]
[31 32 33 34]]

Delete multiple rows in 2D Numpy Array by its row number

To delete multiple rows pass axis=0 and list of row numbers to be deleted along with numpy array to np.delete()

#Program :

import numpy as np

arr2D = np.array([[11 ,12, 13, 11],
                [21, 22, 23, 24],
                [31, 32, 33, 34]])
arr2D = np.delete(arr2D, [1, 2], axis=0)
print('Modified 2D Numpy Array by removing rows at index 1 and 2')
print(arr2D)
Output :
Modified 2D Numpy Array by removing rows at index 1 and 2
[[11 12 13 11]]

Delete specific elements in 2D Numpy Array by its index position

When we don’t mention axis value, the default value is none which means the array gets flattened. After that, we use np.delete() to delete elements from rows and columns. The function will return a flattened array without the deleted rows and column elements.

#program :

import numpy as np

arr2D = np.array([[11 ,12, 13, 11],
                [21, 22, 23, 24],
                [31, 32, 33, 34]])
arr2D = np.delete(arr2D, 2)
print('Modified 2D Numpy Array by removing element at row 0 column 2')
print(arr2D)
Output :
Modified 2D Numpy Array by removing element at row 0 column 2
[11 12 11 21 22 23 24 31 32 33 34]

np.delete(): Remove items/rows/columns from Numpy Array | How to Delete Rows/Columns in a Numpy Array? Read More »

How to save Numpy Array to a CSV File using numpy.savetxt() in Python

How to save Numpy Array to a CSV File using numpy.savetxt() in Python? | Savetxt Function’s Working in Numpy with Examples

NumPy arrays are very essential data structures for working with data in Python, machine learning models. Python’s Numpy module provides a function to save a numpy array to a txt file with custom delimiters and other custom options. In this tutorial, we will discuss the procedure of how to save Numpy Array to a CSV File with clear steps.

numpy.savetxt() Function

The numpy.savetxt() function is the counterpart of the NumPy loadtxt() function and can save arrays in delimited file formats such as CSV. Save the array we created with the following function call:

Synatx : numpy.savetxt(fname, array_name, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ', encoding=None)

Where,

  • fname: If the filename ends in .gz, the file is automatically saved in compressed gzip format. The loadtxt() function can understand gzipped files transparently.
  • arr_name: It indicates data to be saved like 1D or 2D numpy array.
  • fmt: It refers to a formatting pattern or sequence of patterns, which will be used while saving elements to file.
  • delimiter: It is optional, refers to string or character to be used as element separator
  • newline: It is optional, refers to string or character to be used as line separator
  • header: It refers to a string that is written at the beginning of the file.
  • footer: It refers to a string that to be written at the end of the txt file.
  • comments: It refers to a custom comment marker, where the default is ‘#’. It will be pre-appended to the header and footer.

How to save Numpy Array to a CSV File using numpy.savetxt() in Python?

One of the most common file formats for storing numerical data in files is the comma-separated variable format or CSV in Short. Usually, input data are stored in CSV Files as it is one of the most convenient ways for storing data.

Savetxt function is used to save Numpy Arrays as CSV Files. The function needs a filename and array as arguments to save an array to CSV File. In addition, you need to mention the delimiter; for separating each variable in the file or most commonly comma. You can set via the “delimiter” argument.

Example Program on How to Save a Numpy Array to a CSV File

#Program :

import numpy as np
def main():
   # Numpy array created with a list of numbers
   array1D = np.array([9, 1, 23, 4, 54, 7, 8, 2, 11, 34, 42, 3])
   print('Real Array : ', array1D)
   print('<** Saved 1D Numpy array to csv file **>')
   # Save Numpy array to csv
   np.savetxt('array.csv', [array1D], delimiter=',', fmt='%d')
   print('*** Saving 1D Numpy array to csv file with Header and Footer ***')
   # Saving Numpy array to csv with custom header and footer
   np.savetxt('array_hf.csv', [array1D], delimiter=',', fmt='%d' , header='A Sample 2D Numpy Array :: Header', footer='This is footer')

   print('*** Saving 2D Numpy array to csv file ***')
   # A 2D Numpy array list of list created
   array2D = np.array([[111, 11, 45, 22], [121, 22, 34, 14], [131, 33, 23, 7]])
   print('2D Numpy Array')
   print(array2D)
   # Saving 2D numpy array to csv file
   np.savetxt('2darray.csv', array2D, delimiter=',', fmt='%d')
   # Saving 2nd column of 2D numpy array to csv file
   np.savetxt('2darray_column.csv', [array2D[:,1]], delimiter=',', fmt='%d')
   # Saving 2nd row of 2D numpy array to csv file
   np.savetxt('2darray_row.csv', [array2D[1] ], delimiter=',', fmt='%d')

   # Creating the type of a structure
   dtype = [('Name', (np.str_, 10)), ('Marks', np.float64), ('GradeLevel', np.int32)]
   #Creating a Strucured Numpy array
   structuredArr = np.array([('Sam', 33.3, 3), ('Mike', 44.4, 5), ('Aadi', 66.6, 6), ('Riti', 88.8, 7)], dtype=dtype)
   print(structuredArr)
   # Saving 2D numpy array to csv file
   np.savetxt('struct_array.csv', structuredArr, delimiter=',', fmt=['%s' , '%f', '%d'], header='Name,Marks,Age', comments='')
if __name__ == '__main__':
  main()

Python Program for Saving Numpy Array to a CSV File

Output:

Real Array :  [ 9  1 23  4 54  7  8  2 11 34 42  3]
<** Saved 1D Numpy array to csv file **>
<** Saved 1D Numpy array to csv file with custom  Header and Footer **>
<** Save 2D Numpy array to csv file **>
* 2D Numpy Array *
[[111  11  45  22]
[121  22  34  14]
[131  33  23   7]]
[('Rags', 33.3, 3) ('Punit', 44.4, 5) ('Drishti', 66.6, 6)  ('Ritu', 88.8, 7)]

The Passed Delimeter ‘,’ will change to CSV Format. In addition, the format string %d passed will store the elements as integers. By default, it will store numbers in float format. Keep in mind that if you don’t mention [] around numpy array to change it to list while passing numpy.savetxt() comma delimiter willn’t work and uses ‘\n’ by default. Thus, surrounding array by [] i.e. [arr] is mandatory.

Save 1D Numpy array to CSV file with Header and Footer

In order to add comments to the header and footer while saving to a CSV File, we can pass the Header and Footer Parameters as such

# Save Numpy array to csv with custom header and footer
np.savetxt('array_hf.csv', [arr], delimiter=',', fmt='%d' , header='A Sample 2D Numpy Array :: Header', footer='This is footer')

Usually, By default comments in both the header and footer are pre-appended by ‘#’. To change this we can pass the parameter comments like comments=’@’.

Final Words

Numpy savetxt can be an extremely helpful method for saving an array to CSV File. If you want to manipulate or change the existing data set this can be a great method. If you have any queries don’t hesitate to ask us via comment box so that we can get back to you at the soonest possible. Bookmark our site for the latest updates on Python, Java, C++, and Other Programming Languages.

How to save Numpy Array to a CSV File using numpy.savetxt() in Python? | Savetxt Function’s Working in Numpy with Examples Read More »

Python Check if a Process is Running by Name and find its Process ID (PID)

Python: Check if a Process is Running by Name and find its Process ID (PID) | How to find the PID of a Running Process?

Do you want to determine whether a process is running or not by name and find its Process ID as well? Then, this is the right place as this article will shed light on you completely on how to check if a process is running or not using Name. We have provided enough examples for finding the Process ID PID by Name for better understanding. You can visit our site online to seek guidance or simply copy-paste the code from our portal and try to apply this knowledge and write a code on your own.

How to Check if there exists a Process running by Name and find its Process ID (PID)?

In this article, we will discuss finding a running process PIDs by name using psutil.

psutil library can be installed by using

pip install psutil

Determine if Process is Running or Not

Follow the simple guidelines for checking if a process is running or not. They are as such

  • def findProcessIdByName(processName):
  • for proc in psutil. process_iter():
  • pinfo = proc. as_dict(attrs=[‘pid’, ‘name’, ‘create_time’])
  • if processName. lower() in pinfo[‘name’]. lower() :
  • except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :

To check if a process is running or not we will iterate over all the running processes using psutil.process_iter() and during iteration match the process name.

So let’s see this example to check chrome process is running or not.

import psutil
def checkProcessRunning(processName):
    # Checking if there is any running process that contains the given name processName.
    #Iterate over the all the running process
    for proc in psutil.process_iter():
        try:
            # Check if process name contains the given name string.
            if processName.lower() in proc.name().lower():
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    return False;

# Checking if any chrome process is running or not.
if checkProcessRunning('chrome'):
    print('Yes chrome process was running')
else:
    print('No chrome process was running')

How to Check if a Process is Running or Not

Output:

No chrome process was running

As in my system chrome instances are not running it will return False.

Finding the Process ID(PID) by Name?

In the above program, we checked any chrome instances were running or not. But in this example, we will see how to get the Process ID of a running ‘chrome.exe’ process. For that we will iterate over all the running processes and during iteration for each process whose name contains the given string, we will keep its information in a list. The following code helps you find the PID of a running process by Name.

import psutil
def findProcessIdByName(processName):
    # Here is the list of all the PIDs of a all the running process 
    # whose name contains the given string processName
    listOfProcessObjects = []
    #Iterating over the all the running process
    for proc in psutil.process_iter():
       try:
           pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
           # Checking if process name contains the given name string.
           if processName.lower() in pinfo['name'].lower() :
               listOfProcessObjects.append(pinfo)
       except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :
           pass
    return listOfProcessObjects;

# Finding PIDs od all the running instances of process 
# which contains 'chrome' in it's name
listOfProcessIds = findProcessIdByName('chrome')
if len(listOfProcessIds) > 0:
   print('Process Exists | PID and other details are')
   for elem in listOfProcessIds:
       processID = elem['pid']
       processName = elem['name']
       processCreationTime =  time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(elem['create_time']))
       print((processID ,processName,processCreationTime ))
else :
   print('No Running Process found with this text')

Finding Process ID by Name

Now, the contents of the list

(2604, 'chrome.exe', '2018-11-10 19:12:13')
(4276, 'chrome.exe', '2018-11-10 19:12:14')
(9136, 'chrome.exe', '2018-11-10 19:12:14')
(9616, 'chrome.exe', '2018-11-10 19:43:41')
(12904, 'chrome.exe', '2018-11-10 19:12:13')
(13476, 'chrome.exe', '2018-11-10 20:03:04')
(15520, 'chrome.exe', '2018-11-10 20:02:22')

This can also be done using a single line using list comprehension  i.e

# Finding the PIDs of all the running instances of process that contains 'chrome' in it's name
procObjList = [procObj for procObj in psutil.process_iter() if 'chrome' in procObj.name().lower() ]

And the same output will come like

psutil.Process(pid=2604, name='chrome.exe', started='19:12:13')
psutil.Process(pid=4276, name='chrome.exe', started='19:12:14')
psutil.Process(pid=9136, name='chrome.exe', started='19:12:14')
psutil.Process(pid=9616, name='chrome.exe', started='19:43:41')
psutil.Process(pid=12904, name='chrome.exe', started='19:12:13')
psutil.Process(pid=13476, name='chrome.exe', started='20:03:04')
psutil.Process(pid=15520, name='chrome.exe', started='20:02:22')

Related Programs:

 

Python: Check if a Process is Running by Name and find its Process ID (PID) | How to find the PID of a Running Process? Read More »

Delete elements from a Numpy Array by value or conditions in Python

Deleting elements from a NumPy Array by value or conditions in Python.

In this article we will discuss about how to delete elements from a NumPy Array by based on matching values or multiple conditions.

Remove all occurrences of an element with given value from numpy array :

Suppose we have a NumPy array and we want to delete all the occurrences of that particular element from that Numpy array.

#Program :

import numpy as np
# numpy array created from a list
arr = np.array([40,50,60,70,80,90,40,10,20,40])
print('Numpy Array before deleting all occurrences of 40 :')
print(arr)

# Removing all occurrences of elements with value 40 from numpy array
arr = arr[arr != 40]
print('Modified Numpy Array after deleting all occurrences of 40 :')
print(arr)
Output :
Numpy Array before deleting all occurrences of 40 :
[40 50 60 70 80 90 40 10 20 40]
Modified Numpy Array after deleting all occurrences of 40 :
[50 60 70 80 90 10 20]

The condition arr != 40 returns a bool array of same size as arr with value True at places where value is not equal to 40 and returns False at other places. Like this

[ False True True True True True False True True False]

When we will pass it to [] of numpy array arr then it will select the elemnts whose value is True. Means it will return the elements from arr which are not  equal to 40.

You can also delete column using numpy delete column tutorial.

Delete elements in Numpy Array based on multiple conditions :

Like above example, it will create a bool array using multiple conditions on numpy array and when it will be passed to [] operator of numpy array to select the elements then it will return a copy of the numpy array satisfying the condition suppose (arr > 40) & (arr < 80) means elements greater than 40 and less than 80 will be returned.

#Program : 

import numpy as np 
# numpy array created from a list 
arr = np.array([40,50,60,70,80,90,40,10,20,40]) 
print('Numpy Array before deleting any element :') 
print(arr) 

# Remove all occurrences of elements below 40 & greater than 80 
# Means keeping elements greater than 40 and less than 80
arr = arr[ (arr > 40) & (arr < 80) ]
print('Modified Numpy Array after deleting elements :') 
print(arr)
Output :
Numpy Array before deleting any element :
[40 50 60 70 80 90 40 10 20 40]
Modified Numpy Array after deleting elements :
[50 60 70]

Delete elements by value or condition using np.argwhere() & np.delete() :

By using np.argwhere() & np.delete()  we can also delete any elements.

#Program : 

import numpy as np 
# numpy array created from a list 
arr = np.array([40,50,60,70,80,90,40,10,20,40]) 
print('Numpy Array before deleting any element :') 
print(arr) 

# deleting all occurrences of element with value 40
arr = np.delete(arr, np.argwhere(arr == 40))
print('Modified Numpy Array after deleting all occurrences of 40') 
print(arr)
Output : 
Numpy Array before deleting all occurrences of 40 : 
[40 50 60 70 80 90 40 10 20 40] 
Modified Numpy Array after deleting all occurrences of 40 : 
[50 60 70 80 90 10 20]

In the above example np.delete() function will delete the element and np.argwhere() function will detect the index.
Means the condition arr == 40 will return a bool array like

[True False False False False False True False False True]

Where the condition arr == 40 matches it returns True and if condition fails it returns False. And when the bool array will be passed inside the function np.argwhere() then it will return the index positions where the values are True.

i.e

[ [0]
  [6]
  [9]]

These are the indices which represents the value 40.

When this index positions will be passed inside np.delete() function then the element present at those index positions will be deleted.

Delete elements by multiple conditions using np.argwhere() & np.delete() :

The concept is same like above only the difference is the elements will be deleted based on multiple conditions.

So, let’s see the implementation of it.

#Program : 

import numpy as np 
# numpy array created from a list 
arr = np.array([40,50,60,70,80,90,40,10,20,40]) 
print('Numpy Array before deleting any element :') 
print(arr) 

#It will delete all the elements which are greater than 40 and less than 80
arr = np.delete(arr, np.argwhere( (arr >= 40) & (arr <= 80) ))
print('Modified Numpy Array after deleting :') 
print(arr)
Output : 
Numpy Array before deleting any element : 
[40 50 60 70 80 90 40 10 20 40] 
Modified Numpy Array after deleting elements : 
[90 10 20]

Delete elements from a Numpy Array by value or conditions in Python Read More »

What’s placement new operator and why do we need it ?

Placement new operator and it’s need

This article is all about placement new operator.

Placement new operator :

placement new operator we use to pass a memory address to new as a parameter. This memory is used by the placement new operator to create the object and also to call the constructor on it and then returning the same passed address.

Need of placement new operator :

As we know when we create any object using new operator, then the memory is allocated on the heap.

int * ptr = new int;

But, while working sometimes we need to create an object dynamically for which some specific memory location will be allocated.

For example, we do not want new memory to be allocated on heap rather it needs to be allocated on a given memory address. Actually this scenario comes when we work on any embedded product or with shared memory. So, for this requirement we use placement new operator.

Below is an example code to achieve this :

// Program

#include <iostream>
#include <cstdlib>
#include <new>

int main()
{
// Here memory will not be allocated on heap.
int * space = new int[1004];
// It will use passed spacer to allocate the memory
int * ptr = new(space) int;
*ptr = 7;
std::cout<<(*ptr)<<std::endl;
delete [] buffer;
return 0;
}

What’s placement new operator and why do we need it ? Read More »

numpy.linspace() | Create same sized samples over an interval in Python

Create a Numpy array of evenly spaced samples over a range using numpy.linspace()

In this article we will discus about how to create a Numpy array of evenly spaced samples over a range using numpy.linspace().

Numpy.linspace()

Numpy module provides a function numpy.linspace() to create a evenly spaced samples over a specified interval i.e.

Syntax: numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
Where,
  • start : Represents the start value of the range
  • stop : Represents the end value of the range (Actually array doesn’t include this value but it acts as an end point)
  • num : Represents the number of samples to be generated (If it is not provided then optional value is taken as 50)
  • dtype : Represents datatype of elements.

To use Numpy we have to include the below module i.e.

import numpy as np

Let’s see below two examples to understand the concept.

Example-1 : Create 5 evenly spaced samples in interval [10, 50]

import numpy as np
# Create 5 evenly spaced samples in interval [10, 50]
temp_arr = np.linspace(10,50,5)
print(temp_arr)
Output :
[10. 20. 30. 40. 50.]
We can also specify the datatype as int32 by dtype.
import numpy as np
# Create 5 evenly spaced samples in interval [10, 50]
temp_arr = np.linspace(10,50,5,dtype=np.int32)
print(temp_arr)
Output :
[10 20 30 40 50]

Example-2 : Get the Step size from numpy.linspace()

When we will pass retstep=True in numpy.linspace().

Then it will return step size between samples.
import numpy as np
# Create 5 evenly spaced samples in interval [10, 50]
temp_arr, step = np.linspace(10,50,5,dtype=np.int32,retstep=True)
print('Printing the Numpy Array contents: ')
print(temp_arr)
print('The step size between two samples  : ', step)
Output :
Contents of the Numpy Array : 
[10 20 30 40 50]
Step size between two elements : 10.0

numpy.linspace() | Create same sized samples over an interval in Python Read More »