Machine Learning Part 05
Machine Learning Part 05
In [ ]: # Sample Input
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Output
print(even_numbers)
[2, 4, 6, 8, 10]
In [ ]: # Sample Input
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Output
print(result)
# output
print(squred_numbers)
In [ ]: list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
print(common_elements)
[3, 4, 5]
print(squares)
[1, 0, 9, 0, 25, 0]
print(vowels)
Input:
list1 = [1,2,3,4,5,6]
Output:
[1,3,6,10,15,21]
In [ ]: list1 = [1, 2, 3, 4, 5, 6]
print(list1)
Output:
print(common_items)
Example:
Input:
[1,2,3,4,5,1]
[2,3,5,7,8]
Output:
[1,2,3,4,5,7,8]
In [ ]: list1 = [1, 2, 3, 4, 5, 1]
list2 = [2, 3, 5, 7, 8]
print(union_result)
[1, 2, 3, 4, 5, 7, 8]
Input:
[[1,2,3],[4,5,6],[7,8,9]]
Output:
[3,6,9]
In [ ]: matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(max_numbers)
[3, 6, 9]
t1 = (1,2,3,0)
t2 = (0,1,2,3)
In [ ]: t1 = (1,2,3,0)
t2 = (0,1,2,3)
def are_tuple_same(tuple1,tuple2):
# check if the length of the tuple is same or not
if len(tuple1)!=len(tuple2):
return False
if are_tuple_same(t1,t2):
print('t1 and t2 are the same')
else:
print('t1 and t2 are not same')
In [ ]: # EXAMPLE 2
t1 = (1,2,3,4,5)
t2 = (1,2,3,4,5)
if are_tuple_same(t1,t2):
print('t1 and t2 are the same')
else:
print('t1 and t2 are not same')
Output:
List-2
Set-2
Tuples-1
In [ ]: list1 = [{'hi', 'bye'}, {'Geeks', 'forGeeks'}, ('a', 'b'), ['hi', 'bye'], ['a', 'b'
# Initialize counters
tuple_count = 0
list_count = 0
set_count = 0
List-2
Set-2
Tuples-1
In [ ]: arr1 = [1,5,10,20,40,80]
arr2 = [6,7,20,80,100]
arr3 = [3,4,15,20,30,70,80,120]
# Output
print("Output:", result_list)
Input:
[[1, 2, 2, 4, 3, 6],
[5, 1, 3, 4],
[9, 5, 7, 1],
[2, 4, 1, 3]]
Output:
[1, 2, 3, 4, 5, 6, 7, 9]
In [ ]: def find_union(arrays):
# Initialize an empty set for the union
union_set = set()
return union_list
# Example usage
input_arrays = [
[1, 2, 2, 4, 3, 6],
[5, 1, 3, 4],
[9, 5, 7, 1],
[2, 4, 1, 3]
]
output_union = find_union(input_arrays)
print("Output:", output_union)
Output: [1, 2, 3, 4, 5, 6, 7, 9]
[1,2,3,3,3,3,4,5]
Output:
[1, 2, 3, 4, 5]
In [ ]: def unique_elements(input_list):
# Convert the list to a set to remove duplicates
unique_set = set(input_list)
return result_list
# Example usage
input_list = [1, 2, 3, 3, 3, 3, 4, 5]
output_list = unique_elements(input_list)
print(output_list)
[1, 2, 3, 4, 5]
green-red-yellow-black-white
Output:
black-green-red-white-yellow
In [ ]: def sort_words(input_sequence):
# Split the hyphen-separated sequence into a list of words
words_list = input_sequence.split('-')
return result_sequence
# Example
input_sequence = "green-red-yellow-black-white"
output_sequence = sort_words(input_sequence)
print(output_sequence)
black-green-red-white-yellow
In [ ]: def print_even_numbers(input_list):
# Use a list comprehension to filter even numbers
even_numbers = [i for i in input_list if i % 2 == 0]
sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print_even_numbers(sample_list)
[2, 4, 6, 8]
In [ ]: def concatenate_dictionaries(*dict):
# Initialize an empty dictionary to store the result
result_dict = {}
return result_dict
# Example usage
dic1 = {1: 10, 2: 20}
dic2 = {3: 30, 4: 40}
dic3 = {5: 50, 6: 60}
Pandas Questions
Que-1: Write a program to create an empty series.
In [ ]: import pandas as pd
print(empty_series)
# Addition
addition_result = series1 + series2
# Subtraction
subtraction_result = series1 - series2
# Multiplication
multiplication_result = series1 * series2
# Division
division_result = series1 / series2
# Comparing elements
comparison_result_greater = series1 > series2
comparison_result_less = series1 < series2
comparison_result_equal = series1 == series2
return converted_series
In Python 2:
1. range() :
In Python 3:
The xrange() function is no longer available. The range() function in Python 3 was
modified to behave like xrange() from Python 2, meaning it is an iterator and
generates values on-the-fly.
So, in Python 3, you can use range() for creating sequences, and it will be memory-
efficient like xrange() in Python 2. The use of xrange() is no longer necessary in
Python 3.
Pickling:
Definition: Pickling is the process of converting a Python object into a byte stream.
Module: The pickle module in Python is used for pickling.
Function: The primary function for pickling is pickle.dump() or pickle.dumps() .
The former writes the pickled representation to a file-like object, and the latter returns a
string containing the pickled representation.
file:///C:/Users/disha/Downloads/Day77 - Python Interview Question.html 1/3
12/8/23, 3:59 PM Day77 - Python Interview Question
In [ ]: import pickle
Unpickling:
Definition: Unpickling is the process of converting a byte stream back into a Python
object.
Module: The pickle module in Python is also used for unpickling.
Function: The primary function for unpickling is pickle.load() or
pickle.loads() . The former reads the pickled representation from a file-like object,
and the latter reads from a string containing the pickled representation.
In [ ]: import pickle
print(loaded_data)
It's important to note that while pickling is a convenient way to serialize Python objects,
caution should be exercised when unpickling data from untrusted or unauthenticated
sources, as it can lead to security vulnerabilities (e.g., arbitrary code execution). In such cases,
alternative serialization formats like JSON may be more appropriate.
Parameters: __init__ can take parameters, which are passed when you create an
instance of the class. These parameters are used to initialize the attributes of the object.
In [ ]: class MyClass:
def __init__(self, name, age):
self.name = name
self.age = age
John
30
In the example above, the __init__ method takes self (which represents the instance
being created) along with two additional parameters ( name and age ). Inside the method,
it initializes the attributes name and age with the values passed during the object
creation.
Remember that __init__ is just one of several special methods in Python classes. Other
dunder methods include __str__ for string representation, __repr__ for a detailed
representation, and so on. These methods allow you to define how objects of your class
behave in various contexts.
1. Public: Attributes and methods that are accessible from outside the class without any
restrictions. By default, all attributes and methods in Python are public.
2. Protected: Attributes and methods that are intended to be used only within the class
and its subclasses. In Python, you can denote a protected attribute or method by
prefixing it with a single underscore (e.g., _my_attribute ).
3. Private: Attributes and methods that are intended to be used only within the class. In
Python, you can denote a private attribute or method by prefixing it with a double
underscore (e.g., __my_attribute ).
class Myclass:
def __init__(self):
self.public_attribute = 55
self._protected_attribute = 'Hello'
self.__private_attribute = 'World'
def public_method(self):
return "This is public method"
def _protected_method(self):
return "This is Protected Method"
def __private_method(self):
return "this is private Method"
In [ ]: obj = Myclass()
In [ ]: # we have to access the public attribaute dirctly and don't give any tyoe of error
print(obj.public_attribute)
print(obj.public_method())
55
This is public method
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
c:\Users\disha\Downloads\Pythoon 100 Days\100_Days_OF_Python\Day17 - Encapsulation
in OOP in Python.ipynb Cell 7 line 2
<a href='vscode-notebook-cell:/c%3A/Users/disha/Downloads/Pythoon%20100%20Da
ys/100_Days_OF_Python/Day17%20-%20Encapsulation%20in%20OOP%20in%20Python.ipynb#W6s
ZmlsZQ%3D%3D?line=0'>1</a> # Accessing private attributes and methods directly wil
l result in an error:
----> <a href='vscode-notebook-cell:/c%3A/Users/disha/Downloads/Pythoon%20100%20Da
ys/100_Days_OF_Python/Day17%20-%20Encapsulation%20in%20OOP%20in%20Python.ipynb#W6s
ZmlsZQ%3D%3D?line=1'>2</a> print(obj.__private_attribute) # Raises an AttributeE
rror
<a href='vscode-notebook-cell:/c%3A/Users/disha/Downloads/Pythoon%20100%20Da
ys/100_Days_OF_Python/Day17%20-%20Encapsulation%20in%20OOP%20in%20Python.ipynb#W6s
ZmlsZQ%3D%3D?line=2'>3</a> print(obj.__private_method())
In a bustling city, there was a high-tech security system protecting a valuable treasure. The
system had a keypad (public interface) that allowed authorized personnel to enter a secret
code. Behind the keypad was a locked room (protected layer) with advanced security
features, and inside that room was a safe (private layer) holding the treasure. Only a select
few knew the secret code, ensuring the treasure's safety while providing access to those who
needed it. This is encapsulation in action, safeguarding valuable data while allowing
controlled access.
3. Information Hiding: It hides complex internal details, reducing the complexity for users
of a class and allowing developers to change the implementation without impacting
external code.
3. Code Organization: Encapsulation helps organize code by bundling related data and
methods within a class. This modular approach enhances code readability and
maintainability, making it easier to manage large codebases.
4. Security: By encapsulating sensitive data and operations, you can control who has
access to them. This enhances security by preventing unauthorized access or
manipulation of critical information.
These two class relationships, inheritance and aggregation, are fundamental concepts in
OOP and are used extensively in software design to model different types of relationships
between classes and objects.
1. Aggregation(Has a Realtionship)
In object-oriented programming (OOP) with Python, aggregation is a type of association
between classes where one class, known as the "container" or "composite" class, contains or
references another class, known as the "contained" or "component" class. Aggregation
represents a "has-a" relationship, where the container class has one or more instances of the
contained class as part of its own structure.
Aggregation is often used to model relationships between objects when one object is
composed of or contains other objects. It is a way to create more complex objects by
combining simpler objects. A classic example of aggregation is a university system where a
University class can contain Department objects, and each Department can contain
`Professor objects.
In [ ]: class Professor:
def __init__(self, name):
self.name = name
class Department:
def __init__(self, name):
self.name = name
self.professors = [] # Aggregation: Department contains Professor objects
class University:
def __init__(self, name):
self.name = name
self.departments = [] # Aggregation: University contains Department object
# Creating objects
professor1 = Professor("John Doe")
professor2 = Professor("Jane Smith")
In this example:
Aggregation is a way to represent the idea that one class is composed of or contains
instances of another class, and it is often used to model real-world relationships between
objects in a system.
The solid diamond indicates Composition. Notice how teams belong to the single
company object. If the company object is deleted, the teams will have no reason to exist
anymore.
The open diamond indicates Aggregation. When a team is deleted, the employees that
were in the team, still exist. Employees might also belong to multiple teams. A team
object does not "own" an employee object.
Inheritance is typically used to model an "is-a" relationship between classes, where the
derived class is a more specific or specialized version of the base class. The derived class
inherits the attributes and methods of the base class and can also have its own additional
attributes and methods or override the inherited ones.
In [ ]: class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass # Base class method, to be overridden by subclasses
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
# Creating objects
dog = Dog("Buddy")
cat = Cat("Whiskers")
In this example:
'Animal' is the base class, and 'Dog' and 'Cat' are subclasses of 'Animal'.
Both 'Dog' and 'Cat' inherit the 'name' attribute and the 'speak' method from the
'Animal' class.
However, each subclass overrides the 'speak' method to provide its own
implementation, representing the specific behavior of each animal. Inheritance is a
powerful mechanism in OOP because it allows you to create a hierarchy of classes, with
each level of the hierarchy adding or modifying functionality as needed. This promotes
code reuse, encapsulation, and abstraction, making it easier to manage and extend your
codebase.
4. Supports Code Extensibility: With inheritance, you can extend existing classes to add
or modify functionality. Derived classes can override inherited methods to provide
specialized behavior while still benefiting from the base class's shared attributes and
methods. This makes it straightforward to adapt and extend your code to accommodate
changing requirements.
Inheritance in summary
Child class can override the attributes or methods. This is called method overriding
super() is an inbuilt function which is used to invoke the parent class methods and
constructor
Types of Inheritance
1. Single Inheritance:
In single inheritance, a class inherits properties and behaviors from a single parent
class. This is the simplest form of inheritance, where each class has only one
immediate base class.
2. Multilevel Inheritance:
In multilevel inheritance, a class derives from a class, which in turn derives from
another class. This creates a chain of inheritance where each class extends the
previous one. It forms a hierarchy of classes.
3. Hierarchical Inheritance:
Multiple inheritance occurs when a class inherits properties and behaviors from
more than one parent class. This can lead to a complication known as the
"diamond problem," where ambiguity arises when a method or attribute is called
that exists in multiple parent classes. Some programming languages, like Python,
provide mechanisms to resolve this ambiguity.
5. Hybrid Inheritance:
These different types of inheritance allow developers to model various relationships and
structures in object-oriented programming. Choosing the appropriate type of inheritance
depends on the specific requirements and design of the software being developed.
In [ ]: # single inheritance
class Phone:
def __init__(self, price, brand, camera):
print ("Inside phone constructor")
file:///C:/Users/disha/Downloads/Day80 - Revision of Inheritance in OOP.html 3/5
12/13/23, 1:33 PM Day80 - Revision of Inheritance in OOP
self.__price = price
self.brand = brand
self.camera = camera
def buy(self):
print ("Buying a phone")
class SmartPhone(Phone):
pass
SmartPhone(1000,"Apple","13px").buy()
In [ ]: # multilevel
class Product:
def review(self):
print ("Product customer review")
class Phone(Product):
def __init__(self, price, brand, camera):
print ("Inside phone constructor")
self.__price = price
self.brand = brand
self.camera = camera
def buy(self):
print ("Buying a phone")
class SmartPhone(Phone):
pass
In [ ]: # Hierarchical
class Phone:
def __init__(self, price, brand, camera):
print ("Inside phone constructor")
self.__price = price
self.brand = brand
self.camera = camera
def buy(self):
print ("Buying a phone")
class SmartPhone(Phone):
pass
In [ ]: # Multiple
class Phone:
def __init__(self, price, brand, camera):
print ("Inside phone constructor")
self.__price = price
self.brand = brand
self.camera = camera
def buy(self):
print ("Buying a phone")
class Product:
def review(self):
print ("Customer review")
Polymorphism:
Polymorphism is one of the four fundamental principles of Object-Oriented Programming
(OOP), along with encapsulation, inheritance, and abstraction. It allows objects of different
classes to be treated as objects of a common superclass. In Python, polymorphism is
implemented through method overriding and method overloading, which are two related
concepts.
1. Method Overriding
2. Method Overloading
1. Method Overriding
Method overriding occurs when a subclass provides a specific implementation of a method
that is already defined in its superclass. This allows the subclass to provide its own behavior
for a method while still adhering to the method signature defined in the superclass.
In [ ]: class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
dog = Dog()
cat = Cat()
print(dog.speak())
print(cat.speak())
Woof!
Meow!
2.Method Overloading:
Method overloading allows a class to define multiple methods with the same name but
different parameters. Python does not support traditional method overloading with different
parameter types like some other languages (e.g., Java or C++). Instead, Python achieves a
form of method overloading using default arguments and variable-length argument lists.
Here's an example:
In [ ]: class Calculator:
def add(self, a, b):
return a + b
calc = Calculator()
#result1 = calc.add(1, 2) # Error: The second add method with three paramete
result2 = calc.add(1, 2, 3) # This works fine.
In Python, only the latest defined method with a particular name is accessible. Traditional
method overloading with different parameter types isn't supported.
In [ ]: class Shape:
def area(self,a,b=0):
if b == 0:
return 3.14*a*a
else:
return a*b
s = Shape()
print(s.area(2))
print(s.area(3,4))
12.56
12
Polymorphism allows you to write more flexible and reusable code by treating different
objects in a consistent way, regardless of their specific class. This promotes code flexibility
and makes it easier to extend and maintain your programs as they grow in complexity.
Abstraction
Abstraction is one of the four fundamental principles of Object-Oriented Programming
(OOP), along with encapsulation, inheritance, and polymorphism. Abstraction is the process
of simplifying complex reality by modeling classes based on the essential properties and
behaviors of objects, while ignoring or hiding the non-essential details.
1. Modeling: Abstraction involves defining classes and objects that capture the essential
characteristics and behaviors of real-world entities or concepts. You create classes to
represent these abstractions, defining their attributes (data) and methods (behavior) to
interact with them.
2. Hiding Complexity: Abstraction allows you to hide the internal details and
complexities of an object's implementation from the outside world. This is achieved
through encapsulation, where you define private attributes and provide public methods
to interact with the object.
4. Focus on What, Not How: When you work with abstractions, you can focus on using
objects based on what they do (their methods) rather than how they do it (their
implementation details). This separation of concerns simplifies the design and
maintenance of complex systems.
In [ ]: class Vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
def start(self):
pass
def stop(self):
pass
class Car(Vehicle):
def start(self):
print(f"{self.make} {self.model} is starting")
def stop(self):
print(f"{self.make} {self.model} is stopping")
class Motorcycle(Vehicle):
def start(self):
print(f"{self.make} {self.model} is revving up")
def stop(self):
print(f"{self.make} {self.model} is slowing down")
In this example, the Vehicle class represents an abstraction of a general vehicle with
common attributes (make and model) and methods (start and stop). Concrete subclasses
like Car and Motorcycle inherit from Vehicle and provide specific implementations
for the start and stop methods. Users of these classes can interact with them without
needing to know the exact implementation details of each vehicle type.
Abstraction helps in managing complexity, improving code organization, and making code
more maintainable and understandable by focusing on high-level concepts and behaviors.
def database(self):
print('connected to database')
@abstractmethod
def display(self):
pass
In [ ]: class MobileApp(BankApp):
def mobile_login(self):
print('login into mobile')
def display(self):
print('display')
In [ ]: mob = MobileApp()
In [ ]: mob.security()
mobile security
In [ ]: obj = BankApp()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
c:\Users\disha\Downloads\Pythoon 100 Days\100_Days_OF_Python\Polymorphism and Abst
raction - Object-Oriented Programming (OOP).ipynb Cell 19 line 1
----> <a href='vscode-notebook-cell:/c%3A/Users/disha/Downloads/Pythoon%20100%20Da
ys/100_Days_OF_Python/Polymorphism%20and%20Abstraction%20-%20Object-Oriented%20Pro
gramming%20%28OOP%29.ipynb#X26sZmlsZQ%3D%3D?line=0'>1</a> obj = BankApp()
TypeError: Can't instantiate abstract class BankApp with abstract methods display,
security
The error message indicates that you are trying to instantiate an abstract class called
BankApp, but this class contains abstract methods display and security that have not been
implemented in the BankApp class or its subclasses.
In Python, an abstract class is a class that cannot be instantiated directly, and it often serves
as a blueprint for other classes. Abstract classes can contain abstract methods, which are
methods declared without an implementation in the abstract class. Subclasses of the
abstract class are required to provide implementations for these abstract methods.
"Stacking" and "unstacking" are operations that you can perform on multi-indexed
DataFrames to change the arrangement of the data, essentially reshaping the data between
a wide and a long format (or vice versa).
1. Stacking:
Stacking is the process of "melting" or pivoting the innermost level of column labels to
become the innermost level of row labels.
This operation is typically used when you want to convert a wide DataFrame with multi-
level columns into a long format.
You can use the .stack() method to perform stacking. By default, it will stack the
innermost level of columns.
In [ ]: import numpy as np
import pandas as pd
A B
0 X 0.960684 0.900984
Y 0.118538 0.485585
1 X 0.946716 0.444658
Y 0.049913 0.991469
2 X 0.656110 0.759727
Y 0.158270 0.203801
3 X 0.360581 0.797212
Y 0.965035 0.102426
2. Unstacking:
Unstacking is the reverse operation of stacking. It involves pivoting the innermost level
of row labels to become the innermost level of column labels.
You can use the .unstack() method to perform unstacking. By default, it will unstack
the innermost level of row labels.
Example:
A B
X Y X Y
0 0.960684 0.118538 0.900984 0.485585
1 0.946716 0.049913 0.444658 0.991469
2 0.656110 0.158270 0.759727 0.203801
3 0.360581 0.965035 0.797212 0.102426
You can specify the level you want to stack or unstack by passing the level parameter to
the stack() or unstack() methods. For example:
Out[ ]: A B
0 X 0.960684 0.900984
Y 0.118538 0.485585
1 X 0.946716 0.444658
Y 0.049913 0.991469
2 X 0.656110 0.759727
Y 0.158270 0.203801
3 X 0.360581 0.797212
Y 0.965035 0.102426
Out[ ]: A B
0 1 2 3 0 1 2 3
In [ ]: index_val = [('cse',2019),('cse',2020),('cse',2021),('cse',2022),('ece',2019),('ece
multiindex = pd.MultiIndex.from_tuples(index_val)
multiindex.levels[1]
In [ ]: branch_df1 = pd.DataFrame(
[
[1,2],
[3,4],
[5,6],
[7,8],
[9,10],
[11,12],
[13,14],
[15,16],
],
index = multiindex,
columns = ['avg_package','students']
)
branch_df1
cse 2019 1 2
2020 3 4
2021 5 6
2022 7 8
ece 2019 9 10
2020 11 12
2021 13 14
2022 15 16
branch_df2
2019 1 2 0 0
2020 3 4 0 0
2021 5 6 0 0
2022 7 8 0 0
In [ ]: branch_df1
cse 2019 1 2
2020 3 4
2021 5 6
2022 7 8
ece 2019 9 10
2020 11 12
2021 13 14
2022 15 16
In [ ]: branch_df1.unstack().unstack()
In [ ]: branch_df1.unstack().stack()
cse 2019 1 2
2020 3 4
2021 5 6
2022 7 8
ece 2019 9 10
2020 11 12
2021 13 14
2022 15 16
In [ ]: branch_df2
2019 1 2 0 0
2020 3 4 0 0
2021 5 6 0 0
2022 7 8 0 0
In [ ]: branch_df2.stack()
2019 avg_package 1 0
students 2 0
2020 avg_package 3 0
students 4 0
2021 avg_package 5 0
students 6 0
2022 avg_package 7 0
students 8 0
In [ ]: branch_df2.stack().stack()
Stacking and unstacking can be very useful when you need to reshape your data to make it
more suitable for different types of analysis or visualization. They are common operations in
data manipulation when working with multi-indexed DataFrames in pandas.
When you apply groupby() to a DataFrame, it creates a GroupBy object, which acts as a
kind of intermediate step before applying aggregation functions or other operations to the
grouped data. This intermediate step helps you perform operations on subsets of data
based on the grouping criteria. Some common aggregation functions you can apply to a
GroupBy object include sum() , mean() , count() , max() , min() , and more.
Here's a basic example of how you can create a GroupBy object and perform aggregation
with it:
In [ ]: import pandas as pd
import numpy as np
df = pd.DataFrame(data)
Category
A 55
B 45
Name: Value, dtype: int64
Practical Use
In [ ]: movies = pd.read_csv('Data\Day35\imdb-top-1000.csv')
In [ ]: movies.head(3)
The
Frank Tim
0 Shawshank 1994 142 Drama 9.3 2343110
Darabont Robbins
Redemption
Francis
The Marlon
1 1972 175 Crime 9.2 Ford 1620367
Godfather Brando
Coppola
In [ ]: genres.sum(3)
Genre
In [ ]: genres.mean(3)
Genre
Genre
Out[ ]:
Drama 3.540997e+10
Action 3.263226e+10
Comedy 1.566387e+10
Name: Gross, dtype: float64
In [ ]: movies.groupby('Genre').sum()['Gross'].sort_values(ascending=False).head(3)
Genre
Out[ ]:
Drama 3.540997e+10
Action 3.263226e+10
Comedy 1.566387e+10
Name: Gross, dtype: float64
Genre
Out[ ]:
Western 8.35
Name: IMDB_Rating, dtype: float64
Star1
Out[ ]:
Tom Hanks 12
Robert De Niro 11
Clint Eastwood 10
Al Pacino 10
Leonardo DiCaprio 9
..
Glen Hansard 1
Giuseppe Battiston 1
Giulietta Masina 1
Gerardo Taracena 1
Ömer Faruk Sorak 1
Name: Series_Title, Length: 660, dtype: int64
In [ ]: import numpy as np
import pandas as pd
movies = pd.read_csv('Data\Day35\imdb-top-1000.csv')
In [ ]: genres = movies.groupby('Genre')
1. len
In [ ]: len(movies.groupby('Genre'))
14
Out[ ]:
2. nunique
In [ ]: movies['Genre'].nunique()
14
Out[ ]:
3. size
In [ ]: movies.groupby('Genre').size()
4. nth
In [ ]: genres = movies.groupby('Genre')
# genres.first()
# genres.last()
genres.nth(6)
Star Wars:
Episode V - Irvin Mark
16 1980 124 Action 8.7 1159
The Empire Kershner Hamill
Strikes Back
David Morgan
27 Se7en 1995 127 Crime 8.6 1445
Fincher Freeman
It's a
Frank James
32 Wonderful 1946 130 Drama 8.6 405
Capra Stewart
Life
Andrew Ben
66 WALL·E 2008 98 Animation 8.4 999
Stanton Burtt
Mel
102 Braveheart 1995 178 Biography 8.3 Mel Gibson 959
Gibson
Joseph L. Laurence
420 Sleuth 1972 138 Mystery 8.0 44
Mankiewicz Olivier
Jordan Daniel
724 Get Out 2017 104 Horror 7.7 492
Peele Kaluuya
5. get_group
In [ ]: genres.get_group('Fantasy')
Das
Robert Werner
321 Cabinet des 1920 76 Fantasy 8.1 57428 3
Wiene Krauss
Dr. Caligari
F.W. Max
568 Nosferatu 1922 94 Fantasy 7.9 88794 4
Murnau Schreck
Das
Robert Werner
321 Cabinet des 1920 76 Fantasy 8.1 57428 3
Wiene Krauss
Dr. Caligari
F.W. Max
568 Nosferatu 1922 94 Fantasy 7.9 88794 4
Murnau Schreck
6. describe
In [ ]: genres.describe()
count mean std min 25% 50% 75% max count mean ...
Genre
Action 172.0 129.046512 28.500706 45.0 110.75 127.5 143.25 321.0 172.0 7.949419 ...
Adventure 72.0 134.111111 33.317320 88.0 109.00 127.0 149.00 228.0 72.0 7.937500 ...
Animation 82.0 99.585366 14.530471 71.0 90.00 99.5 106.75 137.0 82.0 7.930488 ...
Biography 88.0 136.022727 25.514466 93.0 120.00 129.0 146.25 209.0 88.0 7.938636 ...
Comedy 155.0 112.129032 22.946213 68.0 96.00 106.0 124.50 188.0 155.0 7.901290 ...
Crime 107.0 126.392523 27.689231 80.0 106.50 122.0 141.50 229.0 107.0 8.016822 ...
Drama 289.0 124.737024 27.740490 64.0 105.00 121.0 137.00 242.0 289.0 7.957439 ...
Family 2.0 107.500000 10.606602 100.0 103.75 107.5 111.25 115.0 2.0 7.800000 ...
Fantasy 2.0 85.000000 12.727922 76.0 80.50 85.0 89.50 94.0 2.0 8.000000 ...
Film-Noir 3.0 104.000000 4.000000 100.0 102.00 104.0 106.00 108.0 3.0 7.966667 ...
Horror 11.0 102.090909 13.604812 71.0 98.00 103.0 109.00 122.0 11.0 7.909091 ...
Mystery 12.0 119.083333 14.475423 96.0 110.75 117.5 130.25 138.0 12.0 7.975000 ...
Thriller 1.0 108.000000 NaN 108.0 108.00 108.0 108.00 108.0 1.0 7.800000 ...
Western 4.0 148.250000 17.153717 132.0 134.25 148.0 162.00 165.0 4.0 8.350000 ...
14 rows × 40 columns
7. sample
In [ ]: genres.sample(2,replace=True)
The
Willem
648 Boondock 1999 108 Action 7.8 Troy Duffy
Dafoe
Saints
Aaron
Matthew
908 Kick-Ass 2010 117 Action 7.6 Taylor-
Vaughn
Johnson
Lee Adrian
61 Coco 2017 105 Animation 8.4
Unkrich Molina
Satoshi Megumi
758 Papurika 2006 90 Animation 7.7
Kon Hayashibara
328 Lion 2016 118 Biography 8.0 Garth Davis Dev Patel
Predrag
Emir
256 Underground 1995 170 Comedy 8.1 'Miki'
Kusturica
Manojlovic
Stanley Sterling
441 The Killing 1956 84 Crime 8.0
Kubrick Hayden
Brokeback Jake
773 2005 134 Drama 7.7 Ang Lee
Mountain Gyllenhaal
Willy Wonka
& the Gene
698 1971 100 Family 7.8 Mel Stuart
Chocolate Wilder
Factory
E.T. the
Steven Henry
688 Extra- 1982 115 Family 7.8
Spielberg Thomas
Terrestrial
F.W. Max
568 Nosferatu 1922 94 Fantasy 7.9
Murnau Schreck
Das Cabinet
Robert Werner
321 des Dr. 1920 76 Fantasy 8.1
Wiene Krauss
Caligari
932 Saw 2004 103 Horror 7.6 James Wan Cary Elwes
Joseph L. Laurence
420 Sleuth 1972 138 Mystery 8.0
Mankiewicz Olivier
Alfred James
119 Vertigo 1958 128 Mystery 8.3
Hitchcock Stewart
Il buono, il
Sergio Clint
12 brutto, il 1966 161 Western 8.8
Leone Eastwood
cattivo
8. nunique()
In [ ]: genres.nunique()
Genre
Adventure 72 49 58 10 59 59 72 72
Animation 82 35 41 11 51 77 82 82
Biography 88 44 56 13 76 72 88 88
Family 2 2 2 1 2 2 2 2
Fantasy 2 2 2 2 2 2 2 2
Film-Noir 3 3 3 3 3 3 3 3
Horror 11 11 10 8 10 11 11 11
Mystery 12 11 10 8 10 11 12 12
Thriller 1 1 1 1 1 1 1 1
Western 4 4 4 4 2 2 4 4
9. agg method
In [ ]: # passing dict
genres.agg(
{
file:///C:/Users/disha/Downloads/Day84 - Revision Of GroupBy object in Pandas(part2).html 6/9
12/19/23, 7:24 PM Day84 - Revision Of GroupBy object in Pandas(part2)
'Runtime':'mean',
'IMDB_Rating':'mean',
'No_of_Votes':'sum',
'Gross':'sum',
'Metascore':'min'
}
)
Genre
In [ ]: genres.agg(
{
'Runtime':['min','mean'],
'IMDB_Rating':'mean',
'No_of_Votes':['sum','max'],
'Gross':'sum',
'Metascore':'min'
}
)
Genre
Genre
Abhishek Aamir
Action 300 1924 45 Action 7.6
Chaubey Khan
2001: A
Akira Aamir
Adventure Space 1925 88 Adventure 7.6
Kurosawa Khan
Odyssey
Adam Adrian
Animation Akira 1940 71 Animation 7.6
Elliot Molina
Aamir Abhay
Drama 1917 1925 64 Drama 7.6
Khan Deol
E.T. the
Gene
Family Extra- 1971 100 Family 7.8 Mel Stuart
Wilder
Terrestrial
Das
F.W. Max
Fantasy Cabinet des 1920 76 Fantasy 7.9
Murnau Schreck
Dr. Caligari
Alejandro Anthony
Horror Alien 1933 71 Horror 7.6
Amenábar Perkins
Bernard-
Alex
Mystery Dark City 1938 96 Mystery 7.6 Pierre
Proyas
Donnadieu
Il buono, il
Clint Clint
Western brutto, il 1965 132 Western 7.8
Eastwood Eastwood
cattivo
Vectorized string operations in Pandas refer to the ability to apply string functions and
operations to entire arrays of strings (columns or Series containing strings) without the
need for explicit loops or iteration. This is made possible by Pandas' integration with the
NumPy library, which allows for efficient element-wise operations.
When you have a Pandas DataFrame or Series containing string data, you can use
various string methods that are applied to every element in the column simultaneously.
This can significantly improve the efficiency and readability of your code. Some of the
commonly used vectorized string operations in Pandas include methods like
.str.lower() , .str.upper() , .str.strip() , .str.replace() , and many
more.
Vectorized string operations not only make your code more concise and readable but
also often lead to improved performance compared to explicit for-loops, especially
when dealing with large datasets.
In [ ]: import numpy as np
import pandas as pd
In [ ]: s = pd.Series(['cat','mat',None,'rat'])
0 True
Out[ ]:
1 False
2 None
3 False
dtype: object
In [ ]: df.head()
Out[ ]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin
Braund,
A/5
0 1 0 3 Mr. Owen male 22.0 1 0 7.2500 NaN
21171
Harris
Cumings,
Mrs. John
Bradley
1 2 1 1 female 38.0 1 0 PC 17599 71.2833 C85
(Florence
Briggs
Th...
Heikkinen,
STON/O2.
2 3 1 3 Miss. female 26.0 0 0 7.9250 NaN
3101282
Laina
Futrelle,
Mrs.
Jacques
3 4 1 1 female 35.0 1 0 113803 53.1000 C123
Heath
(Lily May
Peel)
Allen, Mr.
4 5 0 3 William male 35.0 0 0 373450 8.0500 NaN
Henry
In [ ]: df['Name']
Common Functions
In [ ]: # lower/upper/capitalize/title
df['Name'].str.upper()
df['Name'].str.capitalize()
df['Name'].str.title()
In [ ]: # len
df['Name'][df['Name'].str.len()]
In [ ]: df['Name'][df['Name'].str.len() == 82].values[0]
'Penasco y Castellana, Mrs. Victor de Satode (Maria Josefa Perez de Soto y Vallej
Out[ ]:
o)'
In [ ]: # strip
df['Name'].str.strip()
Out[ ]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin
Braund,
A/5
0 1 0 3 Mr. Owen male 22.0 1 0 7.2500 NaN
21171
Harris
Cumings,
Mrs. John
Bradley
1 2 1 1 female 38.0 1 0 PC 17599 71.2833 C85
(Florence
Briggs
Th...
Heikkinen,
STON/O2.
2 3 1 3 Miss. female 26.0 0 0 7.9250 NaN
3101282
Laina
Futrelle,
Mrs.
Jacques
3 4 1 1 female 35.0 1 0 113803 53.1000 C123
Heath
(Lily May
Peel)
Allen, Mr.
4 5 0 3 William male 35.0 0 0 373450 8.0500 NaN
Henry
In [ ]: df[['title','firstname']] = df['Name'].str.split(',').str.get(1).str.strip().str.sp
df.head()
Out[ ]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin
Braund,
A/5
0 1 0 3 Mr. Owen male 22.0 1 0 7.2500 NaN
21171
Harris
Cumings,
Mrs. John
Bradley
1 2 1 1 female 38.0 1 0 PC 17599 71.2833 C85
(Florence
Briggs
Th...
Heikkinen,
STON/O2.
2 3 1 3 Miss. female 26.0 0 0 7.9250 NaN
3101282
Laina
Futrelle,
Mrs.
Jacques
3 4 1 1 female 35.0 1 0 113803 53.1000 C123
Heath
(Lily May
Peel)
Allen, Mr.
4 5 0 3 William male 35.0 0 0 373450 8.0500 NaN
Henry
In [ ]: df['title'].value_counts()
In [ ]: # replace
df['title'] = df['title'].str.replace('Ms.','Miss.')
df['title'] = df['title'].str.replace('Mlle.','Miss.')
In [ ]: df['title'].value_counts()
title
Out[ ]:
Mr. 517
Miss. 185
Mrs. 125
Master. 40
Dr. 7
Rev. 6
Major. 2
Col. 2
Don. 1
Mme. 1
Lady. 1
Sir. 1
Capt. 1
the 1
Jonkheer. 1
Name: count, dtype: int64
filtering
In [ ]: # startswith/endswith
df[df['firstname'].str.endswith('A')]
Out[ ]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin
Stewart,
Mr. PC
64 65 0 1 male NaN 0 0 27.7208 NaN
Albert 17605
A
Keane,
303 304 1 2 Miss. female NaN 0 0 226593 12.3500 E101
Nora A
In [ ]: # isdigit/isalpha...
df[df['firstname'].str.isdigit()]
Out[ ]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
slicing
In [ ]: df['Name'].str[::-1]
Interview Questions
1.What does the else clause in a loop do?
The else clause in a loop is executed when the loop finishes execution (i.e., when the
loop condition becomes False). It won't execute if the loop was exited using a break
statement.
In [ ]: for i in range(5):
print(i)
else:
print("Loop finished")
0
1
2
3
4
Loop finished
The pass statement is a no-op (does nothing). It's used as a placeholder where
syntactically some code is required, but you don't want to execute any command or
code.
3.How do you retrieve all the keys, values, and items from a dictionary?
In [ ]: d = {'name':'John','age':24,}
print(d.keys())
print(d.values())
print(d.items())
dict_keys(['name', 'age'])
dict_values(['John', 24])
dict_items([('name', 'John'), ('age', 24)])
'John'
Out[ ]:
Inheritance is achieved by defining a new class, derived from an existing class. The
derived class inherits attributes and behaviors of the base class and can also have
additional attributes or behaviors.
In [ ]: class Animal:
def speak(self):
pass
class Dog(Animal):
In [ ]: # Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound")
# Calling methods
animal.speak() # Output: Generic Animal makes a sound
dog.speak() # Output: Buddy barks
cat.speak() # Output: Whiskers meows
In this example:
The Animal class is the parent class with a init method and a speak method.
The Dog and Cat classes are child classes that inherit from the Animal class. They
override the speak method to provide their own implementation.
Instances of Dog and Cat can access both the attributes and methods of the Animal
class, and they can also have their own additional attributes and methods.
The str method is a special method that should return a string representation of the
object. It's invoked by the built-in str() function and by the print() function when
outputting the object.
In [ ]: class Person:
def __init__(self, name):
self.name = name
def __str__(self):
return f"Person named {self.name}"
p = Person("Alice")
print(p)
# Example usage:
list1 = [1, 3, 5]
list2 = [2, 4, 6]
result = merge_sorted_lists(list1, list2)
print(result)
[1, 2, 3, 4, 5, 6]
# Example usage:
numbers = [0, 1, 3, 4, 5]
result = find_missing_number(numbers)
print(result)
Given two lists, write a function that returns the elements that are
common to both lists.
find_common_element(l1,l2)
[3, 4, 5]
Out[4]:
In [5]: # function
def merge_two_dictionaries(dict1,dict2):
merged = dict1.copy()
merged.update(dict2)
return merged
In [7]: # list
numbers = [1, 2, 3, 4, 4, 5, 5, 5, 6]
print(central_measures(numbers))
string = "radar"
print(is_palindrome(string))
True
number = 5
print(factorial_iterative(number))
120
# Example usage:
word1 = "listen"
word2 = "silent"
result = are_anagrams(word1, word2)
print(result)
True
Write a Python function to find the Nth number in the Fibonacci sequence.
# Example usage:
n = 6
result = fibonacci(n)
print(result)
# Example usage:
numbers = [1, 2, 2, 3, 4, 4, 5]
result = remove_duplicates(numbers)
print(result)
[1, 2, 3, 4, 5]
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
def is_empty(self):
return len(self.items) == 0
# Example usage:
number = 11
result = is_prime(number)
print(result)
True
# Example usage:
my_list = [1, 2, 2, 3, 4, 4, 5]
result = remove_duplicates(my_list)
print(result)
[1, 2, 3, 4, 5]
# Example usage:
arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
result = max_subarray_sum(arr)
print(result)
Write a Python function to find the Nth number in the Fibonacci sequence.
# Example usage:
N = 8
result = fibonacci(N)
print(result)
21
# Example usage:
list_x = [1, 2, 3, 4, 5]
list_y = [3, 4, 5, 6, 7]
result = find_intersection(list_x, list_y)
print(result)
[3, 4, 5]
# Example usage:
result = power(2, 3)
print(result)
There are many reasons why OOPs is mostly preferred, but the most important among them
are:
OOPs helps users to understand the software easily, although they don’t know the
actual implementation.
With OOPs, the readability, understandability, and maintainability of the code increase
multifold.
Even very big software can be easily written and managed easily using OOPs.
2. What is a class?
A class can be understood as a template or a blueprint, which contains some values, known
as member data or member, and some set of rules, known as behaviors or functions. So
when an object is created, it automatically takes the data and functions that are defined in
the class. Therefore the class is basically a template or blueprint for objects. Also one can
create as many objects as they want based on a class.
For example, first, a car’s template is created. Then multiple units of car are created based on
that template.
# Instance method
def bark(self):
print(f"{self.name} says Woof!")
3. What is an object?
An object refers to the instance of the class, which contains the instance of the members and
behaviors defined in the class template. In the real world, an object is an actual entity to
which a user interacts, whereas class is just the blueprint for that object. So the objects
consume space and have some characteristic behavior.
4. What is encapsulation?
One can visualize Encapsulation as the method of putting everything that is required to do
the job, inside a capsule and presenting that capsule to the user. What it means is that by
Encapsulation, all the necessary data and methods are bind together and all the unnecessary
details are hidden to the normal user. So Encapsulation is the process of binding data
members and methods of a program together to do a specific job, without revealing
unnecessary details.
5.what is inheritance
Inheritance is a key concept in object-oriented programming (OOP) that allows a new class
(subclass or derived class) to inherit attributes and behaviors from an existing class (base
class or superclass). The new class can extend or override the functionalities of the existing
class, promoting code reuse and the creation of a hierarchy of classes.
def speak(self):
pass
In this example, the Animal class is the superclass, and the Dog and Cat classes are
subclasses. Both Dog and Cat inherit the __init__ method (constructor) and the
speak method from the Animal class. However, each subclass provides its own
implementation of the speak method, allowing them to exhibit different behaviors.
Inheritance is a powerful mechanism in OOP that helps in organizing and structuring code in
a hierarchical manner, making it easier to manage and extend software systems.
[2, 4, 6, 8, 10]
# Output
print(result)
# output
print(squred_numbers)
print(common_elements)
[3, 4, 5]
print(squares)
[1, 0, 9, 0, 25, 0]
print(vowels)
[5, 6, 4, 6]
For instance, consider the following example that creates a dictionary of squares for even
numbers from 0 to 8:
print(even_squares_dict)
{x: x**2 for x in range(5)} , x is the key_expression, and x**2 is the value_expression.
For instance, consider the following example that filters out odd squares from a dictionary:
print(original_dict)
print('--------------')
print(filtered_dict)
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
--------------
{0: 0, 2: 4, 4: 16}
In terms of efficiency, dictionary comprehensions are generally more concise and can be
as efficient as traditional methods.
They provide a more readable and compact way to create dictionaries. However, for
very large datasets or complex conditions, the difference in performance may be
negligible.
It's always essential to consider readability and maintainability alongside performance
when choosing between different approaches.