Commit 5b4930c2 authored by christian.foerster's avatar christian.foerster

intital tutorial files

parent acd227c6
This diff is collapsed.
This diff is collapsed.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Tutorial Part 2 - Advanced Topics\n",
"\n",
"## Object Oriented Programming (OOP)\n",
"\n",
"OOP is a programming paradigm based on the concept of \"objects\", which can contain data, in the form of attributes and code, in the form of methods.\n",
"It's a different way of you thinking about your model/problem. Instead of thinking of a problem as a sequence of commands that need to be executed (procedural, **linear**), you identify single processes/actors and define their attributes and functionalities, so that they can interact with other actors (**nonlinear**).\n",
"\n",
"**Example Problem:\n",
"Create a plotting library**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Structering your code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Performance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Large Projects\n",
"Pycharm, Git, Debugging, Naming conventions, goot practice"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
This diff is collapsed.
# PythonTutorial
The tutorial consists of multiple parts.
- Python Basics
- Python Advanced
- Pandas and Plotting
\ No newline at end of file
- Pandas and Plotting
## Requierements
You must have python3 installed on your computer. Additionally we'll be using a few packages in the tutorial what we still need to install.
### Installation Python
***
Go to the [official python website](https://www.python.org) click on downloads and select your operating system.
Then go ahead and download the latest stable release. Windows users most likely need the 64 bit version, alternatively you can check your system version in the system information.
Run the downloaded file and make sure you select the add to Path option!
In case you need further assistance please check out this [website](https://realpython.com/installing-python/#step-2-run-the-installer).
**Check the Installation**
After you finished installing python open a console and type `pip`. This commands output should look something like this:
```bash
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR,
and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
--trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the certificate in PEM
format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for download.
Implied with --no-index.
```
### Install Python Packages
***
We'll be using:
- jupyter notebook -> lightweight IDE
- pandas -> a library for working with data
- numpy -> library for efficient matrix, array,... manipulation
- matplotlib -> standard plotting library
- plotly -> dynamic plots
- cufflinks -> pandas adapter to plotly
- seaborn -> fancy plots out of the box
- sklearn -> machine learning library
To install the packages open a console and run the following command.
(Linux users should use pip3 instead of pip since they do have Python2 and Python3 installed by default.)
```bash
pip install pandas numpy matplotlib plotly cufflinks seaborn sklearn jupyter notebook
```
They're many dependencies involved, so the installation might take a minute.
## Starting Jupyter Notebook
I choose *jupyter notebook* (*JN*) as the appropriate IDE for this tutorial, as it is light weight, so it's easy to understand and does not need much resources. Additionally *JN* is great for plotting!
One way to start *JN* is:
- to open a console and navigate to your working directory (using the cd command)
- now you simply type jupyter notebook
A browser tab will open and this is your IDE. If you don't know what to do from this point on forward check out this comprehensive [tutorial](https://realpython.com/jupyter-notebook-introduction/).
### The rest we'll figure out together in the tutorial!
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solutions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read in some of your data with pandas, plot it and get the main statistics on the data.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"%matplotlib inline\n",
"import cufflinks as cf\n",
"cf.go_offline()\n",
"\n",
"fp=r\"M:\\Projekte\\2013_UWRM\\3_messen und daten\\Messstellen\\Pegel Kreischa\\Q_Kreischa.txt\"\n",
"my_df=pd.read_csv(fp,parse_dates=True,index_col=0,sep=\",\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_df.iplot(secondary_y=\"q\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(my_df.describe())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a class, that takes a filepath as an argument when it is initialized. Create a method, that takes a string as an argument and returns the number of occurances of that string in that file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"\n",
"class StrCount():\n",
" def __init__(self,filepath):\n",
" self.filepath=filepath\n",
" return\n",
" \n",
" def count(self,string):\n",
" count=0\n",
" with open(self.filepath) as file:\n",
" for line in file:\n",
" count+=len(line.split(string))-1\n",
" return count"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig,ax=plt.subplots(2,2)\n",
"fig.text(0,0.5,r\"\\sum$\",rotation=90, verticalalignment='center')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filepath=r\"C:\\Users\\Gabi\\Documents\\Python\\NN\\Inputdata\\PCM_MEZ_good_shape.txt\"\n",
"count=StrCount(filepath).count(\";\")\n",
"print(count)\n",
"\n",
"# if this takes too long it might be the server connection... (depending on the file size)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a function that takes an arbitrary number of inputs and prints them."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#this function only takes list like entries\n",
"def arbitrary_list(*arguments):\n",
" print(arguments)\n",
" \n",
"arbitrary_list(2,3,4,5,4,3,2,4,[432],[{2:3}])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#this function only takes list like entries\n",
"def arbitrary_dict(**dictarguments):\n",
" print(dictarguments)\n",
" \n",
"arbitrary_dict(a=3,b=4,wer=88)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#this function can do all list arguments must come before dict arguments\n",
"def arbitrary(*list_args,**dict_args):\n",
" print(list_args)\n",
" print(dict_args)\n",
" \n",
"arbitrary(2,3,4,5,6,a=3,b=33,d=\"dd\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a function, that creates the fibonacci sequence up to the n'th term."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def fib(n):\n",
" fibo=[1,1]\n",
" if n==1:\n",
" return fibo[0]\n",
" elif n==2:\n",
" return fibo\n",
" else:\n",
" for run in range(n-2):\n",
" fibo.append(fibo[-1]+fibo[-2])\n",
" return fibo\n",
"\n",
"fib(16)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make python list of all files in the current directory and sort them by their file type."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"# list all contest\n",
"foldercontent=os.listdir()\n",
"\n",
"#filter files\n",
"files=[file for file in foldercontent if os.path.isfile(file)]\n",
"\n",
"#sort key makes sure i sort by the file ending\n",
"def sortkey(x):\n",
" return x.split(\".\")[1]\n",
"# sorting\n",
"files.sort(key=sortkey)\n",
"\n",
"print(files)"
]
}
],
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tasks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read in some of your data with pandas, plot it and get the main statistics on the data.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a class, that takes a filepath as an argument when it is initialized. Create a method, that takes a string as an argument and returns the number of occurances of that string in that file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a function that takes an arbitrary number of inputs and prints them."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a function, that creates the fibonacci sequence up to the n'th term."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make python list all files in the current directory."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment