Browse Source

adding jupyter instructions and homework

main
parent
commit
07fa8b7eb6
2 changed files with 350 additions and 13 deletions
  1. +142
    -13
      Readme.md
  2. +208
    -0
      main.ipynb

+ 142
- 13
Readme.md View File

@ -2,23 +2,26 @@
Python is a dynamic, interpreted (bytecode-compiled) language. There are no type declarations of variables, parameters, functions, or methods in source code. This makes the code short and flexible, and you lose the compile-time type checking of the source code. Python tracks the types of all values at runtime and flags code that does not make sense as it runs[^1].
Actually, Python is widely used by engineers, scientists, statistics and data-science. Why should I learn Python?
Actually, Python is widely used by engineers, scientists, statistics and data-science. Why should I learn Python?
- Well, is a free and open source,
- Well, is a free and open source,
- is versatile (multiparadign)
- is portable and usable in other platforms
- There are several modules to work with
# Installing python
# Installing python
**Note**: this guide only focus on GNU/Linux Ubuntu and Unix based (MacOS) operate systems (OS).
## Ubuntu
To install the latest Python version, above 3.8, use the next commands in the terminal to let the *APT* package manager to install it:
## Ubuntu
To install the latest Python version, above 3.8, use the next commands in the terminal to let the _APT_ package manager to install it:
```
sudo apt update
sudo apt install python3
```
we are going to use the Integrated Development Environment (IDLE), thus, please run the next commands to install it:
```
@ -27,22 +30,26 @@ sudo apt-get install idle3
```
## Unix-based
In the MacOS system there is recommended package manager call *Homebrew*, to install it run the next command in the terminal:
In the MacOS system there is recommended package manager call _Homebrew_, to install it run the next command in the terminal:
```
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
then, you will have access to the `brew` command to install and manage your packages. Next, you can install Python by using:
```
brew install python3 python-tk
```
these both packages allow us to run the IDLE for Python.
# Testing Python
## Interactive
```
$ python ## Run the Python interpreter
Python 3.X.X (XXX, XXX XX XXXX, 03:41:42) [XXX] on XXX
Type "help", "copyright", "credits" or "license" for more information.
@ -58,13 +65,13 @@ Type "help", "copyright", "credits" or "license" for more information.
2
>>> a + len(a) ## try something that doesn't work
Traceback (most recent call last):
File "", line 1, in
File "", line 1, in
TypeError: can only concatenate str (not "int") to str
>>> a + str(len(a)) ## probably what you really wanted
'hi2'
>>> foo ## try something else that doesn't work
Traceback (most recent call last):
File "", line 1, in
File "", line 1, in
NameError: name 'foo' is not defined
>>> ^D ## type CTRL-d to exit (CTRL-z in Windows/DOS terminal)
```
@ -92,8 +99,8 @@ if __name__ == '__main__':
main()
```
to run the code use
```
$ python hello.py Guido
Hello there Guido
@ -101,7 +108,70 @@ $ ./hello.py Alice ## without needing 'python' first (Unix)
Hello there Alice
```
# `if`, `for` and plot
# Jupyter Notebook
## Basic commands
Jupyter Notebook is widely used for data analysis. The notebooks documents are documents produced by the Jupyter Notebook App, which can contain both code (e.g. Python) and rich text elements (paragraphs, equations, links, etc.).
The Jupyter Notebook App is a client-server application that allows editing and running notebook documents by a browser.
### Shortcuts in both modes:
- Shift + Enter run the current cell, select below
- Ctrl + Enter run selected cells
- Alt + Enter run the current cell, insert below
- Ctrl + S save and checkpoint
### While in command mode (press Esc to activate):
**Enter take you into edit mode** and then the command that you need like change the cell type to Markdown:
- H show all shortcuts
- Up select cell above
- Down select cell below
- Shift + Up extend selected cells above
- Shift + Down extend selected cells below
- A insert cell above
- B insert cell below
- X cut selected cells
- C copy selected cells
- V paste cells below
- Shift + V paste cells above
- D, D (press the key twice) delete selected cells
- Z undo cell deletion
- S Save and Checkpoint
- Y change the cell type to Code
- M change the cell type to Markdown
- P open the command palette
This dialog helps you run any command by name. It’s really useful if you don’t know some shortcut or when you don’t have a shortcut for the wanted command.
- Shift + Space scroll notebook up
- Space scroll notebook down
### While in edit mode (press Enter to activate)
- Esc take you into command mode
- Tab code completion or indent
- Shift + Tab tooltip
- Ctrl + ] indent
- Ctrl + [ deindent //]
- Ctrl + A select all
- Ctrl + Z undo
- Ctrl + Shift + Z or Ctrl + Y redo
- Ctrl + Home go to cell start
- Ctrl + End go to cell end
- Ctrl + Left go one word left
- Ctrl + Right go one word right
- Ctrl + Shift + P open the command palette
- Down move cursor down
- Up move cursor up
# Basics of Python programming language
## `if`, `for` and plot
```
x = int(input("Please enter an integer: "))
Please enter an integer: 42
@ -117,7 +187,9 @@ Please enter an integer: 42
...
More
```
## for statement
```
words = ['cat', 'window', 'defenestrate']
for w in words:
@ -136,7 +208,8 @@ for i in range(len(a)):
print(i, a[i])
```
# Numpy arrays
## Numpy arrays
Numpy, on the other hand, is a core Python library for scientific computation (hence the name “Numeric Python” or Numpy). The library provides methods and functions to create and work with multi-dimensional objects called arrays. Arrays are grids of values, and unlike Python lists, they are of the same data type:
```
@ -149,8 +222,12 @@ array([[1, 2],
[5, 6]])
```
```
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
```
# Matplotlib
## Matplotlib
Matplotlib is a 2d plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments. Matplotlib can be used in Python scripts, Python and IPython shell, Jupyter Notebook, web application servers and GUI toolkits.
@ -171,6 +248,58 @@ import matplotlib.pyplot as plt
>>> plt.show()
```
```
# how to crate functions
def myFuncion(x):
y = np.sin(x)/np.cos(x)
return y
```
```
import matplotlib.pyplot as plt
time = np.linspace(0,10)
y = myFuncion(time)
y2 = np.tan(time)
plt.plot(time, y/2,'-g*')
plt.plot(time, y2, ':m+')
plt.show()
```
## Subplots
A subplot for one column and three rows
```
plt.subplot(3,1,1)
plt.plot(time, np.sin(time),'r', label='$y_1$')
plt.legend()
plt.subplot(3,1,2)
plt.plot(time, np.cos(time),'g',label='y2')
plt.legend()
plt.subplot(3,1,3)
plt.plot(time, np.tan(time),'k', label='y3')
plt.legend()
plt.show()
```
A plot for three columns and two rows
```
plt.subplot(6,3,1)
plt.plot(time, np.sin(time),'r', label='y_1')
plt.subplot(6,3,2)
plt.plot(time, np.cos(time),'g',label='y2')
plt.subplot(6,3,3)
plt.plot(time, np.tan(time),'k', label='y3')
plt.subplot(6,3,4)
plt.plot(time, np.sin(time),'r', label='$y_1$')
plt.subplot(6,3,5)
plt.plot(time, np.cos(time),'g',label='y2')
plt.subplot(6,3,6)
plt.plot(time, np.tan(time),'k', label='$y_1$')
plt.show()
```
# References
[^1]: https://developers.google.com/edu/python/introduction

+ 208
- 0
main.ipynb View File

@ -0,0 +1,208 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Importing data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal-length</th>\n",
" <th>sepal-width</th>\n",
" <th>petal-length</th>\n",
" <th>petal-width</th>\n",
" <th>class</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>3.5</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>3.0</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>3.2</td>\n",
" <td>1.3</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>3.6</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>5.4</td>\n",
" <td>3.9</td>\n",
" <td>1.7</td>\n",
" <td>0.4</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>4.6</td>\n",
" <td>3.4</td>\n",
" <td>1.4</td>\n",
" <td>0.3</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>5.0</td>\n",
" <td>3.4</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>4.4</td>\n",
" <td>2.9</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>4.9</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.1</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal-length sepal-width petal-length petal-width class\n",
"0 5.1 3.5 1.4 0.2 Iris-setosa\n",
"1 4.9 3.0 1.4 0.2 Iris-setosa\n",
"2 4.7 3.2 1.3 0.2 Iris-setosa\n",
"3 4.6 3.1 1.5 0.2 Iris-setosa\n",
"4 5.0 3.6 1.4 0.2 Iris-setosa\n",
"5 5.4 3.9 1.7 0.4 Iris-setosa\n",
"6 4.6 3.4 1.4 0.3 Iris-setosa\n",
"7 5.0 3.4 1.5 0.2 Iris-setosa\n",
"8 4.4 2.9 1.4 0.2 Iris-setosa\n",
"9 4.9 3.1 1.5 0.1 Iris-setosa"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pandas import read_csv\n",
"url = \"https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv\"\n",
"names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']\n",
"dataset = read_csv(url, names=names)\n",
"dataset.head(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**This is the part of code you have to solve and deliver as a part of your report.**\n",
"\n",
"# Statistical Summary\n",
"For this section you can create your own functions or also you can writte down the code as chunks to compute:\n",
"- Mean\n",
"- Number of elements per properti\n",
"- Standard deviation "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plots and data visualization\n",
"Make the next plots for data exploration and visulization:\n",
"- Box and Whiskers plot\n",
"- histogram plot\n",
"- scatter plot for sepal length and width\n",
"- scatter plot for petal length and width"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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.11.4"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

Loading…
Cancel
Save