4 Efficient Ways to Use the Numpy c_ Function

In the Numpy module, we have discussed many functions used to operate on the multidimensional array like matrix addition, matrix subtraction, etc. Sometimes, we come to a situation where we want to join the multidimensional array in a single array. In this tutorial, we will discuss the concept of the Numpy c_ method, which is used to join matrices by row, adding the two matrices left and right.

What is numpy c_?

Numpy c_[ ] method is used to translates slice objects to concatenation along the second axis. It is used to join two matrices by row, which is to add the two matrices left and right, and the number of rows is equal. It is similar to the merge() function in pandas.

Syntax of numpy c_

numpy.c_ = <numpy.lib.index_tricks.CClass object>

Different Examples to Show How to Use Numpy c_

Here, we will be discussing how we can write the c_[ ] method from the numpy library.

1. Taking two numpy array

In this example, we will import the numpy library. Then, we will take two numpy arrays and apply the numpy c_[ ] method in them and store the output in the variable name arr. Hence, you will see the output. Let us look at the example for understanding the concept in detail.

#importing numpy library
import numpy as np
arr = np.c_[np.array([[1,2,3]]), np.array([[4,5,6]])]
print(arr)

Output:

[[1 2 3 4 5 6]]

Explanation:

  • Firstly, we will be importing the numpy library with an alias name as np.
  • Then, we will apply the numpy c_ [ ] method.
  • Inside, which we have passed two numpy arrays.
  • At last, we will print the output.
  • Hence, in the output, you can see both the arrays are concatenated.

2. Taking two numpy array and one different values

In this example, we will import the numpy module. Then, we will have two numpy arrays, and one integer value will be applied inside the numpy c_[ ] method and store the output in the arr variable. Hence, we will see the output. Let us look at the example for understanding the concept in detail.

#importing numpy library
import numpy as np
arr = np.c_[np.array([[1,2,3]]),0, np.array([[4,5,6]])]
print(arr)

Output:

[[1 2 3 0 4 5 6]]

Explanation:

  • Firstly, we will be importing the numpy library with an alias name as np.
  • Then, we will apply the numpy c_[ ] method.
  • Inside, which we have passed two numpy arrays and an integer value which is 0.
  • At last, we will print the output.
  • Hence in the output, you can see both the arrays are concatenated with an integer value included in between them.

3. Taking two numpy array and two different values

In this example, we will import the numpy module. We will then have two numpy arrays, and two integer values will be applied inside the numpy c_[ ] method and store the output in the arr variable. Hence, we will see the output. Let us look at the example for understanding the concept in detail.

#importing numpy library
import numpy as np
arr = np.c_[np.array([[1,2,3]]),0,1, np.array([[4,5,6]])]
print(arr)

Output:

[[1 2 3 0 1 4 5 6]]

Explanation:

  • Firstly, we will be importing the numpy library with an alias name as np.
  • Then, we will apply the numpy c_[ ] method.
  • Inside, which we have passed two numpy arrays and an integer value which is 0 and 1.
  • At last, we will print the output.
  • Hence in the output, you can see both the arrays are concatenated with an integer value included in between them.

4. Taking input from ones and zeros in numpy c_

In this example, we will import the numpy library. Then, we will take the input from numpy ones and numpy zeros and apply the numpy c_[ ] method in it and store the output in the variable arr. hence, you can see the output. Let us look at the example for understanding the concept in detail.

#import numpy library
import numpy as np
a = np.ones((5))
b = np.zeros((5,5))
arr = np.c_[a,b]
print(arr)

Output:

[[1. 0. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0. 0.]]

Explanation:

  • Firstly, we will be importing the numpy library with an alias name as np.
  • Then, we will ones() function with numpy.
  • After that, we will zeros() function with numpy.
  • Then, we will apply the numpy c_[ ] method, passing both the values of ones and zeros.
  • At last, we will print the output.
  • Hence in the output, you can see ones and zeros.

Conclusion

In this tutorial, we have learned about the concept of the numpy c_[ ] method. We have seen what the numpy c_[ ] method is. All the ways discussed with the help of examples are explained in detail. You can use any of the functions according to your choice and your requirement in the program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments