def svm_process(self,theList) : # SVM process np.set_printoptions(threshold=np.NaN) datas, labels = self.loadData(theList) #dalam datas yang berbentuk list ada dict print("sebelum normalisasi") #win.textbox.insertPlainText("sebelum normalisasi") #win.add_table_data(datas) #desktop2.add_text(desktop.window,"jancokqq") datas = svm.normalize(datas,win.normType.currentText()) print("setelah normalisasi") #win.add_table_data2(datas) list_temp=list() for i in datas: sublist=list() for key in i : sublist.append(i[key]) list_temp.append(sublist) datas=mat(list_temp) m, n = shape(datas) kernel= mat(zeros((m,m))) print("size=",m) for i in range(m) : kernel[:,i] = svm.kernelTrans(datas, datas[i, :],kTup={'type': win.kernelType.currentText(), 'gamma': win.paramGamma.value(),'degree': win.paramDegree.value()\ ,'coef' :win.paramCoef.value()}) #print("kernel=",kernel) b, alphas = svm.smoSimple(kernel, labels, win.paramC.value(), win.paramTolerance.value(), win.paramIterate.value()) # print('alphas') # print(alphas) # print('b') # print(b) return b, alphas
def eval(self,alphas,b,datas,training) : dataTrain,labelTrain = self.loadData(training) dataTrain = svm.normalize(dataTrain,win.normType.currentText()) dataEval, labelEval = self.loadData(datas) dataEval = svm.normalize(dataEval,win.normType.currentText()) dataTrain=mat(self._dictToList(dataTrain)) dataEval=mat(self._dictToList(dataEval)) #dataEval = mat(dataEval) m, n = shape(dataEval) labelTrain = mat(labelTrain).transpose() panjang = list() #simpan data buat f-score errorCount = 0 for i in range (m) : kernelEval = svm.kernelTrans(dataTrain, dataEval[i, :], kTup={'type': win.kernelType.currentText(), 'gamma': win.paramGamma.value(),'degree': win.paramDegree.value()\ ,'coef' :win.paramCoef.value()}) predict = kernelEval.T * multiply(labelTrain, alphas) + b panjang.append(float(sign(predict))) #print("panjang=",predict) if sign(predict) != sign(labelEval[i]): errorCount += 1 print("the error rate is: %f" % (float(+errorCount) / m)) print("labelArr", labelEval) print("panjang", panjang) f1_positive = f1_score(labelEval, panjang,pos_label=1) f1_negative = f1_score(labelEval, panjang, pos_label=-1) print("the f1_score is :", (f1_positive + f1_negative) / 2) win.label_fscore.setText("f-score :"+str((f1_positive + f1_negative) / 2))
def testPrc(kTup=('rbf', 10)): dataArr, labelArr = loadDataSet('201404') print 'load data done' b, alphas = smoP(dataArr, labelArr, 200, 0.0001, 10000, kTup) datMat = mat(dataArr); labelMat = mat(labelArr).transpose() svInd = nonzero(alphas.A > 0)[0] sVs = datMat[svInd] labelSV = labelMat[svInd]; print "there are %d Support Vectors" % shape(sVs)[0] saveSVM(svInd, sVs, labelSV, alphas[svInd]) #保存支持向量 m, n = shape(datMat) errorCount = 0 for i in range(m): kernelEval = kernelTrans(sVs, datMat[i, :], kTup) predict = kernelEval.T * multiply(labelSV, alphas[svInd]) + b if sign(predict) != sign(labelArr[i]): errorCount += 1 print "the training error rate is: %f" % (float(errorCount) / m)
def handwritingIndentify(kTup): featureList,labelList = loadImage('digits/trainingDigits') print featureList print labelList b, alphas = svm.smoP(array(featureList),labelList,200,0.0001,10000,kTup) print "b:",b, "alpha:",alphas svInd = nonzero(alphas.A > 0)[0] sVs = mat(array(featureList))[svInd] labelSV= mat(labelList).T[svInd] print "there a %s Support Vectors."%shape(sVs)[0] featureMat = mat(array(featureList));labelMat = mat(labelList).T m,n = shape(featureMat) errorCount = 0 for i in range(m): kernelMat = svm.kernelTrans(sVs, featureMat[i,:], kTup) predict = kernelMat.T * multiply(labelSV,alphas[svInd]) + b if sign(predict) != sign(labelMat[i]): errorCount += 1 print "the training data error rate is %s"%(float(errorCount)/m)
def handwritingIndentify(kTup): featureList, labelList = loadImage('digits/trainingDigits') print featureList print labelList b, alphas = svm.smoP(array(featureList), labelList, 200, 0.0001, 10000, kTup) print "b:", b, "alpha:", alphas svInd = nonzero(alphas.A > 0)[0] sVs = mat(array(featureList))[svInd] labelSV = mat(labelList).T[svInd] print "there a %s Support Vectors." % shape(sVs)[0] featureMat = mat(array(featureList)) labelMat = mat(labelList).T m, n = shape(featureMat) errorCount = 0 for i in range(m): kernelMat = svm.kernelTrans(sVs, featureMat[i, :], kTup) predict = kernelMat.T * multiply(labelSV, alphas[svInd]) + b if sign(predict) != sign(labelMat[i]): errorCount += 1 print "the training data error rate is %s" % (float(errorCount) / m)
file = "C:\\Users\\Daniel\\PycharmProjects\\Spotiwew\\Sandro Yusuf Sihaloho\\main.csv" csvReader = csv.DictReader(open(file)) thelist = list() for i in csvReader: thelist.append(i) datas, labels = loadData( thelist ) #dalam datas yang berbentuk list ada dict. disini memisahkan data dan label #print(thelist) list_temp = list() for i in datas: sublist = list() for key in i: sublist.append(i[key]) # get value of the key list_temp.append(sublist) # add to list datas = mat(list_temp) m, n = shape(datas) kernel = mat(zeros((m, m))) print("size=", m) for i in range(m): kernel[:, i] = svm.kernelTrans(datas, datas[i, :], kTup={ 'type': "laplacian", 'gamma': 0.5 }) # print("kernel=",kernel) b, alphas = svm.smoSimple(kernel, labels, C=10, toler=0.1, maxIter=2) print(b) print(alphas)
def recommend(self) : with open("resultSVM.txt", "rb") as fp: alphas, b, training = pickle.load(fp) connect.set_headers(win.labelbox.text()) song=list() with open("listPositive.txt", "rb") as fp: listPositive= pickle.load(fp) print("listPositive=",listPositive) listPositiveNew = list() if len(listPositive)>200 : #biar tidak tlalu banyak request] listPositive=self.random_del(listPositive,200) print("listnew=", len(listPositive)) if connect.get_display_name() != None: mypath = str(connect.get_display_name()) else: mypath = str(connect.get_current_user()) for i in listPositive : tempJson=connect.recommendation_based_seeds(i) for j in tempJson['tracks'] : song.append(j['id']) if not os.path.exists(mypath+"/recommendations.csv"): print("cari rekomendasi") headers = ['id', 'danceability', 'energy', 'loudness', 'speechiness', 'acousticness', 'instrumentalness', 'valence', 'tempo'] self.create_csv("recommendations.csv",mypath,headers) mypath = mypath+"/recommendations.csv" print(mypath) self.get_audio_features(song,mypath) #skaligus write to csv else : mypath = mypath + "/recommendations.csv" recomTrain, labelTrain = self.loadData(training) # win.add_table_data(dataTrain) recomTrain = svm.normalize(recomTrain, win.normType.currentText()) templist=list() for i in recomTrain : sublist=list() for key in i : sublist.append(i[key]) templist.append(sublist) recomTrain=templist print("dataTrain=", shape(mat(recomTrain))) print(recomTrain) # win.add_table_data2(dataTrain) # win.addAlphasItem(alphas) # win.label_alphas.setText("alphas size :" + str(len(alphas))) # win.label_bias.setText("Bias :"+str(b)) csvReader = csv.DictReader(open(mypath)) RecoCSV=list() print(csvReader) for i in csvReader : RecoCSV.append(i) dataReco, id_lagu = self.loadData(RecoCSV) # simpan id lagu bukan label print("datareco=",dataReco) dataReco = svm.normalize(dataReco, win.normType.currentText()) print("datareconorm=", dataReco) templist2=list() for j in dataReco : sublist2=list() for key2 in j : sublist2.append(j[key2]) #print("sublist2=",sublist2) templist2.append(sublist2) dataReco=templist2 print(dataReco) recomTrain = mat(recomTrain) print(recomTrain) #win.add_table_dataRecom(dataReco) dataReco = mat(dataReco) m, n = shape(dataReco) labelTrain = mat(labelTrain).transpose() print('udah mo abis bgst') print(id_lagu) listPredict=list() print("komall", shape(recomTrain)) print(shape(dataReco)) print(shape(labelTrain)) print(shape(alphas)) print("datareco=",dataReco) print("dataTrain=", recomTrain) for i in range(m): if (id_lagu[i] in listPredict)==False: kernelEval = svm.kernelTrans(recomTrain, dataReco[i, :], kTup={'type': win.kernelType.currentText(), 'gamma': win.paramGamma.value(), 'degree': win.paramDegree.value(), 'coef': win.paramCoef.value()}) predict = kernelEval.T * multiply(labelTrain, alphas) + b temp = {'id' : id_lagu[i],"value":predict } if temp not in listPredict : listPredict.append(temp) print("list predict=",listPredict) print("list predict=", type(listPredict)) seen=set() newListPredict=list() # for d in list(listPredict) : #in here we delete the dict with same # t=tuple(d.items()) # if t not in seen : # seen.add(t) # newListPredict.append(t) # listPredict=newListPredict listPredict.sort(key=itemgetter('value'),reverse=True) print("listPredict=",listPredict) # listRecomm = list() # count=0 # while len(listRecomm) < 10: # temp_l.append(listPredict[count]) # count+=1 listTemp=listPredict[:10] listRecomm=list() for i in listTemp : listRecomm.append(i['id']) recom_song = self.__list_to_str(listRecomm) print(recom_song) result = connect.get_several_tracks(recom_song) self.get_song_name(result)