Notes: 677

How to get visitor ip address for a django project ?

Example of how to get visitor ip address for a django project: [TOC] ### Get visitor ip address def visitor_ip_address(request): x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') return ip ### Check if ip address is valid To check if the ip address is valid, a solution is to use the [socket](h

How to reverse columns order in a table with python ?

Example of how to reverse columns order in a table with python: let's consider the following table: >>> import numpy as np >>> table = np.array([[0., 1., 2., 3., 4., 5., 6., 7.], ... [6., 6., 4., 9., 7., 5., 4., 8.], ... [4., 9., 3., 7., 5., 2., 3., 1.], ... [6., 9., 8., 1., 4., 3., 2., 1.], ... [4., 8., 7., 6., 1., 7., 1., 1.]]) \begin{equation} \begin{array}{cccccc} 0. & 1. & 2. & 3. & 4. & 5. & 6. & 7.\\\\ 6. & 6. & 4. & 9. & 7. &

Comment inverser l'ordre des colonnes d'un tableau en python avec numpy ?

Exemples de comment inverser l'ordre des colonnes d'un tableau en python avec numpy. Soit un tableau quelconque: >>> import numpy as np >>> table = np.array([[0., 1., 2., 3., 4., 5., 6., 7.], ... [6., 6., 4., 9., 7., 5., 4., 8.], ... [4., 9., 3., 7., 5., 2., 3., 1.], ... [6., 9., 8., 1., 4., 3., 2., 1.], ... [4., 8., 7., 6., 1., 7., 1., 1.]]) \begin{equation} \begin{array}{cccccc} 0. & 1. & 2. & 3. & 4. & 5. & 6. & 7.\\\\ 6. & 6. & 4. &

How to reverse rows order in a table with python ?

Examples of how to reverse rows order in a table with python. Let's consider a random table: >>> import numpy as np >>> table = np.array([[0., 4., 4., 2., 7., 0.], ... [1., 2., 7., 2., 3., 4.], ... [2., 2., 3., 3., 6., 3.], ... [3., 9., 0., 6., 3., 3.], ... [4., 3., 1., 8., 2., 5.], ... [5., 9., 3., 7., 6., 8.], ... [6., 9., 5., 8., 1., 8.], ... [7., 9., 6., 7., 3., 7.], ... [8., 2., 1., 4., 4., 8.], ... [9.,

Comment inverser l'ordre des lignes d'un tableau en python avec numpy ?

