Categories: NumpyPython

Using conditional statements to select parts of NumPy arrays in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

In addition to simply using indexing to select elements from a Numpy array, Python lets you easily use conditional tests to systematically select parts of a Numpy array. In the following example, elements less than 0.8 from a two-dimensional array is selected via a conditional test inside the brackets, and the returning result is a one-dimensional array.

#import Numpy module
import numpy as np
#create an array of shape 3,2
T = np.random.random((3, 2))
T
#output
array([[0.39818887, 0.09905403],
       [0.62459552, 0.34570658],
       [0.25420698, 0.9049677 ]])
#select element of array, with condition less than 0.8
T[T < 0.8]
#result
array([0.39818887, 0.09905403, 0.62459552, 0.34570658, 0.25420698])

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Main framework for PyTorch music generation model

If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…

1 day ago

affine_grid() in pytorch

According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…

2 months ago

How to create an Android mobile app with a deep learning AI model ?

Creating an Android mobile app with a deep learning AI model involves several key steps:…

11 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 year ago

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

1 year ago

Install PyTorch on Windows

PyTorch is a deep learning package for machine learning, or deep learning in particular for…

1 year ago