หลายครั้งที่เราใช้วิธีการที่เรียกว่าการปรับข้อมูลให้เรียบเพื่อให้ข้อมูลเหมาะสมและมีคุณภาพสำหรับการวิเคราะห์ทางสถิติ ในระหว่างกระบวนการสูบบุหรี่ เรากำหนดช่วงที่เรียกว่า bin และค่าข้อมูลใดๆ ภายในช่วงนั้นถูกสร้างขึ้นเพื่อให้พอดีกับถังขยะ นี้เรียกว่าวิธีการ binning ด้านล่างนี้เป็นตัวอย่างของ binning จากนั้นเราจะมาดูกันว่าเราจะใช้วิธีการ binning สำเร็จโดยใช้โปรแกรม Python ได้อย่างไร
ตัวอย่างการโยน
มาดูชุดตัวเลขกัน ค้นหาค่าสูงสุดและต่ำสุด ตัดสินใจเลือกจำนวนถังขยะที่เราต้องการ โดยขึ้นอยู่กับจำนวนข้อมูลที่การวิเคราะห์ต้องการ สร้างกลุ่มเหล่านี้และกำหนดตัวเลขแต่ละหมายเลขให้กับกลุ่มนี้ ไม่รวมค่าบนและอยู่ในกลุ่มถัดไป
ตัวอย่าง
Given numbers: 12, 32, 10, 17, 19, 28, 22, 26, 29,16 Number of groups : 4 Here Max Value: 32 Min Value: 10 So the groups are – (10-15), (15-21), (21-27), (27-32)
ผลลัพธ์
เมื่อใส่ตัวเลขลงในถังขยะ เราจะได้ผลลัพธ์ดังต่อไปนี้ -
12 -> (10-15) 32 -> (27-32) 10 -> (10-15) 17 -> (15-21) 19 -> (15-21) 28 -> (27-32) 22 -> (21-27) 26 -> (21-27) 29 -> (27-32) 16 -> (15-21)
โปรแกรม Binning
สำหรับโปรแกรมนี้ เรากำหนดสองฟังก์ชัน หนึ่งสำหรับสร้างถังขยะโดยกำหนดขอบเขตบนและล่าง อีกหน้าที่หนึ่งคือการกำหนดค่าอินพุตให้กับแต่ละถัง แต่ละถังยังได้รับดัชนี เราจะเห็นว่าค่าอินพุตแต่ละค่าถูกกำหนดให้กับถังขยะอย่างไร และติดตามว่ามีกี่ค่าที่ส่งไปยังถังขยะเฉพาะ
ตัวอย่าง
from collections import Counter
def Binning_method(lower_bound, width, quantity):
binning = []
for low in range(lower_bound, lower_bound + quantity * width + 1, width):
binning.append((low, low + width))
return binning
def bin_assign(v, b):
for i in range(0, len(b)):
if b[i][0] <= v < b[i][1]:
return i
the_bins = Binning_method(lower_bound=50,
width=4,
quantity=10)
print("The Bins: \n",the_bins)
weights_of_objects = [89.2, 57.2, 63.4, 84.6, 90.2, 60.3,88.7, 65.2, 79.8, 80.2, 93.5, 79.3,72.5, 59.2, 77.2, 67.0, 88.2, 73.5]
print("\nBinned Values:\n")
binned_weight = []
for val in weights_of_objects:
index = bin_assign(val, the_bins)
#print(val, index, binning[index])
print(val,"-with index-", index,":", the_bins[index])
binned_weight.append(index)
freq = Counter(binned_weight)
print("\nCount of values in each index: ")
print(freq) ผลลัพธ์
การเรียกใช้โค้ดข้างต้นทำให้เราได้ผลลัพธ์ดังต่อไปนี้ -
The Bins:
[(50, 54), (54, 58), (58, 62), (62, 66), (66, 70), (70, 74), (74, 78), (78, 82), (82, 86), (86, 90), (90, 94)]
Binned Values:
89.2 -with index- 9 : (86, 90)
57.2 -with index- 1 : (54, 58)
63.4 -with index- 3 : (62, 66)
84.6 -with index- 8 : (82, 86)
90.2 -with index- 10 : (90, 94)
60.3 -with index- 2 : (58, 62)
88.7 -with index- 9 : (86, 90)
65.2 -with index- 3 : (62, 66)
79.8 -with index- 7 : (78, 82)
80.2 -with index- 7 : (78, 82)
93.5 -with index- 10 : (90, 94)
79.3 -with index- 7 : (78, 82)
72.5 -with index- 5 : (70, 74)
59.2 -with index- 2 : (58, 62)
77.2 -with index- 6 : (74, 78)
67.0 -with index- 4 : (66, 70)
88.2 -with index- 9 : (86, 90)
73.5 -with index- 5 : (70, 74)
Count of values in each index:
Counter({9: 3, 7: 3, 3: 2, 10: 2, 2: 2, 5: 2, 1: 1, 8: 1, 6: 1, 4: 1})