Hướng dẫn remove outliers iqr python - loại bỏ các ngoại lệ iqr python

Bộ dữ liệu ban đầu.

print[df.head[]]

   Col0  Col1  Col2  Col3  Col4  User_id
0    49    31    93    53    39       44
1    69    13    84    58    24       47
2    41    71     2    43    58       64
3    35    56    69    55    36       67
4    64    24    12    18    99       67

Đầu tiên xóa cột User_id

filt_df = df.loc[:, df.columns != 'User_id']

Sau đó, phần trăm tính toán.

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05

Các giá trị lọc tiếp theo dựa trên phần trăm được tính toán. Để làm điều đó, tôi sử dụng apply theo các cột và đó là nó!

filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]

Đưa User_id trở lại.

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]

Cuối cùng, các hàng có giá trị

filt_df = df.loc[:, df.columns != 'User_id']
1 có thể được bỏ đơn giản như thế này.

filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40

Kiểm tra kết quả

print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000

Cách tạo bộ dữ liệu kiểm tra

np.random.seed[0]
nb_sample = 100
num_sample = [0,100]

d = dict[]
d['User_id'] = np.random.randint[num_sample[0], num_sample[1], nb_sample]
for i in range[5]:
    d['Col' + str[i]] = np.random.randint[num_sample[0], num_sample[1], nb_sample]

df = DataFrame.from_dict[d]

new_df = df [liling_entries].

Một ngoại lệ là một mục/đối tượng dữ liệu/đối tượng lệch đáng kể so với phần còn lại của các đối tượng [được gọi là bình thường]. Chúng có thể được gây ra bởi các lỗi đo lường hoặc thực hiện. Phân tích để phát hiện ngoại lệ được gọi là khai thác ngoại lệ. Có nhiều cách để phát hiện các ngoại lệ và quy trình loại bỏ là khung dữ liệu giống như xóa mục dữ liệu khỏi khung dữ liệu Panda.Show

  • Nội phân Chính show
  • Phát hiện các ngoại lệ
  • 1. Trực quan hóa
  • Loại bỏ các ngoại lệ
  • low = .05
    high = .95
    quant_df = filt_df.quantile[[low, high]]
    print[quant_df]
    
           Col0   Col1  Col2   Col3   Col4
    0.05   2.00   3.00   6.9   3.95   4.00
    0.95  95.05  89.05  93.0  94.00  97.05
    
    44
    low = .05
    high = .95
    quant_df = filt_df.quantile[[low, high]]
    print[quant_df]
    
           Col0   Col1  Col2   Col3   Col4
    0.05   2.00   3.00   6.9   3.95   4.00
    0.95  95.05  89.05  93.0  94.00  97.05
    
    39
    low = .05
    high = .95
    quant_df = filt_df.quantile[[low, high]]
    print[quant_df]
    
           Col0   Col1  Col2   Col3   Col4
    0.05   2.00   3.00   6.9   3.95   4.00
    0.95  95.05  89.05  93.0  94.00  97.05
    
    40
    low = .05
    high = .95
    quant_df = filt_df.quantile[[low, high]]
    print[quant_df]
    
           Col0   Col1  Col2   Col3   Col4
    0.05   2.00   3.00   6.9   3.95   4.00
    0.95  95.05  89.05  93.0  94.00  97.05
    
    3
    low = .05
    high = .95
    quant_df = filt_df.quantile[[low, high]]
    print[quant_df]
    
           Col0   Col1  Col2   Col3   Col4
    0.05   2.00   3.00   6.9   3.95   4.00
    0.95  95.05  89.05  93.0  94.00  97.05
    
    42
    print[filt_df.head[]]
    
       User_id  Col0  Col1  Col2  Col3  Col4
    0       44    49    31   NaN    53    39
    1       47    69    13    84    58    24
    2       64    41    71   NaN    43    58
    3       67    35    56    69    55    36
    4       67    64    24    12    18   NaN
    
    print[filt_df.describe[]]
    
              User_id       Col0       Col1       Col2       Col3       Col4
    count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
    mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
    std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
    min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
    25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
    50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
    75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
    max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
    
    2
  • Loại bỏ các ngoại lệ tại chỗ = true được sử dụng để nói với Python để thực hiện thay đổi cần thiết trong bộ dữ liệu gốc. Row_index chỉ có thể là một giá trị hoặc danh sách các giá trị hoặc mảng numpy nhưng nó phải là một chiều. Mã đầy đủ: Phát hiện các ngoại lệ bằng IQR và xóa chúng.
  • Một cách khác, chúng ta có thể loại bỏ các ngoại lệ là tính toán ranh giới trên và ranh giới dưới bằng cách lấy 3 độ lệch chuẩn so với giá trị trung bình của các giá trị [giả sử dữ liệu thường được phân phối/Gaussian].

