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

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

2 weeks ago

Install PyTorch on Windows

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

3 weeks ago

Topic Modeling using Latent Dirichlet Allocation with Python

Topic modeling is a subcategory of unsupervised machine learning method, and a clustering task in…

1 month ago

Document sentiment classification using bag-of-words in Python

For online Python training registration, click here ! Sentiment classification is a type of machine…

2 months ago

Download R Course source files

Click here to download R Course source files !

11 months ago

Download Python Course source files

Click here to download Python Course Source Files !

11 months ago