# modf() Python Numpy Function Explained In this article, we’ll understand the practical implementation of the Python numpy modf function. We’ll understand this function by practically implementing and explaining its parameters.

## modf() Python Numpy Function

The use of this function is to separate the integer and fractional parts of the array given to it as parameters. It means that two arrays are returned from this function. The fractional array is the first array that it returns and the second array elements consist of integers.

## Syntax of modf()

`numpy.modf(x, [out1, out2, ]/, [out=(None, None), ]*, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'modf'>`

## Parameters

1. The parameter ‘x‘ specifies the array that will be passed as an argument to the modf function and elements of this array will be divided into two arrays(integers, fractional).
2. The parameter ‘[out1, out2]‘ represents the arrays in which the resulting integer and fractional arrays will be stored. Using this parameter, we can specify our own arrays where we want to store the resulting arrays. If this parameter is not used, a tuple of two arrays will be generated as a result.
3. The ‘where‘ specifies on which elements the operation should be performed. If it’s True(default), then the operation will be applied to all elements.
4. The ‘casting‘ parameter controls the casting. It can be ‘safe’, ‘unsafe’, ‘same_kind’, ‘auto’, ‘equiv’, or ‘no’.
5. The ‘order‘ defines the memory layout order of output arrays. The value it takes can be ‘A'(any),  ‘C'(C-style), or ‘F'(Fortran-style).
6. We can provide a specific datatype to the resulting arrays using the ‘dtype‘. By default, it takes the data type of the array that is being input as an argument.
7. The ‘subok‘ can be used to decide whether to make the resulting arrays to be of the same subclass as an input array or not.

## Practical Implementation of modf Function (8 Examples)

### Example 1: Separate Integer and Fractional Part from 1D Numpy Array (parameter ‘out’ also used)

```import numpy as np

ar1D = np.array([123.45, 67.89, 100.0, 45.67, 78.90])

fract_part = np.empty_like(ar1D)
int_part = np.empty_like(ar1D)

np.modf(ar1D, out=(fract_part, int_part))

print("Original array:", ar1D)
print("Fractional part:", fract_part)
print("Integer part:", int_part)```

#### Output

```Original array: [123.45  67.89 100.    45.67  78.9 ]
Fractional part: [0.45 0.89 0.   0.67 0.9 ]  # e.g. .45 of original array is fractional part
Integer part: [123.  67. 100.  45.  78.]     # e.g. 123 is the integer part of original array first item```
• In this code, we first generated a numpy 1D(one-dimensional) array.
• Then we created 2 empty arrays that will have the same shape as the input array(ar1D).
• After that, we passed these 2 arrays to the ‘out’ parameter of the modf() function.
• Now the resulting arrays will be stored in the specified arrays we passed to the ‘out’ parameter. The fractional elements array will be stored in the first item of the tuple passed to the ‘out’ parameter while the integer items array will be stored inside the second array of ‘out’.
• The result can be seen in the above output.

### Example 2: Tuple Returned by modf Function

```arr1D = np.array([5.4,7.24,6.65,87.6,4.653])
resul=np.modf(arr1D)
print(resul)```

#### Output

`(array([0.4  , 0.24 , 0.65 , 0.6  , 0.653]), array([ 5.,  7.,  6., 87.,  4.]))`

Keep in mind that not specifying that parameter ‘out’ will result in a tuple returned by the modf function which will contain both the arrays(fractional, integer).

### Example 3: Storing the Resulting arrays(integer, fractional) using Tuple Unpacking

```array1D = np.array([8.3,87.2,43.5,77.2,6.21])

resulF,resulI=np.modf(arr1D)

print(resulF)
print(resulI)```

#### Output

```[0.4   0.24  0.65  0.6   0.653]
[ 5.  7.  6. 87.  4.]```

In this code, we used Python tuple unpacking to store the fractional and integer arrays in different variables.

### Example 4: Using parameter ‘where’ to decide on which items to perform the operation

```inArr = np.array([2.0, 1.1, 7.1, 2.2])
maskCond = np.array([True, False, True, False])
print(resArrF)
print(resArrI)```

