Exemples de comment appliquer un logarithme à tous les éléments d'une matrice avec numpy en python:
La fonction numpy log()
Pour appliquer le logarithme une solution est d'utiliser la fonction numpy numpy.log, illustration
import numpy as npimport mathA = np.array((math.e))print(A)A = np.log(A)print(A)
donne respectivement:
2.718281828459045
et
1.0
Autre exemple:
A = np.arange(1.0,10.0,1.0)A = np.log(A)
donne
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
et
[0. 0.69314718 1.09861229 1.38629436 1.60943791 1.79175947
1.94591015 2.07944154 2.19722458]
Tracer avec matplotlilb en mettant un axe en échelle logarithmique
Note: Pour tracer avec matplotlilb en mettant un axe en échelle logarithmique, on peut utiliser ax.set_yscale('log'), exemple:
from pylab import figure, cmimport matplotlib.pyplot as pltx = np.arange(0.0,10.0,0.1)y = np.exp(x)fig = figure(num=None, figsize=(12, 10), dpi=80, facecolor='w', edgecolor='k')plt.plot(x,y)plt.grid(True,which="both", linestyle='--')plt.savefig("log_fig_01.png", bbox_inches='tight')plt.show()

fig = figure(num=None, figsize=(12, 10), dpi=80, facecolor='w', edgecolor='k')ax = fig.add_subplot(1, 1, 1)plt.plot(x,y)ax.set_yscale('log')plt.grid(True,which="both", linestyle='--')plt.savefig("log_fig_02.png", bbox_inches='tight')plt.show()

