@@ -0,0 +1,1755 @@
+#!/usr/bin/env python3%0A# -*- coding: utf-8 -*-%0A%0Aimport time%0Aimport pandas as pd%0Afrom sklearn.preprocessing import normalize%0Afrom sklearn.neighbors import KNeighborsClassifier as KNN%0Afrom sklearn.metrics import confusion_matrix%0Afrom sklearn.model_selection import KFold%0Afrom scipy.spatial.distance import cosine as Cos%0A%0Adataset_url = 'http://mlr.cs.umass.edu/ml/machine-learning-databases/' + %5C%0A                'wine-quality/winequality-white.csv'%0Adata = pd.read_csv(dataset_url, sep=';')%0A%0Afeature = normalize(data.iloc%5B:, :-2%5D)%0Atarget = data.iloc%5B:, -1%5D%0A%0Astart = time.time()%0Anbr = KNN(n_neighbors=11, weights='distance', %5C%0A        algorithm='brute', metric='manhattan')%0Anbr.fit(feature, target)%0Apred = nbr.predict(feature)%0Amx = confusion_matrix(target, pred)%0Aprint(mx.trace()/mx.sum())%0Aprint(%22===== used %25s seconds =====%22 %25 (time.time()-start))%0A%0A%0Astart = time.time()%0Akf = KFold(n_splits=12)%0Atotal_rate = 0%0Afor train_index, test_index in kf.split(data):%0A    train_feature, test_feature = feature%5Btrain_index%5D, feature%5Btest_index%5D%0A    train_target, test_target = target%5Btrain_index%5D, target%5Btest_index%5D%0A    nbr = KNN(n_neighbors=11, weights='distance', %5C%0A            algorithm='brute', metric='manhattan')%0A    nbr.fit(train_feature, train_target)%0A    pred = nbr.predict(test_feature)%0A    mx = confusion_matrix(test_target, pred)%0A    total_rate += mx.trace()/mx.sum()%0Aprint(total_rate/12)%0Aprint(%22===== used %25s seconds =====%22 %25 (time.time()-start))%0A%0A%0A# Cos re%0Adef cosDist(a, b):%0A    return Cos(a, b)%0A%0A%0Astart = time.time()%0Anbr = KNN(n_neighbors=11, algorithm='brute',  metric=cosDist)%0Anbr.fit(feature, target)%0Apred = nbr.predict(feature)%0Amx = confusion_matrix(target, pred)%0Aprint(mx.trace()/mx.sum())%0Aprint(%22===== used %25s seconds =====%22 %25 (time.time()-start))%0A

Add ML lab2 vedio version