Exemples de comment inverser l'ordre des lignes d'un tableau en python avec numpy. Soit un tableau quelconque: >>> import numpy as np >>> table = np.array([[0., 4., 4., 2., 7., 0.], ... [1., 2., 7., 2., 3., 4.], ... [2., 2., 3., 3., 6., 3.], ... [3., 9., 0., 6., 3., 3.], ... [4., 3., 1., 8., 2., 5.], ... [5., 9., 3., 7., 6., 8.], ... [6., 9., 5., 8., 1., 8.], ... [7., 9., 6., 7., 3., 7.], ... [8., 2., 1., 4., 4., 8.],

How to find the minimum and maximum values in an array using numpy and python ?

Examples of how to find the minimum and maximum values in an array using numpy and python: [TOC] ### Approach 1: To find the minimum and maximum values in an array, a solution is to use the numpy functions max() and min(): >>> import numpy as np >>> A = np.array(([2,7,-1],[1,9,8],[-1,5,6])) >>> A array([[ 2, 7, -1], [ 1, 9, 8], [-1, 5, 6]]) >>> np.min(A) -1 >>> np.max(A) 9 ### Approach 2: Another solutio

How to rotate an image using python ?

Examples of how to rotate an image using python: [TOC] ### Rotate an image and keep the same size as the original image To rotate an image, a solution is to use the pillow function [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example [images:eiffel-tower;eiffel-tower-rotate-01 dim:1*2 size:75 caption:How to rotate an image using python ?] from PIL import Image im = Image.open("eiffel_tower.jpg") print(im.size) im = im.rotate(45) print(i

Comment tourner une image en python avec le module pillow ?

Exemples de comment tourner une image en python avec le module pillow [TOC] ### Tourner une image en gardant la même taille Pour tourner une image, il existe la fonction pillow [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), exemple [images:eiffel-tower;eiffel-tower-rotate-01 dim:1*2 size:75 caption:Comment tourner une image en python avec le module pillow ?] from PIL import Image im = Image.open("eiffel_tower.jpg") print(im.size) im = im.rota

Comment retourner une image horizontalement en python ?

Exemples de comment retourner une image horizontalement en python [TOC] ### Obtenir une image retournée horizontalement avec pillow Pour retourner une image horizontalement en python, il existe la fonction pillow [mirror()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image from PIL import ImageOps im = Image.open("lena.png") im = ImageOps.mirror(im) im.save("lena_mirror.png") im.show() [images:458;lena-mirror dim:1*2 size

How to flip an image horizontally in python ?

Examples of how to flip an image horizontally in python: [TOC] ### Get a vertically flipped image using pillow To flip an image horizontally a solution is to use the function [mirror()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image from PIL import ImageOps im = Image.open("lena.png") im = ImageOps.mirror(im) im.save("lena_mirror.png") im.show() [images:458;lena-mirror dim:1*2 size:75 caption:How to flip an image horiz

How to flip an image vertically in python ?

Examples of how to flip an image vertically in python: [TOC] ### Get a vertically flipped image using pillow To flip an image vertically a solution is to use the function [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image im = Image.open("eiffel_tower.jpg") im = im.rotate(180) im.save("eiffel_tower_fliped_vertically.jpg") [images:eiffel-tower;eiffel-tower-fliped-vertically dim:1*2 size:75 caption:How to flip an imag

How to calculate the inverse of a matrix in python using numpy ?

To calculate the inverse of a matrix in python, a solution is to use the linear algebra numpy method [linalg](https://docs.scipy.org/doc/numpy/reference/routines.linalg.html). Example \begin{equation} A = \left( \begin{array}{ccc} 1 & 3 & 3 \\\\ 1 & 4 & 3 \\\\ 1 & 3 & 4 \end{array}\right) \end{equation} inverse matrix A_inv \begin{equation} A^{-1} = \left( \begin{array}{ccc} 7 & -3 & -3 \\\\ -1 & 1 & 0 \\\\ -1 & 0 & 1 \end{array}\right) \end{equation} >>> import

How to invert the elements of a boolean array in python ?

Examples of how to invert the elements of a boolean array in python using the numpy function [invert()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html) >>> import numpy as np >>> a = np.array((True,True,False,True,False)) >>> b = np.invert(a) >>> b array([False, False, True, False, True], dtype=bool) Another example >>> import numpy as np >>> a = np.array((1,1,0,1,0), dtype=bool) >>> b = np.invert(a) >>> b array(

Comment inverser les éléments d'une matrice booléenne avec numpy de python ?

Exemples de comment inverser les éléments d'une matrice booléenne en utilisant la fonction numpy [invert()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html) >>> import numpy as np >>> a = np.array((True,True,False,True,False)) >>> b = np.invert(a) >>> b array([False, False, True, False, True], dtype=bool) Autre exemple: >>> import numpy as np >>> a = np.array((1,1,0,1,0), dtype=bool) >>> b = np.invert(a) >>> b arra

How to overlay / superimpose two images using python and pillow ?

Examples of how to overlay / superimpose two images using python and pillow [TOC] ### Overlay two images of same size To overlay two images in python, a solution is to use the pillow function [paste()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.paste), example: from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), img) ba

Comment superposer deux images avec le module python pillow ?

Exemples de comment superposer deux images avec le module python pillow: [TOC] ### Superposer deux images de même taille Pour superposer deux images de même taille on peut utiliser la fonction pillow [paste()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.paste), exemple: from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), i

How to mask an array using another array in python ?

Examples of how to mask/select elements of an array using another array in python [TOC] ### Mask an array from another array To mask an array, there are several approaches with numpy (see the module called [ma](https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html)). A solution for example is to use [numpy.ma.masked_where](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ma.masked_where.html) to mask the elements of the array x if the elements of the ar

How to create a sitemap for a website based on Django ?

An example of how to create a sitemap for a website based on [Django](https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/) [TOC] Let's assume a Django website with an application called "notebooks" with the following files: database.db manage.py myproject / settings.py urls.py notebooks / views.py models.py urls.py with models.py class Article(models.Model): full_path = models.TextField() date_created = models.DateTimeField(default=datetime

Comment créer un sitemap avec le web-framework Django ?

Exemple de comment créer un sitemap pour un site web basé sur [Django](https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/) (version utilisée 1.11.10): [TOC] On va ici supposer un site avec une application intitulée "notebooks" avec l'organisation suivante: database.db manage.py myproject / settings.py urls.py notebooks / views.py models.py urls.py avec models.py class Article(models.Model): full_path = models.TextField() date_created = mod

Comment masquer les valeurs d'une matrice depuis une autre matrice sous python ?

Exemples de comment sélectionner ou masquer les valeurs d'une matrice d'après les valeurs d'une autre matrice sous python: [TOC] ### Masquer une matrice à partir d'une autre matrice Pour masquer une matrice à partir d'une autre matrice, il existe plusieurs possibilités avec numpy (voir le module intitulé [ma](https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html)). On peut par exemple utiliser [numpy.ma.masked_where](https://docs.scipy.org/doc/numpy-1.13.0/reference/genera

How to save a dictionary in a json file with python ?

Examples of how to save a dictionary in a json (JavaScript Object Notation) file with python [TOC] See also: [pickle — Python object serialization](https://docs.python.org/3/library/pickle.html#module-pickle) and [marshal — Internal Python object serialization](https://docs.python.org/3/library/marshal.html#module-marshal) ### Save a python dictionary in a json file To save a dictionary in python to a json file, a solution is to use the json function [dump()](https://docs.python.org/

Comment sauvegarder un dictionnaire dans un fichier json avec python ?

Exemples de comment sauvegarder un dictionnaire dans un fichier json (JavaScript Object Notation) avec python: [TOC] Voir aussi: [pickle — Python object serialization](https://docs.python.org/3/library/pickle.html#module-pickle) et [marshal — Internal Python object serialization](https://docs.python.org/3/library/marshal.html#module-marshal) ### Sauvegarder un dictionnaire dans un fichier json Pour sauvegarder un dictionnaire avec python dans un fichier json, on peut utiliser la fonc

How to plot points in front of a line in matplotlib ?

By default in matplotlib, if a line and a scatter plot are plotted in a same figure, the points are placed behind the line, illustration (no matter if the scatter() is called before plot()): [image:scatter-points-order-01 size:50 caption:How to plot points in front of a line in matplotlib ?] import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8,9] y = [1,2,3,4,5,6,7,8,9] plt.plot([0,10],[0,10]) plt.scatter(x,y,s=300,color='red') plt.title("Tracer un nuage de points devant u

How to add a legend to a scatter plot in matplotlib ?

Examples of how to add a legend to a scatter plot in matplotlib: [TOC] ### Add a simple legend to a scatter plot Using the pyplot function [legend()](http://stackoverflow.com/questions/17411940/matplotlib-scatter-plot-legend): [image:ScatterPlot-09 size:50 caption:How to add a legend for a scatter plot in matplotlib ?] import matplotlib.pyplot as plt x = [1,2,3,4] y = [4,1,3,6] plt.scatter(x, y, c='coral', label='Class 1') x = [5,6,7,8] y = [1,3,5,2] plt.scatt

How to increase the size of scatter points in matplotlib ?

Examples of how to increase the size of scatter points in matplotlib: [TOC] ### Increase the size of all points To increase the size of scatter points, a solution is to use the option "s" from the function [scatter()](http://matplotlib.org/api/pyplot_api.html#module-matplotlib.pyplot), example [image:ScatterPlot-07 size:50 caption:How to increase the size of scatter points in matplotlib ?] import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8] y = [4,1,3,6,1,3,5,2] plt.s

About


More information and options about the notebook can be found in the following dropdown menus:

Search


Use the following form to retrieve notes: