Interview Questions
1. What is the concept behind the 'self' variable?
2. What does self refer to in the __init__ method?
3. When does a class method does not take self as the first argurment?
4. What is the concept behind the __init__ method?
5. Explain the need for Object Oriented Programming when we could do stuff with Functional programing?
6. What is the difference between a function and a method?
7. Apart from attributes and methods, what else can a Python object hold?
In Python, an object can hold several types of data beyond just attributes (data members) and methods (functions). Here’s a breakdown of other things a Python object can hold:
1. Class Variables
- Definition: Variables that are shared among all instances of a class. These are defined within the class but outside any instance methods.
- Usage: Often used to maintain shared state or constants across all instances of a class.
pythonclass MyClass:
shared_variable = "I am shared"
def __init__(self, value):
self.instance_variable = value
2. Static Methods and Class Methods
- Static Methods: Methods that do not operate on an instance of the class. They are bound to the class and not the object instance.
pythonclass MyClass:
@staticmethod
def static_method():
print("This is a static method")
- Class Methods: Methods that are bound to the class itself and not to any particular object instance. They receive the class as the first argument (
cls
).
pythonclass MyClass:
@classmethod
def class_method(cls):
print("This is a class method")
3. Properties (Getter and Setter Methods)
- Properties: Special methods that allow controlled access to an object’s attributes. They are often used to encapsulate data and add logic to getting and setting attribute values.
pythonclass MyClass:
def __init__(self, value):
self._value = value
@property
def value(self):
return self._value
@value.setter
def value(self, new_value):
self._value = new_value
4. Descriptors
- Definition: Objects that define the behavior of attribute access, specifically through the use of
__get__()
, __set__()
, and __delete__()
methods. Descriptors are a way to create reusable and controlled attribute management.
pythonclass Descriptor:
def __get__(self, instance, owner):
return "Descriptor get method"
class MyClass:
attr = Descriptor()
5. Custom __call__
Method
- Definition: An object can be made callable by defining the
__call__()
method. This allows the object to be called as if it were a function.
pythonclass MyCallable:
def __call__(self, x):
return x * x
obj = MyCallable()
print(obj(5)) # Output: 25
6. Slots
- Definition: A special mechanism (
__slots__
) that restricts the creation of instance attributes to a predefined set, reducing memory usage for instances of the class.
pythonclass MyClass:
__slots__ = ['attr1', 'attr2']
def __init__(self, attr1, attr2):
self.attr1 = attr1
self.attr2 = attr2
7. Magic Methods (Dunder Methods)
- Definition: Special methods that begin and end with double underscores, like
__init__
, __str__
, __repr__
, __eq__
, etc. These methods allow objects to interact with Python’s built-in functions and operators.
pythonclass MyClass:
def __str__(self):
return "String representation of MyClass"
8. Type Annotations
- Definition: While not held directly by the object, Python objects can have type annotations for attributes, which are hints for developers and tools to understand what type of data the attribute should hold.
pythonclass MyClass:
attr: int
def __init__(self, attr: int):
self.attr = attr
9. Metaclasses
- Definition: The class of a class, controlling the creation of classes themselves. Metaclasses define how classes behave.
pythonclass Meta(type):
def __new__(cls, name, bases, attrs):
return super(Meta, cls).__new__(cls, name, bases, attrs)
class MyClass(metaclass=Meta):
pass
10. Annotations (__annotations__
)
- Definition: A dictionary attribute where type annotations are stored. This can be accessed via the
__annotations__
attribute on the class or instance.
pythonclass MyClass:
attr: int = 5
print(MyClass.__annotations__) # Output: {'attr': <class 'int'>}
11. Custom Attributes Added Dynamically
- Definition: Python objects are dynamic, so you can add attributes to instances even after they’ve been created.
pythonclass MyClass:
pass
obj = MyClass()
obj.new_attr = "Dynamically added attribute"
These additional elements allow Python objects to be highly flexible and powerful, supporting a wide range of programming paradigms and advanced behaviors.
8. Can you explain this with an example?
Certainly! Let's explore the various things a Python object can hold by creating an example class that demonstrates these concepts.
Example Class: Car
We'll create a class called Car
that showcases attributes, methods, class variables, static methods, class methods, properties, descriptors, magic methods, and more.
python# Descriptor example
class FuelEfficiency:
def __get__(self, instance, owner):
return instance._mileage / instance._fuel_used
class Car:
# Class variable
vehicle_type = "Automobile"
# __slots__ example to limit attributes
__slots__ = ['make', 'model', 'year', '_mileage', '_fuel_used']
# Annotations for type hints
make: str
model: str
year: int
# Static variable for descriptor
fuel_efficiency = FuelEfficiency()
def __init__(self, make, model, year, mileage, fuel_used):
self.make = make
self.model = model
self.year = year
self._mileage = mileage
self._fuel_used = fuel_used
# Instance method
def drive(self, distance):
self._mileage += distance
print(f"Driven {distance} miles. Total mileage: {self._mileage} miles")
# Static method
@staticmethod
def honk():
print("Honk! Honk!")
# Class method
@classmethod
def from_string(cls, car_string):
make, model, year = car_string.split('-')
return cls(make, model, int(year), 0, 0)
# Property for controlled access to mileage
@property
def mileage(self):
return self._mileage
@mileage.setter
def mileage(self, value):
if value < 0:
raise ValueError("Mileage cannot be negative")
self._mileage = value
# Magic method to represent the object as a string
def __str__(self):
return f"{self.year} {self.make} {self.model}"
# Magic method to make the object callable
def __call__(self, action):
return f"The car is now {action}."
# Creating a car object using the class
my_car = Car("Toyota", "Corolla", 2020, 5000, 200)
# Demonstrating different aspects of the class
# Accessing class variable
print(f"Vehicle Type: {Car.vehicle_type}")
# Using an instance method
my_car.drive(150)
# Using a static method
Car.honk()
# Using a class method to create an instance
new_car = Car.from_string("Honda-Civic-2022")
print(f"New Car: {new_car}")
# Using a property
print(f"Mileage: {my_car.mileage}")
my_car.mileage = 6000 # Setting mileage via the property
print(f"Updated Mileage: {my_car.mileage}")
# Using the descriptor for fuel efficiency
print(f"Fuel Efficiency: {my_car.fuel_efficiency} miles per gallon")
# Using a magic method (__str__) to get a string representation
print(str(my_car))
# Using a magic method (__call__) to make the object callable
print(my_car("accelerating"))
# Adding a dynamic attribute (not allowed due to __slots__)
# my_car.color = "Blue" # This will raise an AttributeError
Explanation of Each Concept
Class Variable (vehicle_type
):
- This variable is shared by all instances of the class. It is accessible through the class itself (
Car.vehicle_type
) or any instance (my_car.vehicle_type
).
Instance Variables (make
, model
, year
, _mileage
, _fuel_used
):
- These are specific to each instance of the
Car
class, holding the state of the object. For example, my_car.make
holds the value "Toyota"
.
Static Method (honk
):
- This method does not depend on the instance's state and can be called on the class itself.
Car.honk()
prints a honk sound.
Class Method (from_string
):
- This method is bound to the class, not the instance. It's used here to create a
Car
object from a string. Car.from_string("Honda-Civic-2022")
creates a new Car
object.
Properties (mileage
):
- Properties provide a controlled way to access and set instance variables. The
mileage
property ensures that the mileage cannot be set to a negative value.
Descriptor (fuel_efficiency
):
- The
FuelEfficiency
descriptor calculates the car's fuel efficiency dynamically based on the mileage and fuel used. The descriptor is accessed like a normal attribute (my_car.fuel_efficiency
).
Magic Methods (__str__
, __call__
):
__str__
: Provides a readable string representation of the object, used when you print the object or convert it to a string.__call__
: Makes the object callable like a function, allowing my_car("accelerating")
to work.
Slots (__slots__
):
- The
__slots__
attribute limits the attributes that can be added to an instance, reducing memory usage. Attempting to add an attribute not listed in __slots__
will raise an AttributeError
.
This example demonstrates how a Python object can hold various types of data and behavior, making it a powerful and flexible construct in object-oriented programming.
9. Can I change a class variable?
And what would it be it's impant on the various instances of this class?
Yes, you can change a class variable in Python. The impact of changing a class variable depends on how you modify it, and it can affect all instances of the class in specific ways. Here's a detailed explanation:
1. Changing the Class Variable via the Class
When you change a class variable directly via the class, the change affects all instances of the class that do not have an overridden value for that variable.
pythonclass Car:
# Class variable
vehicle_type = "Automobile"
def __init__(self, make, model):
self.make = make
self.model = model
# Create two instances
car1 = Car("Toyota", "Corolla")
car2 = Car("Honda", "Civic")
# Access the class variable
print(car1.vehicle_type) # Output: Automobile
print(car2.vehicle_type) # Output: Automobile
# Change the class variable via the class
Car.vehicle_type = "Motor Vehicle"
# Access the class variable again
print(car1.vehicle_type) # Output: Motor Vehicle
print(car2.vehicle_type) # Output: Motor Vehicle
Impact:
- When
Car.vehicle_type
is modified, all instances (car1
, car2
, etc.) reflect the new value, "Motor Vehicle"
, because they are still referencing the class variable. - Since
vehicle_type
is shared across all instances, changing it through the class affects every instance that hasn't overridden this variable.
2. Overriding the Class Variable via an Instance
If you change the class variable via an instance, it doesn't change the class variable itself but instead creates (or overrides) an instance variable with the same name.
pythonclass Car:
# Class variable
vehicle_type = "Automobile"
def __init__(self, make, model):
self.make = make
self.model = model
# Create two instances
car1 = Car("Toyota", "Corolla")
car2 = Car("Honda", "Civic")
# Override the class variable in car1
car1.vehicle_type = "Electric Vehicle"
# Access the class variable
print(car1.vehicle_type) # Output: Electric Vehicle
print(car2.vehicle_type) # Output: Automobile
Impact:
car1.vehicle_type
now points to an instance variable, not the class variable. So car1
shows "Electric Vehicle"
.car2.vehicle_type
still references the class variable, so it continues to show "Automobile"
.- Changing
car1.vehicle_type
does not affect car2
or any other instance.
3. Resetting the Class Variable via the Class
If you reset the class variable after instances have overridden it, only those instances that have not overridden the variable will see the change.
pythonclass Car:
# Class variable
vehicle_type = "Automobile"
def __init__(self, make, model):
self.make = make
self.model = model
# Create two instances
car1 = Car("Toyota", "Corolla")
car2 = Car("Honda", "Civic")
# Override the class variable in car1
car1.vehicle_type = "Electric Vehicle"
# Change the class variable via the class
Car.vehicle_type = "Motor Vehicle"
# Access the class variable
print(car1.vehicle_type) # Output: Electric Vehicle (instance variable, unchanged)
print(car2.vehicle_type) # Output: Motor Vehicle (class variable, changed)
Impact:
car1
still holds its instance variable ("Electric Vehicle"
), so it isn't affected by the change in the class variable.car2
, which didn't override the class variable, now shows the updated value "Motor Vehicle"
.
Summary
- Class-Level Change: Changing a class variable directly via the class affects all instances that have not overridden that variable.
- Instance-Level Override: Changing a class variable via an instance creates an instance-specific variable, leaving the class variable unaffected for other instances.
- Instance Impact: Instances that override the class variable are unaffected by changes to the class variable, while those that don't will reflect any changes made to the class variable.
Monday, August 19, 2024
Intro to Classes and Objects in Python (Video and Interview Questions Inside)
To Watch All Python Videos: YouTube Academy For Python
Thursday, August 15, 2024
What is Machine Learning (With Video and Interview Questions)
To See All ML Articles: Index of Machine Learning
Intro to Machine Learning (ML Zero to Hero - Part 1) By: TensorFlow Machine Learning Foundations: Ep #1 - What is ML? By: Google for DevelopersTags: Machine Learning,VideoInterview Questions
Q1: What does this model seem to represent? a. Coding b. Machine Learning Q2: Can you draw a similar model for Machine Learning? Q3: Take a look at these numbers: X = -1, 0, 1, 2, 3, 4 Y = -3, -1, 1, 3, 5, 7 What kind of model would you use for this data for best results? Q4: Why and how is Laurence able to use Neural Network for this data from Q3? Q5: What kind of problem is this from Q3? Is it Regression, Clustering or Classification? Q6: What is the role of an optimizer in training a Neural Network? Q7: What would be the output of this code? Assuming all the imports are there. model = keras.Sequential([keras.layers.Dense (units=1, input_shape=[1])]) model.compile(optimizer='sgd', loss='mean_squared_error') xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float) ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float) model.fit(xs, ys, epochs=500) print(model.predict([10.0])) Q8: Would the answer to Q7 be exactly 19 or roughly 19? Explain why.
Wednesday, August 14, 2024
Cost Function For Logistic Regression Model
To See All ML Articles: Index of Machine Learning
Tags: Machine Learning,Mathematical Foundations for Data Science,Can you show the derivation for this Cost function?
Here's a step-by-step derivation of the cost function:
1. Understanding Logistic Regression:
- In logistic regression, the probability that a given sample x(i) belongs to class y(i) (which can be either 0 or 1) is modeled using the logistic function: Ï•(z)=1+e−z1wherez=wTx
- For binary classification, y(i) can either be 0 or 1, so: P(y(i)=1∣x(i);w)=Ï•(wTx(i)) P(y(i)=0∣x(i);w)=1−Ï•(wTx(i))
2. Likelihood Function for a Single Observation:
- The likelihood for a single observation i can be written as: P(y(i)∣x(i);w)=Ï•(wTx(i))y(i)⋅(1−Ï•(wTx(i)))1−y(i)
- This formula works because:
- If y(i)=1, the likelihood is Ï•(wTx(i)).
- If y(i)=0, the likelihood is 1−Ï•(wTx(i)).
3. Likelihood Function for the Entire Dataset:
- Assuming that the samples are independent, the likelihood of the entire dataset (with n samples) is the product of the likelihoods of individual observations: L(w)=i=1∏nP(y(i)∣x(i);w)=i=1∏n[Ï•(wTx(i))y(i)⋅(1−Ï•(wTx(i)))1−y(i)]
4. Log-Likelihood:
- It is often easier to work with the log-likelihood (because the logarithm turns the product into a sum), which is given by: logL(w)=i=1∑n[y(i)logÏ•(wTx(i))+(1−y(i))log(1−Ï•(wTx(i)))]
5. Cost Function:
- In practice, we minimize the negative log-likelihood, which gives us the cost function for logistic regression: J(w)=−logL(w)=−i=1∑n[y(i)logÏ•(wTx(i))+(1−y(i))log(1−Ï•(wTx(i)))]
- This cost function is convex, which makes it suitable for optimization algorithms like gradient descent.
Conclusion:
The derivation of the likelihood function for logistic regression involves expressing the probability of each individual sample given the model's parameters and then combining these probabilities across all samples in the dataset, assuming independence. The final likelihood function provides a way to measure how well the model's parameters fit the data, and minimizing the negative log-likelihood (or equivalently, maximizing the likelihood) leads to the optimal model parameters.
Reference
Python Machine Learning Sebastian Raschka Packt Publishing (2015)
Subscribe to:
Posts (Atom)