14

CSP 202006-1线性分类器python实现

 3 years ago
source link: http://www.cnblogs.com/pxsong/p/13582877.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

思路

这题问题是对于这一群点和一条直线,我们也不知道直线上方的是A类还是直线下方的是A类。其实对于这个二分类问题,我们也没必要知道。 我们只需要判断直线每一测的点是不是一类(A类或B类)就可以了。

至于如何判断这一侧的点是不是一类,用一个set就可以了:把这一侧的点的自身类别(A或B)全都扔进一个set,如果是同一类,那么set的大小肯定是1。

进而,如果直线两侧对应的set大小全都是1,那么就二分类成功了。

代码

m, n = [int(i) for i in input().split()]

dict = {}
for i in range(m):
    line = input().split()
    x, y, s = line
    x, y = int(x), int(y)
    dict[(x, y)] = s

for i in range(n):
    aimsA = set()
    aimsB = set()
    line = input().split()
    a, b, c = [int (i) for i in line]
    for k, v in dict.items():
        x, y = k[0], k[1]
        if  a + b * x + c * y > 0:
            aimsA.add(v)
        else:
            aimsB.add(v)
    if len(aimsA) == 1 and len(aimsB) == 1:
        print("Yes")
    else:
        print("No")

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK