Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions snippets/python/math-and-numbers/neuron.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Basic Neuron Class
description: A Python class representing a single artificial neuron that computes the weighted sum of inputs and applies an optional activation function.
tags: python, machine-learning, neural-networks
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read the guidelines in CONTRIBUTING.md before submitting any snippets, Tags shouldn't contain the name of the language they are coded in

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding typing to function could be great too !

author: jimmydin7
---

```py
import numpy as np

class Neuron:
def __init__(self, inputs, weights, bias):
self.inputs = inputs
self.weights = weights
self.bias = bias

def get_output(self):
weighted_sum = np.dot(self.inputs, self.weights) + self.bias
return weighted_sum

# Example usage
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow proper formatting of snippets, using # Usage: for the usage section.
Could you also improve the example a bit ? There is some text that's not in a comment, and you could add a comment showing what the result of a calculation is/looks like

inputs = np.array([1.0, 2.0, 3.0])
weights = np.array([0.2, 0.8, -0.5])
bias = 2.0

neuron = Neuron(inputs, weights, bias)
output = neuron.get_output() (you can add an activation function)
print(f"Neuron output: {output}")
```