Background Knowledge
Logistic Regression is a fundamental algorithm in Machine Learning used for binary classification problems. It's based on the idea of predicting the probability of an event occurring by fitting data to a logistic curve, also known as the sigmoid function. The sigmoid function, denoted as σ(x), maps any real-valued number to a value between 0 and 1, making it suitable for modeling probabilities.
The prediction step in logistic regression involves calculating the probability of a sample belonging to a particular class (in this case, y=1) given its features x. This is done using the formula P(y=1∣x)=σ(x⋅w+b), where w is the weight vector, b is the bias, and x⋅w represents the dot product of the feature vector x and the weight vector w. The result of this calculation gives us the probability, which can then be used to make a prediction by comparing it to a threshold, typically 0.5.
Understanding the sigmoid function is crucial for logistic regression. The sigmoid function is defined as σ(x)=1+e−x1, where e is the base of the natural logarithm. This function has an S-shaped curve, where values of x less than 0 approach 0, and values greater than 0 approach 1. This property makes the sigmoid function ideal for modeling binary outcomes.
Algorithm/Approach
The general approach to solving this problem involves:
- Computing the dot product of each feature vector x in the feature matrix X with the weight vector w.
- Adding the bias b to each result from the previous step.
- Applying the sigmoid function to each result to obtain the probabilities.
- Comparing each probability to the threshold (0.5) to make predictions.
Step-by-Step Strategy
- Initialize an empty list to store the results.
- Iterate over each feature vector x in the feature matrix X.
- For each x, compute the dot product x⋅w.
- Add the bias b to the result from step 3.
- Apply the sigmoid function to the result from step 4 to get the probability.
- Round the probability to 4 decimal places.
- Compare the probability to 0.5 and make a prediction (1 if P≥0.5, 0 otherwise).
- Append a tuple containing the rounded probability and the prediction to the results list.
- Return the list of results.
Common Pitfalls
- Incorrectly implementing the sigmoid function or the dot product.
- Forgetting to add the bias b.
- Not rounding the probabilities to the specified number of decimal places.
- Incorrectly comparing the probabilities to the threshold for making predictions.
Time & Space Complexity
The time complexity of this solution is O(n⋅m), where n is the number of samples in the feature matrix X and m is the number of features. This is because for each sample, we are performing a dot product operation that involves iterating over all its features. The space complexity is O(n), as we need to store the results for each sample.