<jupyter_start><jupyter_text>口袋算法<jupyter_code>def sign(a):
    if a>0:
        return 1
    else:
        return -1
def checkErrorRate(X_train,Y_train,w,c):
    count=0
    for i in range(len(X_train)):
        x=np.array(X_train[i])
        y=Y_train[i]
        if sign(np.sum(w*x)+c)!=sign(y):
            count+=1
    return count/len(X_train)
def Pocket():
    w = np.zeros(2)
    lr = 0.01
    c = 1
    cnt = 0
    best_w = w
    best_c = c
    bestRate = 1
    while True:
        cnt += 1
        if cnt > 1000:
            break
        for i in range(len(X_train)):
            x = np.array(X_train[i])
            y = Y_train[i]

            s = np.sum(w*x)+c
            z = sign(s)
            if y * z <= 0:
                w = w+lr*y*x
                c = c+lr*y
                rate = checkErrorRate(X_train,Y_train,w,c)
#                 print(rate)
                if rate < bestRate:
                    bestRate = rate
                    print("bestRate update",rate)
                    best_w = w
                    best_c = c
    return cnt,best_w,best_c
if __name__=='__main__':
    cnt,best_w,best_c=Pocket()
    print("迭代次数：",cnt)
    print("超平面法向量：",w)
    print("阈值：",c)
    count = 0
    for i in range(len(X_test)):
        target = Y_test[i]
        result = sign(np.sum(best_w*X_test[i])+best_c)
        if(target == result):
            count += 1
    accuracy = float(count/len(X_test))
    for i in range(len(X_test)):
        if Y_test[i] == 1:
            plt.scatter(X_test[i][0],X_test[i][1],c='r',marker='o')
        else:
            plt.scatter(X_test[i][0],X_test[i][1],c='b',marker='x')
    X = np.random.uniform(0.2,0.8,100)
    Y = -(best_w[0]*X+best_c)/best_w[1]
    plt.xlabel('potential')
    plt.ylabel('age')
    plt.plot(X,Y,'g')
    plt.show()
    print("测试集准确率：",accuracy)
for i in range(len(data)):
    if data[i][-1] == 1:
        plt.scatter(data_feature[i][0],data_feature[i][1],c='r',marker='o')
    else:
        plt.scatter(data_feature[i][0],data_feature[i][1],c='b',marker='x')
X = np.random.uniform(0.2,0.8,100)
Y = -(best_w[0]*X+best_c)/best_w[1]
plt.xlabel('potential')
plt.ylabel('age')
plt.plot(X,Y,'g')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17155756207674944, 'fraction_numerical': 0.022121896162528215, 'mean_word_length': 2.006784260515604, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '40503', 'license': 'MIT', 'n_tokens_mistral': 929, 'n_tokens_neox': 897, 'n_words': 142, 'path': '3-24homework/feeling.ipynb', 'repo_name': 'ZZh2333/NeuralNetworkAndDeepLearning', 'size': '2215'}