Sigmoid Function Practice Problem
This data science coding problem helps you practice Logistic Regression, sigmoid function, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Logistic Regression.
- Problem ID: 15
- Problem key: 15-sigmoid-function
- URL: https://datacrack.app/solve/15-sigmoid-function
- Difficulty: easy
- Topic: Logistic Regression
- Module: Introduction to Machine Learning
Problem Statement
# Sigmoid Function
---
### π― Goal
Understand and implement the **sigmoid function**, which converts any real number into a probability value between 0 and 1.
Itβs the mathematical heart of **logistic regression**, turning model outputs (called logits) into probabilities.
---
### π Explanation of Symbols
| Symbol | Meaning | Shape / Type |
| :-------------: | :------------------------------------------------ | :------------ |
| **$z$** | Input value(s) β single number or list of numbers | float or list |
| **$\sigma(z)$** | Output of sigmoid (probability) | float or list |
| **$e$** | Eulerβs number (β 2.71828) | constant |
---
### π₯ Input / π€ Output
* **Input:**
* `z`: float or list β raw input values or scores.
* **Output:**
* list or float β sigmoid-transformed values between 0 and 1.
---
### π» Task
Implement the **sigmoid function** using its mathematical definition:
$$
\sigma(z) = \frac{1}{1 + e^{-z}}
$$
---
### π§© Starter Code
```python
import numpy as np
def sigmoid(z):
"""
Compute the sigmoid function for input z.
Args:
z (float or list): input value(s)
Returns:
float or list: sigmoid of z
"""
# Convert input to numpy array for easy computation
z = np.array(z)
# TODO: implement sigmoid formula
pass
```
---
### π‘ Example
```python
print(sigmoid(0))
print(sigmoid([-2, 0, 2]))
```
**Expected Output:**
```
0.5
[0.11920292, 0.5, 0.88079708]
```
---
Starter Code
import numpy as np
def sigmoid(z):
"""
Compute the sigmoid function for input z.
Args:
z (float or list): input value(s)
Returns:
float or list: sigmoid of z
"""
# Convert input to numpy array for easy computation
z = np.array(z)
# TODO: implement sigmoid formula
pass