-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify_points.py
59 lines (43 loc) · 1.69 KB
/
classify_points.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Finding out to what cluster the point belongs.
import sys
import argparse
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report, confusion_matrix
#--------------FUNCTIONS START ------------------------------------------------
#returns tuple (ids, data) for further usage
def readDataAndIDs(filePath):
reader = pd.read_csv(filePath, names=names)
ids = reader.iloc[:, 0].values
data = reader.iloc[:, 1:].values
return [data, ids]
#--------------FUNCTIONS END ------------------------------------------------
parser = argparse.ArgumentParser()
parser.add_argument("inFile", help="csv file to read unclassified points from")
parser.add_argument("medoidFile", help="csv file to read medoid data from")
#TODO add euclidean vs cosine difference
args = parser.parse_args()
pInFile = args.inFile
pMedoidFile = args.medoidFile
# Assign colum names to the dataset
names = ['cluster', 'x', 'y', 'z']
# Read dataset to pandas dataframe
medoids = readDataAndIDs(pMedoidFile) #x_train, y_train
unclassified = readDataAndIDs(pInFile) #x_test, y_test
x_train = medoids[0]
y_train = medoids[1]
x_test = unclassified[0]
y_test = unclassified[1]
scaler = StandardScaler() #TODO read how this works
scaler.fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
classifier = KNeighborsClassifier(n_neighbors=5)
classifier.fit(x_train, y_train)
y_pred = classifier.predict(x_test)
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))