Làm thế nào để bạn loại bỏ các ngoại lệ trong gấu trúc?

Dataset:

Làm thế nào để loại bỏ các ngoại lệ khỏi khung dữ liệu gấu trúc trong Python.

Python3

filt_df = df.loc[:, df.columns != 'User_id']
2
filt_df = df.loc[:, df.columns != 'User_id']
3

filt_df = df.loc[:, df.columns != 'User_id']
4
filt_df = df.loc[:, df.columns != 'User_id']
5
filt_df = df.loc[:, df.columns != 'User_id']
2
filt_df = df.loc[:, df.columns != 'User_id']
7

filt_df = df.loc[:, df.columns != 'User_id']
2
filt_df = df.loc[:, df.columns != 'User_id']
9

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
2
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
4

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
2
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
4

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
5
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
7

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
8
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
0

filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
1
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
3

filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
4

 Output: Output: 

apply9

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
01
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8
filt_df = df.loc[:, df.columns != 'User_id']
03
filt_df = df.loc[:, df.columns != 'User_id']
044

Nội phân Chính show

Phát hiện các ngoại lệ

Phát hiện các ngoại lệ

1. Trực quan hóaExample 1: Using Box Plot

3. IQR [Phạm vi tứ phân Inter]

Python3

Ở đây, khung dữ liệu PANDAS được sử dụng cho một cách tiếp cận thực tế hơn vì trong dự án trong thế giới thực cần phát hiện các ngoại lệ khơi dậy trong bước phân tích dữ liệu, cách tiếp cận tương tự có thể được sử dụng trên danh sách và các đối tượng loại chuỗi.

filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
7
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
9

Output::

Bộ dữ liệu được sử dụng là bộ dữ liệu nhà ở Boston vì nó được tải sẵn trong thư viện Sklearn.

filt_df = df.loc[:, df.columns != 'User_id']
2
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
1

Python3

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
0
filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
1
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8
filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
3
filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
4
filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
5

Output:

Chỉ số ngoại lệ

Ví dụ 2: Sử dụng Scatterplot.ScatterPlot.

Nó được sử dụng khi bạn đã ghép nối dữ liệu số hoặc khi biến phụ thuộc của bạn có nhiều giá trị cho mỗi biến độc lập đọc hoặc khi cố gắng xác định mối quan hệ giữa hai biến. Trong quá trình sử dụng biểu đồ phân tán, người ta cũng có thể sử dụng nó để phát hiện ngoại lệ.

Để vẽ sơ đồ biểu đồ phân tán, người ta yêu cầu hai biến có liên quan đến nhau. Vì vậy, ở đây, tỷ lệ các mẫu kinh doanh phi bán lẻ trên mỗi thị trấn và tỷ lệ thuế tài sản có giá trị đầy đủ trên 10.000 đô la được sử dụng có tên cột là tên của Indus Indus và và thuế.

Python3

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
6
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
8
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
0
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
1
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
22.

filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
5
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
6
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
7
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
8
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
9

print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
0
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
1
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
2

print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
3
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
4
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
2

print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
6

Output::

Cốt truyện phân tán

Nhìn vào biểu đồ có thể tóm tắt rằng hầu hết các điểm dữ liệu nằm ở góc dưới bên trái của biểu đồ nhưng có một vài điểm chính xác, đối diện đó là góc trên bên phải của biểu đồ. Những điểm ở góc trên cùng bên phải có thể được coi là ngoại lệ.

