NumPy Init & Python Review

A Crash Python Review & Initialization at NumPy lib — #PySeries#Episode 05

J3
Jungletronics
8 min readSep 6, 2020

--

Hi, let's init our study focused on Python For Data Science: Meet NumPy!

Here we going to initialize with a crash review in Python, like functions, Maps, Lambdas, Filters, Strings, dictionaries, Lists, Tuple Unpacking, and more…

Then we head up to NUMPY, A Three-Dimensional Blue Cube with the Letter N Highlighted.

NumPy is The fundamental Package for Scientific Computing with Python!

Fig 1. Meet NumPy: the reason it is so important for Data Science with Python is that almost all the libraries in the Python Ecosystem rely on NUMPY as one of their main building blocks (Jose Portilla — Python For Data Science course)

Be very Welcome o/

You’ll find all the code down on my Colab Project Repo in Colab or GitHub page!

PYTHON REVIEW

Functions:

A function can return data as a result.

Creating a Function: in Python, a function is defined using this keyword: def.

#Python Functions:

Here how to use above function times2:

Another example: here you see how to use Docstring to document your code:

Here is how to use square(num):

# Hit Shift-Tab to bring up the signature and docstring of the class:

Fig 2. Please, document your code — This is a good practice :)

MAP

Cast your Map object to a list:

# Mapping each item in the list seq to times2 function and cast to a anonymous list:

LAMBDA

(for more info see this post)

HowTo To Get A Lambda Expression just from times2 funtion?

Here is the original function:

The times2 function can be written in one line:

Rewritting it in one line:

Now, get rid of the def keyword:

Now, to get a lambda expression just get rid of the name of the function (typing lambda in its place — cuz lambda is an anonymous function);

And get rid of the parenteses adding space in front (cleaning the code:);

And finally, get rid of return word (its function is assumed by the colon ), like this :)

It Reads: lambda takes var and returns var*2.

Just in one line. Simple Like That! Awesome!

This is not usual for lambda, but it is a feasible use (saving lambda times2 to var t2):

Now use Map plus Lambda together:

# Using Map function plus Lambda function (what it is build for!):

# Casting Map to a List:

FILTER

# Filters out the even number from seq list casted to a list:

Another example:

# Filters out 's' init words using lambda expression:

STRING

Upper function:

Lower function:

Split Function:

Tweet use:

More Split Function:

Creating a function to count dog occurrences:

# Number of time the word ‘dog’ occurs in the string:

Use countDog function:

DICTIONARY

Keys:

Items:

Values:

LIST

Pop & Append Methods:

IN word

TUPLE UNPACKING

Python tuples are immutable means that they can not be modified in the whole program.

Packing and Unpacking a Tuple: In Python, there is a very powerful tuple assignment feature that assigns the right-hand side of values into the left-hand side. In another way, it is called the unpacking of a tuple of values into a variable. In packing, we put values into a new tuple while in unpacking we extract those values into a single variable.

Using for loop:

Unpacking:

Or just like this:

NUMPY

The reason it is so important for Data Science with Python is that a lot of all the libraries in the Python Ecosystem rely on NUMPY as one of their main building blocks (Jose Portilla — Python For Data Science course).

NUMPY is also incredibly fast as it has bound to C libraries.

Installation:

ARRAYS (Vectors & Matrices) in NUMPY

-> Always cast a list into NUMPY array

# Vectors:

# 1 bracket mean 1-dimensional array ([]):

# Matrices:

# 2 brackets mean 2-dimensional array ([[]]):

ARANGE in NUMPY

# All the way up to 10, but not including 10 -> return AN NUMPY ARRAY sequence of 10 digits counting from 0 to 9

# np.arange(start, stop):

The third argument is the step:

# Third argument: step size you may want

# np.arange(start, stop, step):

ZEROES & ONES & EYE(Identity matrix) in NUMPY

Ones:

Identity Matrix:

LINSPACE (SPACED EVENLY) in NUMPY

# From 0, to 5 returning 10 digits spaced evenly:

PLEASE DON’T CONFUSE linspace WITH arange:

arange: will take in the third argument as the STEPS SIZE (you want)

linspace: will take in the third argument as the NUMBER OF POINTS (evenly separated)

RANDOM NUMBER in NUMPY

# RAND differs from ARANGE because RAND returns random numbers:

N means Normal Distribution:

