示例#1
0
    y = np.exp(-0.5*np.diag(np.dot(x, np.dot(S, x.T))))

    return y*(2*np.pi)**(-d/2.0)/(np.sqrt(np.prod(v))+1e-6)

if __name__ == '__main__':
    # with open('points_ring.pkl', 'r') as f:
    with open('points_normal.pkl', 'r') as f:
        class_1 = pickle.load(f)
        class_2 = pickle.load(f)
        labels = pickle.load(f)
    bc = BayesClassifier()
    bc.train([class_1, class_2], [1, -1])

    with open('points_normal_test.pkl', 'r') as f:
    # with open('points_ring_test.pkl', 'r') as f:
        class_1 = pickle.load(f)
        class_2 = pickle.load(f)
        labels = pickle.load(f)
    print bc.classify(class_2[:10])[0]

    def classify(x, y, bc=bc):
        points = np.vstack((x, y))
        return bc.classify(points.T)[0]

    img_tools.plot_2D_boundary([-6, 6, -6, 6], [class_1, class_2], classify, [1, -1])





示例#2
0
    class_1 = pickle.load(f)
    class_2 = pickle.load(f)
    labels = pickle.load(f)

class_1 = map(list, class_1)
class_2 = map(list, class_2)
labels = list(labels)
samples = class_1 + class_2
prob = svm.svm_problem(labels, samples)
param = svm.svm_parameter('-t 2')

m = svm.svm_train(prob, param)
res = svm.svm_predict(labels, samples, m)

with open('points_ring_test.pkl', 'r') as f:
    class_1 = pickle.load(f)
    class_2 = pickle.load(f)
    labels = pickle.load(f)

class_1 = map(list, class_1)
class_2 = map(list, class_2)


def predict(x, y, model=m):
    result = zip(x, y)
    result = map(list, result)
    return np.array(svm.svm_predict([0] * len(x), result, model)[0])


img_tools.plot_2D_boundary(
    [-6, 6, -6, 6], [np.array(class_1), np.array(class_2)], predict, [-1, 1])
示例#3
0
    angle = 2 * np.pi * np.random.randn(n, 1)
    testt = r * np.cos(angle)
    class_2 = np.hstack((r * np.cos(angle), r * np.sin(angle)))
    labels = np.hstack((np.ones(n), -1 * np.ones(n)))
    with open('points_ring.pkl', 'w') as f:
        pickle.dump(class_1, f)
        pickle.dump(class_2, f)
        pickle.dump(labels, f)


if __name__ == '__main__':
    # generate_data()
    # with open('points_normal.pkl', 'r') as f:
    with open('points_ring.pkl', 'r') as f:
        class_1 = pickle.load(f)
        class_2 = pickle.load(f)
        labels = pickle.load(f)
    model = KnnClassifier(labels, np.vstack((class_1, class_2)))

    # with open('points_normal_test.pkl', 'r') as f:
    with open('points_ring_test.pkl', 'r') as f:
        class_1 = pickle.load(f)
        class_2 = pickle.load(f)
        labels = pickle.load(f)
    print model.classify(class_1[0])

    def classify(x, y, models=model):
        return np.array([models.classify([xx, yy]) for (xx, yy) in zip(x, y)])

    plot_2D_boundary([-6, 6, -6, 6], [class_1, class_2], classify, [1, -1])
示例#4
0
with open('points_ring.pkl', 'r') as f:
    class_1 = pickle.load(f)
    class_2 = pickle.load(f)
    labels = pickle.load(f)

class_1 = map(list, class_1)
class_2 = map(list, class_2)
labels = list(labels)
samples = class_1+class_2
prob = svm.svm_problem(labels, samples)
param = svm.svm_parameter('-t 2')

m = svm.svm_train(prob, param)
res = svm.svm_predict(labels, samples, m)

with open('points_ring_test.pkl', 'r') as f:
    class_1 = pickle.load(f)
    class_2 = pickle.load(f)
    labels = pickle.load(f)

class_1 = map(list, class_1)
class_2 = map(list, class_2)

def predict(x, y, model=m):
    result = zip(x, y)
    result = map(list, result)
    return np.array(svm.svm_predict([0]*len(x), result, model)[0])

img_tools.plot_2D_boundary([-6, 6, -6, 6], [np.array(class_1), np.array(class_2)], predict, [-1, 1])
示例#5
0
    angle = 2 * np.pi * np.random.randn(n, 1)
    testt = r * np.cos(angle)
    class_2 = np.hstack((r * np.cos(angle), r * np.sin(angle)))
    labels = np.hstack((np.ones(n), -1 * np.ones(n)))
    with open("points_ring.pkl", "w") as f:
        pickle.dump(class_1, f)
        pickle.dump(class_2, f)
        pickle.dump(labels, f)


if __name__ == "__main__":
    # generate_data()
    # with open('points_normal.pkl', 'r') as f:
    with open("points_ring.pkl", "r") as f:
        class_1 = pickle.load(f)
        class_2 = pickle.load(f)
        labels = pickle.load(f)
    model = KnnClassifier(labels, np.vstack((class_1, class_2)))

    # with open('points_normal_test.pkl', 'r') as f:
    with open("points_ring_test.pkl", "r") as f:
        class_1 = pickle.load(f)
        class_2 = pickle.load(f)
        labels = pickle.load(f)
    print model.classify(class_1[0])

    def classify(x, y, models=model):
        return np.array([models.classify([xx, yy]) for (xx, yy) in zip(x, y)])

    plot_2D_boundary([-6, 6, -6, 6], [class_1, class_2], classify, [1, -1])