Sử dụng xấp xỉ có thể nói tất cả những điểm dữ liệu là x> 20 và y> 600 là ngoại lệ. Mã sau đây có thể lấy vị trí chính xác của tất cả những điểm thỏa mãn các điều kiện này. & NBSP;

Python3

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
0
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
8
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
6
filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
3
np.random.seed[0]
nb_sample = 100
num_sample = [0,100]

d = dict[]
d['User_id'] = np.random.randint[num_sample[0], num_sample[1], nb_sample]
for i in range[5]:
    d['Col' + str[i]] = np.random.randint[num_sample[0], num_sample[1], nb_sample]

df = DataFrame.from_dict[d]
1
np.random.seed[0]
nb_sample = 100
num_sample = [0,100]

d = dict[]
d['User_id'] = np.random.randint[num_sample[0], num_sample[1], nb_sample]
for i in range[5]:
    d['Col' + str[i]] = np.random.randint[num_sample[0], num_sample[1], nb_sample]

df = DataFrame.from_dict[d]
2
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
8____43
np.random.seed[0]
nb_sample = 100
num_sample = [0,100]

d = dict[]
d['User_id'] = np.random.randint[num_sample[0], num_sample[1], nb_sample]
for i in range[5]:
    d['Col' + str[i]] = np.random.randint[num_sample[0], num_sample[1], nb_sample]

df = DataFrame.from_dict[d]
5557777771

Output::

Chỉ số ngoại lệ

2. Điểm Z.

Z- điểm cũng được gọi là điểm tiêu chuẩn. Giá trị/điểm số này giúp hiểu rằng điểm dữ liệu từ giá trị trung bình là bao xa. Và sau khi thiết lập giá trị ngưỡng, người ta có thể sử dụng các giá trị điểm Z của các điểm dữ liệu để xác định các ngoại lệ.

ZScore = [data_point -mean] / std. sai lệch

Python3

filt_df = df.loc[:, df.columns != 'User_id']
4
np.random.seed[0]
nb_sample = 100
num_sample = [0,100]

d = dict[]
d['User_id'] = np.random.randint[num_sample[0], num_sample[1], nb_sample]
for i in range[5]:
    d['Col' + str[i]] = np.random.randint[num_sample[0], num_sample[1], nb_sample]

df = DataFrame.from_dict[d]
8
filt_df = df.loc[:, df.columns != 'User_id']
2 User_id0

filt_df = df.loc[:, df.columns != 'User_id']
2 User_id2

User_id3

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3 User_id5User_id6User_id7
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8User_id9

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
0apply1

Output::

một phần của danh sách [z]

Đầu ra trên chỉ là một ảnh chụp nhanh của một phần dữ liệu; Độ dài thực tế của danh sách [z] là 506 là số lượng hàng. Nó in các giá trị điểm z của từng mục dữ liệu của cột

Bây giờ để xác định giá trị ngưỡng ngoại lệ được chọn là 3.0. Vì 99,7% các điểm dữ liệu nằm giữa độ lệch chuẩn +/- 3 [sử dụng phương pháp phân phối Gaussian].

Python3

apply2

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3 apply4

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
0apply6apply4
filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
5

Output:

Chỉ số ngoại lệ

3. IQR [Phạm vi tứ phân Inter]

IQR [Phạm vi tứ phân tứ hợp] Phương pháp tiếp cận phạm vi tứ tấu để tìm ra các ngoại lệ là cách tiếp cận được sử dụng phổ biến nhất và đáng tin cậy nhất được sử dụng trong lĩnh vực nghiên cứu.

IQR = Quartile3 - Quartile1

Python3

apply9

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
01
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8
filt_df = df.loc[:, df.columns != 'User_id']
03
filt_df = df.loc[:, df.columns != 'User_id']
044

filt_df = df.loc[:, df.columns != 'User_id']
06
filt_df = df.loc[:, df.columns != 'User_id']
07
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
09
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
2

filt_df = df.loc[:, df.columns != 'User_id']
11
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
01
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8
filt_df = df.loc[:, df.columns != 'User_id']
03
filt_df = df.loc[:, df.columns != 'User_id']
16
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
2

