summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Kolset <christian.kolset@gmail.com>2025-03-31 17:26:14 -0600
committerChristian Kolset <christian.kolset@gmail.com>2025-03-31 17:27:21 -0600
commit9cea323dee47040c9935119dbc84694c911ac9ac (patch)
treef5658fd15038ef2162d882067bd2b0b99f110b00
parent2abe25f95ff9ac0500f01bf385e991c9a6dae019 (diff)
Added tutorial: Linear regression. And added Notebook: Basics of python
with link in TOC.
-rw-r--r--README.md6
-rw-r--r--tutorials/figures/python_blocks.pngbin0 -> 15681 bytes
-rw-r--r--tutorials/linear_regression.md18
-rw-r--r--tutorials/module_1/basics_of_python.ipynb305
4 files changed, 327 insertions, 2 deletions
diff --git a/README.md b/README.md
index dbdf675..7373b8b 100644
--- a/README.md
+++ b/README.md
@@ -52,7 +52,7 @@ See the tutorials under the /tutorials/ directory.
- [AI assisted programming](tutorials/module_2/2_ai_assisted_programming)
- Verification and Validation
- Roots and Optimization
-- Linear Regression
+
#### Applications of Computational Mathematics in ME
- Systems of Equations and LU Decomposition
@@ -61,9 +61,11 @@ See the tutorials under the /tutorials/ directory.
- Numerical Integration
- Ordinary Differential Equations
#### Data Analysis of Processing
-- [Plotting](tutorials/4_plotting.md)
+
- Importing scientific data
+- [Linear Regression](tutorials/linear_regression.md)
- Data Processing
+- [Plotting](tutorials/4_plotting.md)
- Data Visualization
#### Introduction to AI for Engineering Problems
- Supervised vs unsupervised learning
diff --git a/tutorials/figures/python_blocks.png b/tutorials/figures/python_blocks.png
new file mode 100644
index 0000000..be8d0bc
--- /dev/null
+++ b/tutorials/figures/python_blocks.png
Binary files differ
diff --git a/tutorials/linear_regression.md b/tutorials/linear_regression.md
new file mode 100644
index 0000000..b21415b
--- /dev/null
+++ b/tutorials/linear_regression.md
@@ -0,0 +1,18 @@
+# Linear Regression
+
+
+
+## Statical tools
+Numpy comes with some useful statistical tools that we can use to analyze our data.
+
+### Mean
+The mean is the average of a set of numbers. It is calculated by summing all the numbers and dividing by the count of numbers.
+
+```python
+import numpy as np
+
+mean = np.mean([1, 2, 3, 4, 5])
+median = np.median([1, 2, 3, 4, 5])
+std = np.std([1, 2, 3, 4, 5])
+variance = np.var([1, 2, 3, 4, 5])
+```
diff --git a/tutorials/module_1/basics_of_python.ipynb b/tutorials/module_1/basics_of_python.ipynb
new file mode 100644
index 0000000..2fb996c
--- /dev/null
+++ b/tutorials/module_1/basics_of_python.ipynb
@@ -0,0 +1,305 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "d9cee0e6-c132-44cd-acd4-1208721e2007",
+ "metadata": {},
+ "source": [
+ "# "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "a5c32509-d4c7-4fd5-92cf-1a923b78f443",
+ "metadata": {},
+ "source": [
+ "# Basics of Python\n",
+ "\n",
+ "This page contains important fundamental concepts used in Python such as syntax, operators, order or precedence and more.\n",
+ "\n",
+ "[Syntax](1_05_basics_of_python#Syntax)\n",
+ "[Operators](1_05_basics_of_python#Operators)\n",
+ "[Order of operation](1_05_basics_of_python#Order%20of%20Operation)\n",
+ "[Data types](1_05_basics_of_python#data%20types)\n",
+ "[Variables](1_05_basics_of_python#Variables)\n",
+ "\n",
+ "---"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "01d694d9-2a73-4ba0-be56-dfa9b819a11a",
+ "metadata": {
+ "jp-MarkdownHeadingCollapsed": true
+ },
+ "source": [
+ "## Syntax\n",
+ "### Indentations and blocks\n",
+ "In python *indentations* or the space at the start of each line, signifies a block of code. This becomes important when we start working with function and loops. We will talk more about this in the controls structures tutorial. \n",
+ "### Comments\n",
+ "Comments can be added to your code using the hash operator (#). Any text behind the comment operator till the end of the line will be rendered as a comment.\n",
+ "If you have an entire block of text or code that needs to be commented out, the triple quotation marks (\"\"\") can be used. Once used all the code after it will be considered a comment until the comment is ended with the triple quotation marks.\n",
+ "\n",
+ " ---"
+ ]
+ },
+ {
+ "attachments": {},
+ "cell_type": "markdown",
+ "id": "e0c46483-6263-4a7c-b1cc-bc932d3a1393",
+ "metadata": {
+ "jp-MarkdownHeadingCollapsed": true
+ },
+ "source": [
+ "## Operators\n",
+ "In python, operators are special symbols or keywords that perform operations on values or variables. This section covers some of the most common operator that you will see in this course.\n",
+ "### Arithmetic operators\n",
+ "| Operator | Name |\n",
+ "| --- | --- |\n",
+ "| + | Addition |\n",
+ "| - | Subtraction |\n",
+ "| * | Multiplication |\n",
+ "| / | Division |\n",
+ "| % | Modulus |\n",
+ "| ** | Exponentiation |\n",
+ "| // | Floor division |\n",
+ "\n",
+ "### Comparison operators\n",
+ "Used in conditional statements such as `if` statements or `while` loops. Note that in the computer world a double equal sign (`==`) means *is equal to*, where as the single equal sign assigns the variable or defines the variable to be something. \n",
+ "\n",
+ "| Operator | Name |\n",
+ "| --- | --- |\n",
+ "| == | Equal |\n",
+ "| != | Not equal |\n",
+ "| > | Greater than |\n",
+ "| < | Less than |\n",
+ "| >= | Greater than or equal to |\n",
+ "| <= | Less than or equal to |\n",
+ "\n",
+ "### Logical operators\n",
+ "| Operator | Descrription |\n",
+ "| --- | --- |\n",
+ "| and | Returns True if both statemetns are true |\n",
+ "| or | Returns True if one of the statements is true |\n",
+ "| not | Reerse the result, returns False if the result is true |\n",
+ "\n",
+ "### Identity operators\n",
+ "| Operator | Description |\n",
+ "| --- | --- |\n",
+ "| is | Returns True if both variables are the same object |\n",
+ "| is not | Returns True if both variables are not the same object |\n",
+ "\n",
+ "---"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "2f22ddf1-592a-4a43-897c-e3addc57df24",
+ "metadata": {
+ "jp-MarkdownHeadingCollapsed": true
+ },
+ "source": [
+ "## Order of Operation\n",
+ "Similarly to the order or precedence in mathematics, different computer languages have their own set of rules. Here is a comprehensive table of the order of operation that python follows.\n",
+ "\n",
+ "| Operator | Description |\n",
+ "| ------------------------------------------------------- | ----------------------------------------------------- |\n",
+ "| `()` | Parentheses |\n",
+ "| `**` | Exponentiation |\n",
+ "| `+x` `-x` `~x` | Unary plus, unary minus, and bitwise NOT |\n",
+ "| `*` `/` `//` `%` | Multiplication, Division, floor division, and modulus |\n",
+ "| `+` `-` | Addition and subtraction |\n",
+ "| `<<` `>>` | Bitwise left and right shifts |\n",
+ "| & | Bitwise AND |\n",
+ "| ^ | Bitwise XOR |\n",
+ "| \\| | Bitwise OR |\n",
+ "| `==` `!=` `>` `>=` `<` `<=` `is` `is not` `in` `not in` | Comparision, identity and membership operators |\n",
+ "| `not` | logical NOT |\n",
+ "| `and` | AND |\n",
+ "| `or` | OR |\n",
+ "\n",
+ "---"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "6c79d7d3-5f06-4206-9f90-46a02f9b3ffb",
+ "metadata": {
+ "jp-MarkdownHeadingCollapsed": true
+ },
+ "source": [
+ "## Data types\n",
+ "Data types are different ways a computer stores data. Other data types use fewer bits than others allowing you to better utilize your computer memory. This is important for engineers because\n",
+ "The most common data types that an engineer encounters in python are numeric types.\n",
+ "- `int` - integer\n",
+ "- `float` - a decimal number\n",
+ "- `complex` - imaginary number\n",
+ "\n",
+ "The comprehensive table below show all built-in data types available in python.\n",
+ "\n",
+ "| Category | Data Type |\n",
+ "| -------- | ---------------------------- |\n",
+ "| Text | int, float, complex |\n",
+ "| Sequance | list, tuple, range |\n",
+ "| Mapping | dict |\n",
+ "| Set | set, frozenset |\n",
+ "| Boolean | bytes, bytearray, memoryview |\n",
+ "| Binary | bytes, bytearray, memoryview |\n",
+ "| None | NoneType |\n",
+ "\n",
+ "---"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "6edf45e8-504c-415e-9b74-852332167097",
+ "metadata": {},
+ "source": [
+ "## Variables\n",
+ "\n",
+ "A **variable** in Python is a name that stores a value, allowing you to use and manipulate data efficiently.\n",
+ "\n",
+ "#### Declaring and Assigning Variables\n",
+ "\n",
+ "It is common in low-level computer languages to declare the datatype if the variable. In python, the datatype is set whilst you assign it. We assign values to variables using a single `=`. \n",
+ "\n",
+ "```python"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "id": "a9bc3bda-e55d-4f5a-813d-8f6316f12c64",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "x = 33 # Integer\n",
+ "y = 3.14 # Float\n",
+ "name = \"Joe\" # String\n",
+ "is_valid = True # Boolean"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "id": "f6af843b-159f-4d10-9c84-abebde7e7bbc",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "1098.42\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(x**2+3*y)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "bcc9a481-2421-4df7-82a8-ee1e76c7f4a1",
+ "metadata": {},
+ "source": [
+ "Change the x and y values above, re-run the notebook and see what happens.\n",
+ "\n",
+ "You can assign multiple variables at once:\n",
+ "\n",
+ "```python\n",
+ "a, b, c = 1, 2, 3\n",
+ "```\n",
+ "\n",
+ "Similarly we can assign the same value to multiple variables:\n",
+ "\n",
+ "```python\n",
+ "x = y = z = 100\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "691c0a0a-6143-4ba5-b92a-769979b7d695",
+ "metadata": {},
+ "source": [
+ "##### Rules\n",
+ "\n",
+ "- Must start with a letter or `_`\n",
+ "- Cannot start with a number\n",
+ "- Can only contain letters, numbers, and `_`\n",
+ "- Case-sensitive (`Name` and `name` are different)\n",
+ "\n",
+ "#### Updating Variables\n",
+ "\n",
+ "You can change a variable’s value at any time.\n",
+ "\n",
+ "```python\n",
+ "x = 5\n",
+ "x = x + 10 # Now x is 15\n",
+ "```\n",
+ "\n",
+ "Or shorthand:\n",
+ "\n",
+ "```python\n",
+ "x += 10 # Same as x = x + 10\n",
+ "```\n",
+ "\n",
+ "#### Variable Types & Type Checking\n",
+ "\n",
+ "Use `type()` to check a variable’s type."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "id": "a7e71be9-dd9a-42c9-84e2-cb5f8f8939b1",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ " Variable x is type: <class 'int'>\n",
+ " Variable y is type: <class 'str'>\n"
+ ]
+ }
+ ],
+ "source": [
+ "x = 10\n",
+ "print(f' Variable x is type: {type(x)}')\n",
+ "\n",
+ "y = \"Hello\"\n",
+ "print(f' Variable y is type: {type(y)}')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d26ad8d0-a96e-4436-aedd-b07ddd7ae696",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "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.13.2"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}