How to select elements from Numpy array in Python?

How to select elements from Numpy array in Python?

How to select elements from Numpy array in Python?

This recipe helps you select elements from Numpy array in Python


Recipe Objective

In a numpy array array how do you select an element? Selecting an element is very easy if you know the index of the element. In this we will be selecting element from vector matrix and tensor.

So this is the recipe on how we can Select Elements from Numpy Array.

Step 1 - Import the library

import numpy as np

We have only imported numpy which is needed.

Step 2 - Selecting element from vector

We have created a vector and we will select a element from it by passing the index in the object. vector = np.array([1, 2, 3, 4, 5, 6]) print(vector[1]) We have created a matrix and we will select a element from it by passing the index in the object. For matrix we have to pass two values in the form (row , column) to select the element. matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(matrix[1,1]) We have created a tensor and we will select a element from it by passing the index in the object. For tensor we have to pass three value to select the element. tensor = np.array([ [[[1, 1], [1, 1]], [[2, 2], [2, 2]]], [[[3, 3], [3, 3]], [[4, 4], [4, 4]]] ]) print(tensor[1,1,1]) So the output comes as



[4 4]

Relevant Projects

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.