How to Implement a Binary Classifier in Python


In this example, we will learn how to classify a list of input values into two groups with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

# Define a dictionary
dic = {"group-1":[],
       "group-2":[]}
num=int(input('Please input the num of values: '))

# Input a list of values from terminal
List=[]
for i in range(num):
    print('Input value-%d:'%(i+1), end = " ")
    tmp = int(input())
    List.append(tmp)

# Classification
for i in List:
    if i>= 66:
        dic["group-1"].append(i)
    else:
        dic["group-2"].append(i)

print("\nClassification results:")
print(dic)

Output:

Please input the number of values: 10
Input value-1: 1
Input value-2: 56
Input value-3: 77
Input value-4: 89
Input value-5: 23
Input value-6: 57
Input value-7: 78
Input value-8: 101
Input value-9: 98
Input value-10: 10

Classification results:
{'group-1': [77, 89, 78, 101, 98], 'group-2': [1, 56, 23, 57, 10]}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments