0

I'm working on a program that predicts hand movements based on EMG signals. So far, i have a CSV file to be used as a database for the LDA program. The issue that i'm finding is actually being able to predict with the program. Is there a way in which I can predict the finger movement based on the values I get from my serial port (my sensors)?


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
import serial as ser

names = ['Finger', 'Val1', 'Val2', 'Val3']
dataset = pd.read_csv('EmgSig.csv', names=names)

X = dataset.iloc[:, 1:3].values
y = dataset.iloc[:, 0].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

lda = LDA(n_components=1)
X_train = lda.fit_transform(X_train, y_train)
X_test = lda.transform(X_test)

classifier = RandomForestClassifier(max_depth=2, random_state=0)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)

cm = confusion_matrix(y_test, y_pred)
print(cm)
print('Accuracy' + str(accuracy_score(y_test, y_pred)))

while True:
        data = ser.readline()
        decode = (data[0:len(data)-2].decode("utf-8"))
        datasplit = decode.split('-')
        Val1 = int(datasplit[0])
        Val2 = int(datasplit[1])
        Val3 = int(datasplit[2])
Crowned 31
  • 11
  • 3
  • Do you mean that the results in `y_pred` aren't similar to `y_test`? I'd consider changing the 1 in `lda = LDA(n_components=1)` to a higher number... maybe try 3? – userABC123 Aug 29 '19 at 20:10
  • @snd I mean that i need the program to continuously predict whether a finger is opening or closing based on he values that my sensors are giving it. – Crowned 31 Aug 29 '19 at 20:18
  • Ah, sorry I think I understand now. This would really depend on what the output of your sensor is. In everything I've done before, a new file is generated with each new scan. With that, you can use something like this: https://stackoverflow.com/questions/23459095/check-for-file-existence-in-python-3, where `if os.path.exists(file_name):` then run your `classifier.predict()`. But that's really dependent on your sensor output / how you have things organized – userABC123 Aug 29 '19 at 20:43

0 Answers0