# One neuron: w·x + b and an activation

Every neural network is built from one simple unit: multiply the inputs by weights, add a bias, pass the sum through an activation. Here is that unit on its own, with its two inputs drawn as a plane.

<div data-sim="neuron"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

The colour shows the neuron's output for every point (x₁, x₂). The white line is where w·x + b = 0, the neuron's decision line; the amber arrow is the weight vector, which always points across the line toward the high side. Drag the white probe to see the calculation for one point.

## Things to try

- Change only b: the line slides without turning.
- Change w₁ and w₂: the line turns; their size sets how sharp the transition is.
- Switch between step, sigmoid and ReLU: same line, very different outputs.
- Make both weights zero: the neuron ignores its inputs and outputs a constant.

## How it works in real systems

A single neuron is logistic regression when the activation is a sigmoid, and a perceptron when it is a step. Real networks stack thousands of them so that many straight lines combine into any shape.

## Code lab

The same idea in real code, step by step. Copy each block into a notebook or a file and run it in order.

<div class="cl-start"></div>

### Step 1 · A neuron is three lines

```python
import numpy as np
w = np.array([1.2, -0.8]); b = 0.2
def neuron(x, act="sigmoid"):
    z = w @ x + b                               # weighted sum plus bias
    return {"step": float(z > 0), "sigmoid": 1 / (1 + np.exp(-z)), "relu": max(0.0, z)}[act]
print(neuron(np.array([0.4, 0.3])))             # z = 0.44 → sigmoid 0.608, as in the simulation
```

### Step 2 · The decision line

The neuron's output is 0.5 exactly where z = 0, the line w₁x₁ + w₂x₂ + b = 0. The weight vector is perpendicular to it; the bias shifts it.

```python
import matplotlib.pyplot as plt
xx, yy = np.meshgrid(np.linspace(-1, 1, 200), np.linspace(-1, 1, 200))
Z = 1 / (1 + np.exp(-(w[0] * xx + w[1] * yy + b)))
plt.contourf(xx, yy, Z, 20, cmap="Blues"); plt.contour(xx, yy, Z, [0.5], colors="k"); plt.show()
```

### Step 3 · Train it: logistic regression

One sigmoid neuron trained with cross-entropy is logistic regression.

```python
from sklearn.datasets import make_classification
X, y = make_classification(200, n_features=2, n_redundant=0, random_state=1)
w, b, lr = np.zeros(2), 0.0, 0.1
for _ in range(1000):
    p = 1 / (1 + np.exp(-(X @ w + b)))
    w -= lr * X.T @ (p - y) / len(y); b -= lr * (p - y).mean()
print(w, b, ((p > 0.5) == y).mean())
```

<div class="cl-end"></div>

## More simulations

[← k-means](/ai-simulations/k-means-clustering) · [All AI Simulations](/ai-simulations) · [Activations →](/ai-simulations/activation-functions)
