Cách tạo phân phối chuẩn trong python

Phân phối chuẩn là một dạng trình bày dữ liệu bằng cách sắp xếp phân phối xác suất của từng giá trị trong dữ liệu. Hầu hết các giá trị vẫn xoay quanh giá trị trung bình làm cho sự sắp xếp đối xứng

Chúng tôi sử dụng các hàm khác nhau trong thư viện numpy để tính toán một cách toán học các giá trị cho phân phối chuẩn. Biểu đồ được tạo trên đó chúng tôi vẽ đường cong phân phối xác suất

import matplotlib.pyplot as plt
import numpy as np

mu, sigma = 0.5, 0.1
s = np.random.normal[mu, sigma, 1000]

# Create the bins and histogram
count, bins, ignored = plt.hist[s, 20, normed=True]

# Plot the distribution curve
plt.plot[bins, 1/[sigma * np.sqrt[2 * np.pi]] *
    np.exp[ - [bins - mu]**2 / [2 * sigma**2] ],       linewidth=3, color='y']
plt.show[]

Đầu ra của nó như sau -

Phân phối chuẩn còn được gọi là phân phối Gauss hoặc phân phối Laplace-Gauss

Phân phối bình thường với Python Ví dụ

Phân phối chuẩn đại diện cho phân phối đối xứng trong đó hầu hết các quan sát tập trung xung quanh đỉnh trung tâm được gọi là giá trị trung bình của phân phối. Tham số dùng để đo mức độ biến thiên của các quan sát quanh giá trị trung bình được gọi là độ lệch chuẩn. Xác suất cho các giá trị xảy ra gần giá trị trung bình cao hơn các giá trị ở xa giá trị trung bình. Phân phối chuẩn là một biểu đồ phân phối xác suất. Các tham số của biểu đồ phân phối chuẩn xác định hình dạng và xác suất là độ lệch trung bình và độ lệch chuẩn. Diện tích của biểu đồ giữa hai điểm khác nhau trong biểu đồ phân phối chuẩn biểu thị xác suất của giá trị xảy ra giữa hai điểm đó

Dưới đây là một số tính chất của phân phối chuẩn của dân số

  • Các điểm trong phân phối chuẩn là đối xứng. Phân phối bình thường không thể được sử dụng để mô hình phân phối sai lệch
  • Giá trị trung bình, trung vị và phương thức phân phối bình thường là như nhau
  • Một nửa dân số nhỏ hơn mức trung bình và một nửa lớn hơn mức trung bình
  • Quy tắc thực nghiệm của phân phối chuẩn diễn ra như sau. 68% quan sát nằm trong +/- 1 độ lệch chuẩn so với giá trị trung bình, 95% quan sát nằm trong +/- 2 độ lệch chuẩn so với giá trị trung bình và 99. 7% quan sát nằm trong khoảng +/- 3 độ lệch chuẩn so với giá trị trung bình

Đây là hàm mật độ xác suất cho phân phối chuẩn

Hình 1. Hàm mật độ xác suất phân phối chuẩn

Trong hàm trên, μμ đại diện cho giá trị trung bình và σσ đại diện cho độ lệch chuẩn. Với các giá trị khác nhau của biến ngẫu nhiên [x], người ta có thể tính xác suất bằng cách sử dụng hàm mật độ xác suất ở trên

Dưới đây là biểu đồ phân phối xác suất mẫu biểu thị phân phối chuẩn với giá trị trung bình là 5 và độ lệch chuẩn là 10. Biểu đồ được tạo cho biến ngẫu nhiên lấy giá trị từ -100 đến 100.  

Hình 2. Biểu đồ phân phối bình thường

Đoạn mã sau có thể được sử dụng để tạo biểu đồ phân phối bình thường ở trên

#
# Create a normal distribution with mean as 5 and standard deviation as 10
#
mu = 5
std = 10
snd = stats.norm[mu, std]
#
# Generate 1000 random values between -100, 100
#
x = np.linspace[-100, 100, 1000]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -100, 100
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-60, 60]
plt.title['Normal Distribution [Mean = 5, STD = 10]', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]

Đây là mã đại diện cho nhiều ô phân phối bình thường giống như sau

Hình 2. Nhiều lô phân phối bình thường

Mã sau đây có thể được sử dụng để tạo nhiều biểu đồ phân phối bình thường được hiển thị ở trên có các phương tiện và độ lệch chuẩn khác nhau

#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]

Phân phối chuẩn chuẩn với Python Ví dụ

Phân phối chuẩn chuẩn là phân phối chuẩn với giá trị trung bình là 0 và độ lệch chuẩn là 1.  

