Daidalos

Open Science Notebook

About



Follow

Timeline


How to sum a given row of a data array in python with numpy ?

Daidalos December 05, 2019

Creative Commons License

Example of how to sum a given row of a data array in python with numpy: [TOC] ### Sum a row of data Lets consider the following array: >>> import numpy as np >>> data = np.arange(80).reshape((8, 10)) >>> data array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, ...

How to sum a given column of a data array in python with numpy ?

Daidalos December 05, 2019

Creative Commons License

Example of how to sum a column of data in python with numpy: [TOC] ### Sum a given column of data Let's consider the following matrix >>> import numpy as np >>> data = np.arange(80).reshape((8, 10)) >>> data array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, ...

Comment additionner une colonne d'un tableau de données avec numpy ?

Daidalos December 04, 2019

Creative Commons License

Exemple de comment additionner les nombres sur une colonne d'un tableau de données en python avec numpy: [TOC] ### Additionner les nombres avec sum() Soit la matrice suivante: >>> import numpy as np >>> data = np.arange(80).reshape((8, 10)) >>> data array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47 ...

Comment additionner une ligne d'un tableau de données en python avec numpy ?

Daidalos December 03, 2019

Creative Commons License

Exemple de comment additionner les nombres sur une ligne d'un tableau de données en python avec numpy: [TOC] ### Additionner les nombres avec sum() Soit la matrice suivante: >>> import numpy as np >>> data = np.arange(80).reshape((8, 10)) >>> data array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, ...

Comment sélectionner aléatoirement les éléments d'une matrice en python avec numpy ?

Daidalos December 03, 2019

Creative Commons License

Exemples de comment sélectionner aléatoirement les éléments d'une matrice en python avec numpy [TOC] ### Échantillonnage aléatoire avec choice() Soit une matrice 1D avec 10 éléments: >>> import numpy as np >>> data = np.arange(10) >>> data array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) \begin{equation} A = \left( \begin{array}{ccc} 0 & 1& 2& 3& 4& 5& 6& 7& 8& 9 \end{array}\right) \end{equation} Pour sélectionner aléatoirement n éléments de la matrice on peut utiliser la fon ...

How to randomly select elements of an array with numpy in python ?

Daidalos November 26, 2019

Creative Commons License

Examples of how to randomly select elements of an array with numpy in python: [TOC] ### Randomly select elements of a 1D array using choice() Lets create a simple 1D array with 10 elements: >>> import numpy as np >>> data = np.arange(10) >>> data array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) \begin{equation} A = \left( \begin{array}{ccc} 0 & 1& 2& 3& 4& 5& 6& 7& 8& 9 \end{array}\right) \end{equation} To select randomly n elements, a solution is to use [choice()](https://do ...

How to randomly select rows of an array in python with numpy ?

Daidalos November 26, 2019

Creative Commons License

Examples of how to randomly select rows of an array in python with numpy: [TOC] ### Create an array with numpy Let create the following array: >>> import numpy as np >>> data = np.arange(80).reshape((8, 10)) >>> data array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, ...

How to change the color background of a matplotlib figure ?

Daidalos November 20, 2019

Creative Commons License

Examples of how to change the color background of a matplotlib figure: [TOC] ### Using subplot and the option axisbg from pylab import * subplot(111, axisbg='#ababab') t = arange(0.0, 2.0, 0.01) s = sin(2*pi*t) plot(t, s) xlabel('x') ylabel('y') title('How to change background color in matplotlib ?') savefig('exemple_01.png') show() [image:376 size:50 caption:How to change the color background of a matplotlib figure ?] source: [How to set opacity of backgroun ...

Elementary operations on lists in python

Daidalos November 20, 2019

Creative Commons License

Examples of elementary operations on lists in python [TOC] ### Create a list Create an empty list >>> l = [] >>> type(myfirstlist) <type 'list'> Create a list with 3 elements >>> l = ['a','b','c'] Note: in a list the elements can have different typrs >>> l = ['hello',1,2] ### Get the number of elements in a list To get the number of elements in a list a solution is to use len() >>> l = ['Pierre', 'Paul', 'Mathieu'] >>> len(l) 3 ...

Hot to sum / add two or several matrices together in python ?

Daidalos November 20, 2019

Creative Commons License

Examples of how to sum / add two or several matrices together in python using numpy: [TOC] ### Add two matrices of same size Let sum two matrices of same size. Let's consider the matrix A: >>> import numpy as np >>> A = np.array(([3,1],[6,4])) >>> A array([[3, 1], [6, 4]]) \begin{equation} A = \left( \begin{array}{ccc} 3 & 1 \\\\ 6 & 4 \end{array}\right) \end{equation} and matrix B: >>> B = np.array(([1,8],[4,2])) >>> B array([[1, 8], [4, 2]] ...

