Cara menggunakan pmf pdf python

The line df["AGW"].sort_values[] doesn't change df. Maybe you meant df.sort_values[by=['AGW'], inplace=True]. In that case the full code will be :

import numpy as np
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
import scipy.stats as stats

x = np.random.normal[50, 3, 1000]
source = {"Genotype": ["CV1"]*1000, "AGW": x}
df=pd.DataFrame[source]

df.sort_values[by=['AGW'], inplace=True]
df_mean = np.mean[df["AGW"]]
df_std = np.std[df["AGW"]]
pdf = stats.norm.pdf[df["AGW"], df_mean, df_std]

plt.plot[df["AGW"], pdf]

Which gives :

Edit :

I think here we already have the distribution [x is normally distributed] so we dont need to generate the pdf of x. As the use of the pdf is for something like this :

mu = 50
variance = 3
sigma = math.sqrt[variance]
x = np.linspace[mu - 5*sigma, mu + 5*sigma, 1000]
plt.plot[x, stats.norm.pdf[x, mu, sigma]]
plt.show[]

Here we dont need to generate the distribution from x points, we only need to plot the density of the distribution we already have . So you might use this :

Make a Binomial Random variable $X$ and compute its probability mass function [PMF] or cumulative density function [CDF]. We love the scipy stats library because it defines all the functions you would care about for a random variable, including expectation, variance, and even things we haven't talked about in CS109, like entropy. This example declares $X \sim \text{Bin}[n = 10, p = 0.2]$. It calculates a few statistics on $X$. It then calculates $P[X = 3]$ and $P[X \leq 4]$. Finally it generates a few random samples from $X$:

from scipy import stats
X = stats.binom[10, 0.2] # Declare X to be a binomial random variable
print X.pmf[3]           # P[X = 3]
print X.cdf[4]           # P[X 

Bài mới nhất

Chủ Đề