Đây là mã Python và đồ thị cho phân phối chuẩn chuẩn. Lưu ý rằng phân phối chuẩn chuẩn có giá trị trung bình là 0 và độ lệch chuẩn là 1. Hãy chú ý đến một số điều sau đây trong đoạn mã dưới đây

Hình 3. Biểu đồ phân phối chuẩn chuẩn [Mean = 0, STD = 1]

Sau đây là mã Python được sử dụng để tạo biểu đồ phân phối chuẩn ở trên. Hãy chú ý đến một số điều sau đây trong đoạn mã được đưa ra dưới đây

  • Mô-đun Scipy Stats được sử dụng để tạo một phiên bản phân phối chuẩn chuẩn với giá trị trung bình là 0 và độ lệch chuẩn là 1 [số liệu thống kê. định mức]
  • Hàm mật độ xác suất pdf[] được gọi trong trường hợp thống kê. tiêu chuẩn để tạo ước tính xác suất của các giá trị khác nhau của biến ngẫu nhiên với phân phối chuẩn chuẩn
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]

kết luận

Dưới đây là tóm tắt những gì bạn đã học được trong bài đăng này liên quan đến Phân phối chuẩn

  • Phân phối chuẩn là phân phối xác suất đối xứng với số lượng quan sát bằng nhau trên một nửa giá trị trung bình
  • Các tham số biểu thị hình dạng và xác suất của phân phối chuẩn là trung bình và độ lệch chuẩn
  • Có thể sử dụng mô-đun Python Scipy stats để tạo phân phối chuẩn với các tham số trung bình và độ lệch chuẩn bằng cách sử dụng phương pháp norm
  • Phân phối chuẩn chuẩn là phân phối chuẩn với trung bình là 0 và độ lệch chuẩn là 1
  • Trong phân phối chuẩn, 68% quan sát nằm trong 1 độ lệch chuẩn, 95% quan sát nằm trong 2 độ lệch chuẩn và 99. 7% quan sát nằm trong 3 độ lệch chuẩn so với giá trị trung bình

Ngay cả khi bạn không làm trong lĩnh vực thống kê, bạn chắc hẳn đã bắt gặp thuật ngữ “Phân phối chuẩn”

Phân phối xác suất là một hàm thống kê mô tả khả năng nhận được các giá trị có thể có mà một biến ngẫu nhiên có thể nhận. Bằng cách này, chúng tôi muốn nói đến phạm vi giá trị mà một tham số có thể nhận khi chúng tôi chọn ngẫu nhiên các giá trị từ tham số đó

Phân phối xác suất có thể rời rạc hoặc liên tục. https. //imasdk. googleapis. com/js/lõi/cầu3. 476. 0_vi. html#goog_1402046895

Giả sử trong một thành phố, chúng ta có chiều cao của những người trưởng thành trong độ tuổi từ 20-30 tuổi, dao động từ 4. 5 ft. đến 7 ft

Nếu chúng ta được yêu cầu chọn ngẫu nhiên 1 người trưởng thành và hỏi chiều cao của anh ấy/cô ấy [giả sử giới tính không ảnh hưởng đến chiều cao] là bao nhiêu? . Nhưng nếu chúng ta có sự phân bổ chiều cao của người trưởng thành trong thành phố, chúng ta có thể đặt cược vào kết quả có thể xảy ra nhất

Phân phối bình thường là gì?

Phân phối chuẩn còn được gọi là phân phối Gaussian hay Đường cong hình chuông nổi tiếng. Mọi người sử dụng cả hai từ thay thế cho nhau, nhưng nó có nghĩa giống nhau. Đó là một phân phối xác suất liên tục

Hàm mật độ xác suất [pdf] cho Phân phối chuẩn

Hàm mật độ xác suất của phân phối chuẩn

trong đó, μ = Giá trị trung bình , σ = Độ lệch chuẩn , x = giá trị đầu vào

Thuật ngữ

  • Giá trị trung bình – Giá trị trung bình là giá trị trung bình thông thường. Tổng của tổng số điểm chia cho tổng số điểm
  • Độ lệch chuẩn – Độ lệch chuẩn cho chúng tôi biết mức độ “phân tán” của dữ liệu. Nó là thước đo xem mỗi giá trị được quan sát cách giá trị trung bình bao xa.

Có vẻ khó khăn, phải không?

1. Ví dụ thực hiện phân phối bình thường

Hãy cùng xem đoạn mã dưới đây. Chúng tôi sẽ sử dụng numpy và matplotlib cho minh họa này

# Importing required libraries
 
