def Adaboost_onNonDynamicData():
    #Parsing Full training dataset
    XFull = common.parseFile('../UCI HAR Dataset/train/X_train.txt')
    YFull = common.parseFile('../UCI HAR Dataset/train/y_train.txt')

    #Parsing Full testing dataset
    XFullTest = common.parseFile('../UCI HAR Dataset/test/X_test.txt')
    YFullTest = common.parseFile('../UCI HAR Dataset/test/y_test.txt')

    #Getting the dataset associated with Non-Dynamic Activities on training
    X_NonDynamic, Y_NonDynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                      [4, 5, 6])
    #Getting the dataset associated with Non-Dynamic Activities on testing
    X_NonDynamicTest, Y_NonDynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [4, 5, 6])

    #Fitting data using Adaboost classifier
    for i in [50, 100, 200, 300, 500]:
        clf = ensemble.AdaBoostClassifier(n_estimators=i)
        clf.fit(X_NonDynamic, Y_NonDynamic.flatten())

        precision, recall, fscore = common.checkAccuracy(
            clf.predict(X_NonDynamicTest), Y_NonDynamicTest, [4, 5, 6])
        print("For the NonDynamic dataset with n_estimators = ", i)
        common.createConfusionMatrix(
            clf.predict(X_NonDynamicTest).flatten(),
            Y_NonDynamicTest.flatten(), [4, 5, 6])
        print(fscore)

    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3])
    print(len(X_DynamicTest), len(Y_DynamicTest))

    #Fitting data using Adaboost classifier
    clf = ensemble.AdaBoostClassifier(n_estimators=300)
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3])
    common.createConfusionMatrix(
        clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
        [1, 2, 3])

    print(fscore)
Ejemplo n.º 2
0
def QDA_onNonDynamicData():
    #Parsing Full training dataset
    XFull = common.parseFile('../UCI HAR Dataset/train/X_train.txt')
    YFull = common.parseFile('../UCI HAR Dataset/train/y_train.txt')

    #Parsing Full testing dataset
    XFullTest = common.parseFile('../UCI HAR Dataset/test/X_test.txt')
    YFullTest = common.parseFile('../UCI HAR Dataset/test/y_test.txt')

    #Getting the dataset associated with Non-Dynamic Activities on training
    X_NonDynamic, Y_NonDynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                      [4, 5, 6])
    #Getting the dataset associated with Non-Dynamic Activities on testing
    X_NonDynamicTest, Y_NonDynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [4, 5, 6])

    #Fitting data using QDA classifier

    clf = QDA()
    clf.fit(X_NonDynamic, Y_NonDynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_NonDynamicTest), Y_NonDynamicTest, [4, 5, 6])
    common.createConfusionMatrix(
        clf.predict(X_NonDynamicTest).flatten(), Y_NonDynamicTest.flatten(),
        [4, 5, 6])
    print fscore

    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3])
    print len(X_DynamicTest), len(Y_DynamicTest)

    #Fitting data using QDA classifier
    clf = QDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3])
    common.createConfusionMatrix(
        clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
        [1, 2, 3])

    print fscore
Ejemplo n.º 3
0
def Adaboost_onNonDynamicData():
    #Parsing Full training dataset
    XFull = common.parseFile('../UCI HAR Dataset/train/X_train.txt')
    YFull = common.parseFile('../UCI HAR Dataset/train/y_train.txt')

    #Parsing Full testing dataset
    XFullTest = common.parseFile('../UCI HAR Dataset/test/X_test.txt')
    YFullTest = common.parseFile('../UCI HAR Dataset/test/y_test.txt')

    #Getting the dataset associated with Non-Dynamic Activities on training 
    X_NonDynamic,Y_NonDynamic = common.getDataSubset(XFull,YFull.flatten(),[4,5,6])
    #Getting the dataset associated with Non-Dynamic Activities on testing
    X_NonDynamicTest,Y_NonDynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[4,5,6])

    #Fitting data using Adaboost classifier
    for i in [50,100,200,300,500]:
        clf = ensemble.AdaBoostClassifier(n_estimators = i)
        clf.fit(X_NonDynamic, Y_NonDynamic.flatten())

        precision,recall,fscore = common.checkAccuracy(clf.predict(X_NonDynamicTest),Y_NonDynamicTest,[4,5,6])
        print "For the NonDynamic dataset with n_estimators = ",i
        common.createConfusionMatrix(clf.predict(X_NonDynamicTest).flatten(),Y_NonDynamicTest.flatten(),[4,5,6])
        print fscore

    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3])
    print len(X_DynamicTest),len(Y_DynamicTest)

    #Fitting data using Adaboost classifier
    clf = ensemble.AdaBoostClassifier(n_estimators = 300)
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3])
    common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3])

    print fscore
Ejemplo n.º 4
0
def MLP_onNonDynamicData():
    #Parsing Full training dataset
    XFull = common.parseFile('../UCI HAR Dataset/train/X_train.txt')
    YFull = common.parseFile('../UCI HAR Dataset/train/y_train.txt')

    #Parsing Full testing dataset
    XFullTest = common.parseFile('../UCI HAR Dataset/test/X_test.txt')
    YFullTest = common.parseFile('../UCI HAR Dataset/test/y_test.txt')

    #Getting the dataset associated with Non-Dynamic Activities on training 
    X_NonDynamic,Y_NonDynamic = common.getDataSubset(XFull,YFull.flatten(),[4,5,6])
    #Getting the dataset associated with Non-Dynamic Activities on testing
    X_NonDynamicTest,Y_NonDynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[4,5,6])

    #Fitting data using MLP classifier

    clf = MLPClassifier()
    clf.fit(X_NonDynamic, Y_NonDynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_NonDynamicTest),Y_NonDynamicTest,[4,5,6])
    common.createConfusionMatrix(clf.predict(X_NonDynamicTest).flatten(),Y_NonDynamicTest.flatten(),[4,5,6])
    print fscore

    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3])
    print len(X_DynamicTest),len(Y_DynamicTest)

    #Fitting data using MLP classifier
    clf = MLPClassifier()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3])
    common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3])

    print fscore
Ejemplo n.º 5
0
# 每一行原数据用上他们的幂次形成2倍大小的特征矩阵
X_train = common.getPowerK(X_train, [1, 2])

mean_array = []
cov_array = []
label_array = []

for i in trainSubjects:
    for j in requiredLabels:

        # Get subject info
        X_train_new, Y_train_new, subjectInfo = common.getSubjectData(
            X_train, Y_train, [i])

        # Get Data Subset #
        X_train_new, Y_train_new = common.getDataSubset(
            X_train, Y_train, requiredLabels)

        mean, cov = common.getDistribution(X_train_new, Y_train_new, j)
        mean_array.append(mean)
        cov_array.append(cov)
        label_array.append(j)

print("Done")

print("Pre_processing Training Data")
# 每一行原数据加上他们的欧氏距离,特征矩阵  +len(trainSubjects)*len(requiredLabels)
X_train, Y_train = common.getDataSubset(X_train, Y_train, requiredLabels)

featureArray = []
trainSubjects = [
    1, 3, 5, 6, 7, 8, 11, 14, 15, 16, 17, 19, 21, 22, 23, 25, 26, 27, 28, 29,
# Parse the files

X_train=common.parseFile('X_train.txt')				 
Y_train=(common.parseFile('Y_train.txt'))	
Y_train = Y_train.flatten()

X_test=common.parseFile('X_test.txt')			
Y_test=(common.parseFile('Y_test.txt'))
Y_test = Y_test.flatten()	

#######################
# Pre-processing of data

print "Pre_processing"

X_test , Y_test = common.getDataSubset(X_test, Y_test, [4,5])

X_train = common.getPowerK( X_train, [1,2])  
X_test  = common.getPowerK( X_test, [1,2])  

print "Done"

#######################

# These hold the information about the clusters for a SPECIFIC ACTIVITY of a SPECIFIC PERSON #
# TRAINING HERE#

print "Training"

trainSubjects = [1,3,5,6,7,8,11,14,15,16,17,19,21,22,23,25,26,27,28,29,30]
requiredLabels = [4,5]
Ejemplo n.º 7
0
def LDA_onGyroData():
    XFull = common.parseFile('../UCI HAR Dataset/train/X_train.txt')
    YFull = common.parseFile('../UCI HAR Dataset/train/y_train.txt')

    XFull = common.getGyroFeatures(XFull)

    #Parsing Full testing dataset
    XFullTest = common.parseFile('../UCI HAR Dataset/test/X_test.txt')
    YFullTest = common.parseFile('../UCI HAR Dataset/test/y_test.txt')

    XFullTest = common.getGyroFeatures(XFullTest)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3, 4, 5, 6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3, 4, 5, 6])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3, 4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3, 4, 5, 6]))

    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3, 4, 5, 6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3, 4, 5, 6])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3, 4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3, 4, 5, 6]))

    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3, 4, 5, 6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3, 4, 5, 6])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2, 3])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2, 3])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3, 4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3, 4, 5, 6]))

    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3])
    print(len(X_DynamicTest), len(Y_DynamicTest))

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3]))
    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3]))
    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2, 3])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2, 3])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3]))
    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Non-Dynamic Activities on training
    X_NonDynamic, Y_NonDynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                      [4, 5, 6])
    #Getting the dataset associated with Non-Dynamic Activities on testing
    X_NonDynamicTest, Y_NonDynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [4, 5, 6])

    #Fitting data using LDA classifier

    clf = LDA()
    clf.fit(X_NonDynamic, Y_NonDynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_NonDynamicTest), Y_NonDynamicTest, [4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_NonDynamicTest).flatten(),
            Y_NonDynamicTest.flatten(), [4, 5, 6]))
    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [4, 5, 6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [4, 5, 6])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [4, 5, 6]))
    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [4, 5, 6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [4, 5, 6])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2, 3])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2, 3])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [4, 5, 6]))
    print(fscore)
Ejemplo n.º 8
0
set_printoptions(suppress=True)

filename = '../UCI HAR Dataset/'  # Dataset Used (Change as per your computer's path)
#--------------------------------------------------------------------------------------------#

X_train = common.parseFile(filename + 'train/X_train.txt')  # Read X Train
Y_train = (common.parseFile(filename + 'train/y_train.txt')
           ).flatten()  # Read Y Train and flatten it to 1D array
X_test = common.parseFile(filename + 'test/X_test.txt')  # Read X Test
Y_test = (common.parseFile(filename + 'test/y_test.txt')
          ).flatten()  # Read Y test and flatten it to 1D array

print len(X_train), len(Y_train)  # Printing Lengths of Train and Test Data
print len(X_test), len(Y_test)

X_dynamic_train, Y_dynamic_train = common.getDataSubset(
    X_train, Y_train, [1, 2, 3])  # Get Train sub data for [1,2,3]
X_nondynamic_train, Y_nondynamic_train = common.getDataSubset(
    X_train, Y_train, [4, 5, 6])  # Get Train sub data for [4,5,6]

X_dynamic_test, Y_dynamic_test = common.getDataSubset(
    X_test, Y_test, [1, 2, 3])  # Get Test sub data for [1,2,3]
X_nondynamic_test, Y_nondynamic_test = common.getDataSubset(
    X_test, Y_test, [4, 5, 6])  # Get Test sub data for [4,5,6]

X_nondynamic_train = common.getPowerK(X_nondynamic_train,
                                      [1, 2])  # Convert X Train to X+X^2
X_nondynamic_test = common.getPowerK(X_nondynamic_test,
                                     [1, 2])  # Convert X Test to X+X^2