Comment additionner / sommer deux ou plusieurs matrices en python ?

Daidalos November 19, 2019

Creative Commons License

Exemples de comment additionner (sommer) deux ou plusieurs matrices en python: [TOC] ### Additionner deux matrices de même taille Le cas le plus simple est si deux matrices A et B ont les mêmes dimensions. Soit par exemple la matrice A suivante: >>> import numpy as np >>> A = np.array(([3,1],[6,4])) >>> A array([[3, 1], [6, 4]]) \begin{equation} A = \left( \begin{array}{ccc} 3 & 1 \\\\ 6 & 4 \end{array}\right) \end{equation} et la matrice B: >>> B = np ...

How to get a complex number in polar coordinates in python ?

Daidalos November 16, 2019

Creative Commons License

Examples of how to get a complex number in polar coordinates in python: [TOC] ### Using the python module cmath To get a complex number in polar coordinate a solution is to use the python module [cmath](https://docs.python.org/2/library/cmath.html): >>> import cmath Let's consider the following complex number: >>> z = 2 + 3j to get the polar coordinates: >>> r,theta = cmath.polar(z) >>> r 3.605551275463989 >>> theta 0.982793723247329 Note: to return the c ...

Comment calculer les coordonnées polaires d'un nombre complexe en python ?

Daidalos November 16, 2019

Creative Commons License