#### Output

```[0.  1.1 0.1 2.2]
[ 2.  3.  7. 32.]```

In this code, we generated a numpy array of Booleans and then passed it to the ‘where’ parameter which will decide when to perform the operation and on which items. We can see that when the condition was False, then the operation was not performed but when it was true, then the operation was performed.

### Example 5: Parameter ‘casting’

```array1D = np.array([5.6, 3.10, 65.34, 32.0])
resF,resI= np.modf(array1D,casting='unsafe')
print(resF)
print(resI)```

#### Output

```[0.6  0.1  0.34 0.  ]
[ 5.  3. 65. 32.]```

By setting the ‘casting’ parameter to ‘unsafe’, we can see the output for both arrays.

### Example 6: Memory layout order (Parameter ‘order’)

```array1D = np.array([5.6, 3.10, 65.34, 32.0])
resFOrderF,resIOrderF= np.modf(array1D,order='F')
resFOrderC,resIOrderC= np.modf(array1D,order='C')

print('Order F')
print(resFOrderF)
print(resIOrderF)

print('Order C')
print(resFOrderC)
print(resIOrderC)```

#### Output

```Order F
[0.6  0.1  0.34 0.  ]
[ 5.  3. 65. 32.]
Order C
[0.6  0.1  0.34 0.  ]
[ 5.  3. 65. 32.]```

We specified the order ‘C’ and ‘F’ using the order parameter and displayed it. It specifies the memory layout for the resulting arrays.

### Example 7: Using parameter ‘dtype’

```input_arr = np.array([9, 2, 19])

f_part, int_part = np.modf(input_arr, dtype=float)

print("Input array:", input_arr)
print("Fractional", f_part)
print("Integer", int_part)```

#### Output

```Input array: [ 9  2 19]
Fractional [0. 0. 0.]
Integer [ 9.  2. 19.]```

We specified float in the ‘dtype’ parameter of the modf function. This function always returns fractional and integer arrays which have the same data type as input arrays.

The modf function does not allow you to specify a datatype of integer to the output arrays directly. But still if you want the array of integers part to be integers then use the numpy floor function with ‘.astype(int)‘. Let’s implement it practically.

```arr1D = np.array([9, 2, 19])
fracti_part, _ = np.modf(arr1D)
inte_part = np.floor(arr1D).astype(int)

print("Original 1D array:", arr1D)
print("Fractional part:", fracti_part)
print("Integer part:", inte_part)```

#### Output

```Original 1D array: [ 9  2 19]
Fractional part: [0. 0. 0.]
Integer part: [ 9  2 19]```

In this code, we can see that the integer part of arrays now contains integer datatype items.

### Example 8: Deciding to Make the Result Arrays Of the same Subclass as Input Arrays (Parameter ‘subok’)

```class MyArr(np.ndarray):
pass

array1Dimen = np.array([84.23, 73.21, 92.0], dtype=np.float64).view(MyArr)

fractSubokF, intSubokF = np.modf(array1Dimen, subok=False)
fractSubokT, intSubokT = np.modf(array1Dimen, subok=True)

print('When subok is False')
print(type(fractSubokF))
print(type(intSubokF))

print('When subok is True')
print(type(fractSubokT))
print(type(intSubokT))```

#### Output

```When subok is False
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
When subok is True
<class '__main__.MyArr'>
<class '__main__.MyArr'>```

In this example code, we created a subclass of ‘numpy.ndarray‘. Then we created an array as an instance of this subclass. We then try passing True and False to the parameter ‘subok’. It results in that when we specify the parameter ‘subok’ to True then the resulting arrays were of the same subclass as the input array. But when we set it to False, then the resulting arrays were not of the same subclass as the input array.

## Images of Code Examples    ## Conclusion

In conclusion, hope you have a clear-cut understanding of how to properly make use of the modf() Python Numpy function. We’ve tried to use the simplest examples so you can better understand the role of its parameters.

Thank you so much for reading it.