Bạn có thể nhận được CDF dễ dàng. Vì vậy, PDF thông qua CDF
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
import scipy.stats
def setGridLine[ax]:
#//jonathansoma.com/lede/data-studio/matplotlib/adding-grid-lines-to-a-matplotlib-chart/
ax.set_axisbelow[True]
ax.minorticks_on[]
ax.grid[which='major', linestyle='-', linewidth=0.5, color='grey']
ax.grid[which='minor', linestyle=':', linewidth=0.5, color='#a6a6a6']
ax.tick_params[which='both', # Options for both major and minor ticks
top=False, # turn off top ticks
left=False, # turn off left ticks
right=False, # turn off right ticks
bottom=False] # turn off bottom ticks
data1 = np.random.normal[0,1,1000000]
x=np.sort[data1]
y=np.arange[x.shape[0]]/[x.shape[0]+1]
f2 = scipy.interpolate.interp1d[x, y,kind='linear']
x2 = np.linspace[x[0],x[-1],1001]
y2 = f2[x2]
y2b = np.diff[y2]/np.diff[x2]
x2b=[x2[1:]+x2[:-1]]/2.
f3 = scipy.interpolate.interp1d[x, y,kind='cubic']
x3 = np.linspace[x[0],x[-1],1001]
y3 = f3[x3]
y3b = np.diff[y3]/np.diff[x3]
x3b=[x3[1:]+x3[:-1]]/2.
bins=np.arange[-4,4,0.1]
bins_centers=0.5*[bins[1:]+bins[:-1]]
cdf = scipy.stats.norm.cdf[bins_centers]
pdf = scipy.stats.norm.pdf[bins_centers]
plt.rcParams["font.size"] = 18
fig, ax = plt.subplots[3,1,figsize=[10,16]]
ax[0].set_title["cdf"]
ax[0].plot[x,y,label="data"]
ax[0].plot[x2,y2,label="linear"]
ax[0].plot[x3,y3,label="cubic"]
ax[0].plot[bins_centers,cdf,label="ans"]
ax[1].set_title["pdf:linear"]
ax[1].plot[x2b,y2b,label="linear"]
ax[1].plot[bins_centers,pdf,label="ans"]
ax[2].set_title["pdf:cubic"]
ax[2].plot[x3b,y3b,label="cubic"]
ax[2].plot[bins_centers,pdf,label="ans"]
for idx in range[3]:
ax[idx].legend[]
setGridLine[ax[idx]]
plt.show[]
plt.clf[]
plt.close[]
Cải thiện bài viết
Lưu bài viết
Prerequisites:
- Matplotlib
- Numpy
- Scipy
- Số liệu thống kê
Phân phối bình thường là một hàm xác suất được sử dụng trong các số liệu thống kê cho biết cách phân phối giá trị dữ liệu. Đây là chức năng phân phối xác suất quan trọng nhất được sử dụng trong số liệu thống kê vì lợi thế của nó trong trường hợp thực tế. Ví dụ, chiều cao của dân số, kích thước giày, mức IQ, lăn một cái chết, và nhiều hơn nữa. & NBSP; is a probability function used in statistics that tells about how the data values are distributed. It is the most important probability distribution function used in statistics because of its advantages in real case scenarios. For example, the height of the population, shoe size, IQ level, rolling a die, and many more.
Hàm mật độ xác suất của phân phối bình thường hoặc Gaussian được đưa ra bởi:
Hàm mật độ xác suất
Ở đâu, x là biến, MU là giá trị trung bình và độ lệch chuẩn Sigma
Các mô -đun cần thiết
- Matplotlib là thư viện trực quan hóa dữ liệu Python, được sử dụng rộng rãi cho mục đích trực quan hóa dữ liệu.is python’s data visualization library which is widely used for the purpose of data visualization.
- Numpyis một gói xử lý mảng đa năng. Nó cung cấp một đối tượng mảng đa chiều hiệu suất cao và các công cụ để làm việc với các mảng này. Đây là gói cơ bản để điện toán khoa học với Python. is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.
- Scipy là một thư viện Python hữu ích trong việc giải nhiều phương trình toán học và thuật toán.is a python library that is useful in solving many mathematical equations and algorithms.
- Mô -đun thống kê cung cấp các chức năng để tính toán số liệu thống kê toán học của dữ liệu số.module provides functions for calculating mathematical statistics of numeric data.
Các chức năng được sử dụng
- Để tính toán trung bình của dữ liệu
Syntax:
mean[data]
- Để tính toán độ lệch chuẩn của dữ liệu
Syntax:
stdev[data]
- Để tính toán mật độ xác suất bình thường của định mức dữ liệu.pdf được sử dụng, nó đề cập đến hàm mật độ xác suất bình thường là mô -đun trong thư viện SCIPY sử dụng hàm mật độ xác suất trên để tính giá trị.
Syntax:
Norm.pdf [dữ liệu, LỘC, tỷ lệ]
Ở đây, tham số LỘC còn được gọi là giá trị trung bình và tham số tỷ lệ còn được gọi là độ lệch chuẩn.
Cách tiếp cận
- Nhập mô -đun
- Tạo dữ liệu
- Tính toán trung bình và độ lệch
- Tính mật độ xác suất bình thường
- Sơ đồ sử dụng các giá trị được tính toán ở trên
- Hiển thị cốt truyện
Dưới đây là việc thực hiện.
Python3
import
numpy as np
import
matplotlib.pyplot as plt
from
scipy.stats
import
mean[data]0
import
mean[data]2
mean[data]3
mean[data]4
mean[data]5
mean[data]6
mean[data]7
mean[data]8
mean[data]7
mean[data]8
stdev[data]1
stdev[data]2
stdev[data]3
mean[data]4
stdev[data]5
stdev[data]6
mean[data]4
stdev[data]8
stdev[data]9
import
0
Output:
Đầu ra của mã trên