Nhiều thử nghiệm thống kê đưa ra giả định rằng các bộ dữ liệu thường được phân phối. Tuy nhiên, điều này thường không phải là trường hợp trong thực tế.
Một cách để giải quyết vấn đề này là chuyển đổi phân phối các giá trị trong bộ dữ liệu bằng cách sử dụng một trong ba phép biến đổi:
1. Chuyển đổi nhật ký: & nbsp; chuyển đổi biến phản hồi từ y thành & nbsp; log [y].Transform the response variable from y to log[y].
2. Biến đổi căn bậc hai: Biến đổi biến phản hồi từ y thành √y.Transform the response variable from y to √y.
3. Biến đổi gốc khối: & NBSP; Biến đổi biến phản hồi từ Y thành Y1/3.Transform the response variable from y to y1/3.
Bằng cách thực hiện các phép biến đổi này, bộ dữ liệu thường được phân phối bình thường hơn.
Các ví dụ sau đây cho thấy cách thực hiện các biến đổi này trong Python.
Chuyển đổi nhật ký trong Python
Mã sau đây cho thấy cách thực hiện chuyển đổi nhật ký trên một biến và tạo các sơ đồ bên cạnh để xem phân phối ban đầu và phân phối chuyển đổi log của dữ liệu:log transformation on a variable and create side-by-side plots to view the original distribution and the log-transformed distribution of the data:
import numpy as np import matplotlib.pyplot as plt #make this example reproducible np.random.seed[0] #create beta distributed random variable with 200 values data = np.random.beta[a=4, b=15, size=300] #create log-transformed data data_log = np.log[data] #define grid of plots fig, axs = plt.subplots[nrows=1, ncols=2] #create histograms axs[0].hist[data, edgecolor='black'] axs[1].hist[data_log, edgecolor='black'] #add title to each histogram axs[0].set_title['Original Data'] axs[1].set_title['Log-Transformed Data']
Lưu ý cách phân phối chuyển đổi log được phân phối bình thường hơn so với phân phối ban đầu.
Nó vẫn không phải là một hình ảnh chuông hoàn hảo, nhưng nó gần với một phân phối bình thường mà phân phối ban đầu.
Biến đổi gốc ở Python
Mã sau đây cho thấy cách thực hiện chuyển đổi căn bậc hai trên một biến và tạo các ô bên cạnh để xem phân phối gốc và phân phối chuyển đổi gốc của dữ liệu:square root transformation on a variable and create side-by-side plots to view the original distribution and the square root transformed distribution of the data:
import numpy as np import matplotlib.pyplot as plt #make this example reproducible np.random.seed[0] #create beta distributed random variable with 200 values data = np.random.beta[a=1, b=5, size=300] #create log-transformed data data_log = np.sqrt[data] #define grid of plots fig, axs = plt.subplots[nrows=1, ncols=2] #create histograms axs[0].hist[data, edgecolor='black'] axs[1].hist[data_log, edgecolor='black'] #add title to each histogram axs[0].set_title['Original Data'] axs[1].set_title['Square Root Transformed Data']
Lưu ý cách dữ liệu biến đổi căn bậc hai được phân phối bình thường hơn nhiều so với dữ liệu gốc.
Biến đổi gốc khối trong Python
Mã sau đây cho thấy cách thực hiện chuyển đổi gốc khối trên một biến và tạo các sơ đồ cạnh nhau để xem phân phối gốc và phân phối chuyển đổi gốc của khối lượng của dữ liệu:cube root transformation on a variable and create side-by-side plots to view the original distribution and the cube root transformed distribution of the data:
import numpy as np import matplotlib.pyplot as plt #make this example reproducible np.random.seed[0] #create beta distributed random variable with 200 values data = np.random.beta[a=1, b=5, size=300] #create log-transformed data data_log = np.cbrt[data] #define grid of plots fig, axs = plt.subplots[nrows=1, ncols=2] #create histograms axs[0].hist[data, edgecolor='black'] axs[1].hist[data_log, edgecolor='black'] #add title to each histogram axs[0].set_title['Original Data'] axs[1].set_title['Cube Root Transformed Data']
Lưu ý cách dữ liệu biến đổi gốc khối được phân phối bình thường hơn nhiều so với dữ liệu gốc.
Tài nguyên bổ sung
Làm thế nào để tính toán điểm z trong Python làm thế nào để bình thường hóa dữ liệu trong Python Giả định về tính quy tắc trong thống kê là gì?
How to Normalize Data in Python
What is the Assumption of Normality in Statistics?