Skip to content
Snippets Groups Projects
Commit 2b7cdafc authored by Antoine Lucas's avatar Antoine Lucas ⛷️
Browse files

Replace EDS_2021_2022_Python_Positionning_v1_1_no_sol.ipynb

parent 4f4cfd20
No related branches found
No related tags found
No related merge requests found
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "EDS_2020_2021_Python_Positionning_v1.1_no_sol.ipynb",
"provenance": [],
"collapsed_sections": [
"8KYU1qRgupg8",
"GIsNHvOEIMJE"
],
"toc_visible": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "8KYU1qRgupg8"
},
"source": [
"# Python: positionning test or refreshing exercises\n",
"---\n",
"What are we talking about: \n",
"\n",
"This notebook should allow you to evaluate your skills and your knowledges in Python and in particular your mastery of the **Numpy** and **Matplolib** libraries which will be the basis of this [Earth]-Data Science course. \n",
"\n",
"The following exercices are supposed to be fully understood to really take advantages of this data science lectures. If it's not the case, no problem, just go through this notebook. \n",
"\n",
"It is impossible to cover all the functionnalities of each library. We just give very few examples. \n",
"If you need help, besides the traditionnal \"call a friend\" \"50/50\" and \"The public\", one can use: \n",
"\n",
"1. Official documentation of the library\n",
"2. Online help: ie -> print?, \n",
"3. Stack overflow\n",
"4. ...\n",
"\n",
"Solutions will be given later... Training first\n",
"\n",
"---\n",
"Table of content : \n",
"\n",
"1. Few elements of Lists and Tuples\n",
"2. A bit of numpy\n",
"3. Matplotlib in a nutshell\n",
"4. Next step\n",
"\n",
"\n",
"VERSIONS : \n",
"- v1.0 : 10/10/2020 - Initial release\n",
"- v1.1 : 11/10/2020 - Add matrices exercices\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "zLleOGdCXCva",
"outputId": "51e97647-661f-415c-88a7-d6b2cc12bda3",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 82
}
},
"source": [
"%%html\n",
"<h2><span style=\"color:red\"> WARNING</span></h2>\n",
"If you'are using google Colab, please make a copy of this notebook in your Own drive. "
],
"execution_count": null,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"<h2><span style=\"color:red\"> WARNING</span></h2>\n",
"If you'are using google Colab, please make a copy of this notebook in your Own drive. "
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jNTSWHAWHkb6"
},
"source": [
"# 1. Few elements of Lists and Tuples"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ki2wP0PVxdzS"
},
"source": [
"## 1.1 Exercice 1: basic operations on lists"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "f_85TwUlu8sd"
},
"source": [
" \n",
"1. Create, in a new cell, a list containing the integers from 4 to 6. Store this list in a variable called ** list1 ** then display the content of ** list1 **.\n",
"\n",
"2. Create, in a new cell, a list containing the first 3 letters of the alphabet. Store this list in a variable called ** list2 ** then display the contents of ** list2 **.\n",
"\n",
"3. Display the second element of ** list2 **. Replace this second element with the letter \"d\", without redefining the entire list, then redisplay the second element of ** list2 **\n",
"\n",
"4. Add the two lists **list1** and **list2**, what do you see?\n",
"\n",
"5. Multiply **list2** by 3.\n",
"\n",
"6. Try to multiply **list2** by **list2** -> What is happening ?\n",
"\n",
"7. Print the type of list2 (off course it's a list...)\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "NvCkygPR0juO"
},
"source": [
"# Insert your code here, then test it \n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "JDNEqGcs0XnH"
},
"source": [
"## 1.2 Exercice 2: find data in lists\n",
"\n",
"1. Create the following list [\"CH4\", \"H2O\", \"NH3\", \"O3\", \"C02\", \"CH3COOH\"] and store is in the variable *molec*.\n",
"\n",
"2. Find the index of the molecule \"H2O\" in the list molec.\n",
"\n",
"3. Now, find all indexes of \"H2O\" in the following list : [\"CH4\", \"H2O\", \"NH3\", \"O3\", \"H2O\", \"CH3COOH\", \"H20\"]\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "wum8HCLYz6vp"
},
"source": [
"# Insert your code here, then test it\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "1DhpahCH0KRR"
},
"source": [
"## 1.3 Exercice 3 add list, tuple, both... \n",
"1. Créez, dans une nouvelle cellule, un tuple **tuple1** contenant les trois premières lettres de l'alphabet, puis affichez le contenu de **tuple1**. Remplacez le deuxième élément de **tuple1** par la lettre \"d\" \n",
"\n",
"2. Additionnez une liste et un tuple, que constatez vous ?"
]
},
{
"cell_type": "code",
"metadata": {
"id": "bgQpOoS56BSY"
},
"source": [
"# Insert your code here, then test it "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "pWBRRF9BKA4P"
},
"source": [
"# 2. A bit of numpy \n",
"\n",
"Numpy can be considered as the mother of (most of) all scientific libraries in Python (like for example Scipy, Pandas, Obspy, PyWavelets, Astropy...)\n",
"\n",
"So useless to say that there is no way to avoid Numpy. \n",
"\n",
"Here, we will see just few functions to be able to work with the data science notebooks. One more time, impossible to make a full tutorial. \n",
"\n",
"Documentation of Numpy: https://numpy.org/doc/stable/index.html\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qZXbxBJtMoED"
},
"source": [
"## 2.1 Exercice 1: quick start\n",
"\n",
"1. Create a vector **vec1** from 0 to 14\n",
"2. Create a vector **vec2** from 0 to 100 \n",
"3. Create a vector **vec3** from $-\\pi$ to$+\\pi$ with 30 values\n",
"4. Create a vector **vec4** of size 10 with 0 for all the values\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "qjSgUmSFRFaU"
},
"source": [
"# Insert your code here, then test it "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "jovJKkLUYfrU"
},
"source": [
"## 2.2 Exercice 2: Basic operations \n",
"\n",
"1. Using the numpy functions, calculate: \n",
"- the square root of 10.\n",
"- *tan*, *sin* and *cos* de $\\pi$.\n",
"- logarithm and exponent of a given number.\n",
"- logarithm (base 10) of the number 100. \n",
"\n",
"2. Create a vector with a normal distribution with $\\mu = 0$ and $\\sigma =0.1$ (one can use ``np.random.normal``)\n",
"\n",
"3. Using numpy functions, calculate, the *mean*, *standard deviation*, *min*, *max*\n",
"\n",
"4. Create two five-elements vectors (vecA and vecB) with integers\n",
"\n",
"5. Calculate vecA*vecB then calculate the dot product (''produit scalaire'' in french)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "MR_jIEcFsF0S"
},
"source": [
"# Insert your code here, then test it "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "SFPe063IkbJd"
},
"source": [
"## 2.3 Exercice 3 Stacking & Reshaping\n",
"\n",
"\n",
"1. Reshape the vector **vec3** (previous ex.) to a 10x3 matrix **mat3** (``np.reshape``)\n",
"\n",
"2. Create **vecStack_h** which is a stack of the **vecA** and **vecB** to create a unique vector with the size equal to the size of vecA and vecB (``np.hstack(...)``)\n",
"\n",
"3. Create **vecStack_v** which is the stack of **vecA** and **vecB** to create a matrix. (``np.vstack(...)``)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Yzl8uElgngai"
},
"source": [
"# Insert your code here, then test it "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "HMglaWWLuWmS"
},
"source": [
"## 2.4 Exercice 4: read a CVS file with numpy\n",
"\n",
"Important step here, since it's very important to know how to open CSV files in case you need input data.\n",
"\n",
"``genfromtxt`` - Load data from a text file, with missing values handled as specified. Each line past the first skip_header lines is split at the delimiter character, and characters following the comments character are discarded.\n",
"\n",
"``loadtxt`` - Load data from a text file. Each row in the text file must have the same number of values.\n",
"\n",
"1. Load the file data_E.dat (PRECISER LA POSITION DE LA SOURCE)\n",
"\n",
"2. Separate in 3 distincts vectors the three columns \n",
"\n",
"3. Explore each vectors (type, min, max, mean, standard deviation, Nan values...) -> Think about creating a function\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ydnRpQDp0EC_"
},
"source": [
"# Insert your code here, then test it "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "5vyNhRRtFzIo"
},
"source": [
"## 2.5 Exercice 5 : Matrices\n",
"\n",
"1. Create a 4 x 4 identity matrix\n",
"2. Create a 10 x 10 all-of-ones matrix\n",
"3. Create a 6 x 5 zeros matrix \n",
"4. Create a 6 x 6 random matrix of float\n",
"5. Create a 6 x 1 random of integer vector.\n",
"6. Find the transpose of matrix (ex. 4.)\n",
"7. Multiply the matrix (4.) by the vector (5.)\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ynCq9wMQGyLd"
},
"source": [
"# Insert your code here, then test it "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "mgawXSyguDov"
},
"source": [
"# 3. Matplotlib in a Nutshell\n",
"\n",
"As a picture says a thousand words, it's now time to revise our knowledge with the famous ``matplotlib`` library.\n",
"\n",
"Don't forget to import matplotlib first : \n",
"\n",
"``import matplotlib.pyplot as plt``"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GIsNHvOEIMJE"
},
"source": [
"## 3.1 Exercice 1: warmup\n",
"\n",
"1. Create a vector from 0 to $2\\pi$ with 200 points\n",
"2. Overplot the function *sin* and *cos* on the same plot.\n",
"3. Add the title 'Sine and Cosine functions' then add the axis labels, the legend\n",
"4. Add the grid\n",
"5. Sine function is plotted in red with a dotted linestyle and Cosine is plotted in green with a dash-dot linestyle.\n",
"\n",
"6. Plot sur same functions in two vertical subplots. \n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "vDcokoO3H9ol"
},
"source": [
"# Insert your code here, then test it "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "O2nzVE-HPAg4"
},
"source": [
"## 3.2 Exercice 2: plot data and the fit\n",
"\n",
"1. Create a vector **x** from 0 to 50 with a step 2.\n",
"2. Create a random vector y which is a linear to x.\n",
"3. Make a scatter plot of y=f(x).\n",
"\n",
"4. Plot the fit of the function in red (``np.polyfit`` and ``np.poly1d``)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "_10ba2vsJSBG"
},
"source": [
"# Insert your code here, then test it "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "oll4r_RO0Qt-"
},
"source": [
"## 3.3 One last example: plot a map\n",
"\n",
"We often need to represent data on map \n",
"\n",
"Here is a simple exercice to plot a map and to put the cities of Paris and London.\n",
"\n",
"You will need fisrt to ensure that the useful libraries are install.\n",
"The following lines of codes install then if needed. So execute the following line of code.\n",
"\n",
"1. Plot the map of the earth between (-10, 40) and (10, 55) with a Mercator projection.\n",
"2. Add the parallels and the meridians on the map\n",
"3. Add the the cost lines\n",
"4. Add a point on the cities of Paris and London.\n",
"5. Add a line between Paris and London. \n",
"\n",
"\n",
"Documentation : https://basemaptutorial.readthedocs.io/en/latest/basemap.html\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RVvA7dvKFUZc"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6stGjd-9BsRO"
},
"source": [
"#### Install prerequisitory for Google Colab\n",
"Execute the following cell to install missing basemap lib if you are executing in Google Colab."
]
},
{
"cell_type": "code",
"metadata": {
"id": "64FwlJpuy_im"
},
"source": [
"!apt-get install libgeos-3.5.0\n",
"!apt-get install libgeos-dev\n",
"!pip install https://github.com/matplotlib/basemap/archive/master.zip"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "EQh2SFroFOvm"
},
"source": [
"# Insert your code here, then test it "
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "QwJt2E5HCIP3"
},
"source": [
"# Next step"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dfTMt6igCMDQ"
},
"source": [
"As already mentioned, this note cover the very basic knowledges necessary to apprehend the Data Science, the machine learning.\n",
"\n",
"- A very useful extension of numpy is the library `PANDAS`. Google Colab offer a good tutorial to learn Pandas : \n",
"\n",
"- And of course, the famous `SCIKIT-LEARN` library a very powerful toolkit to analyze data in Python : https://scikit-learn.org/stable/index.html\n",
"\n",
"\n",
"\n"
]
}
]
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment