SA3 Python Flashcards
short for Numerical Python
Numpy
is a foundational package for numerical computing in Python
Numpy
Why NumPy? (3)
Efficient calculations for large array of numbers
Computations on entire arrays without for loops
array-oriented computing
To utilize the package, the initial step involves employing an import command
import numpy as np
To highlight one advantage of NumPy over traditional Python structures like lists, consider the scenario of performing ______
element-wise multiplication of two arrays of numbers
a generic multidimensional container for homogeneous data
ndarray
Array index starts at _ and increments by 1 in each succeeding position
0
To create a NumPy array, you can use the function
np.array()
Import and declare numpy as np variable
import numpy as np
Create one-dimensional array using numpy and display the value of an array
x = np.array([2,4,6])
print (x)
output?
[2 4 6]
Create two-dimensional array using numpy and display the value of an array
x = np.array([[2,4,6],[2,4,6]]) print (x)
[[2 4 6]
[2 4 6]]
It takes a _____ as input and produces a NumPy array containing the passed data
sequence
data = np.array([[1,2,3],[4,5,6]])
data
output?
array([[1, 2, 3],
[4, 5, 6]])
Every array has:
shape
dtype
ndim
tuple indicating size of each dimension
shape
data type of the array
dtype
number of dimensions
ndim
import numpy as np
data = np.array([[1,2,3],[4,5,6]])
data.shape
output?
(2, 3)
import numpy as np
data = np.array([[1,2,3],[4,5,6]])
data.dtype
output?
dtype(‘int32’)
import numpy as np
data = np.array([[1,2,3],[4,5,6]])
data.ndim
output?
2
More ways to create arrays
np.zeros()
np.ones()
np.arange()
x = np.array([2,4,6])
y = np.array([3,6,9])
z = x*y
print(z)
output?
[ 6 24 54]
np.arange(7)
output?
array([0, 1, 2, 3, 4, 5, 6])
np.zeros(5)
output?
array([0., 0., 0., 0., 0.])
np.ones((2,4))
output?
array([[1., 1., 1., 1.],
[1., 1., 1., 1.]])
np.arange(1,20,2)
output?
array([1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
Indexing in one-dimensional array is similar to lists or tuples
Array indexing and slicing
Indexing in one-dimensional array is similar to ______
lists or tuples
data = np.array([1,2,3,4,5,6,7,8])
data[1]
output?
2
data = np.array([1,2,3,4,5,6,7,8])
data[1:]
output?
array([2, 3, 4, 5, 6, 7, 8])
data = np.array([1,2,3,4,5,6,7,8])
data[-1]
output?
8
data = np.array([1,2,3,4,5,6,7,8])
data[[1,3,5,7]]
array([2, 4, 6, 8])
However, in arrays, we can use a list of _____ inside square brackets
indices
data = np.array([[1,2,3],[4,5,6]])
data[0,:]
output?
array([[1, 2, 3],
[4, 5, 6]])
data = np.array([[1,2,3],[4,5,6]])
data[:,-1]
output?
array([3, 6])
data[:,[0,2]]
array([[1, 3],
[4, 6]])
We also change ____ of an array to a single value
multiple values
data = np.array([[1,2,3],[4,5,6]])
data[0,1] = 7
data
output?
array([[1, 7, 3],
[4, 5, 6]])
data = np.array([[1,2,3],[4,5,6]])
data[1,:] = 8
data
output?
array([[1, 7, 3],
[8, 8, 8]])
data = np.array([[1,2,3],[4,5,6]])
data[0,:2] = [9,10]
data
output?
array([[ 9, 10, 3],
[ 8, 8, 8]])
data = np.array([[1,2,3],[4,5,6]])
data[1,[0,2]] = [9,10]
data
output?
array([[ 9, 10, 3],
9, 8, 0]])
data1 = np.array([8.25, 3.26, 5.64])
data1 - 10
data1
output?
array([-1.75, -6.74, -4.36])
data1 = np.array([8.25, 3.26, 5.64])
data1 * 2
data1
output?
array([16.5 , 6.52, 11.28])
data1 = np.array([8.25, 3.26, 5.64])
data1 / 2
data1
output?
array([4.125, 1.63 , 2.82])
data = np.array([[1,2,3], [4,5,6]])
data2 = np.array([[7,8,9],[10,11,7]])
data * data2
array([[ 7, 16, 27],
[40, 55, 42]])
data = np.array([[1,2,3], [4,5,6]])
data2 = np.array([[7,8,9],[10,11,7]])
data + data2
array([[ 8, 10, 12],
[14, 16, 13]])
data2 = np.array([[7,8,9],[10,11,7]])
data2 > 8
array([[False, False, True],
[ True, True, False]])
data = np.array([1,2,3], [4,5,6]])
data2 = np.array([[7,8,9],[10,11,7]])
data > data 2
array([[False, False, False],
[False, False, False]])
NumPy has many functions that can take arrays as _____.
input argument
The output is the ____ evaluated for every element of the input array
function
data2 = np.array([[7,8,9],[10,11,7]])
np.sqrt(data2)
array([[2.64575131, 2.82842712, 3. ],
[3.16227766, 3.31662479, 2.64575131]])
data2 = np.array([[7,8,9],[10,11,7]])
np.round(np.sqrt(data2),2)
array([[2.65, 2.83, 3. ],
[3.16, 3.32, 2.65]])
data = np.array([1,2,3], [4,5,6]])
data2 = np.array([[7,8,9],[10,11,7]])
np.maximum(data, data2)
array([[ 7, 8, 9],
[10, 11, 7]])
Another attribute of an array is called T attribute. T stands for ____
transpose
Another attribute of an array is called _ attribute.
T
data2 = np.array([[7,8,9],[10,11,7]])
data2.T
array([[ 7, 10],
[ 8, 11],
[ 9, 7]])
Array methods that compute some descriptive statistics:
min()
max()
mean()
sum()
data2 = np.array([[7,8,9],[10,11,7]])
data2.min()
7
data2 = np.array([[7,8,9],[10,11,7]])
data2.sum()
52
data2 = np.array([[7,8,9],[10,11,7]])
data2.mean()
8.666666667
Just like lists, arrays are ____
mutable
the method _____ on an array object will change the ordering of values in the original array
sort()
data3 = np.array([2, 6, 4, 5, 10])
data3
array([ 2, 6, 4, 5, 10])
data3 = np.array([2, 6, 4, 5, 10])
np.sort(data3)
array([ 2, 4, 5, 6, 10])
data = np.array([6,4,3,4,6,5])
data.sort()
data
array([3, 4, 4, 5, 6, 6])
Python can ____ of an array that satisfy a logical expression
index elements
data4 = np.array([1, 4, 2, 7, 6])
data4[data4 > 3]
array([4, 7, 6])
data4 = np.array([1, 4, 2, 7, 6])
data3 = np.array([2, 6, 4, 5, 10])
data4[data3 > 3] = 0
data4
array([1, 0, 0, 0, 0])
Code is structured so that programming tasks are organized into so-called objects
Object-Oriented Programming (OOP)
Code is structured so that programming tasks are organized into so-called ____
objects
allows us to create our own data types
Object-Oriented Programming (OOP)
Better representation of real-world entity. We want our solutions to be designed around such type
Why create our own data types
Object have two main features:
Attributes (data/property)
Methods(functions/behaviors
data/property
Attributes
functions/behaviors
Methods
blueprint to create objects
Class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self): print("Greetings, I'm" + self.name)
person1 = Person(“Juan dela Cruz”, 40)
person1.introduce()
print(person1.name)
print(person1.age)
Greetings, I’mJuan dela Cruz
Juan dela Cruz
40
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self): print("Greetings, I'm" + self.name)
person2 = Person(“Jhian Dale Sabonsolin”, 30)
person2.introduce()
print(person2.name)
print(person2.age)
Greetings, I’mJhian Dale Sabonsolin
Jhian Dale Sabonsolin
30
class ClassName:
def __init__(self, parameter1, parameter2, …):
#define or assign object attributes
def other_methods(self, parameter1, parameter2, …):
#body of the method
Defining a Class
a special method automatically run as soon as an object is instantiated
__init__()
Just like in strings and lists, we can also use _____ to determine the attributes and methods associated with a certain object
dot+TAB
_____ attributes are attributes that are accessible across all the created instances
Class
class Personv2:
n = 0 def \_\_init\_\_(self, name, age): self.name = name self.age = age Personv2.n += 1 def num_instances(self): print("we have", Personv2.n, "number of persons in total")
person1 = Personv2(“Tandang Sora”, 108)
person1.num_instance()
person2 = Personv2(“Maria CLara”, 20)
person2.num_instances()
person2.num_instances()
we have 1 number of persons in total
we have 2 number of persons in total
we have 2 number of persons in total
Creation of a class that inherits all the methods and attributes of the another class
Inheritance
___ extends available methods from Parent Class
Child Class
class Personv2:
n = 0
def \_\_init\_\_(self,name,age): self.name = name self.age = age Personv2.n += 1 def introduce(self): print("Greetings, I'm " + self.name) print("I'm ", self.age, "years old") def num_instances(self): print("We have", Personv2.n, "number of Persons in total.")
class Politician(Personv2):
def show_occupation(self):
print(“I’m a politician”)
politician1 = Politician(“Isko Moreno”, 46)
politician1.introduce()
politician1.show_occupation()
Greetings, I’m Isko Moreno
I’m 46 years old
I’m a politician
Existing method in Parent class is implemented differently in Child Class
Method Overriding
Child Class extends available methods from Parent Class
Extending New Method
class Personv2:
n = 0
def \_\_init\_\_(self,name,age): self.name = name self.age = age Personv2.n += 1 def introduce(self): print("Greetings, I'm " + self.name) print("I'm ", self.age, "years old") def num_instances(self): print("We have", Personv2.n, "number of Persons in total.")
class Politician(Personv2):
def show_occupation(self):
print(“I’m a politician”)
class Senator(Politician):
def show_occupation(self):
print(“I’m “ + self.name)
print(“I’m a Senator”)
politician2 = Senator(“Grace Poe”, 53)
politician2.show_occupation()
I’m Grace Poe
I’m a Senator
Processing of converting raw data into a usable form
Data Wrangling
Also called as data munging or data remediation
Data Wrangling
Data Wrangling is also called as _______
data munging or data remediation
Data Wrangling steps:
Discovery
Transformation
Validation
Publishing
data examination for next step
Discovery
data structuring, normalization & denormalization, data cleaning, data enriching
Transformation
4 elements of Transformation
data structuring, normalization & denormalization, data cleaning, data enriching
verifying data consistency, sufficient quality, and secure
Validation
preferred format for sharing with teammates
Publishing
Pandas library provides several functions for loading dataset
Loading
_____ provides several functions for loading dataset
Pandas library
Dataset can be in any of ____ formats
supported
Dataset can be in any of supported formats
Loading
____ data structure holds input dataset
DataFrame