C'è un modo per farlo? Non riesco a sembrare un modo semplice per interfacciare le serie di panda con la creazione di un CDF.Plottaggio CDF di una serie di panda in python
risposta
Credo che la funzionalità che stai cercando è nel metodo hist di un oggetto serie che avvolge la funzione hist() in matplotlib
Ecco la relativa documentazione
In [10]: import matplotlib.pyplot as plt
In [11]: plt.hist?
...
Plot a histogram.
Compute and draw the histogram of *x*. The return value is a
tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*,
[*patches0*, *patches1*,...]) if the input contains multiple
data.
...
cumulative : boolean, optional, default : True
If `True`, then a histogram is computed where each bin gives the
counts in that bin plus all bins for smaller values. The last bin
gives the total number of datapoints. If `normed` is also `True`
then the histogram is normalized such that the last bin equals 1.
If `cumulative` evaluates to less than 0 (e.g., -1), the direction
of accumulation is reversed. In this case, if `normed` is also
`True`, then the histogram is normalized such that the first bin
equals 1.
...
Per esempio
In [12]: import pandas as pd
In [13]: import numpy as np
In [14]: ser = pd.Series(np.random.normal(size=1000))
In [15]: ser.hist(cumulative=True, normed=1, bins=100)
Out[15]: <matplotlib.axes.AxesSubplot at 0x11469a590>
In [16]: plt.show()
prova ad aggiungere qualche descrizione e link per il backup del codice, se possibile – Ram
C'è un modo per ottenere solo il funzione passo e non avere le barre compilate? – robertevansanders
Sarebbe 'histtype = 'step'' che si trova anche nella documentazione' pyplot.hist' troncata sopra –
Un grafico di funzione di distribuzione cumulativa o CDF è fondamentalmente un grafico con sull'asse X i valori ordinati e sull'asse Y la distribuzione cumulativa. Quindi, vorrei creare una nuova serie con i valori ordinati come indice e la distribuzione cumulativa come valori.
prima creare un serie di esempio:
import pandas as pd
import numpy as np
ser = pd.Series(np.random.normal(size=100))
Ordina la serie:
ser = ser.sort_values()
Ora, prima di procedere, aggiungere di nuovo l'ultimo valore (e più grande). Questo passaggio è importante soprattutto per le piccole dimensioni del campione al fine di ottenere un CDF imparziale:
ser[len(ser)] = ser.iloc[-1]
creare una nuova serie con i valori ordinati come indice e la distribuzione cumulativa come valori:
cum_dist = np.linspace(0.,1.,len(ser))
ser_cdf = pd.Series(cum_dist, index=ser)
Infine, tracciare la funzione come segue:
ser_cdf.plot(drawstyle='steps')
per me, questo sembrava un modo semplice per farlo:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
heights = pd.Series(np.random.normal(size=100))
# empirical CDF
def F(x,data):
return float(len(data[data <= x]))/len(data)
vF = np.vectorize(F, excluded=['data'])
plt.plot(np.sort(heights),vF(x=np.sort(heights), data=heights))
Questo è il modo più semplice.
import pandas as pd
df = pd.Series([i for i in range(100)])
df.hist(cumulative='True')
Questa dovrebbe essere la risposta accettata! –
Potresti definire il vostro problema? Qual è l'input e l'output? scipy.stats ha le funzioni cdf a cui potresti essere interessato. –
C'era una richiesta di funzionalità per questo, ma è fuori dal dominio dei panda. Usa [seaborn] (http://web.stanford.edu/~mwaskom/software/seaborn/tutorial/plotting_distributions.html#basic-visualization-with-histograms)'s 'kdeplot' con' cumulative = True' – TomAugspurger
L'input è una serie, l'output è un grafico di una funzione CDF. – robertevansanders