def mixgauss_init(M, data, cov_type, method='kmeans'): ''' % MIXGAUSS_INIT Initial parameter estimates for a mixture of Gaussians % function [mu, Sigma, weights] = mixgauss_init(M, data, cov_type. method) % % INPUTS: % data(:,t) is the t'th example % M = num. mixture components % cov_type = 'full', 'diag' or 'spherical' % method = 'rnd' (choose centers randomly from data) or 'kmeans' (needs netlab) % % OUTPUTS: % mu(:,k) % Sigma(:,:,k) % weights(k) ''' if isinstance(data, list): data = np.hstack(data) elif data.ndim == 3: O, T, N = data.shape data = np.reshape(np.transpose(data, (0, 2, 1)), (O, T * N)) d, T = data.shape if method == 'rnd': C = np.atleast_2d(np.cov(data)) Sigma = np.transpose(np.tile(np.diag(np.diag(C)) * 0.5, (M, 1, 1)), (2, 1, 0)) # Initialize each mean to a random data point indices = np.arange(T) np.random.shuffle(indices) mu = data[:, indices[0:M]] weights, _ = normalise(np.ones((M, 1))) elif method == 'kmeans': gmm = GMM(n_components=M, covariance_type=cov_type, thresh=1e-2, min_covar=1e-3, n_iter=5, n_init=1, params='wmc', init_params='wmc') gmm.fit(data.T) mu = gmm.means_.T weights = np.asmatrix(gmm.weights_).T covars = gmm.covars_ Sigma = np.zeros((d, d, M)) for m in range(M): if cov_type == 'diag': Sigma[:, :, m] = np.diag(covars[m, :]) elif cov_type == 'full': Sigma[:, :, m] = covars[:, :, m] elif cov_type == 'spherical': Sigma[:, :, m] = covars[m] * np.eye(d) return mu, Sigma, weights
def mixgauss_init(M, data, cov_type, method='kmeans'): ''' % MIXGAUSS_INIT Initial parameter estimates for a mixture of Gaussians % function [mu, Sigma, weights] = mixgauss_init(M, data, cov_type. method) % % INPUTS: % data(:,t) is the t'th example % M = num. mixture components % cov_type = 'full', 'diag' or 'spherical' % method = 'rnd' (choose centers randomly from data) or 'kmeans' (needs netlab) % % OUTPUTS: % mu(:,k) % Sigma(:,:,k) % weights(k) ''' if isinstance(data, list): data = np.hstack(data) elif data.ndim==3: O, T, N = data.shape data = np.reshape(np.transpose(data, (0, 2, 1)), (O, T*N)) d, T = data.shape if method=='rnd': C = np.atleast_2d(np.cov(data)) Sigma = np.transpose(np.tile(np.diag(np.diag(C))*0.5, (M, 1, 1)), (2, 1, 0)) # Initialize each mean to a random data point indices = np.arange(T) np.random.shuffle(indices) mu = data[:,indices[0:M]] weights, _ = normalise(np.ones((M,1))) elif method=='kmeans': gmm = GMM(n_components=M, covariance_type=cov_type, thresh=1e-2, min_covar=1e-3, n_iter=5, n_init=1, params='wmc', init_params='wmc') gmm.fit(data.T) mu = gmm.means_.T weights = np.asmatrix(gmm.weights_).T covars = gmm.covars_ Sigma = np.zeros((d,d,M)) for m in range(M): if cov_type=='diag': Sigma[:,:,m] = np.diag(covars[m,:]) elif cov_type=='full': Sigma[:,:,m] = covars[:,:,m] elif cov_type=='spherical': Sigma[:,:,m] = covars[m] * np.eye(d) return mu, Sigma, weights
def classifyByGMMHMM(seq, models, configs): Y = [] for config in configs: _rawdata_type = config["logType"] _event_type = config["eventType"] _motion_type = config["motionType"] _sound_type = config["soundType"] _location_type = config["locationType"] d = Dataset( rawdata_type=_rawdata_type, event_type=_event_type, motion_type=_motion_type, sound_type=_sound_type, location_type=_location_type ) # Initiation of data need prediction. y = np.array(d._convetNumericalSequence(seq)) Y.append(y) _GMMHMMs = [] for model in models: _GMMs = [] for gmm in model["gmmParams"]["params"]: _GMM = GMM( n_components=model["nMix"], covariance_type=model["covarianceType"] ) _GMM.covars_ = np.array(gmm["covars"]) _GMM.means_ = np.array(gmm["means"]) _GMM.weights_ = np.array(gmm["weights"]) _GMMs.append(_GMM) _GMMHMM = GMMHMM( n_components=model["nComponent"], n_mix=model["nMix"], startprob=np.array(model["hmmParams"]["startProb"]), transmat=np.array(model["hmmParams"]["transMat"]), gmms=_GMMs, covariance_type=model["covarianceType"] ) _GMMHMMs.append(_GMMHMM) results = [] # for _GMMHMM in _GMMHMMs: # res = _GMMHMM.score(Y) # results.append(res) for i in range(0, len(models)): res = _GMMHMMs[i].score(Y[i]) results.append(res) return results
def updateParameter(self, smps, responsibilities, gf2hv): nV = self.gmdistr.means_.shape[1] nCompo = self.gmdistr.get_params()['n_components'] # update weights if nCompo == 1: piV = np.array([1.0]) else: if np.sum(gf2hv) == 0 or np.isnan(np.sum(gf2hv)) or np.isinf( np.sum(gf2hv)): raise ValueError("[updating weights]: zero division") piV = np.sum(np.dot(gf2hv[:, np.newaxis], np.ones( (1, nCompo))) * responsibilities, axis=0) / np.sum(gf2hv) piV = piV / np.sum(piV) # update mean #if nCompo == 1: # rm_mean = self.r_array[0].stats(moments='m') # rv_mean = self.r_array[1].stats(moments='m') # muV = np.array([[rm_mean, rv_mean]]) #else: muV = np.zeros(self.gmdistr.means_.shape) for iCompo in range(nCompo): tmps = gf2hv * responsibilities[:, iCompo] if np.sum(tmps) == 0 or np.isnan(np.sum(tmps)) or np.isinf( np.sum(tmps)): raise ValueError("[updating means]: zero division") for iV in range(nV): muV[iCompo, iV] = np.dot(smps[:, iV], tmps) / np.sum(tmps) # update covariance covarV = np.zeros(self.gmdistr.covars_.shape) muVold = self.gmdistr.means_ for iCompo in range(nCompo): tmps = gf2hv * responsibilities[:, iCompo] if np.sum(tmps) == 0 or np.isnan(np.sum(tmps)) or np.isinf( np.sum(tmps)): raise ValueError("[updating covariance]: zero division") for iV in range(nV): for jV in range(nV): smps_i = smps[:, iV] smps_j = smps[:, jV] covarV[iCompo, iV, jV] = np.dot( (smps_i - muVold[iCompo, iV]) * (smps_j - muVold[iCompo, jV]), tmps) / np.sum(tmps) self.gmdistr = GMM(n_components=nCompo, covariance_type='full') self.gmdistr.weights_ = piV self.gmdistr.means_ = muV self.gmdistr.covars_ = covarV
def adaptation(self, ti): kadp = np.ones(self.nadp) kadp[0] = STD_RATIO1 kadp[1:3] = STD_RATIO2 kadp[3:5] = STD_RATIO3 sumX = 0 sumX2 = 0 for iadp in range(self.nadp): nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo, covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * kadp[iadp]**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.rv_array, self.r_array, self.sl_array, self.gd_array, self.gcov_array) i = 0 while i <= MAX_NUM_UPDATE: try: smps = subSmp.sample() f = subSmp.priorPDF(smps) g = 1 - subSmp.condAvailability(smps, ti) hv, responsibilities = subSmp.score_samples(smps) gf2hv = g * f / hv self.updateParameter(smps, responsibilities, gf2hv) i += 1 break except ValueError: i += 1 continue sumX += np.sum(gf2hv) sumX2 += np.sum(gf2hv**2) npre = self.nadp * self.nsmp pfpre = 1. / npre * sumX Spf2pre = 1. / (npre * (npre - 1)) * (sumX2 - 2 * sumX * pfpre + npre * pfpre**2) return pfpre, Spf2pre
def build_models(data,class_name,file_name): print("Building EM") #Gausian_EM=GMM(n_components=6,n_init=1000) Gausian_EM=GMM(n_components=6) Gausian_EM.fit(data) print("Building SVM") #SVM_Model=SVC(probability=True,kernel='RBF') #SVM_Model=SVC(probability=True,kernel='poly') SVM_Model=SVC(probability=True) print(SVM_Model.fit(data,class_name)) print("Building Gausian Naive") Gausian_Naive_Model=GaussianNB() Gausian_Naive_Model.fit(data,class_name) print("Building Kmeans") K_means_model=KMeans(n_clusters=7) K_means_model.fit(data) return Gausian_EM,SVM_Model,Gausian_Naive_Model,K_means_model
def setPreSmp(self, r_array, sl_array, rate_array, gd_array, gcov_array): # initial peaks corrTarget = np.eye(self.ncomp) rv_list = [] for icomp in xrange(self.ncomp): cov_variable = Normal('cov', mean=0., stdv=COVT0_COV) rv_list.append(cov_variable.rv) rv_array = np.array(rv_list) peaks0 = getInitialPeaks(rv_array, self.ncomp, corrTarget=corrTarget, options={'disp':False, 'anneal':True}) weights0 = 1./self.ncomp * np.ones(self.ncomp) covar0 = getInitialCovar(rv_array, self.ncomp) # initial values gmdistr = GMM(n_components=self.ncomp , covariance_type='full') gmdistr.weights_ = weights0 gmdistr.means_ = peaks0 gmdistr.covars_ = covar0 self.preSmp = PreSmp(self.analysis_type, self.nadp, self.nsmp, gmdistr, rv_array, r_array, sl_array, rate_array, gd_array, gcov_array)
def getKopt(self, k_array, ti): pf = np.zeros(k_array.shape) for k in k_array: nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo,covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * k**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.rv_array, self.r_array, self.sl_array, self.gd_array, self.gcov_array) smps = subSmp.sample() indx = np.where(k_array==k)[0][0] pf[indx], dummy = subSmp.getPf(smps, ti) if indx>0 and pf[indx] < KRATIO_CRITERIA * np.mean(pf[:indx]): k_indx = indx-1 break else: k_indx = -1 return k_array[k_indx]
def getKopt(self, k_array, ti): pf = np.zeros(k_array.shape) for k in k_array: nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo, covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * k**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.r_array, self.sl_array, self.gd_array) smps = subSmp.sample() indx = np.where(k_array == k)[0][0] pf[indx], dummy = subSmp.getPf(smps, ti) if indx > 0 and pf[indx] < KRATIO_CRITERIA * np.mean(pf[:indx]): k_indx = indx - 1 break else: k_indx = -1 return k_array[k_indx]
def adaptation(self, ti): kadp = np.ones(self.nadp) kadp[0] = STD_RATIO1 kadp[1:3] = STD_RATIO2 kadp[3:5] = STD_RATIO3 sumX = 0; sumX2 = 0; for iadp in range(self.nadp): nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo,covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * kadp[iadp]**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.rv_array, self.r_array, self.sl_array, self.gd_array, self.gcov_array) i = 0 while i<=MAX_NUM_UPDATE: try: smps = subSmp.sample() f = subSmp.priorPDF(smps) g = 1 - subSmp.condAvailability(smps, ti) hv, responsibilities = subSmp.score_samples(smps) gf2hv = g*f/hv self.updateParameter(smps, responsibilities, gf2hv) i += 1 break except ValueError: i += 1 continue sumX += np.sum(gf2hv) sumX2 += np.sum(gf2hv**2) npre = self.nadp * self.nsmp pfpre = 1./npre * sumX Spf2pre = 1./(npre*(npre-1)) * (sumX2 - 2*sumX*pfpre + npre * pfpre**2) return pfpre, Spf2pre
def __init__(self, _models): super(GMMHMMClassifier, self).__init__(_models) self.gmmhmms = {} self.predict_data_ = None for label, value in _models.iteritems(): _model = value['param'] hmm_params = _model['hmmParams'] gmm_params = _model['gmmParams'] n_iter = _model.get('nIter', 50) transmat = np.array(hmm_params['transMat']) transmat_prior = np.array(hmm_params['transMatPrior']) n_component = hmm_params['nComponent'] startprob = np.array(hmm_params['startProb']) startprob_prior = np.array(hmm_params['startProbPrior']) n_mix = gmm_params['nMix'] covariance_type = gmm_params['covarianceType'] gmms = gmm_params.get('gmms', None) gmm_obj_list = [] if not gmms: gmm_obj_list = None else: for gmm in gmms: gmm_obj = GMM(n_components=gmm['nComponent'], covariance_type=gmm['covarianceType']) gmm_obj.covars_ = np.array(gmm['covars']) gmm_obj.means_ = np.array(gmm['means']) gmm_obj.weights_ = np.array(gmm['weights']) gmm_obj_list.append(gmm_obj) gmmhmm = GMMHMM(n_components=n_component, n_mix=n_mix, gmms=gmm_obj_list, n_iter=n_iter, covariance_type=covariance_type, transmat=transmat, transmat_prior=transmat_prior, startprob=startprob, startprob_prior=startprob_prior) self.gmmhmms[label] = {'gmmhmm': gmmhmm, 'status_set': value['status_set']}
def updateParameter(self, smps, responsibilities, gf2hv): nV = self.gmdistr.means_.shape[1] nCompo = self.gmdistr.get_params()['n_components'] # update weights if nCompo == 1: piV = np.array([1.0]) else: if np.sum(gf2hv) == 0 or np.isnan(np.sum(gf2hv)) or np.isinf(np.sum(gf2hv)): raise ValueError("[updating weights]: zero division") piV = np.sum( np.dot(gf2hv[:, np.newaxis], np.ones((1,nCompo))) * responsibilities, axis=0 ) / np.sum(gf2hv) piV = piV / np.sum(piV) # update mean #if nCompo == 1: # rm_mean = self.r_array[0].stats(moments='m') # rv_mean = self.r_array[1].stats(moments='m') # muV = np.array([[rm_mean, rv_mean]]) #else: muV = np.zeros(self.gmdistr.means_.shape) for iCompo in range(nCompo): tmps = gf2hv * responsibilities[:, iCompo] if np.sum(tmps) == 0 or np.isnan(np.sum(tmps)) or np.isinf(np.sum(tmps)): raise ValueError("[updating means]: zero division") for iV in range(nV): muV[iCompo, iV] = np.dot( smps[:, iV], tmps ) / np.sum(tmps) # update covariance covarV = np.zeros(self.gmdistr.covars_.shape) muVold = self.gmdistr.means_ for iCompo in range(nCompo): tmps = gf2hv * responsibilities[:, iCompo] if np.sum(tmps) == 0 or np.isnan(np.sum(tmps)) or np.isinf(np.sum(tmps)): raise ValueError("[updating covariance]: zero division") for iV in range(nV): for jV in range(nV): smps_i = smps[:, iV] smps_j = smps[:, jV] covarV[iCompo, iV, jV] = np.dot( (smps_i-muVold[iCompo, iV]) * (smps_j-muVold[iCompo, jV]), tmps ) / np.sum(tmps) self.gmdistr = GMM(n_components = nCompo, covariance_type='full') self.gmdistr.weights_ = piV self.gmdistr.means_ = muV self.gmdistr.covars_ = covarV
class PreSmp(MainSmp): def __init__(self, nadp, nsmp, gmdistr, rv_array,r_array, sl_array, gd_array, gcov_array): self.nadp = nadp MainSmp.__init__(self, nsmp, gmdistr, rv_array, r_array, sl_array, gd_array, gcov_array) def setAdpNum(self, new_nadp): self.nadp = new_nadp def getAdpNum(self): return self.nadp def updateParameter(self, smps, responsibilities, gf2hv): nV = self.gmdistr.means_.shape[1] nCompo = self.gmdistr.get_params()['n_components'] # update weights if nCompo == 1: piV = np.array([1.0]) else: if np.sum(gf2hv) == 0 or np.isnan(np.sum(gf2hv)) or np.isinf(np.sum(gf2hv)): raise ValueError("[updating weights]: zero division") piV = np.sum( np.dot(gf2hv[:, np.newaxis], np.ones((1,nCompo))) * responsibilities, axis=0 ) / np.sum(gf2hv) piV = piV / np.sum(piV) # update mean #if nCompo == 1: # rm_mean = self.r_array[0].stats(moments='m') # rv_mean = self.r_array[1].stats(moments='m') # muV = np.array([[rm_mean, rv_mean]]) #else: muV = np.zeros(self.gmdistr.means_.shape) for iCompo in range(nCompo): tmps = gf2hv * responsibilities[:, iCompo] if np.sum(tmps) == 0 or np.isnan(np.sum(tmps)) or np.isinf(np.sum(tmps)): raise ValueError("[updating means]: zero division") for iV in range(nV): muV[iCompo, iV] = np.dot( smps[:, iV], tmps ) / np.sum(tmps) # update covariance covarV = np.zeros(self.gmdistr.covars_.shape) muVold = self.gmdistr.means_ for iCompo in range(nCompo): tmps = gf2hv * responsibilities[:, iCompo] if np.sum(tmps) == 0 or np.isnan(np.sum(tmps)) or np.isinf(np.sum(tmps)): raise ValueError("[updating covariance]: zero division") for iV in range(nV): for jV in range(nV): smps_i = smps[:, iV] smps_j = smps[:, jV] covarV[iCompo, iV, jV] = np.dot( (smps_i-muVold[iCompo, iV]) * (smps_j-muVold[iCompo, jV]), tmps ) / np.sum(tmps) self.gmdistr = GMM(n_components = nCompo, covariance_type='full') self.gmdistr.weights_ = piV self.gmdistr.means_ = muV self.gmdistr.covars_ = covarV def adaptation(self, ti): kadp = np.ones(self.nadp) kadp[0] = STD_RATIO1 kadp[1:3] = STD_RATIO2 kadp[3:5] = STD_RATIO3 sumX = 0; sumX2 = 0; for iadp in range(self.nadp): nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo,covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * kadp[iadp]**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.rv_array, self.r_array, self.sl_array, self.gd_array, self.gcov_array) i = 0 while i<=MAX_NUM_UPDATE: try: smps = subSmp.sample() f = subSmp.priorPDF(smps) g = 1 - subSmp.condAvailability(smps, ti) hv, responsibilities = subSmp.score_samples(smps) gf2hv = g*f/hv self.updateParameter(smps, responsibilities, gf2hv) i += 1 break except ValueError: i += 1 continue sumX += np.sum(gf2hv) sumX2 += np.sum(gf2hv**2) npre = self.nadp * self.nsmp pfpre = 1./npre * sumX Spf2pre = 1./(npre*(npre-1)) * (sumX2 - 2*sumX*pfpre + npre * pfpre**2) return pfpre, Spf2pre def getKopt(self, k_array, ti): pf = np.zeros(k_array.shape) for k in k_array: nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo,covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * k**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.rv_array, self.r_array, self.sl_array, self.gd_array, self.gcov_array) smps = subSmp.sample() indx = np.where(k_array==k)[0][0] pf[indx], dummy = subSmp.getPf(smps, ti) if indx>0 and pf[indx] < KRATIO_CRITERIA * np.mean(pf[:indx]): k_indx = indx-1 break else: k_indx = -1 return k_array[k_indx]
Spf2main = 1./self.nsmp * np.var(gf2hv, ddof=1) return pfmain, Spf2main if __name__ == '__main__': ti = 1 nCompo = 4 nmain = 200 muVopt = np.array([[891.404727222421, 913.501588431349, 905.090608103064, 920.997600729747], [1828.76493195385, 1709.76493998393, 1729.75265083924, 1689.58888405073]]).T piVopt = np.array([0.591259881095148, 0.0904722135483567, 0.310287166921472, 0.00798073843502256]) covarVopt = np.array([[[3637.0, -892.0],[-892.0, 95600.0]], [[8332.0, -6257.0],[-6257.0, 52611.0]], [[6567.0, -4470.0],[-4470.0, 56876.0]], [[9948.0, -7620.0], [-7620.0, 48147.0]]]) gmdistr = GMM(n_components=4, covariance_type='full') gmdistr.weights_ = piVopt gmdistr.means_ = muVopt gmdistr.covars_ = covarVopt resistRv = Lognormal('r', mean=1790.1, stdv=283.37) rDistr = resistRv.rv #r_array = np.array([stats.norm(loc=1790.1, scale=283.37), stats.norm(loc=1790.1, scale=283.37)]) r_array = np.array([rDistr, rDistr]) sl_array = np.array([stats.norm(loc=301.1, scale=120.44)]) ti_mean = 15.91 a = 0.0135; b = 0.8580 gd = lambda x: (1-a*(x-ti_mean)**b) if x>ti_mean else 1.0 gd_array = np.array([gd, gd]) testSmp = MainSmp(nmain, gmdistr, r_array, sl_array, gd_array) smps = testSmp.sample()
return k_array[k_indx] if __name__ == '__main__': ti = 1 nCompo = 4 nsmp = 400 nadp = 20 nk = 100 muV0 = np.array([[2768.03327821769, 1519.21878505205, 2060.98121494796, 812.166721782312], [812.166721782312, 2060.98121494796, 1519.21878505205, 2768.03327821769]]).T muV0 = np.load('peaks0.npy') piV0 = np.array([0.25, 0.25, 0.25, 0.25]) covarV0 = np.array([[[80300.0, 0.0],[0.0, 80300.0]], [[80300.0, 0.0],[0.0, 80300.0]], [[80300.0, 0.0],[0.0, 80300.0]], [[80300.0, 0.0],[0.0, 80300.0]]]) gmdistr = GMM(n_components=4, covariance_type='full') gmdistr.weights_ = piV0 gmdistr.means_ = muV0 gmdistr.covars_ = covarV0 resistRv = Lognormal('r', mean=1790.1, stdv=283.37) rDistr = resistRv.rv #r_array = np.array([stats.norm(loc=1790.1, scale=283.37), stats.norm(loc=1790.1, scale=283.37)]) r_array = np.array([rDistr, rDistr]) sl_array = np.array([stats.norm(loc=301.1, scale=120.44)]) ti_mean = 15.91 a = 0.0135; b = 0.8580 gd = lambda x: (1-a*(x-ti_mean)**b) if x>ti_mean else 1.0 gd_array = np.array([gd, gd]) k_array = np.arange(2.0, 1.0-0.05/2, -0.05) testSmp = PreSmp(nadp, nsmp, gmdistr, r_array, sl_array, gd_array)
def trainingGMMHMM( dataset, # training dataset. n_c, # number of hmm's components (ie. hidden states) n_m, # number of gmm's mixtures (ie. Gaussian model) start_prob_prior=None, # prior of start hidden states probabilities. trans_mat_prior=None, # prior of transition matrix. start_prob=None, # the start hidden states probabilities. trans_mat=None, # the transition matrix. gmms=None, # models' params of gmm covar_type='full', n_i=50): # Initiation of dataset. # d = Dataset(dataset) X = dataset.getDataset() # Initiation of GMM. _GMMs = [] if gmms is None: _GMMs = None else: for gmm in gmms: _GMM = GMM(n_components=n_m, covariance_type=covar_type) _GMM.covars_ = np.array(gmm["covars"]) _GMM.means_ = np.array(gmm["means"]) _GMM.weights_ = np.array(gmm["weights"]) _GMMs.append(_GMM) # Initiation of GMMHMM. model = GMMHMM(startprob_prior=np.array(start_prob_prior), transmat_prior=np.array(trans_mat_prior), startprob=np.array(start_prob), transmat=np.array(trans_mat), gmms=_GMMs, n_components=n_c, n_mix=n_m, covariance_type=covar_type, n_iter=n_i) # Training. model.fit(X) # The result. new_gmmhmm = { "nComponent": n_c, "nMix": n_m, "covarianceType": covar_type, "hmmParams": { "startProb": model.startprob_.tolist(), "transMat": model.transmat_.tolist() }, "gmmParams": { "nMix": n_m, "covarianceType": covar_type, "params": [] } } for i in range(0, n_m): gaussian_model = { "covars": model.gmms_[i].covars_.tolist(), "means": model.gmms_[i].means_.tolist(), "weights": model.gmms_[i].weights_.tolist() } new_gmmhmm["gmmParams"]["params"].append(gaussian_model) return new_gmmhmm
W = W / np.sum(W, axis=1, keepdims=True) # print W # plot projection pca = PCA(n_components=2) pca.fit(omegas[:,1:4]) projs = pca.transform(omegas[:,1:4]) plt.plot(projs[:,0], projs[:,1], 'bo') plt.savefig('projs.png') plt.close() # fit mixture of gaussians N = omegas.shape[0] for k in range(1,10): g = GMM(n_components=k, covariance_type='full') mixture = g.fit(omegas[0:(int(0.75*N)),1:4]) print k, ":", sum(mixture.score_samples(omegas[(int(0.75*N)):N,1:4])[0]) g = GMM(n_components=3, covariance_type='full') mixture = g.fit(omegas[:,1:4]) print "weights:", mixture.weights_ print "means:", mixture.means_ print "covariances:", mixture.covars_ A = np.transpose(pca.transform(np.eye(3))) print A new_means = np.zeros((3, 2)) new_covars = np.zeros((3, 2, 2))
unknown_words = {} for token in test_tokens: if token in w2v_model: test_vectors.append(w2v_model[token]) elif token in unknown_words: test_vectors.append(unknown_words[token]) else: unknown_vec = w2v_model.seeded_vector(token) unknown_words[token] = unknown_vec test_vectors.append(unknown_vec) # Train GMM print 'Starting GMM training' words = w2v_model.vocab.keys() word_vectors = w2v_model.syn0 gmm_model = GMM(n_components=num_topics, n_iter=num_gmm_iterations, covariance_type='diag') gmm_model.fit(word_vectors) # joblib.dump(gmm_model, gmm_output_file) print 'Done GMM training' # Get the likelihood of each word vector under each Gaussian component scores = gmm_model.score(test_vectors) print scores ll = sum(scores) print "LL: "+str(ll) # Print topics if desired if print_topics: log_probs = log_multivariate_normal_density(word_vectors, gmm_model.means_, gmm_model.covars_, gmm_model.covariance_type) print np.min(log_probs) _, num_col = log_probs.shape
nCompo = 4 nsmp = 400 nadp = 20 nk = 100 muV0 = np.array([[ 2768.03327821769, 1519.21878505205, 2060.98121494796, 812.166721782312 ], [ 812.166721782312, 2060.98121494796, 1519.21878505205, 2768.03327821769 ]]).T muV0 = np.load('peaks0.npy') piV0 = np.array([0.25, 0.25, 0.25, 0.25]) covarV0 = np.array([[[80300.0, 0.0], [0.0, 80300.0]], [[80300.0, 0.0], [0.0, 80300.0]], [[80300.0, 0.0], [0.0, 80300.0]], [[80300.0, 0.0], [0.0, 80300.0]]]) gmdistr = GMM(n_components=4, covariance_type='full') gmdistr.weights_ = piV0 gmdistr.means_ = muV0 gmdistr.covars_ = covarV0 resistRv = Lognormal('r', mean=1790.1, stdv=283.37) rDistr = resistRv.rv #r_array = np.array([stats.norm(loc=1790.1, scale=283.37), stats.norm(loc=1790.1, scale=283.37)]) r_array = np.array([rDistr, rDistr]) sl_array = np.array([stats.norm(loc=301.1, scale=120.44)]) ti_mean = 15.91 a = 0.0135 b = 0.8580 gd = lambda x: (1 - a * (x - ti_mean)**b) if x > ti_mean else 1.0 gd_array = np.array([gd, gd]) k_array = np.arange(2.0, 1.0 - 0.05 / 2, -0.05)
class PreSmp(MainSmp): def __init__(self, nadp, nsmp, gmdistr, r_array, sl_array, gd_array): self.nadp = nadp MainSmp.__init__(self, nsmp, gmdistr, r_array, sl_array, gd_array) def setAdpNum(self, new_nadp): self.nadp = new_nadp def getAdpNum(self): return self.nadp def updateParameter(self, smps, responsibilities, gf2hv): nV = self.gmdistr.means_.shape[1] nCompo = self.gmdistr.get_params()['n_components'] # update weights if nCompo == 1: piV = np.array([1.0]) else: if np.sum(gf2hv) == 0 or np.isnan(np.sum(gf2hv)) or np.isinf( np.sum(gf2hv)): raise ValueError("[updating weights]: zero division") piV = np.sum(np.dot(gf2hv[:, np.newaxis], np.ones( (1, nCompo))) * responsibilities, axis=0) / np.sum(gf2hv) piV = piV / np.sum(piV) # update mean #if nCompo == 1: # rm_mean = self.r_array[0].stats(moments='m') # rv_mean = self.r_array[1].stats(moments='m') # muV = np.array([[rm_mean, rv_mean]]) #else: muV = np.zeros(self.gmdistr.means_.shape) for iCompo in range(nCompo): tmps = gf2hv * responsibilities[:, iCompo] if np.sum(tmps) == 0 or np.isnan(np.sum(tmps)) or np.isinf( np.sum(tmps)): raise ValueError("[updating means]: zero division") for iV in range(nV): muV[iCompo, iV] = np.dot(smps[:, iV], tmps) / np.sum(tmps) # update covariance covarV = np.zeros(self.gmdistr.covars_.shape) muVold = self.gmdistr.means_ for iCompo in range(nCompo): tmps = gf2hv * responsibilities[:, iCompo] if np.sum(tmps) == 0 or np.isnan(np.sum(tmps)) or np.isinf( np.sum(tmps)): raise ValueError("[updating covariance]: zero division") for iV in range(nV): for jV in range(nV): smps_i = smps[:, iV] smps_j = smps[:, jV] covarV[iCompo, iV, jV] = np.dot( (smps_i - muVold[iCompo, iV]) * (smps_j - muVold[iCompo, jV]), tmps) / np.sum(tmps) self.gmdistr = GMM(n_components=nCompo, covariance_type='full') self.gmdistr.weights_ = piV self.gmdistr.means_ = muV self.gmdistr.covars_ = covarV def adaptation(self, ti): kadp = np.ones(self.nadp) kadp[0] = STD_RATIO1 kadp[1:3] = STD_RATIO2 kadp[3:5] = STD_RATIO3 sumX = 0 sumX2 = 0 for iadp in range(self.nadp): nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo, covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * kadp[iadp]**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.r_array, self.sl_array, self.gd_array) i = 0 while i <= MAX_NUM_UPDATE: try: smps = subSmp.sample() f = subSmp.priorPDF(smps) g = 1 - subSmp.condAvailability(smps, ti) hv, responsibilities = subSmp.score_samples(smps) gf2hv = g * f / hv self.updateParameter(smps, responsibilities, gf2hv) i += 1 break except ValueError: i += 1 continue sumX += np.sum(gf2hv) sumX2 += np.sum(gf2hv**2) npre = self.nadp * self.nsmp pfpre = 1. / npre * sumX Spf2pre = 1. / (npre * (npre - 1)) * (sumX2 - 2 * sumX * pfpre + npre * pfpre**2) return pfpre, Spf2pre def getKopt(self, k_array, ti): pf = np.zeros(k_array.shape) for k in k_array: nCompo = self.gmdistr.get_params()['n_components'] tmp_gmdistr = GMM(n_components=nCompo, covariance_type='full') tmp_gmdistr.weights_ = self.gmdistr.weights_ tmp_gmdistr.means_ = self.gmdistr.means_ tmp_gmdistr.covars_ = self.gmdistr.covars_ * k**2 subSmp = MainSmp(self.nsmp, tmp_gmdistr, self.r_array, self.sl_array, self.gd_array) smps = subSmp.sample() indx = np.where(k_array == k)[0][0] pf[indx], dummy = subSmp.getPf(smps, ti) if indx > 0 and pf[indx] < KRATIO_CRITERIA * np.mean(pf[:indx]): k_indx = indx - 1 break else: k_indx = -1 return k_array[k_indx]
#min_max_scaler=preprocessing.MinMaxScaler(feature_range=(-1000, 10)) features_scaled = data ''' print(features_scaled.shape) print(features_scaled.min(axis=0)) print(features_scaled.max(axis=0)) scatter(features_scaled[:,0],features_scaled[:,1]) ''' #labels=model.fit_predict(features_scaled) #print(labels) print("checking for the song", file_name[2]) print("original class", class_name1[2]) model = GMM(n_components=7) model.fit(data) print("EM predict class", model.predict(data[2])) print("EM predict class", model.predict_proba(data[2])) #print("EM score",model.score(data, class_name1)) model = SVC(probability=True) #model.fit(data,class_name1) #print("SVM predict class",model.predict(data[2])) #print("SVM predict class",model.predict_proba(data[2])) #print("SVM score",model.score(data, class_name1)) model = GaussianNB() #model.fit(data,class_name1) #print("Gausian Naive predict class",model.predict(data[2])) #print("Gausian Naive predict class",model.predict_proba(data[2]))
#plt.show() #sys.exit(1) # initial peaks corrTarget = np.eye(2) cov_variable1 = Normal('cov1', mean=0., stdv=COVT0_COV) cov_rv1 = cov_variable1.rv cov_variable2 = Normal('cov2', mean=0., stdv=COVT0_COV) cov_rv2 = cov_variable2.rv rv_array = np.array([cov_rv1, cov_rv2]) peaks0 = getInitialPeaks(rv_array, NUM_COMPONENT, corrTarget=corrTarget, options={'disp':False, 'anneal':True}) weights0 = 1./NUM_COMPONENT * np.ones(NUM_COMPONENT) covar0 = getInitialCovar(rv_array, NUM_COMPONENT) # initial values gmdistr = GMM(n_components=NUM_COMPONENT , covariance_type='full') gmdistr.weights_ = weights0 gmdistr.means_ = peaks0 gmdistr.covars_ = covar0 def timeVariantReliability(ti): # CE-based smpling # preliminary sampling preSmp = PreSmp(NUM_ADAPTATION, NUM_PRE_SMP, gmdistr, rv_array, r_array, sl_array, gd_array, gcov_array) pfpre, Spf2pre = preSmp.adaptation(ti) k_array = np.arange(INIT_K, 1.0-K_STEPS, -K_STEPS) kopt = preSmp.getKopt(k_array, ti) # main sampling mainSmp = MainSmp(preSmp=preSmp) mainSmp.setSmpNum(NUM_MAIN_SMP) smps = mainSmp.sample()
'DPGMM':DPGMM(), 'DecisionTreeClassifier':DecisionTreeClassifier(), 'DecisionTreeRegressor':DecisionTreeRegressor(), 'DictionaryLearning':DictionaryLearning(), 'ElasticNet':ElasticNet(), 'ElasticNetCV':ElasticNetCV(), 'EmpiricalCovariance':EmpiricalCovariance(), 'ExtraTreeClassifier':ExtraTreeClassifier(), 'ExtraTreeRegressor':ExtraTreeRegressor(), 'ExtraTreesClassifier':ExtraTreesClassifier(), 'ExtraTreesRegressor':ExtraTreesRegressor(), 'FactorAnalysis':FactorAnalysis(), 'FastICA':FastICA(), 'FeatureAgglomeration':FeatureAgglomeration(), 'FunctionTransformer':FunctionTransformer(), 'GMM':GMM(), 'GaussianMixture':GaussianMixture(), 'GaussianNB':GaussianNB(), 'GaussianProcess':GaussianProcess(), 'GaussianProcessClassifier':GaussianProcessClassifier(), 'GaussianProcessRegressor':GaussianProcessRegressor(), 'GaussianRandomProjection':GaussianRandomProjection(), 'GenericUnivariateSelect':GenericUnivariateSelect(), 'GradientBoostingClassifier':GradientBoostingClassifier(), 'GradientBoostingRegressor':GradientBoostingRegressor(), 'GraphLasso':GraphLasso(), 'GraphLassoCV':GraphLassoCV(), 'HuberRegressor':HuberRegressor(), 'Imputer':Imputer(), 'IncrementalPCA':IncrementalPCA(), 'IsolationForest':IsolationForest(),
for token in test_tokens: if token in w2v_model: test_vectors.append(w2v_model[token]) elif token in unknown_words: test_vectors.append(unknown_words[token]) else: unknown_vec = w2v_model.seeded_vector(token) unknown_words[token] = unknown_vec test_vectors.append(unknown_vec) # Train GMM print 'Starting GMM training' words = w2v_model.vocab.keys() word_vectors = w2v_model.syn0 gmm_model = GMM(n_components=num_topics, n_iter=num_gmm_iterations, covariance_type='diag') gmm_model.fit(word_vectors) # joblib.dump(gmm_model, gmm_output_file) print 'Done GMM training' # Get the likelihood of each word vector under each Gaussian component scores = gmm_model.score(test_vectors) print scores ll = sum(scores) print "LL: " + str(ll) # Print topics if desired if print_topics: log_probs = log_multivariate_normal_density( word_vectors, gmm_model.means_, gmm_model.covars_,
def train_user(): print('start capturing video.....') time.sleep(3) cam = cv2.VideoCapture(0) cam.set(3, 640) # set video width cam.set(4, 480) # set video height face_detector = cv2.CascadeClassifier( 'haarcascade_frontalface_default.xml') # For each person, enter one numeric face id''' face_id = input('enter user id=') name = input('enter user-name=') path = '/Users/parthpatel/Desktop/Face-Voice-Recognition/dataset/' + face_id os.mkdir(path) print("[INFO] Initializing face capture. Look at the camera and wait ...") # Initialize individual sampling face count count = 0 while (True): ret, img = cam.read() # img = cv2.flip(img, -1) # flip video image vertically gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_detector.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) count += 1 # Save the captured image into the datasets folder cv2.imwrite( "/Users/parthpatel/Desktop/Face-Voice-Recognition/dataset/" + face_id + "/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y + h, x:x + w]) cv2.imshow('image', img) k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting video if k == 27: break elif count >= 30: # Take 30 face sample and stop video break # release camera print("[INFO] Exiting Program .......") cam.release() cv2.destroyAllWindows() # Path for face image database path = '/Users/parthpatel/Desktop/Face-Voice-Recognition/dataset/' + face_id recognizer = cv2.face.LBPHFaceRecognizer_create() detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") # function to get the images and label data def getImagesAndLabels(path): imagePaths = [os.path.join(path, f) for f in os.listdir(path)] faceSamples = [] ids = [] for imagePath in imagePaths: if imagePath == path + '.DS_Store': continue PIL_img = Image.open(imagePath).convert( 'L') # convert it to grayscale img_numpy = np.array(PIL_img, 'uint8') id = int(os.path.split(imagePath)[-1].split(".")[1]) faces = detector.detectMultiScale(img_numpy) for (x, y, w, h) in faces: faceSamples.append(img_numpy[y:y + h, x:x + w]) ids.append(id) return faceSamples, ids print("\n [INFO] Training faces. It will take a few seconds. Wait ...") faces, ids = getImagesAndLabels(path) recognizer.train(faces, np.array(ids)) train_path = '/Users/parthpatel/Desktop/Face-Voice-Recognition/trainer/' + face_id os.mkdir(train_path) # Save the model into trainer/trainer.yml recognizer.write( '/Users/parthpatel/Desktop/Face-Voice-Recognition/trainer/' + face_id + '/trainer.yml') # recognizer.save() worked on Mac, but not on Pi # Print the numer of faces trained and end program print("\n [INFO] {0} faces trained. Exiting Program".format( len(np.unique(ids)))) print( '--------------------------------------------------------------------------------------------' ) time.sleep(5) print( '--------------------------------------------------------------------------------------------' ) # We'll run the script for different people and store # the data into multiple files # Voice authentication CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 RECORD_SECONDS = 3 WAVE_OUTPUT_FILENAME = "./voices/" + face_id os.mkdir(WAVE_OUTPUT_FILENAME) p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print("* recording") print("...........") frames = [] for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): audio_data = stream.read(CHUNK) frames.append(audio_data) print("* done recording") stream.stop_stream() stream.close() p.terminate() # # saving wav file of speaker waveFile = wave.open( WAVE_OUTPUT_FILENAME + '/' + name + '_sample_' + face_id + '.wav', 'wb') waveFile.setnchannels(CHANNELS) waveFile.setsampwidth(p.get_sample_size(FORMAT)) waveFile.setframerate(RATE) waveFile.writeframes(b''.join(frames)) waveFile.close() print("Done") # # path to training data source = WAVE_OUTPUT_FILENAME # # path to save trained model dest = "./gmm_model/" files = [ os.path.join(source, f) for f in os.listdir(source) if f.endswith('.wav') ] print(files[0]) features = np.array([]) sr, audio = read(files[0]) # convert audio to mfcc vector = extract_features(audio, sr) features = vector # for more accurate weights features = np.vstack((features, vector)) features = np.vstack((features, vector)) print(features.shape) #Gaussian-mixture-model to save gmm-model gmm = GMM(n_components=16, n_iter=200, covariance_type='diag', n_init=3) gmm.fit(features) # # picklefile = f.split("\\")[-2].split(".wav")[0]+".gmm" # # model saving.. pickle.dump(gmm, open(dest + name + '_' + face_id + '.gmm', 'wb')) print(name + ' ' + 'added......') features = np.asarray(())
def getPreSmpObject(analysis_type, new_service_time, frp_mean_history, frp_cov_history): # time array time_array = np.arange(RELIABILITY_DT,SERVICE_LIFE+RELIABILITY_DT,RELIABILITY_DT) # resistance array and live load array flexure_mean_history = frp_mean_history[0] flexure_cov_history = frp_cov_history[0] shear_mean_history = frp_mean_history[1] shear_cov_history = frp_cov_history[1] rm_mean = flexure_mean_history[0] rv_mean = shear_mean_history[0] r_array = np.array([rm_mean, rv_mean]) slmRv = Normal('slm', mean=M_LLIM_MEAN, stdv=M_LLIM_MEAN*M_LLIM_COV) slvRv = Normal('slv', mean=V_LLIM_MEAN, stdv=V_LLIM_MEAN*V_LLIM_COV) sldRv = Normal('sld', mean=M_LLIM_DECK_MEAN, stdv=M_LLIM_DECK_MEAN*M_LLIM_DECK_COV) slmDistr = slmRv.rv slvDistr = slvRv.rv sldDistr = sldRv.rv sl_array = np.array([slmDistr, slvDistr, sldDistr]) rate_m = LL_ARRIVAL_RATE rate_v = LL_ARRIVAL_RATE rate_d = LL_ARRIVAL_RATE_DECK rate_array = np.array([rate_m, rate_v, rate_d]) # function gd(t) and gcov(t) #def gd_func(x, a, b): # return a+b*x def gd_func(x, a, b, c, d, e): return a+b*x**1+c*x**2+d*x**3+e*x**4 gt_flex = flexure_mean_history/flexure_mean_history[0] / np.sqrt(1+flexure_cov_history**2) gt_shear = shear_mean_history/shear_mean_history[0] / np.sqrt(1+shear_cov_history**2) gd_flex_popt, pcov = curve_fit(gd_func, new_service_time, gt_flex) gd_shear_popt,pcov = curve_fit(gd_func, new_service_time, gt_shear) gd_flexure = lambda x: gd_func(x, gd_flex_popt[0], gd_flex_popt[1], gd_flex_popt[2], gd_flex_popt[3], gd_flex_popt[4]) gd_shear = lambda x: gd_func(x, gd_shear_popt[0], gd_shear_popt[1], gd_shear_popt[2], gd_shear_popt[3], gd_shear_popt[4]) #gd_flexure = lambda x: gd_func(x, gd_flex_popt[0], gd_flex_popt[1]) #gd_shear = lambda x: gd_func(x, gd_shear_popt[0], gd_shear_popt[1]) def gcov_func(x, a, b): # dummy function, not used return a+b*x at_flex = np.sqrt(np.log(flexure_cov_history**2+1)/COVT0_COV**2) at_shear = np.sqrt(np.log(shear_cov_history**2+1)/COVT0_COV**2) gcov_flex_popt, pcov = curve_fit(gcov_func, new_service_time, at_flex) gcov_shear_popt,pcov = curve_fit(gcov_func, new_service_time, at_shear) #gcov_flexure = lambda x: gcov_func(x, gcov_flex_popt[0], gcov_flex_popt[1], gcov_flex_popt[2], gcov_flex_popt[3], gcov_flex_popt[4]) #gcov_shear = lambda x: gcov_func(x, gcov_shear_popt[0], gcov_shear_popt[1], gcov_shear_popt[2], gcov_shear_popt[3], gcov_shear_popt[4]) gcov_flexure = lambda x: at_flex[0] + 0*x gcov_shear = lambda x: at_shear[0] + 0*x gd_array = np.array([gd_flexure, gd_shear]) gcov_array = np.array([gcov_flexure, gcov_shear]) #plt.close('all') #plt.figure() #plt.plot(new_service_time, gt_flex, 'bo', # new_service_time, gt_shear, 'rs', # new_service_time, gd_flexure(new_service_time), 'b-', # new_service_time, gd_shear(new_service_time), 'r-') #plt.figure() #plt.plot(new_service_time, at_flex, 'bo', # new_service_time, at_shear, 'rs', # new_service_time, gcov_flexure(new_service_time), 'b-', # new_service_time, gcov_shear(new_service_time), 'r-') #plt.show() # initial peaks corrTarget = np.eye(2) cov_variable1 = Normal('cov1', mean=0., stdv=COVT0_COV) cov_rv1 = cov_variable1.rv cov_variable2 = Normal('cov2', mean=0., stdv=COVT0_COV) cov_rv2 = cov_variable2.rv rv_array = np.array([cov_rv1, cov_rv2]) peaks0 = getInitialPeaks(rv_array, NUM_COMPONENT, corrTarget=corrTarget, options={'disp':False, 'anneal':True}) weights0 = 1./NUM_COMPONENT * np.ones(NUM_COMPONENT) covar0 = getInitialCovar(rv_array, NUM_COMPONENT) # initial values gmdistr = GMM(n_components=NUM_COMPONENT , covariance_type='full') gmdistr.weights_ = weights0 gmdistr.means_ = peaks0 gmdistr.covars_ = covar0 # CE-based smpling nTimePoint = time_array.size #pfpre = np.zeros(nTimePoint) #pfmain = np.zeros(nTimePoint) Spf2pre = np.zeros(nTimePoint) Spf2main = np.zeros(nTimePoint) kopt = np.zeros(nTimePoint) #pf = np.zeros(nTimePoint) #Spf2 = np.zeros(nTimePoint) # preliminary sampling preSmp = PreSmp(analysis_type, NUM_ADAPTATION, NUM_PRE_SMP, gmdistr, rv_array, r_array, sl_array, rate_array, gd_array, gcov_array) return preSmp
''' print(features_scaled.shape) print(features_scaled.min(axis=0)) print(features_scaled.max(axis=0)) scatter(features_scaled[:,0],features_scaled[:,1]) ''' #labels=model.fit_predict(features_scaled) #print(labels) print("checking for the song",file_name[2]) print("original class",class_name1[2]) model=GMM(n_components=7) model.fit(data) print("EM predict class",model.predict(data[2])) print("EM predict class",model.predict_proba(data[2])) #print("EM score",model.score(data, class_name1)) model=SVC(probability=True) #model.fit(data,class_name1) #print("SVM predict class",model.predict(data[2])) #print("SVM predict class",model.predict_proba(data[2])) #print("SVM score",model.score(data, class_name1)) model=GaussianNB() #model.fit(data,class_name1) #print("Gausian Naive predict class",model.predict(data[2])) #print("Gausian Naive predict class",model.predict_proba(data[2]))