Learn Python By Example – Indexing And Slicing NumPy Arrays

Indexing And Slicing NumPy Arrays

Slicing Arrays

Explanation Of Broadcasting

Unlike many other data types, slicing an array into a new variable means that any chances to that new variable are broadcasted to the original variable. Put other way, a slice is a hotlink to the original array variable, not a separate and independent copy of it.


/* Import Modules */
import numpy as np
/* Create an array of battle casualties from the first to the last battle */
battleDeaths = np.array([1245, 2732, 3853, 4824, 5292, 6184, 7282, 81393, 932, 10834])
/* Divide the array of battle deaths into start, middle, and end of the war */
warStart = battleDeaths[0:3]; print('Death from battles at the start of war:', warStart)
warMiddle = battleDeaths[3:7]; print('Death from battles at the middle of war:', warMiddle)
warEnd = battleDeaths[7:10]; print('Death from battles at the end of war:', warEnd)

Death from battles at the start of war: [1245 2732 3853]
Death from battles at the middle of war: [4824 5292 6184 7282]
Death from battles at the end of war: [81393   932 10834]
/* Change the battle death numbers from the first battle */
warStart[0] = 11101
/* View that change reflected in the warStart slice of the battleDeaths array */
warStart
array([11101,  2732,  3853])
/* View that change reflected in (i.e. "broadcasted to) the original battleDeaths array */

battleDeaths
array([11101,  2732,  3853,  4824,  5292,  6184,  7282, 81393,   932, 10834])

Indexing Arrays

Note: This multidimensional array behaves like a dataframe or matrix (i.e. columns and rows)

/* Create an array of regiment information */
regimentNames = ['Nighthawks', 'Sky Warriors', 'Rough Riders', 'New Birds']
regimentNumber = [1, 2, 3, 4]
regimentSize = [1092, 2039, 3011, 4099]
regimentCommander = ['Mitchell', 'Blackthorn', 'Baker', 'Miller']

regiments = np.array([regimentNames, regimentNumber, regimentSize, regimentCommander])
regiments
array([['Nighthawks', 'Sky Warriors', 'Rough Riders', 'New Birds'],
       ['1', '2', '3', '4'],
       ['1092', '2039', '3011', '4099'],
       ['Mitchell', 'Blackthorn', 'Baker', 'Miller']], 
      dtype='<U12')
/* View the first column of the matrix */
regiments[:,0]
array(['Nighthawks', '1', '1092', 'Mitchell'], 
      dtype='<U12')
/* View the second row of the matrix */
regiments[1,]
array(['1', '2', '3', '4'], 
      dtype='<U12')
/* View the top-right quarter of the matrix */
regiments[:2,2:]
array([['Rough Riders', 'New Birds'],
       ['3', '4']], 
      dtype='<U12')

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars