如何使用Python对相对于行的数据进行分类?线的左边或右边

2024-04-26 11:31:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将数据相对于行进行分类。如果该点位于直线的左侧,则该点具有符号-1。如果该点位于直线的右侧,则其符号为+1

资料

import matplotlib.pyplot as plt
import numpy as np



A=make_moons(n_samples=1000, noise =.05)
B=make_moons(n_samples=1000, noise =.3)
C=make_moons(n_samples=200, noise =.05)
D=make_moons(n_samples=200, noise =.3)

我对集合A分类的尝试

#any line
x = np.linspace(-1,1,100)
y = 2*x

points=[-2,-4,3,6]
left_side=[]
right_side=[]
line_vector=[5,10]
for i in range(0,1000):
    vector=[A[0][i,0]+2,A[0][i,1]+6]
    determinant=np.cross(line_vector,vector)
    if determinant<0:
        left_side.append([A[0][i,0],A[0][i,1]])
    else:
        right_side.append([A[0][i,0],A[0][i,1]])

不幸的是,它并不完全有效。 第一个图显示了分类后的数据。第二个数字是原始数据

输出

enter image description here