import numpy as np
import matplotlib.pyplot as plt
 
# Creating a series of data of in range of 1-50.
x = np.linspace[1,50,200]
 
#Creating a Function.
def normal_dist[x , mean , sd]:
    prob_density = [np.pi*sd] * np.exp[-0.5*[[x-mean]/sd]**2]
    return prob_density
 
#Calculate mean and Standard deviation.
mean = np.mean[x]
sd = np.std[x]
 
#Apply function to the data.
pdf = normal_dist[x,mean,sd]
 
#Plotting the Results
plt.plot[x,pdf , color = 'red']
plt.xlabel['Data points']
plt.ylabel['Probability Density']
Đường cong bình thường

2. Thuộc tính của phân phối bình thường

Hàm mật độ phân phối chuẩn chỉ chấp nhận một điểm dữ liệu cùng với giá trị trung bình và độ lệch chuẩn rồi đưa ra một giá trị mà chúng tôi gọi là mật độ xác suất

Chúng ta có thể thay đổi hình dạng của đường cong hình chuông bằng cách thay đổi giá trị trung bình và độ lệch chuẩn

Thay đổi giá trị trung bình sẽ dịch chuyển đường cong về phía giá trị trung bình đó, điều này có nghĩa là chúng ta có thể thay đổi vị trí của đường cong bằng cách thay đổi giá trị trung bình trong khi hình dạng của đường cong vẫn nguyên vẹn

Hình dạng của đường cong có thể được kiểm soát bởi giá trị của Độ lệch chuẩn. Độ lệch chuẩn nhỏ hơn sẽ dẫn đến đường cong có giới hạn chặt chẽ trong khi giá trị cao sẽ dẫn đến đường cong trải rộng hơn

Một số tính chất tuyệt vời của phân phối bình thường

  • Giá trị trung bình, chế độ và trung vị đều bằng nhau
  • Tổng diện tích dưới đường cong bằng 1
  • Đường cong đối xứng quanh giá trị trung bình
Phần trăm phân phối dữ liệu xung quanh giá trị trung bình

Quy tắc thực nghiệm cho chúng ta biết rằng

  • 68% dữ liệu nằm trong một độ lệch chuẩn của giá trị trung bình
  • 95% dữ liệu nằm trong hai độ lệch chuẩn của giá trị trung bình
  • 99. 7% dữ liệu nằm trong ba độ lệch chuẩn của giá trị trung bình

Cho đến nay, đây là một trong những phân phối quan trọng nhất trong tất cả các Thống kê. Phân phối chuẩn là kỳ diệu vì hầu hết các hiện tượng xảy ra tự nhiên đều tuân theo phân phối chuẩn. Ví dụ huyết áp, chỉ số IQ, chiều cao tuân theo phân phối chuẩn

Tính xác suất với phân phối chuẩn

Để tìm xác suất của một giá trị xảy ra trong một phạm vi có phân phối chuẩn, chúng ta chỉ cần tìm diện tích dưới đường cong trong phạm vi đó. tôi. e. chúng ta cần tích hợp hàm mật độ

Vì phân phối chuẩn là phân phối liên tục nên diện tích dưới đường cong biểu thị xác suất

Trước khi đi vào chi tiết, trước tiên chúng ta hãy biết Phân phối Chuẩn Chuẩn là gì

Phân phối chuẩn chuẩn giống như phân phối chuẩn với giá trị trung bình = 0 và độ lệch chuẩn = 1

#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
0

Giá trị z ở trên còn được gọi là điểm z. Điểm z cung cấp cho bạn ý tưởng về khoảng cách từ điểm dữ liệu trung bình

Nếu chúng tôi dự định tính toán xác suất theo cách thủ công, chúng tôi sẽ cần tra cứu giá trị z của mình trong bảng z để xem giá trị phần trăm tích lũy. Python cung cấp cho chúng tôi các mô-đun để thực hiện công việc này cho chúng tôi. Hãy đi sâu vào nó

1. Tạo đường cong bình thường

Chúng tôi sẽ sử dụng hàm lớp

#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
1 để tính xác suất từ ​​phân phối chuẩn

Giả sử chúng ta có dữ liệu về chiều cao của người trưởng thành trong một thị trấn và dữ liệu tuân theo phân phối chuẩn, chúng ta có cỡ mẫu đủ với giá trị trung bình bằng 5. 3 và độ lệch chuẩn là 1

Thông tin này đủ để tạo một đường cong bình thường

# import required libraries
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb
 
# Creating the distribution
data = np.arange[1,10,0.01]
pdf = norm.pdf[data , loc = 5.3 , scale = 1 ]
 
#Visualizing the distribution
 
sb.set_style['whitegrid']
sb.lineplot[data, pdf , color = 'black']
plt.xlabel['Heights']
plt.ylabel['Probability Density']
Phân bố độ cao

Phương thức lớp 

#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
2 yêu cầu 
#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
3 và 
#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
4 cùng với dữ liệu dưới dạng đối số đầu vào và đưa ra giá trị mật độ xác suất.
#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
3 chỉ là giá trị trung bình và 
#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
4 là độ lệch chuẩn của dữ liệu. mã tương tự như những gì chúng tôi đã tạo trong phần trước nhưng ngắn hơn nhiều

2. Tính toán xác suất xuất hiện dữ liệu cụ thể

Bây giờ, nếu chúng ta được yêu cầu chọn ngẫu nhiên một người từ phân bố này, thì xác suất để chiều cao của người đó nhỏ hơn 4 là bao nhiêu?. 5 ft. ?

Diện tích dưới đường cong là xác suất

Diện tích dưới đường cong như trong hình trên sẽ là xác suất để chiều cao của người đó nhỏ hơn 4. 5 ft nếu được chọn ngẫu nhiên từ phân phối. Hãy xem cách chúng ta có thể tính toán điều này trong python

Diện tích dưới đường cong không là gì ngoài Tích phân của hàm mật độ với các giới hạn bằng -∞ đến 4. 5

#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
7
#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
8
#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
9
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
0
#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
8
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
2
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
3
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
4
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
5
#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
2

Dòng mã duy nhất ở trên tìm xác suất có 21. 18% cơ hội nếu một người được chọn ngẫu nhiên từ phân phối chuẩn với trung bình là 5. 3 và độ lệch chuẩn là 1 thì chiều cao của người đó sẽ thấp hơn 4. 5 ft

Chúng tôi khởi tạo đối tượng của lớp

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
6 với giá trị trung bình và độ lệch chuẩn, sau đó sử dụng phương pháp 
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
7 chuyển một giá trị lên mà chúng tôi cần tìm giá trị xác suất tích lũy. Hàm phân phối tích lũy [CDF] tính toán xác suất tích lũy cho một giá trị x đã cho

Giá trị xác suất tích lũy từ -∞ đến ∞ sẽ bằng 1

Bây giờ, một lần nữa, chúng tôi được yêu cầu chọn ngẫu nhiên một người từ phân phối này, sau đó xác suất chiều cao của người đó sẽ nằm trong khoảng từ 6. 5 và 4. 5 ft. ?

Khu Vực Dưới Đường Cong Giữa 4. 5 và 6. 5 Ft____25
#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
6

Đoạn mã trên trước tiên tính giá trị xác suất tích lũy từ -∞ đến 6. 5 và sau đó là giá trị xác suất tích lũy từ -∞ đến 4. 5. nếu chúng ta trừ cdf của 4. 5 từ cdf của 6. 5 kết quả chúng ta nhận được là diện tích dưới đường cong giữa các giới hạn 6. 5 và 4. 5

Bây giờ, điều gì sẽ xảy ra nếu chúng ta được hỏi về xác suất chiều cao của một người được chọn ngẫu nhiên sẽ cao hơn 6. 5 ft?

Khu Vực Dưới Đường Cong Giữa 6. 5ft và Vô cực

Thật đơn giản, vì chúng ta biết tổng diện tích dưới đường cong bằng 1 và nếu chúng ta tính giá trị xác suất tích lũy từ -∞ đến 6. 5 và lấy 1 trừ đi, kết quả sẽ là xác suất chiều cao của một người được chọn ngẫu nhiên sẽ cao hơn 6. 5ft

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
8
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
9
#
# Values of random variable
#
x = np.linspace[-10, 10, 100]
#
plt.figure[figsize=[7.5,7.5]]
#
# Normal distribution with mean 0 and std as 1
#
plt.plot[x, stats.norm[0, 1].pdf[x]]
#
# Normal distribution with mean 1 and std as 0.75
#
plt.plot[x, stats.norm[1, 0.75].pdf[x]]
#
# Normal distribution with mean 2 and std as 1.5
#
plt.plot[x, stats.norm[2, 1.5].pdf[x]]
plt.xlim[-10, 10]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
9
# Importing required libraries
 
import numpy as np
import matplotlib.pyplot as plt
 
# Creating a series of data of in range of 1-50.
x = np.linspace[1,50,200]
 
#Creating a Function.
def normal_dist[x , mean , sd]:
    prob_density = [np.pi*sd] * np.exp[-0.5*[[x-mean]/sd]**2]
    return prob_density
 
