Hôm nay tôi đã giải quyết việc vẽ cả hai hàm mật độ xác suất và ước tính mật độ hạt nhân trong Python
Để hiểu các hàm mật độ xác suất hoặc PDF trước tiên, trước tiên chúng ta cần xem tài liệu về scipy. số liệu thống kê. định mức
scipy. số liệu thống kê. định mứcMột biến ngẫu nhiên liên tục bình thường
Vị trí [
loc
] từ khóa xác định giá trị trung bình. Từ khóa thang đo [scale
] xác định độ lệch chuẩn
Đưa ra một khung dữ liệu và một cột trong khung dữ liệu đó, chúng ta có thể tính toán hàm mật độ xác suất của một biến bằng cách sử dụng như sau
from scipy import statsdata = df['column']
loc = data.mean[]
scale = data.std[]pdf = stats.norm.pdf[data, loc=loc, scale=scale]
Chúng ta cũng có thể sử dụng số liệu thống kê. tiêu chuẩn để tìm xác suất mà một sự kiện sẽ xảy ra. Sử dụng hàm phân phối tích lũy, tìm diện tích dưới đường cong cho điểm p, chúng ta có thể tìm thấy xác suất p sẽ xảy ra. Nếu chúng ta cần diện tích dưới đường cong qua điểm p, chúng ta có thể sử dụng 1-p hoặc xác suất sự kiện sẽ không xảy ra
prob = stats.norm[loc=loc, scale=scale]sơ sinh biển. đồ thị
event = prob.cdf[point]
event2 = 1 - prob.cdf[point]# Write the probability as a percentage
event_pct = str[round[event*100, 1]] + '%'
print['The probability that the event will occur is: ', event_pct]
sử dụng seaborn. lineplot, chúng ta có thể vẽ PDF như hình bên dưới
import seaborn as snsfig, ax = plt.subplots[]Trường hợp sử dụng
ax = sns.lineplot[x=data, y=pdf, ax=ax]
plt.show[]
Dữ liệu được lấy từ dự án blackjack của tôi, trong đó shoe_df là khung dữ liệu chứa thông tin về 2.880 đôi giày blackjack sáu bộ bài. Tôi đã tìm thấy tệp PDF về số lần thắng của người chơi, số lần thua của người chơi, số ván bài bị đẩy và số lượng người chơi [số ván bài thắng trừ số ván bài bị thua], sau đó vẽ đồ thị của cả bốn tệp PDF
def plot_pdf[x, ax, xlabel, title, color]:
'Creates subplots of probability density functions' data = shoe_df[x]
loc = data.mean[]
scale = data.std[]
pdf = stats.norm.pdf[data, loc=loc, scale=scale] # Plot pdf using sns.lineplot
ax = sns.lineplot[x=data, y=pdf, color=color, ax=ax] # Change face color and grid lines
ax.set_facecolor['white']
ax.grid[which='major', linewidth='0.2', color='gray'] # Set title, x and y labels
ax.set_title[title]
ax.set_xlabel[xlabel]
ax.set_ylabel['Probability Density']
# Create four subplots
fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots[2,2, figsize=[15,12]]# Find the pdf's of player win, loss, push, and count
plot_pdf['player_win', ax1, 'Hands won', 'PDF of Hands Won', 'purple']
plot_pdf['player_loss', ax2, 'Hands lost', 'PDF of Hands Lost', 'red']
plot_pdf['push', ax3, 'Hands pushed', 'PDF of Hands Pushed', 'orange']
plot_pdf['player_count', ax4, 'Player count', 'PDF of Player Count', 'blue]# Show plots
plt.show[]
sinh ra biển. kdeplot
Chúng ta có thể sử dụng seaborn. kdeplot để vẽ PDF dưới dạng KDE để có đường cong mượt mà hơn
Biểu đồ ước tính mật độ hạt nhân [KDE] là một phương pháp để trực quan hóa sự phân bố của các quan sát trong tập dữ liệu, tương tự như biểu đồ. KDE biểu thị dữ liệu bằng đường cong mật độ xác suất liên tục theo một hoặc nhiều thứ nguyên
def plot_kde[x, ax, xlabel, title, color]:
'Creates subplots of kernel density estimations' data = shoe_df[x]
loc = data.mean[]
scale = data.std[]
pdf = stats.norm.pdf[data, loc=loc, scale=scale] # Plot pdf as a kde
ax = sns.kdeplot[x=data, y=pdf, color=color, fill=True, ax=ax] # Change face color and grid lines
ax.set_facecolor['white']
ax.grid[which='major', linewidth='0.2', color='gray'] # Set title, x and y labels
ax.set_title[title]
ax.set_xlabel[xlabel]
ax.set_ylabel['Probability Density']
# Create subplots
fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots[2,2, figsize=[15,12]]# Find the kde's of player win, loss, push, and count
plot_kde['player_win', ax1, 'Hands won', 'PDF of Hands Won', 'purple']
plot_kde['player_loss', ax2, 'Hands lost', 'PDF of Hands Lost', 'red']
plot_kde['push', ax3, 'Hands pushed', 'PDF of Hands Pushed', 'orange']
plot_kde['player_count', ax4, 'Player count', 'PDF of Player Count', 'blue]# Show plots
plt.show[]
bản đồ màu
Chúng ta có thể thay đổi bản đồ màu của KDE để có được hiệu ứng khác của cốt truyện. Có thể tìm thấy tài liệu về bản đồ màu trong matplotlib tại đây
def plot_kde[x, ax, xlabel, title]:
'Creates subplots of kernel density estimations' data = shoe_df[x]
loc = data.mean[]
scale = data.std[]
pdf = stats.norm.pdf[data, loc=loc, scale=scale] # Plot pdf as a kde
ax = sns.kdeplot[x=data, y=pdf, fill=True,
cmap='coolwarm', ax=ax] # Change face color and grid lines
ax.set_facecolor['white']
ax.grid[which='major', linewidth='0.2', color='gray'] # Set title, x and y labels
ax.set_title[title]
ax.set_xlabel[xlabel]
ax.set_ylabel['Probability Density']
# Create subplots
fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots[2,2, figsize=[15,12]]# Find the kde's of player win, loss, push, and count
plot_kde['player_win', ax1, 'Hands won', 'PDF of Hands Won']
plot_kde['player_loss', ax2, 'Hands lost', 'PDF of Hands Lost']
plot_kde['push', ax3, 'Hands pushed', 'PDF of Hands Pushed']
plot_kde['player_count', ax4, 'Player count', 'PDF of Player Count']# Show plots
plt.show[]
Tôi hy vọng điều này sẽ giúp hiểu được các hàm mật độ xác suất và ước tính mật độ hạt nhân, đồng thời giúp bạn tạo một số biểu đồ thực sự thú vị