Best Python code snippet using molecule_python
fetch_HMM.py
Source: fetch_HMM.py
1# Fetch the data from Firebase2from firebase import firebase3import math4firebase = firebase.FirebaseApplication('https://androidbletutorial.firebaseio.com/',None)5All_Sample = []6result=firebase.get('',None)7length = 108# Define Partition Function9def Partition(value):10 end_value = pow(2,16)-1 # maximam value11 global length12 num_level = length13 return_value = math.ceil(value*num_level/end_value)14 #print(value," ", return_value);15 return return_value16# Sample value17for key in result.keys():18 temp = result[key]["text"].split(":")[7]19 x1, x2 = temp.split(" ")[1], temp.split(" ")[2]20 y1, y2 = temp.split(" ")[3], temp.split(" ")[4]21 z1, z2 = temp.split(" ")[5], temp.split(" ")[6]22 x = int(x1) + int(x2)<<823 y = int(y1) + int(y2)<<824 z = int(z1) + int(z2)<<825 sample = math.sqrt(x*x+y*y+z*z)26 sample = Partition(int(sample))27 All_Sample.append(sample)28Sample_list = ''.join([str(elem) for elem in All_Sample]) 29print(Sample_list)30# find transition matrix and emission matrix31#defining states and sequence symbols32import numpy as np33import decimal34from decimal import Decimal35import math36import time37#transition matrix38transition = np.array([[0.9,0.1],[0.1,0.9]])39#emission matrix40emission = np.array([[0.05,0.05,0.05,0.05,0.1,0.1,0.1,0.1,0.2,0.2],41 [0.2,0.2,0.1,0.1,0.1,0.1,0.05,0.05,0.05,0.05]])42states = ['H','C']43states_dic = {'H':0, 'C':1}44#sequence_syms = {'1':0,'6600':1,'14000':2,'28000':3,'35000':4,'41000':5,'47000':6,'55000':7,'57000':8,'65000':9}45sequence_syms = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}46sequence = ['0','1','2','3','4','5','6','7','8','9']47#sequence = ['1', '6600', '14000', '28000', '35000', '41000', '47000', '55000', '57000', '65000']48#test sequence49test_sequence = Sample_list50#test_sequence = ['47000','35000','65000','65000','41000','55000','57000','65000','47000','55000']51test_sequence = test_sequence52#probabilities of going to end state53end_probs = [0.1, 0.1]54#probabilities of going from start state55start_probs = [0.5, 0.5]56#function to find forward probabilities57def forward_probs():58 # node values stored during forward algorithm59 node_values_fwd = np.zeros((len(states), len(test_sequence)))60 for i, sequence_val in enumerate(test_sequence):61 for j in range(len(states)):62 # if first sequence value then do this63 if (i == 0):64 node_values_fwd[j, i] = start_probs[j] * emission[j, sequence_syms[sequence_val]]65 # else perform this66 else:67 values = [node_values_fwd[k, i - 1] * emission[j, sequence_syms[sequence_val]] * transition[k, j] for k in68 range(len(states))]69 node_values_fwd[j, i] = sum(values)70 #end state value71 end_state = np.multiply(node_values_fwd[:, -1], end_probs)72 end_state_val = sum(end_state)73 return node_values_fwd, end_state_val74#function to find backward probabilities75def backward_probs():76 # node values stored during forward algorithm77 node_values_bwd = np.zeros((len(states), len(test_sequence)))78 #for i, sequence_val in enumerate(test_sequence):79 for i in range(1,len(test_sequence)+1):80 for j in range(len(states)):81 # if first sequence value then do this82 if (-i == -1):83 node_values_bwd[j, -i] = end_probs[j]84 # else perform this85 else:86 values = [node_values_bwd[k, -i+1] * emission[k, sequence_syms[test_sequence[-i+1]]] * transition[j, k] for k in range(len(states))]87 node_values_bwd[j, -i] = sum(values)88 #start state value89 start_state = [node_values_bwd[m,0] * emission[m, sequence_syms[test_sequence[0]]] for m in range(len(states))]90 start_state = np.multiply(start_state, start_probs)91 start_state_val = sum(start_state)92 return node_values_bwd, start_state_val93#function to find si probabilities94def si_probs(forward, backward, forward_val):95 si_probabilities = np.zeros((len(states), len(test_sequence)-1, len(states)))96 for i in range(len(test_sequence)-1):97 for j in range(len(states)):98 for k in range(len(states)):99 si_probabilities[j,i,k] = ( forward[j,i] * backward[k,i+1] * transition[j,k] * emission[k,sequence_syms[test_sequence[i+1]]] ) \100 / forward_val101 return si_probabilities102#function to find gamma probabilities103def gamma_probs(forward, backward, forward_val):104 gamma_probabilities = np.zeros((len(states), len(test_sequence)))105 for i in range(len(test_sequence)):106 for j in range(len(states)):107 #gamma_probabilities[j,i] = ( forward[j,i] * backward[j,i] * emission[j,sequence_syms[test_sequence[i]]] ) / forward_val108 gamma_probabilities[j, i] = (forward[j, i] * backward[j, i]) / forward_val109 return gamma_probabilities110#performing iterations until convergence111for iteration in range(2000):112 print('\nIteration No: ', iteration + 1)113 # print('\nTransition:\n ', transition)114 # print('\nEmission: \n', emission)115 #Calling probability functions to calculate all probabilities116 fwd_probs, fwd_val = forward_probs()117 bwd_probs, bwd_val = backward_probs()118 si_probabilities = si_probs(fwd_probs, bwd_probs, fwd_val)119 gamma_probabilities = gamma_probs(fwd_probs, bwd_probs, fwd_val)120 # print('Forward Probs:')121 # print(np.matrix(fwd_probs))122 123 # print('Backward Probs:')124 # print(np.matrix(bwd_probs))125 #126 # print('Si Probs:')127 # print(si_probabilities)128 # print('Gamma Probs:')129 # print(np.matrix(gamma_probabilities))130 #caclculating 'a' and 'b' matrices131 a = np.zeros((len(states), len(states)))132 b = np.zeros((len(states), len(sequence_syms)))133 #'a' matrix134 for j in range(len(states)):135 for i in range(len(states)):136 for t in range(len(test_sequence)-1):137 a[j,i] = a[j,i] + si_probabilities[j,t,i]138 denomenator_a = [si_probabilities[j, t_x, i_x] for t_x in range(len(test_sequence) - 1) for i_x in range(len(states))]139 denomenator_a = sum(denomenator_a)140 if (denomenator_a == 0):141 a[j,i] = 0142 else:143 a[j,i] = a[j,i]/denomenator_a144 #'b' matrix145 for j in range(len(states)): #states146 for i in range(len(sequence)): #seq147 indices = [idx for idx, val in enumerate(test_sequence) if val == sequence[i]]148 numerator_b = sum( gamma_probabilities[j,indices] )149 denomenator_b = sum( gamma_probabilities[j,:] )150 if (denomenator_b == 0):151 b[j,i] = 0152 else:153 b[j, i] = numerator_b / denomenator_b154 print('\nMatrix a:\n')155 print(np.matrix(a.round(decimals=4)))156 print('\nMatrix b:\n')157 print(np.matrix(b.round(decimals=4)))158 transition = a159 emission = b160 new_fwd_temp, new_fwd_temp_val = forward_probs()161 print('New forward probability: ', new_fwd_temp_val)162 diff = np.abs(fwd_val - new_fwd_temp_val)163 print('Difference in forward probability: ', diff)164 if (diff < 0.000000001):...
classify_nn.py
Source: classify_nn.py
1import operator2import math3import numpy as np4def distance(A, B):5 sum_ = 06 for i in range(len(A)):7 sum_ += (A[i]-B[i])**28 return sum_9def find_L2(sequence, test_sequence):10 assert(len(sequence.frames) == len(test_sequence.frames))11 dist = 012 for i in range(len(sequence.frames)):13 sframe = sequence.frames[i].frame14 tframe = test_sequence.frames[i].frame15 dist += distance(sequence.frames[i].frame, test_sequence.frames[i].frame)16 return dist17def classify_nn(test_sequence, training_gesture_sets):18 """19 Classify test_sequence using nearest neighbors20 :param test_gesture: Sequence to classify21 :param training_gesture_sets: training set of labeled gestures22 :return: a classification label (an integer between 0 and 8)23 """24 min_dist = float('inf')25 result = None26 for gs in training_gesture_sets:27 total = 028 for seq in gs.sequences:29 l2 = find_L2(seq, test_sequence)30 total += l231 avg = total/len(gs.sequences)32 if avg < min_dist:33 min_dist = avg34 result = gs.label...
Check out the latest blogs from LambdaTest on this topic:
People love to watch, read and interact with quality content — especially video content. Whether it is sports, news, TV shows, or videos captured on smartphones, people crave digital content. The emergence of OTT platforms has already shaped the way people consume content. Viewers can now enjoy their favorite shows whenever they want rather than at pre-set times. Thus, the OTT platform’s concept of viewing anything, anytime, anywhere has hit the right chord.
When working on web automation with Selenium, I encountered scenarios where I needed to refresh pages from time to time. When does this happen? One scenario is that I needed to refresh the page to check that the data I expected to see was still available even after refreshing. Another possibility is to clear form data without going through each input individually.
Most test automation tools just do test execution automation. Without test design involved in the whole test automation process, the test cases remain ad hoc and detect only simple bugs. This solution is just automation without real testing. In addition, test execution automation is very inefficient.
I was once asked at a testing summit, “How do you manage a QA team using scrum?” After some consideration, I realized it would make a good article, so here I am. Understand that the idea behind developing software in a scrum environment is for development teams to self-organize.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!