K Nearest Neighbour & Propagation Mode Classification
I have been exploring Nearest Neighbours Methods which are commonly adopted for solving classification problems. In the context of Indoor localisation, such methods are employed to identify LoS and NLoS propagation modes. This blog post basically provides a Jupyter Notebook to try this classification on your own using openly available data-set for UWB Localisation. The link for Jupyter notebook is here and the Markdown version is provided in this post.
Notebook Outlining the Usage of KNN for LoS/NLoS classification¶
This Notebook provides a brief introduction to usage of KNN base LoS/NLoS classification. We employ the UWB Localisation Dataset openly available at: http://log-a-tec.eu/uwb-ds.html You should download and extract the dataset in a subfolder name data You also need to remove first version line from all csv files
We utilise two parameters, i.e. RSS of First Path and Channel Impulse response amplitude of first path.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import glob
from IPython.display import display, HTML
path = r'data/office/measurements/1.07_9.37_1.2/' # use your path
all_files = glob.glob(path+"*.csv") # advisable to use os.path.join as this makes concatenation OS independent
fields =['NLOS','FP_POINT1','RSS_FP']
df_from_each_file = (pd.read_csv(f,skipinitialspace=True, usecols=fields) for f in all_files)
df = pd.concat(df_from_each_file, ignore_index=True)
display(df)
Filter the LoS/NLoS data for visualisation
NLoSFilter = df[df['NLOS'] == 'NLOS'];
LoSFilter = df[df['NLOS'] == 'LOS'];
NLoSFilter.head()
LoSFilter.head()
Visualisation of LoS/NLoS clusters
plt.scatter(NLoSFilter['RSS_FP'],NLoSFilter['FP_POINT1'],c='red')
plt.scatter(LoSFilter['RSS_FP'],LoSFilter['FP_POINT1'],c='blue')
plt.xlabel('RSS_FP')
plt.ylabel('FP_POINT1')
plt.show()
clabel = df['NLOS']
X = df[['RSS_FP','FP_POINT1']]
X
Code for Knn similar to Scikit learn blog and stack abuse:https://stackabuse.com/k-nearest-neighbors-algorithm-in-python-and-scikit-learn/
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,clabel, test_size=0.20)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=5)
classifier.fit(X_train, y_train)
plt.scatter(X_train[:,0],X_train[:,1])
y_pred = classifier.predict(X_test)
display(y_test==y_pred)
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))