KNN算法的实现
在数据集准备完成之后 开始进行算法的实现
其中本次项目分为3个部分
数据的获取
其中数据已经保存到了txt文件
直接读取
print('(1) load texts...')
train_texts = open('../dataset_train/x_train.txt', encoding='utf-8').read().split('\n')
train_labels = open('../dataset_train/y_train.txt', encoding='utf-8').read().split('\n')
test_texts = open('../dataset_test/x_test.txt', encoding='utf-8').read().split('\n')
test_labels = open('../dataset_test/y_test.txt', encoding='utf-8').read().split('\n')
all_text = train_texts + test_texts
特征值抽取
特征值的抽取
使用的是词袋和TifIdf算法
其中有一点注意就是对于词袋 词语特征的提取是从总文档的词语中抽取
然后对测试集和训练集分别进行特征抽取
然后得到两个矩阵
然后进行Tfidf算法
count_v0 = CountVectorizer();
counts_all = count_v0.fit_transform(all_text);
count_v1 = CountVectorizer(vocabulary=count_v0.vocabulary_);
counts_train = count_v1.fit_transform(train_texts);
print("the shape of train is " + repr(counts_train.shape))
count_v2 = CountVectorizer(vocabulary=count_v0.vocabulary_);
counts_test = count_v2.fit_transform(test_texts);
print("the shape of test is " + repr(counts_test.shape))
tfidftransformer = TfidfTransformer();
train_data = tfidftransformer.fit(counts_train).transform(counts_train);
test_data = tfidftransformer.fit(counts_test).transform(counts_test);
x_train = train_data
y_train = train_labels
x_test = test_data
y_test = test_labels
KNN算法实现
在特征值抽取之后
利用KNN算法建模
使用训练集进行建模
然后使用测试集进行模型的套用
然后的到结果
然后进行结果的比对
然后得到正确率
# KNN算法建模
for x in range(1, 15):
knnclf = KNeighborsClassifier(n_neighbors=x)
knnclf.fit(x_train, y_train)
preds = knnclf.predict(x_test);
num = 0
preds = preds.tolist()
for i, pred in enumerate(preds):
if int(pred) == int(y_test[i]):
num += 1
print('K= ' + str(x) + ', precision_score:' + str(float(num) / len(preds)))