#X_nondynamic_train_6, Y_nondynamic_train_6=common.getDataSubset(X_train, Y_train, [6]) # Used earlier to get Sub data for just 6th label
#X_nondynamic_test_6, Y_nondynamic_test_6=common.getDataSubset(X_test, Y_test, [6])
#Y_nondynamic_train_sublabels=common.convertLabel(Y_nondynamic_train, [4,5], [6])	# Used earlier to convert [4,5] to [1] and [6] to [0]
Ejemplo n.º 9
0
def LDA_onGyroData():
    XFull = common.parseFile('../UCI HAR Dataset/train/X_train.txt')
    YFull = common.parseFile('../UCI HAR Dataset/train/y_train.txt')

    XFull = common.getGyroFeatures(XFull)

    #Parsing Full testing dataset
    XFullTest = common.parseFile('../UCI HAR Dataset/test/X_test.txt')
    YFullTest = common.parseFile('../UCI HAR Dataset/test/y_test.txt')

    XFullTest = common.getGyroFeatures(XFullTest)
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3,4,5,6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3,4,5,6])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3,4,5,6])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3,4,5,6])

    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3,4,5,6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3,4,5,6])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3,4,5,6])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3,4,5,6])

    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3,4,5,6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3,4,5,6])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2,3])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2,3])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3,4,5,6])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3,4,5,6])

    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3])
    print len(X_DynamicTest),len(Y_DynamicTest)

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3])
    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3])
    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2,3])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2,3])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3])
    print fscore
#################################################################################################################################
    #Getting the dataset associated with Non-Dynamic Activities on training 
    X_NonDynamic,Y_NonDynamic = common.getDataSubset(XFull,YFull.flatten(),[4,5,6])
    #Getting the dataset associated with Non-Dynamic Activities on testing
    X_NonDynamicTest,Y_NonDynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[4,5,6])

    #Fitting data using LDA classifier

    clf = LDA()
    clf.fit(X_NonDynamic, Y_NonDynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_NonDynamicTest),Y_NonDynamicTest,[4,5,6])
    print common.createConfusionMatrix(clf.predict(X_NonDynamicTest).flatten(),Y_NonDynamicTest.flatten(),[4,5,6])
    print fscore 
#################################################################################################################################   
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[4,5,6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[4,5,6])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[4,5,6])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[4,5,6])
    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[4,5,6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[4,5,6])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2,3])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2,3])

    #Fitting data using LDA classifier
    clf = LDA()
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[4,5,6])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[4,5,6])
    print fscore
#UCI DATASET2
XFull = common.parseFile('hapt/X_train.txt')
YFull = common.parseFile('hapt/y_train.txt')
#WISDM DATASET
# XFull = common.parseCSVFile('X_train.csv')
# YFull = common.parseCSVFile('y_train.csv')
#Parsing Full testing dataset
# XFullTest = common.parseFile('X_test.txt')
# YFullTest = common.parseFile('y_test.txt')
XFullTest = common.parseFile('hapt/X_test.txt')
YFullTest = common.parseFile('hapt/y_test.txt')
# XFullTest = common.parseCSVFile('X_test.csv')
# YFullTest = common.parseCSVFile('y_test.csv')
#################################################################################################################################
#Getting the dataset associated with Dynamic Activities on training
X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                            [1, 2, 3, 4, 5, 6])
#Getting the dataset associated with Dynamic Activities on testing
X_DynamicTest, Y_DynamicTest = common.getDataSubset(XFullTest,
                                                    YFullTest.flatten(),
                                                    [1, 2, 3, 4, 5, 6])

print("Done")

print("Fitting Data")
clf = neighbors.KNeighborsClassifier(n_neighbors=5, weights='distance')
clf.fit(X_Dynamic, Y_Dynamic.flatten())
accuracy, precision, recall, fscore, fw = common.checkAccuracy(
    clf.predict(X_Dynamic), Y_Dynamic, [1, 2, 3, 4, 5, 6])
print(accuracy)
print(fscore)
print(fw)
def LinearSVC_onData():
    #Parsing Full training dataset
    XFull = common.parseFile('../UCI HAR Dataset/train/X_train.txt')
    YFull = common.parseFile('../UCI HAR Dataset/train/y_train.txt')

    #Parsing Full testing dataset
    XFullTest = common.parseFile('../UCI HAR Dataset/test/X_test.txt')
    YFullTest = common.parseFile('../UCI HAR Dataset/test/y_test.txt')
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3, 4, 5, 6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3, 4, 5, 6])

    #Fitting data using LinearSVC classifier
    clf = LinearSVC(multi_class='crammer_singer')
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(clf.predict(X_Dynamic),
                                                     Y_Dynamic,
                                                     [1, 2, 3, 4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_Dynamic).flatten(), Y_Dynamic.flatten(),
            [1, 2, 3, 4, 5, 6]))

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel="linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(clf.predict(X_Dynamic),
                                                     Y_Dynamic,
                                                     [1, 2, 3, 4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_Dynamic).flatten(), Y_Dynamic.flatten(),
            [1, 2, 3, 4, 5, 6]))

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3, 4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3, 4, 5, 6]))

    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3, 4, 5, 6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3, 4, 5, 6])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel="linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3, 4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3, 4, 5, 6]))

    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3, 4, 5, 6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3, 4, 5, 6])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2, 3])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2, 3])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel="linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3, 4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3, 4, 5, 6]))

    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3])
    print(len(X_DynamicTest), len(Y_DynamicTest))

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel="linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3]))
    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel="linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3]))
    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [1, 2, 3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [1, 2, 3])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2, 3])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2, 3])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel="linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [1, 2, 3])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [1, 2, 3]))
    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Non-Dynamic Activities on training
    X_NonDynamic, Y_NonDynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                      [4, 5, 6])
    #Getting the dataset associated with Non-Dynamic Activities on testing
    X_NonDynamicTest, Y_NonDynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [4, 5, 6])

    #Fitting data using LinearSVC classifier

    clf = SVC(kernel="linear")
    clf.fit(X_NonDynamic, Y_NonDynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_NonDynamicTest), Y_NonDynamicTest, [4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_NonDynamicTest).flatten(),
            Y_NonDynamicTest.flatten(), [4, 5, 6]))
    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [4, 5, 6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [4, 5, 6])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel="linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [4, 5, 6]))
    print(fscore)
    #################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training
    X_Dynamic, Y_Dynamic = common.getDataSubset(XFull, YFull.flatten(),
                                                [4, 5, 6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest, Y_DynamicTest = common.getDataSubset(
        XFullTest, YFullTest.flatten(), [4, 5, 6])

    X_Dynamic = common.getPowerK(X_Dynamic, [1, 2, 3])
    X_DynamicTest = common.getPowerK(X_DynamicTest, [1, 2, 3])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel="linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision, recall, fscore = common.checkAccuracy(
        clf.predict(X_DynamicTest), Y_DynamicTest, [4, 5, 6])
    print(
        common.createConfusionMatrix(
            clf.predict(X_DynamicTest).flatten(), Y_DynamicTest.flatten(),
            [4, 5, 6]))
    print(fscore)
## Author: Hariharan Seshadri ##
## This script tries to distinguish between SITTING,STANDING, and LAYING labels using Decision Trees ##

import numpy as np
from sklearn import tree
import common


print "Parsing"

X_train = common.parseFile( 'X_train.txt')
Y_train = common.parseFile( 'Y_train.txt')
Y_train = Y_train.flatten()
X_train,Y_train = common.getDataSubset(X_train, Y_train, [4,5,6])

X_test = common.parseFile( 'X_test.txt')
Y_test = common.parseFile( 'Y_test.txt')
Y_test= Y_test.flatten()
X_test,Y_test = common.getDataSubset(X_test, Y_test, [4,5,6])

print "Done"

print "Fitting Data"

clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train, Y_train) 

print "Done"


Ejemplo n.º 13
0
def LinearSVC_onData():
    #Parsing Full training dataset
    XFull = common.parseFile('../UCI HAR Dataset/train/X_train.txt')
    YFull = common.parseFile('../UCI HAR Dataset/train/y_train.txt')

    #Parsing Full testing dataset
    XFullTest = common.parseFile('../UCI HAR Dataset/test/X_test.txt')
    YFullTest = common.parseFile('../UCI HAR Dataset/test/y_test.txt')
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3,4,5,6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3,4,5,6])

    #Fitting data using LinearSVC classifier
    clf = LinearSVC(multi_class='crammer_singer')
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_Dynamic),Y_Dynamic,[1,2,3,4,5,6])
    print common.createConfusionMatrix(clf.predict(X_Dynamic).flatten(),Y_Dynamic.flatten(),[1,2,3,4,5,6])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel = "linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())
    
    precision,recall,fscore = common.checkAccuracy(clf.predict(X_Dynamic),Y_Dynamic,[1,2,3,4,5,6])
    print common.createConfusionMatrix(clf.predict(X_Dynamic).flatten(),Y_Dynamic.flatten(),[1,2,3,4,5,6])

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3,4,5,6])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3,4,5,6])

    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3,4,5,6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3,4,5,6])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel = "linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3,4,5,6])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3,4,5,6])

    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3,4,5,6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3,4,5,6])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2,3])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2,3])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel = "linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3,4,5,6])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3,4,5,6])

    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3])
    print len(X_DynamicTest),len(Y_DynamicTest)

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel = "linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3])
    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel = "linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3])
    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[1,2,3])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[1,2,3])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2,3])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2,3])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel = "linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[1,2,3])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[1,2,3])
    print fscore
#################################################################################################################################
    #Getting the dataset associated with Non-Dynamic Activities on training 
    X_NonDynamic,Y_NonDynamic = common.getDataSubset(XFull,YFull.flatten(),[4,5,6])
    #Getting the dataset associated with Non-Dynamic Activities on testing
    X_NonDynamicTest,Y_NonDynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[4,5,6])

    #Fitting data using LinearSVC classifier

    clf = SVC(kernel = "linear")
    clf.fit(X_NonDynamic, Y_NonDynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_NonDynamicTest),Y_NonDynamicTest,[4,5,6])
    print common.createConfusionMatrix(clf.predict(X_NonDynamicTest).flatten(),Y_NonDynamicTest.flatten(),[4,5,6])
    print fscore 
#################################################################################################################################   
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[4,5,6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[4,5,6])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel = "linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[4,5,6])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[4,5,6])
    print fscore
#################################################################################################################################
    #Getting the dataset associated with Dynamic Activities on training 
    X_Dynamic,Y_Dynamic = common.getDataSubset(XFull,YFull.flatten(),[4,5,6])
    #Getting the dataset associated with Dynamic Activities on testing
    X_DynamicTest,Y_DynamicTest = common.getDataSubset(XFullTest,YFullTest.flatten(),[4,5,6])

    X_Dynamic = common.getPowerK(X_Dynamic,[1,2,3])
    X_DynamicTest = common.getPowerK(X_DynamicTest,[1,2,3])

    #Fitting data using LinearSVC classifier
    clf = SVC(kernel = "linear")
    clf.fit(X_Dynamic, Y_Dynamic.flatten())

    precision,recall,fscore = common.checkAccuracy(clf.predict(X_DynamicTest),Y_DynamicTest,[4,5,6])
    print common.createConfusionMatrix(clf.predict(X_DynamicTest).flatten(),Y_DynamicTest.flatten(),[4,5,6])
    print fscore
## Author: Hariharan Seshadri ##
## This script tries to distinguish between SITTING,STANDING, and LAYING labels using Decision Trees ##

import numpy as np
from sklearn import tree
import common

print "Parsing"

X_train = common.parseFile('X_train.txt')
Y_train = common.parseFile('Y_train.txt')
Y_train = Y_train.flatten()
X_train, Y_train = common.getDataSubset(X_train, Y_train, [4, 5, 6])

X_test = common.parseFile('X_test.txt')
Y_test = common.parseFile('Y_test.txt')
Y_test = Y_test.flatten()
X_test, Y_test = common.getDataSubset(X_test, Y_test, [4, 5, 6])

print "Done"

print "Fitting Data"

clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train, Y_train)

print "Done"

print "Predicting"

predicted = []
requiredLabels = [4,5,6]

X_train = common.getPowerK( X_train, [1,2])

mean_array = []
cov_array = []
label_array = []

