Skip to content

Instantly share code, notes, and snippets.

@mattbullen
Created October 16, 2025 11:55
Show Gist options
  • Select an option

  • Save mattbullen/a360b2de24b3a0c4376d9f6cd1628ec5 to your computer and use it in GitHub Desktop.

Select an option

Save mattbullen/a360b2de24b3a0c4376d9f6cd1628ec5 to your computer and use it in GitHub Desktop.
Unit07 Ex1 simple_perceptron.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/mattbullen/a360b2de24b3a0c4376d9f6cd1628ec5/unit07-ex1-simple_perceptron.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PV8695XypnqA"
},
"source": [
"### Author: Dr Mike Lakoju, CardiffMet"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dNT3P67epnqC"
},
"source": [
"### We are re-implementing the last code. With Neural networks, most implementation uses large datasets, and it is important to write our code in a more optimized manner. We will be using Numpy Library to achieve this."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tjkmk1MypnqC"
},
"source": [
"# Import Library\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Ci1ugJQPpnqC"
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zW0RL4zXpnqD"
},
"source": [
"# Lets define the Inputs and weights"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "maaHQHP0pnqD"
},
"source": [
"With NumPy, we work with arrays. Hence we will need to define our inputs as arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "eMsY9x4IpnqD"
},
"outputs": [],
"source": [
"inputs = np.array([45, 25])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ZR2ooaL5pnqE",
"outputId": "f1cc9f19-9bad-4320-b2f9-a6998a7c6dcc"
},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check the type of the inputs\n",
"\n",
"type(inputs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "RF1zaQCjpnqE",
"outputId": "7e9fa93c-26ee-4f45-a1da-24552f7e9829"
},
"outputs": [
{
"data": {
"text/plain": [
"45"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# check the value at index position 0\n",
"inputs[0]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "w0n37NBopnqE"
},
"source": [
"# Lets define the weights"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "PMV9KP4mpnqE"
},
"outputs": [],
"source": [
"# creating the weights as Numpy array\n",
"\n",
"weights = np.array([0.7, 0.1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "DzgPHWDopnqE",
"outputId": "4ee46a74-6c4b-425f-a296-129f4ff4a397"
},
"outputs": [
{
"data": {
"text/plain": [
"0.7"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check the value at index 0\n",
"\n",
"weights[0]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PHOS8uFIpnqF"
},
"source": [
"# Create the Sum Function\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "90iAUKE0pnqF"
},
"source": [
"The dot function is called the dot product from linear algebra. If you are dealing with a huge dataset, The processing difference between the for loop used in the last notebook and this dot product will significantly be different."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "9EKwBS-spnqF"
},
"outputs": [],
"source": [
"def sum_func(inputs, weights):\n",
" return inputs.dot(weights)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "-N0F_991pnqF",
"outputId": "afd36f32-574b-4e78-d266-5851426c35e9"
},
"outputs": [
{
"data": {
"text/plain": [
"34.0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# for weights = [0.7, 0.1]\n",
"\n",
"s_prob1 = sum_func(inputs, weights)\n",
"s_prob1"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AvjuiH7ipnqF"
},
"source": [
"# Create Step function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Zez9hgEQpnqF"
},
"outputs": [],
"source": [
"def step_function(sum_func):\n",
" if (sum_func >= 1):\n",
" print(f'The Sum Function is greater than or equal to 1')\n",
" return 1\n",
" else:\n",
" print(f'The Sum Function is NOT greater')\n",
" return 0"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qovc5BVcpnqF"
},
"source": [
"### Result"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "u5o6zQenpnqF",
"outputId": "b4776d0f-1275-4f3b-98a0-589a281059f8"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Sum Function is greater than or equal to 1\n"
]
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"step_function(s_prob1 )"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tG-9Hy7jpnqF"
},
"source": [
" ## If the is weights = [- 0.7, 0.1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Pk9XTxxrpnqF"
},
"outputs": [],
"source": [
"weights = [-0.7, 0.1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ja3bsYbKpnqF",
"outputId": "b2260f69-c87f-4892-f57d-0fe00d66ba48"
},
"outputs": [
{
"data": {
"text/plain": [
"-29.0"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# for weights = [- 0.7, 0.1]\n",
"\n",
"s_prob2 = sum_func(inputs, weights)\n",
"\n",
"round(s_prob2, 2) #round to 2 decimal places"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fn4yC28GpnqF"
},
"source": [
"### Result"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "soh6k8NqpnqF",
"outputId": "303f1ccf-4b74-4323-98f1-83635f70c4aa"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Sum Function is NOT greater\n"
]
},
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"step_function(s_prob2 )"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7ZMh9iYJpnqF"
},
"source": [
"### By changing the input values and weights observe different results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "oBM9zqAzpnqF"
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment