In [1]:
import pandas as pd
import numpy as np
In [2]:
train = pd.read_csv('RS.txt', names=['ID','Timestep','Front','Rear'])
train.head()
Out[2]:
In [3]:
labels = np.zeros(len(train))
startw = [461, 1795, 3424, 5023, 6733, 8391]
endw = [1502, 2275, 4450, 6074, 7760, 9436]
for s,e in zip(startw,endw):
labels[range(s,e)] = 1
In [4]:
train['Label'] = pd.Series(labels, index=train.index)
In [5]:
import matplotlib.pyplot as plt
%matplotlib inline
In [21]:
plt.figure(figsize=(10,8))
plt.subplot(221)
plt.plot(train.Front)
plt.subplot(222)
plt.plot(train.Rear)
plt.subplot(223)
plt.plot(train.Label)
Out[21]:
In [23]:
plt.scatter(train.Timestep,
train.Front,
marker='.',
linewidth=0,
c=['blue' if x == 0 else 'green' for x in train.Label])
Out[23]:
In [8]:
from sklearn.preprocessing import StandardScaler
In [9]:
training_features = [np.append(abs(np.fft.fft(train.Front[x:x+60]))[0:6], abs(np.fft.fft(train.Rear[x:x+60]))[0:6]) for x in range(0, len(train)-60)]
In [10]:
scaler = StandardScaler()
In [11]:
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=11, weights='uniform')
knn.fit(scaler.fit_transform(training_features), train.Label[60:])
Out[11]:
In [12]:
test = pd.read_csv('RS_test.txt', names=['ID','Timestep','Front','Rear'])
test.head()
Out[12]:
In [13]:
testing_features = [np.append(abs(np.fft.fft(test.Front[x:x+60]))[0:6], abs(np.fft.fft(test.Rear[x:x+60]))[0:6]) for x in range(0, len(test)-60)]
In [14]:
test_prediction = knn.predict(scaler.fit_transform(testing_features))
In [15]:
plt.subplot(221)
plt.plot(test.Front)
plt.subplot(222)
plt.plot(test.Rear)
Out[15]:
In [16]:
test_points = test[60:]
In [17]:
plt.figure(figsize=(12,8))
plt.scatter(test_points.Timestep, test_points.Front, marker='.', linewidth=0, c=['blue' if x == 0 else 'green' for x in test_prediction])
Out[17]: