def DataRepresent(dataSet, trainingLabel): trainingFeatures = np.zeros((1, 252)) # Vectorization CurrentGuy = 0 for data in dataSet: vectorFeatures = np.zeros((data.shape[1], 1)) # 9 axis for x in data: vectorFeatures = np.insert(vectorFeatures, vectorFeatures.shape[1], Vectorize(x), axis=1) # Envelope for idx in range(9): tmp = [] for i in range(4): tmp.extend(dataSet[i][idx].tolist()) envelopeResult = np.array(envelope(np.array(trainingLabel[idx*len(tmp):(idx+1)*len(tmp)]), tmp, dataSet[CurrentGuy][idx], 1)) vectorFeatures = np.insert(vectorFeatures, vectorFeatures.shape[1], envelopeResult.T, axis=1) vectorFeatures = np.delete(vectorFeatures, 0, axis=1) trainingFeatures = np.insert(trainingFeatures, trainingFeatures.shape[0], vectorFeatures, 0) CurrentGuy += 1 trainingFeatures = np.delete(trainingFeatures, 0, axis=0) return trainingFeatures
def DataRepresent(trainingData, trainingLabel, rawdata_test, scaleRange, scaleMin): # Preprocessing temp_ind=rawdata_test[:,0] temp_ind = temp_ind.reshape((len(temp_ind),1)) rawdata_test = np.hstack((temp_ind, dynamicRangeCheck(rawdata_test[:,1:7]))) [axis1, axis2, axis3, axis4, axis5, axis6] = Preprocessing(rawdata_test, maxLen=192, n=5) testingData = np.array([axis1, axis2, axis3, axis4, axis5, axis6]) testingFeature = np.zeros((testingData.shape[1], 1)) # Vectorization for x in testingData: testingFeature = np.insert(testingFeature, testingFeature.shape[1], Vectorize(x), axis=1) testingFeature = np.delete(testingFeature, 0, axis=1) # Envelope for idx in range(len(trainingData[0])): tmp = [] for i in range(len(trainingData)): tmp.extend(trainingData[i][idx].tolist()) envelopeResult = np.array(envelope(np.array(trainingLabel[idx*len(tmp):(idx+1)*len(tmp)]), tmp, testingData[idx].tolist(), 1)) testingFeature = np.insert(testingFeature, testingFeature.shape[1], envelopeResult.T, axis=1) # Max-min Normalize testingFeature = (testingFeature-scaleMin)/scaleRange return testingFeature
def DataRepresent(dataPool, trainingLabel, rawdata, scaleRange, scaleMin): # Preprocessing [axis1, _, axis3, axis4, axis5, axis6, press1, press2, press3, press4] = Preprocessing(rawdata, maxLen=192, n=5) testingData = np.array([axis1, axis3, axis4, axis5, axis6, press1, press2, press3, press4]) testingFeature = np.zeros((testingData.shape[1], 1)) # Vectorization for x in testingData: testingFeature = np.insert(testingFeature, testingFeature.shape[1], Vectorize(x), axis=1) testingFeature = np.delete(testingFeature, 0, axis=1) # Envelope for idx in range(9): tmp = [] for i in range(4): tmp.extend(dataPool[i][idx].tolist()) envelopeResult = np.array(envelope(np.array(trainingLabel[idx*len(tmp):(idx+1)*len(tmp)]), tmp, testingData[idx].tolist(), 1)) testingFeature = np.insert(testingFeature, testingFeature.shape[1], envelopeResult.T, axis=1) # Max-min Normalize testingFeature = (testingFeature-scaleMin)/scaleRange return testingFeature
def TrainingModel(dataPool, trainingLabel): labels = [] # The flag of current person i = 0 # Create a feature for each one class features = np.zeros((1, 72)) print features.shape for data in dataPool: # Use to concat all axies data tmp = np.zeros((data.shape[1], 1)) # Create an envelope features for idx in range(6): training_sample = [] map(lambda i: training_sample.extend(dataPool[i][idx].tolist()), xrange(4)) # We have 4 labels, so we will get a vector which size is 12 for each axis envelopeResult = np.array(envelope(np.array(trainingLabel[idx*len(training_sample):(idx+1)*len(training_sample)]), training_sample, dataPool[i][idx], 1)) tmp = np.insert(tmp, tmp.shape[1], envelopeResult.T, axis=1) tmp = np.delete(tmp, 0, axis=1) print tmp.shape features = np.insert(features, features.shape[0], tmp, axis=0) # Create testing lable labels.extend([i for _ in xrange(tmp.shape[0])]) i = i+1 # Remove the first row of zeros features = np.delete(features, 0, axis=0) print np.array(features).shape, np.array(labels).shape model = Training(features, labels, c=10**-5) Evalidation(features, np.array(labels)) ''' # For saving the features for i in range(4): writeInFile(testingData[rangeOfData[i]:rangeOfData[i+1]], i) ''' print model print "Finish" return model
def DataRepresent(dataSet, trainingLabel): trainingFeatures = np.zeros((1, 252)) # Vectorization CurrentGuy = 0 for data in dataSet: vectorFeatures = np.zeros((data.shape[1], 1)) # 9 axis for x in data: vectorFeatures = np.insert(vectorFeatures, vectorFeatures.shape[1], Vectorize(x), axis=1) # Envelope for idx in range(9): tmp = [] for i in range(4): tmp.extend(dataSet[i][idx].tolist()) envelopeResult = np.array( envelope( np.array(trainingLabel[idx * len(tmp):(idx + 1) * len(tmp)]), tmp, dataSet[CurrentGuy][idx], 1)) vectorFeatures = np.insert(vectorFeatures, vectorFeatures.shape[1], envelopeResult.T, axis=1) vectorFeatures = np.delete(vectorFeatures, 0, axis=1) trainingFeatures = np.insert(trainingFeatures, trainingFeatures.shape[0], vectorFeatures, 0) CurrentGuy += 1 trainingFeatures = np.delete(trainingFeatures, 0, axis=0) return trainingFeatures
def DataRepresent(dataPool, trainingLabel, rawdata): # Preprocessing temp_ind=rawdata[:,0] temp_ind = temp_ind.reshape((len(temp_ind),1)) result = np.hstack((temp_ind, dynamicRangeCheck(rawdata[:,1:7]))) [axis1, axis2, axis3, axis4, axis5, axis6] = Preprocessing(result, maxLen=250, n=5) testingData = np.array([axis1, axis2, axis3, axis4, axis5, axis6]) #print testingData testingFeature = np.zeros((testingData.shape[1], 1)) # Envelope for idx in range(6): training_sample = [] map(lambda i: training_sample.extend(dataPool[i][idx].tolist()), xrange(4)) envelopeResult = np.array(envelope(np.array(trainingLabel[idx*len(training_sample):(idx+1)*len(training_sample)]), training_sample, testingData[idx].tolist(), 1)) testingFeature = np.insert(testingFeature, testingFeature.shape[1], envelopeResult.T, axis=1) print testingFeature testingFeature = np.delete(testingFeature, 0, axis=1) return testingFeature
def TrainingModel(trainingData, trainingLabel): #params = [[0.003200000, 0.000759375000], [0.0256000000, 0.0194619506835938], [0.000800000, 0.0656840835571289], [0.025600000, 0.0656840835571289]] #params =[[0.00320000000000000, 0.00864975585937500], [0.025600000000000000, 0.019461950683593], [0.000800000000000000, 0.05], [0.0512000000000000, 0.0656840835571289]] #params = [[0.0128000000000000, 0.00576650390625000], [0.000800000000000000, 0.0437893890380859], [0.000200000000000000, 0.0656840835571289], [0.0128000000000000, 0.000225000000000000]] #params = [[0.000200000000000000, 0.5], [0.0128000000000000, 0.5], [0.00320000000000000, 0.5], [0.00640000000000000,0.5]] #params = [[0.000200000000000000, 0.0229746337890625], [0.0128000000000000, 0.000637500000000000], [0.00320000000000000, 0.00113906250000000], [0.00640000000000000,0.00170859375000000]] #params = [[0.000200000000000000, 0.0001], [0.0128000000000000, 0.0001], [0.00320000000000000, 0.000113906250000000], [0.00640000000000000,0.000170859375000000]] #params = [[0.1868, 0.0225], [0.1868, 0.0759], [0.0830, 0.0506], [0.0830,0.01]] params = [[0.1868, 0.0506], [0.1245, 0.0759], [0.0554, 0.0759], [0.0830,0.0759]] features = [] labels = [] rangeOfData = [0] modelPool = [] p_pool = [] p_table = [] LogRegPool = [] # The flag of current person currentGuy = 0 #print_flag = 0 for data in trainingData: # Vectorization vectorFeature = np.zeros((data.shape[1], 1)) # Combine vectorize features of all attribute. (Dimension = n * 153) for x in data: #vectorized features #if print_flag==0: # print Vectorize(x) # print_flag=1 vectorFeature = np.insert(vectorFeature, vectorFeature.shape[1], Vectorize(x), axis=1) vectorFeature = np.delete(vectorFeature, 0, axis=1) # Envelope for idx in range(len(trainingData[0])): # for each axis tmp = [] for i in range(len(trainingData)): # for each label tmp.extend(trainingData[i][idx].tolist()) envelopeResult = np.array(envelope(np.array(trainingLabel[idx*len(tmp):(idx+1)*len(tmp)]), tmp, trainingData[currentGuy][idx], 1)) vectorFeature = np.insert(vectorFeature, vectorFeature.shape[1], envelopeResult.T, axis=1) if len(features)==0: features=np.zeros((1, vectorFeature.shape[1])) features = np.insert(features, features.shape[0], vectorFeature, 0) # Create testing lable labels.extend([currentGuy for _ in xrange(vectorFeature.shape[0])]) currentGuy +=1 rangeOfData.append(rangeOfData[len(rangeOfData)-1] + data.shape[1]) features = np.delete(features, 0, axis=0) #for i in range(4): # writeInFile(features[rangeOfData[i]:rangeOfData[i+1]], i) # Max-Min Normalize scaleRange = np.abs(np.max(features, 0) - np.min(features, 0)) scaleMin = np.min(features, 0) # Max and Min is 0, avoiding to divide by zero scaleRange[scaleRange == 0] = 1 features = (features - scaleMin)/scaleRange for i in range(len(trainingData)): # OverSampling label = np.array([0 for _ in range(features.shape[0])]) label[rangeOfData[i]:rangeOfData[i+1]] = 1 # sample = OverSampling(np.insert(features, features.shape[1], label, axis=1)) sample=features[rangeOfData[i]:rangeOfData[i+1]] # get predict values of training data model, p_val, p_vals = Training(sample, label, features, params[i]) # Logistic Regression LogReg = LogisticRegression(C=1e-3) LogReg.fit(np.array(p_vals), label) p_pool.append(p_val) #largest predict values p_table.append(p_vals) #historical predict values modelPool.append(model) LogRegPool.append(LogReg) print "Finish" return modelPool, p_pool, p_table, features, labels, scaleRange, scaleMin, rangeOfData, LogRegPool
def TrainingModel(dataPool, trainingLabel): #params = [[0.003200000, 0.000759375000], [0.0256000000, 0.0194619506835938], [0.000800000, 0.0656840835571289], [0.025600000, 0.0656840835571289]] #params =[[0.00320000000000000, 0.00864975585937500], [0.025600000000000000, 0.019461950683593], [0.000800000000000000, 0.05], [0.0512000000000000, 0.0656840835571289]] #params = [[0.0128000000000000, 0.00576650390625000], [0.000800000000000000, 0.0437893890380859], [0.000200000000000000, 0.0656840835571289], [0.0128000000000000, 0.000225000000000000]] params = [[0.000200000000000000, 0.0229746337890625], [0.0128000000000000, 0.000637500000000000], [0.00320000000000000, 0.00113906250000000], [0.00640000000000000,0.00170859375000000]] testingData = np.zeros((1, 252)) testingLabel = [] rangeOfData = [0] modelPool = [] p_pool = [] p_table = [] LogRegPool = [] # The flag of current person currentGuy = 0 for data in dataPool: # Vectorization vectorFeature = np.zeros((data.shape[1], 1)) # Combine vectorize features of all attribute. (Dimension = n * 153) for x in data: vectorFeature = np.insert(vectorFeature, vectorFeature.shape[1], Vectorize(x), axis=1) vectorFeature = np.delete(vectorFeature, 0, axis=1) # Envelope for idx in range(9): tmp = [] for i in range(4): tmp.extend(dataPool[i][idx].tolist()) envelopeResult = np.array(envelope(np.array(trainingLabel[idx*len(tmp):(idx+1)*len(tmp)]), tmp, dataPool[currentGuy][idx], 1)) vectorFeature = np.insert(vectorFeature, vectorFeature.shape[1], envelopeResult.T, axis=1) # Create testing lable testingLabel.extend([currentGuy for _ in range(40)]) testingData = np.insert(testingData, testingData.shape[0], vectorFeature, 0) currentGuy +=1 rangeOfData.append(rangeOfData[len(rangeOfData)-1] + data.shape[1]) testingData = np.delete(testingData, 0, axis=0) #for i in range(4): # writeInFile(testingData[rangeOfData[i]:rangeOfData[i+1]], i) # Max-Min Normalize scaleRange = np.abs(np.max(testingData, 0) - np.min(testingData, 0)) scaleMin = np.min(testingData, 0) # Max and Min is 0, avoiding to divide by zero scaleRange[scaleRange == 0] = 1 testingData = (testingData - scaleMin)/scaleRange for i in range(len(dataPool)): # OverSampling label = np.array([0 for _ in range(testingData.shape[0])]) label[rangeOfData[i]:rangeOfData[i+1]] = 1 sample = OverSampling(np.insert(testingData, testingData.shape[1], label, axis=1)) model, p_val, p_vals = Training(testingData[rangeOfData[i]:rangeOfData[i+1]], sample, params[i]) # Logistic Regression LogReg = LogisticRegression(C=1e3) LogReg.fit(np.array(p_vals), sample[:, -1]) p_pool.append(p_val) p_table.append(p_vals) modelPool.append(model) LogRegPool.append(LogReg) print "Finish" return modelPool, p_pool, p_table, testingData, testingLabel, scaleRange, scaleMin, rangeOfData, LogRegPool