#Calculate mean and Standard deviation.
mean = np.mean[x]
sd = np.std[x]
 
#Apply function to the data.
pdf = normal_dist[x,mean,sd]
 
#Plotting the Results
plt.plot[x,pdf , color = 'red']
plt.xlabel['Data points']
plt.ylabel['Probability Density']
1
# Importing required libraries
 
import numpy as np
import matplotlib.pyplot as plt
 
# Creating a series of data of in range of 1-50.
x = np.linspace[1,50,200]
 
#Creating a Function.
def normal_dist[x , mean , sd]:
    prob_density = [np.pi*sd] * np.exp[-0.5*[[x-mean]/sd]**2]
    return prob_density
 
#Calculate mean and Standard deviation.
mean = np.mean[x]
sd = np.std[x]
 
#Apply function to the data.
pdf = normal_dist[x,mean,sd]
 
#Plotting the Results
plt.plot[x,pdf , color = 'red']
plt.xlabel['Data points']
plt.ylabel['Probability Density']
2
# Importing required libraries
 
import numpy as np
import matplotlib.pyplot as plt
 
# Creating a series of data of in range of 1-50.
x = np.linspace[1,50,200]
 
#Creating a Function.
def normal_dist[x , mean , sd]:
    prob_density = [np.pi*sd] * np.exp[-0.5*[[x-mean]/sd]**2]
    return prob_density
 
#Calculate mean and Standard deviation.
mean = np.mean[x]
sd = np.std[x]
 
#Apply function to the data.
pdf = normal_dist[x,mean,sd]
 
#Plotting the Results
plt.plot[x,pdf , color = 'red']
plt.xlabel['Data points']
plt.ylabel['Probability Density']
3
# Importing required libraries
 
import numpy as np
import matplotlib.pyplot as plt
 
# Creating a series of data of in range of 1-50.
x = np.linspace[1,50,200]
 
#Creating a Function.
def normal_dist[x , mean , sd]:
    prob_density = [np.pi*sd] * np.exp[-0.5*[[x-mean]/sd]**2]
    return prob_density
 
#Calculate mean and Standard deviation.
mean = np.mean[x]
sd = np.std[x]
 
#Apply function to the data.
pdf = normal_dist[x,mean,sd]
 
#Plotting the Results
plt.plot[x,pdf , color = 'red']
plt.xlabel['Data points']
plt.ylabel['Probability Density']
4
# Importing required libraries
 
import numpy as np
import matplotlib.pyplot as plt
 
# Creating a series of data of in range of 1-50.
x = np.linspace[1,50,200]
 
#Creating a Function.
def normal_dist[x , mean , sd]:
    prob_density = [np.pi*sd] * np.exp[-0.5*[[x-mean]/sd]**2]
    return prob_density
 
#Calculate mean and Standard deviation.
mean = np.mean[x]
sd = np.std[x]
 
#Apply function to the data.
pdf = normal_dist[x,mean,sd]
 
#Plotting the Results
plt.plot[x,pdf , color = 'red']
plt.xlabel['Data points']
plt.ylabel['Probability Density']
5
# Importing required libraries
 
import numpy as np
import matplotlib.pyplot as plt
 
# Creating a series of data of in range of 1-50.
x = np.linspace[1,50,200]
 
#Creating a Function.
def normal_dist[x , mean , sd]:
    prob_density = [np.pi*sd] * np.exp[-0.5*[[x-mean]/sd]**2]
    return prob_density
 
#Calculate mean and Standard deviation.
mean = np.mean[x]
sd = np.std[x]
 
#Apply function to the data.
pdf = normal_dist[x,mean,sd]
 
#Plotting the Results
plt.plot[x,pdf , color = 'red']
plt.xlabel['Data points']
plt.ylabel['Probability Density']
6
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#
# Create a standard normal distribution with mean as 0 and standard deviation as 1
#
mu = 0
std = 1
snd = stats.norm[mu, std]
#
# Generate 100 random values between -5, 5
#
x = np.linspace[-5, 5, 100]
#
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
#
plt.figure[figsize=[7.5,7.5]]
plt.plot[x, snd.pdf[x]]
plt.xlim[-5, 5]
plt.title['Normal Distribution', fontsize='15']
plt.xlabel['Values of Random Variable X', fontsize='15']
plt.ylabel['Probability', fontsize='15']
plt.show[]
6

Có rất nhiều điều để hiểu, nhưng tôi khuyến khích tất cả hãy tiếp tục thực hành khái niệm cơ bản này cùng với việc triển khai bằng python

Chủ Đề