NumPy Init & Python Review
Tags: Python Pandas, Machine Learning, Data Science, Numpy, Python Programming

NumPy Init & Python Review

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

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

def times2(var):

return var*2

Here how to use above function times2:

times2(5) 10

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

def square(num):

'''

THIS IS A DOCSTRING,

CAN GO MULTIPLY LINES

THIS FUNCTION SQUARES A NUMBER

'''

return num**2

Here is how to use square(num):

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

output = square(2)

output 4

MAP

seq=[1,2,3,4,5]

Cast your Map object to a list:

# Mapping each item in the list seq to times2 function

# and cast to a anonymous list

list(map(times2, seq)) [2, 4, 6, 8, 10]

LAMBDA

The times2 function can be written in one line:

#def times2(var):return var*2

To get a lambda expression just get rid of the name of the function, cuz lambda is an anonymous function:

lambda var:var*2lambda var:var*2

Read: lambda takes var and returns var*2. Just in one line. Awesome!

This is not usual for lambda, but it is a feasible use:

t=lambda var:var*2 t(5) 10

Now use Map plus Lambda together:

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

map(lambda var:var*2, seq) <map at 0x7f0640b37b38> # Casting Map to a List

list(map(lambda var:var*2, seq)) [2, 4, 6, 8, 10]

FILTER

filter(lambda var:var%2==0, seq) <filter at 0x7f0640b37d30> # Filters out the even number from seq list casted to a list

list(filter(lambda var:var%2==0, seq)) [2, 4] seq=['soup','dog','salad','cat','greed'] # Filters out 's' init words using lambda expression

filter(lambda word:word[0]=='s', seq) <filter at 0x7f06402bb080> list(filter(lambda word:word[0]=='s', seq)) ['soup', 'salad']

STRING

s='hello my name is j3'

Upper function:

s.upper() 'HELLO MY NAME IS J3'

Lower function:

s.lower() 'hello my name is j3'

Split Function:

my_string_list=s.split() my_string_list ['hello', 'my', 'name', 'is', 'j3']

Tweet use:

tweet="Go Sports!#Sports" tweet.split('#') ['Go Sports!', 'Sports'] tweet.split('#')[0] 'Go Sports!'

More Split Function:

tweet.split('#')[1] 'Sports'

Creating a function to count dog occurrences:

# Number of time the word 'dog' occurs in the string def countDog(phrase):

count=0

for word in phrase.lower().split():

if word=='dog':

count+=1

return count

Use countDog function:

countDog('This dog runs faster than the other dog dude!') 2

DICTIONARY

d={'key1':1, 'key2':2}

Keys:

d.keys() dict_keys(['key1', 'key2'])

Items:

d.items() dict_items([('key1', 1), ('key2', 2)])

Values:

d.values() dict_values([1, 2])

LIST

lst=[1,2,3]

Pop & Append Methods:

item=lst.pop() item 3 lst [1, 2] lst.append(3) lst [1, 2, 3]

IN word

'x'in['x','y', 'z'] True 'w'in['x', 'y', 'z'] False

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.

x=[(1,2),(3,4),(5,6)] x[0][0] 1 x[0][1] 2

Using for loop:

for item in x:

print(item) (1, 2)

(3, 4)

(5, 6)

Unpacking:

for (a,b) in x:

print(a) 1

3

5

Or just like this:

for a,b in x:

print(a+1, b+1) 2 3

4 5

6 7

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:

conda install numpy (pip install numpy Anaconda ) or

ARRAYS (Vectors & Matrices) in NUMPY

-> Always cast a list into NUMPY array

Vectors:

#Vectors

my_list=[1,2,3] import numpy as np vec=np.array(my_list)

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

vec array([1, 2, 3])

Matrices:

my_mat=[[1,2,3],[4,5,6],[7,8,9]] import numpy as np mat=np.array(my_mat) # 2 brackets mean 2-dimensional array ([[]])

mat array([[1, 2, 3],

[4, 5, 6],

[7, 8, 9]])

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 10

np.arange(0,10) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

The third argument is the step:

# Third argument: step size you may want

np.arange(0,10,2) array([0, 2, 4, 6, 8])

ZEROES & ONES & EYE(Identity matrix) in NUMPY

np.zeros(3) array([0., 0., 0.]) np.zeros((5,5)) array([[0., 0., 0., 0., 0.],

[0., 0., 0., 0., 0.],

[0., 0., 0., 0., 0.],

[0., 0., 0., 0., 0.],

[0., 0., 0., 0., 0.]])

Ones:

np.ones(4) array([1., 1., 1., 1.]) np.ones((5,5)) array([[1., 1., 1., 1., 1.],

[1., 1., 1., 1., 1.],

[1., 1., 1., 1., 1.],

[1., 1., 1., 1., 1.],

[1., 1., 1., 1., 1.]])

Identity Matrix:

# IDENTITY MATRIX

np.eye(4) array([[1., 0., 0., 0.],

[0., 1., 0., 0.],

[0., 0., 1., 0.],

[0., 0., 0., 1.]])

LINSPACE (SPACED EVENLY) in NUMPY

# From 0, to 5 returning 10 digits spaced evenly

np.linspace(0,5,10) array([0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222,

2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])

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 and ARANGE return a sequence of them

np.random.rand(5) array([0.08930558, 0.21911281, 0.20612461, 0.51468239, 0.11276573])

N means Normal Distribution:

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

np.random.randn(2) array([0.44034175, 1.1805967 ])

Without the Third Parameter:

# Without the third parameter, it will return one digit

# from 1(included) to 100(not included) np.random.randint(1,100) 84

With the Third Parameter:

# The third parameter specifuy the digits you want to return

np.random.randint(1,100,10) array([25, 60, 41, 48, 76, 99, 86, 62, 74, 86])

Difference between ARANGE and RANDINT:

ARANGE: returns sequence of digits

RANDINT: returns randonly selected digits

Vectors:

arr=np.arange(25) arr array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

17, 18, 19, 20, 21, 22, 23, 24])

Matrices:

randarr=np.random.randint(0,50,10) randarr array([ 2, 11, 26, 19, 13, 37, 35, 36, 18, 24])

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)

reshapped_arr=arr.reshape(5,5) reshapped_arr array([[ 0, 1, 2, 3, 4],

[ 5, 6, 7, 8, 9],

[10, 11, 12, 13, 14],

[15, 16, 17, 18, 19],

[20, 21, 22, 23, 24]])

MAX & MIN in NUMPY

reshapped_arr.min() 0 reshapped_arr.max() 24

ATTRIBUTES SHAPE & DTYPE in NUMPY

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

reshapped_arr.shape (5, 5) reshapped_arr.dtype dtype('int64')

To import use this syntax:

from numpy.random import randint

That’s it!

# TODO [PAGE LINK GOES HERE!]

print("Thank you for reading this page! Please favorite it, IF YOU LIKE:)") Thank you for reading this page! Please favorite it, IF YOU LIKE:)

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:

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

Solutions here:

Colab Google link:)

GitHub Repo link:)

Files from Google Drive:)

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