for i in trainSubjects:
	for j in requiredLabels:

		# Get subject info
		X_train_new , Y_train_new , subjectInfo= common.getSubjectData(X_train,Y_train,[i])

		# Get Data Subset #
		X_train_new , Y_train_new = common.getDataSubset(X_train, Y_train, requiredLabels)

		mean,cov = common.getDistribution(X_train_new,Y_train_new,j)
		mean_array.append(mean)
		cov_array.append(cov)
		label_array.append( j )

print "Done"


print "Pre_processing Training Data"

X_train , Y_train = common.getDataSubset(X_train, Y_train, requiredLabels)

featureArray = []  
trainSubjects = [1,3,5,6,7,8,11,14,15,16,17,19,21,22,23,25,26,27,28,29,30]
# Parse the files

X_train = common.parseFile('X_train.txt')
Y_train = (common.parseFile('Y_train.txt'))
Y_train = Y_train.flatten()

X_test = common.parseFile('X_test.txt')
Y_test = (common.parseFile('Y_test.txt'))
Y_test = Y_test.flatten()

#######################
# Pre-processing of data

print "Pre_processing"

X_test, Y_test = common.getDataSubset(X_test, Y_test, [4, 5])

X_train = common.getPowerK(X_train, [1, 2])
X_test = common.getPowerK(X_test, [1, 2])

print "Done"

#######################

# These hold the information about the clusters for a SPECIFIC ACTIVITY of a SPECIFIC PERSON #
# TRAINING HERE#

print "Training"

trainSubjects = [
    1, 3, 5, 6, 7, 8, 11, 14, 15, 16, 17, 19, 21, 22, 23, 25, 26, 27, 28, 29,
Ejemplo n.º 17
0
from matplotlib import pyplot as plt
# Prints the numbers in float instead of scientific format
set_printoptions(suppress=True)

filename='../UCI HAR Dataset/' 							# Dataset Used (Change as per your computer's path)
#--------------------------------------------------------------------------------------------#

X_train=common.parseFile(filename+'train/X_train.txt')				# Read X Train 
Y_train=(common.parseFile(filename+'train/y_train.txt')).flatten()		# Read Y Train and flatten it to 1D array
X_test=common.parseFile(filename+'test/X_test.txt')				# Read X Test
Y_test=(common.parseFile(filename+'test/y_test.txt')).flatten()			# Read Y test and flatten it to 1D array

print len(X_train), len(Y_train)						# Printing Lengths of Train and Test Data
print len(X_test), len(Y_test)

X_dynamic_train, Y_dynamic_train=common.getDataSubset(X_train, Y_train, [1,2,3])	# Get Train sub data for [1,2,3]
X_nondynamic_train, Y_nondynamic_train=common.getDataSubset(X_train, Y_train, [4,5,6])  # Get Train sub data for [4,5,6]

X_dynamic_test, Y_dynamic_test=common.getDataSubset(X_test, Y_test, [1,2,3])		# Get Test sub data for [1,2,3]
X_nondynamic_test, Y_nondynamic_test=common.getDataSubset(X_test, Y_test, [4,5,6])	# Get Test sub data for [4,5,6]

X_nondynamic_train=common.getPowerK(X_nondynamic_train, [1,2])				# Convert X Train to X+X^2
X_nondynamic_test=common.getPowerK(X_nondynamic_test, [1,2])				# Convert X Test to X+X^2
#X_nondynamic_train_6, Y_nondynamic_train_6=common.getDataSubset(X_train, Y_train, [6]) # Used earlier to get Sub data for just 6th label
#X_nondynamic_test_6, Y_nondynamic_test_6=common.getDataSubset(X_test, Y_test, [6])
#Y_nondynamic_train_sublabels=common.convertLabel(Y_nondynamic_train, [4,5], [6])	# Used earlier to convert [4,5] to [1] and [6] to [0]
#Y_nondynamic_test_sublabels=common.convertLabel(Y_nondynamic_test, [4,5], [6])

print len(X_dynamic_train), len(Y_dynamic_train), Y_dynamic_train		# Printing lenghts and Labels extracted for verification
print len(X_nondynamic_train), len(Y_nondynamic_train), Y_nondynamic_train