filt_df = df.loc[:, df.columns != 'User_id']
06
filt_df = df.loc[:, df.columns != 'User_id']
07
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
09
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
2

filt_df = df.loc[:, df.columns != 'User_id']
11
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
01
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8
filt_df = df.loc[:, df.columns != 'User_id']
03
filt_df = df.loc[:, df.columns != 'User_id']
16
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
2

Output:

filt_df = df.loc[:, df.columns != 'User_id']
23
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
11
filt_df = df.loc[:, df.columns != 'User_id']
26
filt_df = df.loc[:, df.columns != 'User_id']
27

Để xác định giá trị cơ sở ngoại lệ được xác định ở trên và bên dưới các bộ dữ liệu phạm vi bình thường là giới hạn trên và dưới, xác định giới hạn trên và giới hạn dưới [giá trị 1.5*IQR được xem xét]:

trên = q3 +1.5*iqr

Hạ = Q1 - 1.5*IQR

Python3

Trong công thức trên như theo thống kê, việc tăng 0,5 của IQR [new_iqr = IQR + 0,5*IQR] được thực hiện, để xem xét tất cả dữ liệu giữa độ lệch chuẩn 2.7 trong phân phối Gaussian.

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
0
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
0
filt_df = df.loc[:, df.columns != 'User_id']
41
filt_df = df.loc[:, df.columns != 'User_id']
42

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
0
filt_df = df.loc[:, df.columns != 'User_id']
44

Các

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
0
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
0
filt_df = df.loc[:, df.columns != 'User_id']
58
filt_df = df.loc[:, df.columns != 'User_id']
59

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
0
filt_df = df.loc[:, df.columns != 'User_id']
61

Output:

Các

Giới hạn được xác định và chỉ mục ngoại lệ tương ứng với giới hạn

Loại bỏ các ngoại lệ

Để loại bỏ ngoại lệ, người ta phải tuân theo cùng một quá trình xóa một mục từ bộ dữ liệu bằng cách sử dụng vị trí chính xác của nó trong tập dữ liệu vì trong tất cả các phương thức phát hiện kết quả cuối cùng của Outliers là danh sách tất cả các mục dữ liệu đáp ứng định nghĩa ngoại lệ Theo phương pháp được sử dụng.

Tài liệu tham khảo: Làm thế nào để xóa chính xác một hàng trong Python?