# n means Normal Distribution (return values around the Mean in gaussian distribution):

Without the Third Parameter:

# Without the third parameter, it will return one digit from 1(included) to 100 (not included):

With the Third Parameter:

# The third parameter specify the number of digits you want to return:

Difference between ARANGE and RANDINT:

ARANGE: returns sequence of digits

RANDINT: returns randomly selected digits

#Vectors:

#Matrices:

RESHAPE in NUMPY

# Get the and Numpy array and reshape it as you want;

Here reshaping arr (1_dimentional) as a matrix of 5x5 (2_dimentional):

MAX & MIN in NUMPY

ATTRIBUTES SHAPE & DTYPE in NUMPY

(there are no Parentheses cuz this is an attribute, remember?)

To import use this syntax:

That’s it!

At this point in time, I just want to say THANK YOU for sticking along with this Python Series so far.

It’s been a long way but I’m hoping you have learned a lot!

There is much more to come... Stay tuned!

Bye and till the next #PySeries Episode o/

Fun Little Quiz

A BONUS FOR NUMPY EPISODE;)

01Make this Matriz a:

Fig 3. Go to shared collab (link bellow)

02 How would you index these parts of the matrix b? Create Matrices c,d, and e :-)

Fig 4. Go to shared collab (link bellow)

Solutions here:

👉Colab Google link:)

👉GitHub Repo link:)

👉Files from Google Drive:)

👆More .ipynb

Credits & References:

Jose Portilla — Python for Data Science and Machine Learning Bootcamp — Learn how to use NumPy, Pandas, Seaborn , Matplotlib , Plotly , Scikit-Learn , Machine Learning, Tensorflow , and more!

Python NumPy Tutorial for Beginners — https://youtu.be/QUT1VHiLmmI by freeCodeCamp.org

Posts Related:

00Episode#PySeries — Python — Jupiter Notebook Quick Start with VSCode — How to Set your Win10 Environment to use Jupiter Notebook

01Episode#PySeries — Python — Python 4 Engineers — Exercises! An overview of the Opportunities Offered by Python in Engineering!

02Episode#PySeries — Python — Geogebra Plus Linear Programming- We’ll Create a Geogebra program to help us with our linear programming

03Episode#PySeries — Python — Python 4 Engineers — More Exercises! — Another Round to Make Sure that Python is Really Amazing!

04Episode#PySeries — Python — Linear Regressions — The Basics — How to Understand Linear Regression Once and For All!

05Episode#PySeries — Python — NumPy Init & Python Review — A Crash Python Review & Initialization at Numpy lib. (this one)

06Episode#PySeries — Python — NumPy Arrays & Jupyter Notebook — Arithmetic Operations, Indexing & Slicing, and Conditional Selection w/ np arrays.

07Episode#PySeries — Python — Pandas — Intro & Series — What it is? How to use it?

08Episode#PySeries — Python — Pandas DataFrames — The primary Pandas data structure! It is a dict-like container for Series objects

09Episode#PySeries — Python — Python 4 Engineers — Even More Exercises! — More Practicing Coding Questions in Python!

10Episode#PySeries — Python — Pandas — Hierarchical Index & Cross-section — Open your Colab notebook and here are the follow-up exercises!

11Episode#PySeries — Python — Pandas — Missing Data — Let’s Continue the Python Exercises — Filling & Dropping Missing Data

12Episode#PySeries — Python — Pandas — Group By — Grouping large amounts of data and compute operations on these groups

13Episode#PySeries — Python — Pandas — Merging, Joining & Concatenations — Facilities For Easily Combining Together Series or DataFrame

14Episode#PySeries — Python — Pandas — Pandas Dataframe Examples: Column Operations

15Episode#PySeries — Python — Python 4 Engineers — Keeping It In The Short-Term Memory — Test Yourself! Coding in Python, Again!

16Episode#PySeries — NumPy — NumPy Review, Again;) — Python Review Free Exercises

17Episode#PySeriesGenerators in Python — Python Review Free Hints

18Episode#PySeries — Pandas Review…Again;) — Python Review Free Exercise

19Episode#PySeriesMatlibPlot & Seaborn Python Libs — Reviewing theses Plotting & Statistics Packs

20Episode#PySeriesSeaborn Python Review — Reviewing theses Plotting & Statistics Packs

review: Aug/2021 (text improvements — better lambda explaination)

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!