Exemples de comment calculer les coordonnées polaires d'un nombre complexe en python [TOC] ### Avec le module cmath Avec python pour calculer les coordonnées polaires d'un nombre complexe , il existe le module [cmath](https://docs.python.org/2/library/cmath.html): >>> import cmath Soit le nombre complexe z suivant sous forme algébrique: >>> z = 2 + 3j avec comme coordonnées cartésiennes (2,3). Pour convertir ces coordonnées en coordonnées polaire on peut utiliser la fonc ...

How to create a matrix of complex numbers in python using numpy ?

Daidalos November 15, 2019

Creative Commons License

Examples of how to create a matrix of complex numbers in python using numpy: [TOC] ### Create a matrix of random numbers >>> Z = np.array([[1+2j,1+3j],[5+6j,3+8j]]) >>> Z array([[ 1.+2.j, 1.+3.j], [ 5.+6.j, 3.+8.j]]) ### Create a matrix of random numbers with 0+0j >>> import numpy as np >>> Z = np.zeros(10, dtype=complex) >>> Z array([ 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0. ...

How to get the conjugate of a complex number in python ?

Daidalos November 15, 2019

Creative Commons License

Examples of how to get the conjugate of a complex number z in python: [TOC] ### Get the conjugate of a complex number A solution is to use the python function [conjugate()](http://stackoverflow.com/questions/8370637/complex-numbers-usage-in-python), example >>> z = complex(2,5) >>> z.conjugate() (2-5j) >>> ### Matrix of complex numbers Another example using a matrix of complex numbers import numpy as np >>> Z = np.random.random(10) + np.random. ...

How to extract the real and imaginary parts of a complex number in python ?

Daidalos November 15, 2019

Creative Commons License

To extract the the real and imaginary parts of a complex number z=a+ib in python, a solution is to use z.real and z.imag: [TOC] ### Extract the real and imaginary parts of a complex number >>> z = complex(2,5) >>> z (2+5j) >>> z.real 2.0 >>> z.imag 5.0 ### Matrix of complex number It also works with matrix of complex numbers: >>> import numpy as np >>> Z = np.array([[1+2j,1+3j],[5+6j,3+8j]]) >>> Z array([[ 1.+2.j, 1.+3.j], [ 5.+6. ...

How to get the modulus of complex number in python ?

Daidalos November 15, 2019

Creative Commons License

Examples of how to get the modulus of complex number in python: [TOC] ### Definition The modulus of complex number $z=a+ib$ is: \begin{equation} |z| = |a+ib| = \sqrt{a^2+b^2} \end{equation} with $a$ and $b$ are the real and imaginary parts. ### Create a function to calculate the modulus of complex number To calculate the modulus of complex number a solution is to define a simple python function: >>> import math >>> def complexe_modulo(z): ... a = z.r ...

Comment ajouter une colonne de 1 à une matrice avec numpy ? (machine learning; python)

Daidalos November 15, 2019

Creative Commons License

Exemples de comment ajouter une colonne de 1 à une matrice avec numpy [TOC] ### Ajouter une colonne de 1 à une matrice avec des données d'entraînement Considérons la matrice X avec des données d'entraînement (avec n=6 observations et d=3 "features"): \begin{equation} X = \left( \begin{array}{ccc} x_{11} & x_{12} & x_{13} \\\\ x_{21} & x_{22} & x_{23} \\\\ x_{31} & x_{32} & x_{33} \\\\ x_{41} & x_{42} & x_{43} \\\\ x_{51} & x_{52} & x_{53} \\\\ x_{61} & x_{62} & x_{63} \en ...

Comment copier un tableau de données DataFrame avec pandas ?

Daidalos November 15, 2019

Creative Commons License

Exemple de comment copier un tableau de données DataFrame avec pandas [TOC] ### Créer une dataframe Pour commencer créons une simple dataframe: >>> import pandas as pd >>> import numpy as np >>> data = np.random.randint(100, size=(10,5)) >>> df = pd.DataFrame(data=data,columns=['a','b','c','d','e']) >>> df a b c d e 0 42 94 3 22 28 1 0 85 93 43 18 2 70 10 98 19 26 3 54 72 89 51 61 4 13 44 94 28 34 5 79 4 89 33 81 6 6 ...

How to copy a dataframe with pandas in python ?

Daidalos November 14, 2019

Creative Commons License

Example of how to copy a data frame with pandas in python: [TOC] ### Create a dataframe To start let's create a simple dataframe: >>> import pandas as pd >>> import numpy as np >>> data = np.random.randint(100, size=(10,5)) >>> df = pd.DataFrame(data=data,columns=['a','b','c','d','e']) >>> df a b c d e 0 42 94 3 22 28 1 0 85 93 43 18 2 70 10 98 19 26 3 54 72 89 51 61 4 13 44 94 28 34 5 79 4 89 33 81 6 69 37 84 89 ...

How to prepend an n-by-1 column of ones to a matrix in python using numpy ? (machine learning)

Daidalos November 14, 2019

Creative Commons License

Examples of how to prepend an n-by-1 column of ones to a matrix of training input data: [TOC] ### Prepend an n-by-1 column of ones to a training dataset Let's consider the following matrix X of training data X (with n=6 observations and d=3 features): \begin{equation} X = \left( \begin{array}{ccc} x_{11} & x_{12} & x_{13} \\\\ x_{21} & x_{22} & x_{23} \\\\ x_{31} & x_{32} & x_{33} \\\\ x_{41} & x_{42} & x_{43} \\\\ x_{51} & x_{52} & x_{53} \\\\ x_{61} & x_{62} & x_{63} \end ...

How to count the occurrence of an element in a pandas data frame column ?

Daidalos November 08, 2019

Creative Commons License

Examples of how to count the occurrence of an element in a pandas data frame column: [TOC] ### Using value_counts() Lets take for example the file ['default of credit card clients Data Set"](https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients#) that can be downloaded [here](https://archive.ics.uci.edu/ml/machine-learning-databases/00350/) >>> import pandas as pd >>> df = pd.read_excel('default of credit card clients.xls', header=1) To get the count of d ...

Comment compter le nombre d'occurrences pour les éléments d'une colonne dans une data frame avec pandas ?

Daidalos November 08, 2019

Creative Commons License

Exemple de comment compter le nombre d'occurrences sur les éléments d'une colonne avec une data frame pandas: [TOC] ### Nombre d'occurrences avec la fonction value_counts() Soit par exemple le fichier de données suivant ['default of credit card clients Data Set"](https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients#) que l'on peut télécharger [ici](https://archive.ics.uci.edu/ml/machine-learning-databases/00350/) >>> import pandas as pd >>> df = pd.read_ex ...

How to get the shape of a DataFrame in python with pandas ?

Daidalos November 08, 2019

Creative Commons License

Examples of how to get the shape of a DataFrame in python with panda: [TOC] ### Get DataFrame shape Let's consider the following file train.csv (that can be downloaded on kaggle): >>> import pandas as pd >>> df = pd.read_csv('train.csv') To get the shape a solution is to use the function [shape()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.shape.html): >>> print(df.shape) (1460, 81) ### Number of columns Get the number of columns ...

Comment obtenir la taille d'un tableau de données DataFrame avec pandas ?

Daidalos November 08, 2019

Creative Commons License

Exemples de comment obtenir la taille d'un tableau de données DataFrame avec pandas: [TOC] ### Taille d'une DataFrame avec shape() Soit par exemple le fichier csv suivant train.csv (que l'on peut télécharger sur kaggle). Pour lire le fichier il existe la fonction pandas read_csv(): >>> import pandas as pd >>> df = pd.read_csv('train.csv') On peut alors utiliser la fonction [shape()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.shape.html) pour ob ...

Search