dataFrame.drop [row_index, inplace = true
 

Example:

df_boston.drop [danh sách [0], inplace = true]

Mã đầy đủ: Phát hiện các ngoại lệ bằng IQR và xóa chúng.: Detecting the outliers using IQR and removing them.

Python3

filt_df = df.loc[:, df.columns != 'User_id']
2
filt_df = df.loc[:, df.columns != 'User_id']
3

filt_df = df.loc[:, df.columns != 'User_id']
4
filt_df = df.loc[:, df.columns != 'User_id']
5
filt_df = df.loc[:, df.columns != 'User_id']
2
filt_df = df.loc[:, df.columns != 'User_id']
7

filt_df = df.loc[:, df.columns != 'User_id']
2
filt_df = df.loc[:, df.columns != 'User_id']
9

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
2
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
4

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
5
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
7

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
8
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
0

filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
1
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
3

filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
4

apply9

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
01
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8
filt_df = df.loc[:, df.columns != 'User_id']
03
filt_df = df.loc[:, df.columns != 'User_id']
044

filt_df = df.loc[:, df.columns != 'User_id']
06
filt_df = df.loc[:, df.columns != 'User_id']
07
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
09
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
2

filt_df = df.loc[:, df.columns != 'User_id']
11
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
01
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8
filt_df = df.loc[:, df.columns != 'User_id']
03
filt_df = df.loc[:, df.columns != 'User_id']
16
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
2

filt_df = df.loc[:, df.columns != 'User_id']
06
filt_df = df.loc[:, df.columns != 'User_id']
07
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
09
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
2

filt_df = df.loc[:, df.columns != 'User_id']
11
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
01
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8
filt_df = df.loc[:, df.columns != 'User_id']
03
filt_df = df.loc[:, df.columns != 'User_id']
16
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
2

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
0
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
0
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
14
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
15

filt_df = df.loc[:, df.columns != 'User_id']
23
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
filt_df = df.loc[:, df.columns != 'User_id']
11
filt_df = df.loc[:, df.columns != 'User_id']
26
filt_df = df.loc[:, df.columns != 'User_id']
27

Các

filt_df = df.loc[:, df.columns != 'User_id']
45
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
18
filt_df = filt_df.apply[lambda x: x[[x>quant_df.loc[low,x.name]] & 
                                    [x < quant_df.loc[high,x.name]]], axis=0]
8__

low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
38
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
39
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
40
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
42
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
2

filt_df = pd.concat[[df.loc[:,'User_id'], filt_df], axis=1]
0
filt_df.dropna[inplace=True]
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
1       47    69    13    84    58    24
3       67    35    56    69    55    36
5        9    95    79    44    45    69
6       83    69    41    66    87     6
9       87    50    54    39    53    40
0
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
52
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
15

Output::


low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
44
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
39
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
40
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
3
low = .05
high = .95
quant_df = filt_df.quantile[[low, high]]
print[quant_df]

       Col0   Col1  Col2   Col3   Col4
0.05   2.00   3.00   6.9   3.95   4.00
0.95  95.05  89.05  93.0  94.00  97.05
42
print[filt_df.head[]]

   User_id  Col0  Col1  Col2  Col3  Col4
0       44    49    31   NaN    53    39
1       47    69    13    84    58    24
2       64    41    71   NaN    43    58
3       67    35    56    69    55    36
4       67    64    24    12    18   NaN

print[filt_df.describe[]]

          User_id       Col0       Col1       Col2       Col3       Col4
count  100.000000  89.000000  88.000000  88.000000  89.000000  89.000000
mean    48.230000  49.573034  45.659091  52.727273  47.460674  57.157303
std     28.372292  25.672274  23.537149  26.509477  25.823728  26.231876
min      0.000000   3.000000   5.000000   7.000000   4.000000   5.000000
25%     23.000000  29.000000  29.000000  29.500000  24.000000  36.000000
50%     47.000000  50.000000  40.500000  52.500000  49.000000  59.000000
75%     74.250000  69.000000  67.000000  75.000000  70.000000  79.000000
max     99.000000  95.000000  89.000000  92.000000  91.000000  97.000000
2

Làm thế nào để bạn xác định và loại bỏ các ngoại lệ trong Python?Inplace =True is used to tell python to make the required change in the original dataset. row_index can be only one value or list of values or NumPy array but it must be one dimensional. Full Code: Detecting the outliers using IQR and removing them.

Loại bỏ các ngoại lệ tại chỗ = true được sử dụng để nói với Python để thực hiện thay đổi cần thiết trong bộ dữ liệu gốc. Row_index chỉ có thể là một giá trị hoặc danh sách các giá trị hoặc mảng numpy nhưng nó phải là một chiều. Mã đầy đủ: Phát hiện các ngoại lệ bằng IQR và xóa chúng.

Làm thế nào để bạn thoát khỏi các ngoại lệ?calculating upper boundary and lower boundary by taking 3 standard deviation from the mean of the values [assuming the data is Normally/Gaussian distributed].

Một cách khác, chúng ta có thể loại bỏ các ngoại lệ là tính toán ranh giới trên và ranh giới dưới bằng cách lấy 3 độ lệch chuẩn so với giá trị trung bình của các giá trị [giả sử dữ liệu thường được phân phối/Gaussian].

Làm thế nào để bạn loại bỏ các ngoại lệ trong gấu trúc?.

print[df].

Làm thế nào để loại bỏ các ngoại lệ khỏi khung dữ liệu gấu trúc trong Python.

z_scores = số liệu thống kê. Zscore [DF] tính toán điểm z của `df`.

abs_z_scores = np. abs [z_scores].

Đã lọc_entries = [abs_z_scores

Bài Viết Liên Quan

Chủ Đề