当前位置: 首页>>代码示例>>Python>>正文


Python KNN类代码示例

本文整理汇总了Python中KNN的典型用法代码示例。如果您正苦于以下问题:Python KNN类的具体用法?Python KNN怎么用?Python KNN使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了KNN类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: crossvalidation

def crossvalidation(userManager, artistManager, folders):
	"""split data into folders and validate the performance"""
	userIDs = userManager.keys()
	userFolders = {}
	for i in range(folders):
		userFolders[i] = []
	for userID in userIDs:
		i = random.randrange(folders)
		userFolders[i].append(userID)
	for f in range(folders):
		testUserSet, testUserIDList, testUserMostFavourite = splitTrainSet(userManager, 1.0/folders, userFolders[f])
		knn = KNN(6)
		knn.training(userManager, artistManager)
		rightNum = 0
		totalNum = len(testUserIDList)
		for i in range(len(testUserIDList)):
			print i, totalNum,
			favOfOne = knn.testing(testUserSet[testUserIDList[i]], userManager, artistManager)
			print testUserIDList[i], testUserMostFavourite[testUserIDList[i]].keys()[0], favOfOne
			if favOfOne == testUserMostFavourite[testUserIDList[i]].keys()[0]:
				rightNum += 1
		print "Folder", f, ":"
		print "Total:", totalNum
		print float(rightNum)/len(testUserIDList)
		for i in range(len(testUserIDList)):
			userManager[testUserIDList[i]] = testUserSet[testUserIDList[i]]
开发者ID:Yaliang,项目名称:last.fm-recommender,代码行数:26,代码来源:baseline.py

示例2: main

def main():
    inputFile=sys.argv[1]
    global trainSet
    global testSet
    global bootstrap

    generateTrainTestSample(inputFile)
    bootstrapping(trainSet)
    KNN.main(bootstrap[1],testSet,3)
开发者ID:aniket-gaikwad,项目名称:Applied-Machine-learning-Project---Fall-2014,代码行数:9,代码来源:Bootstrap_backup.py

示例3: test_simple

def test_simple():
    data_set, labels = KNN.create_data_set()

    test1 = array([1.2, 1.0])
    test2 = array([0.1, 0.3])
    k = 3
    output_label1 = KNN.knn_classify(test1, data_set, labels, k)
    output_label2 = KNN.knn_classify(test2, data_set, labels, k)
    print test1, output_label1
    print test2, output_label2
开发者ID:cristalezx,项目名称:Machine-Learning-in-Action,代码行数:10,代码来源:testKNN.py

示例4: plotwithlable

def plotwithlable():
    
    xcord1 = []; ycord1 = []; zcord1=[]
    xcord2 = []; ycord2 = []; zcord2=[]
    xcord3 = []; ycord3 = []; zcord3=[]  
    #group ,labels = createDataSet()    
    datingDataMat, datingLables = KNN.file2matrix('datingTestSet2.txt')
    #print(datingDataMat)
    #print(datingDataMat[0,2])
    #print(datingLables)
    normDataMat,  ranges, minVals = KNN.autoNorm(datingDataMat)
    #print(normDataMat)
    tmp = datingDataMat
    datingDataMat = normDataMat
    fig = plt.figure() #create pic: fig 
    ax = fig.add_subplot(311) #create a subplot with 1 row 1 colum, select pic 1   
    #type1 = ax.scatter(xcord1, ycord1, s=20, c='red')
    #type2 = ax.scatter(xcord2, ycord2, s=30, c='green')
    #type3 = ax.scatter(xcord3, ycord3, s=50, c='blue')   
    
    for index, value in enumerate(datingLables):
        if value == 1:
            xcord1.append(datingDataMat[index,0]) 
            ycord1.append(datingDataMat[index,1])
            zcord1.append(datingDataMat[index,2])
        elif value == 2:
            xcord2.append(datingDataMat[index,0]) 
            ycord2.append(datingDataMat[index,1])
            zcord2.append(datingDataMat[index,2])
        else:
            xcord3.append(datingDataMat[index,0]) 
            ycord3.append(datingDataMat[index,1])
            zcord3.append(datingDataMat[index,2])
    type1 = ax.scatter(xcord1, ycord1, s=20, c='red')
    type2 = ax.scatter(xcord2, ycord2, s=30, c='green')
    type3 = ax.scatter(xcord3, ycord3, s=50, c='blue')   
    ax.legend([type1, type2, type3], ["Did Not Like", "Liked in Small Doses", "Liked in Large Doses"], loc=2)
    
    ax2 = fig.add_subplot(312)
    type1 = ax2.scatter(xcord1, zcord1, s=20, c='red')
    type2 = ax2.scatter(xcord2, zcord2, s=30, c='green')
    type3 = ax2.scatter(xcord3, zcord3, s=50, c='blue')   
    ax2.legend([type1, type2, type3], ["Did Not Like", "Liked in Small Doses", "Liked in Large Doses"], loc=2)

    plt.xlabel("Frequent Flyier Miles Earned Per Year")
    plt.ylabel("Liters of Ice Cream Consumed Per Week")
    ax3 = fig.add_subplot(313)
    type1 = ax3.scatter(ycord1, zcord1, s=20, c='red')
    type2 = ax3.scatter(ycord2, zcord2, s=30, c='green')
    type3 = ax3.scatter(ycord3, zcord3, s=50, c='blue')   
    ax3.legend([type1, type2, type3], ["Did Not Like", "Liked in Small Doses", "Liked in Large Doses"], loc=2)

    plt.xlabel("Percentage of Body Covered By Tatoos")
    plt.ylabel("Liters of Ice Cream Consumed Per Week")       
    plt.show()
开发者ID:foxli180,项目名称:Self_Learning,代码行数:55,代码来源:my1stplot.py

示例5: classifyPerson

def classifyPerson():
    print "输入相关信息"
    resultList = ['一点不喜欢','有点希望','可能性很大']
    percentTats = float(raw_input("玩游戏时间数目?"))
    ffMiles = float(raw_input("旅游公路数?"))
    ice = float(raw_input("冰淇淋消耗量?"))
    datingDataMat,datingLabels = KNN.file2matrix('datingTestSet2.txt')
    normMat,ranges,minVals = KNN.autoNorm(datingDataMat)
    inArr = np.array([ffMiles,percentTats,ice])
    classfierRt = KNN.classify0((inArr-minVals)/ranges,normMat,datingLabels,3)
    print resultList[classfierRt - 1]
    PrintFigure(normMat, datingLabels)
开发者ID:bzhou830,项目名称:ML-python,代码行数:12,代码来源:test.py

示例6: testUser

def testUser(testUserID):
    if not UserManager.has_key(testUserID):
        return "don't has user with userID = "+str(testUserID)
    testUserSet, testUserIDList = splitTrainSetWithoutRemoving(TrainUserManager, 0, [testUserID])
    knn = KNN(40)
    knn.training(TrainUserManager, ArtistManager)
    favOfOne, allArtist, allTag = knn.testing(testUserSet[testUserID], UserManager, ArtistManager, True)
    realfavOfOne = UserManager[testUserID].getMostFav().keys()[0]
    ret = "The most listen artist:\n"+str(ArtistManager[realfavOfOne])+"\n"
    ret += "The artist we predict:\n"+str(ArtistManager[favOfOne])
    ret = ret.replace("\n","</br>")
    # recovery modified TrainUserManager
    TrainUserManager[testUserID]=testUserSet[testUserID]

    return ret
开发者ID:Yaliang,项目名称:last.fm.server,代码行数:15,代码来源:app.py

示例7: lle

def lle(data, k = 10, target_dim = 2):
	p = data.shape[1]
	graph = KNN.knn(data, k)
 	
	n = len(graph.keys())
	weights_vec = np.zeros((n,k))
	weights_dict = {}
	locals_ = np.zeros((n,k))
	for i, key in enumerate(list(graph.keys())):
		local = construct_knn_vector(key, graph, k)
		local_centered = local - np.repeat(np.array(key).reshape([1, p]), k, axis = 0)
		gram = do_gram(local_centered, k)

		w_num = np.dot(np.linalg.inv(gram), np.ones(gram.shape[0]).T)
		w = w_num / w_num.sum()
		weights_vec[i] = w

		temp_dict = {}
		for q in range(len(local)):
			temp_dict[tuple(local[q])] = w[q]
		weights_dict[tuple(key)] = temp_dict

	weights = reconstruct(data, weights_dict)

	M = np.dot((np.identity(n) - weights).T, (np.identity(n) - weights))
	eigvals, eigvecs = np.linalg.eigh(M)

	index = np.argsort(eigvals)[::1]
	eigvals = eigvals[index]
	eigvecs = eigvecs[:,index]

	return eigvecs[:,1:target_dim + 1]
开发者ID:philipz1,项目名称:ML,代码行数:32,代码来源:LLE.py

示例8: cross_validation_nn

def cross_validation_nn(k,folds_array):

    #Initial values
    corrects = 0
    incorrects = 0

    #Separate train and test data
    for i in range(0,10):
        training_data = []
        test_data = []
        for j in range(0,10):
            if j == i:
                test_data = folds_array[j]
            else:
                training_data = training_data + folds_array[j]
        #Predict values
        for j in range(0,len(test_data)):
            prediction = KNN.knearest(k,training_data,test_data[j],True)
            length = len(test_data[j])-1
            #Check if the value is correct
            if prediction == test_data[j][length]:
                corrects = corrects + 1
            else:
                incorrects = incorrects + 1

    return float(corrects)/float(corrects+incorrects)
开发者ID:juancarpio27,项目名称:MachineLearning,代码行数:26,代码来源:CrossValidation.py

示例9: calculateTestError

def calculateTestError():
    testError=KNN.getTestError(resultKNN,KNN.actualLabel)
    print("****Test Error*****")
    print(testError)
    print("*******Accuracy********")
    accuracy=100-testError
    print(accuracy)
开发者ID:aniket-gaikwad,项目名称:Applied-Machine-learning-Project---Fall-2014,代码行数:7,代码来源:integrator.py

示例10: test_non_norm

def test_non_norm():
    dating_mat, dating_label = KNN.file_to_matrix('datingTestSet2.txt')
    for i in range(30):
        print dating_mat[i], dating_label[i]
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(dating_mat[:, 0], dating_mat[:, 1],
               15.0 * array(dating_label), 15.0 * array(dating_label))
    plt.show()
开发者ID:cristalezx,项目名称:Machine-Learning-in-Action,代码行数:9,代码来源:testKNN.py

示例11: main

def main():
    appid = '[Your eBay Product AppID]'  # Change this to your eBay product AppID
    search_keyword = 'wine'
    categoryId = '38182'  # red wine
    
    items = get_items(appid, search_keyword, categoryId)
    
    # using un-weighted KNN
    print 'using un-weighted KNN:'
    print KNN.get_KNN(items, (1,1000), k = 3)
    print KNN.get_KNN(items, (2,2000), k = 3)
    print '*********************'
    
    # using weighted KNN
    print 'weighted KNN using Gaussian function:'
    print KNN.get_weightedKNN(items, (1,1000), k = 3)
    print KNN.get_weightedKNN(items, (2,1000), k = 3)
    print KNN.get_weightedKNN(items, (2,2000), k = 3)
开发者ID:hanhanwu,项目名称:Hanhan-Machine-Learning-Model-Implementation,代码行数:18,代码来源:eBay_price_predict.py

示例12: buildMockUser

def buildMockUser():
    artists = request.form['artists']
    artistlist = json.loads(artists)
    testUser = User(-100)
    missingArtist = []
    for artistRecord in artistlist:
        artistID = int(artistRecord.keys()[0])
        artistWeight = artistRecord.values()[0]
        if artistWeight == 0:
            artistWeight = 0.0000001
        if ArtistManager.has_key(artistID):
            testUser.insertArt(artistID, artistWeight)
        else:
            missingArtist.append(artistID)
    knn = KNN(40)
    knn.training(UserManager, ArtistManager)
    favOfOne, allArtist, allTag = knn.testing(testUser, UserManager, ArtistManager, True)
    ret = {'artistID': favOfOne}
    if len(missingArtist) > 0:
        ret['warning'] = {'missingArtist':missingArtist}

    ret['artists'] = []
    allArtistLen = len(allArtist)-1
    maxArtistMatchWeight = allArtist[-1][1]
    for i in range(allArtistLen, max(-1, allArtistLen-10), -1):
        artistID = allArtist[i][0]
        matchWeight = allArtist[i][1] / maxArtistMatchWeight
        artistName = ArtistManager[artistID].Name
        topTag = ArtistManager[artistID].getTopTag()
        if topTag == -1:
            topTagName = ""
        else:
            topTagName = TagManager[topTag]
        ret['artists'].append({'id':artistID, 'name':artistName, 'match':matchWeight, 'tag':topTag, 'tagName':topTagName})

    ret['tags'] = []
    allTagLen = len(allTag)-1
    for i in range(allTagLen, max(-1, allTagLen-10), -1):
        tagID = allTag[i][0]
        tagWeight = allTag[i][1]
        tagName = TagManager[tagID]
        ret['tags'].append({'id':tagID, 'name':tagName, 'match':tagWeight})
    # dataObj = {'artists-num':len(artistlist)}
    return json.dumps(ret)
开发者ID:Yaliang,项目名称:last.fm.server,代码行数:44,代码来源:app.py

示例13: date_class_test

def date_class_test():
    ratio = 0.04    # ratio of the test examples
    # data_set:1000*3,  data_labels: 1000*1
    data_set, data_labels = KNN.file_to_matrix('datingTestSet2.txt')

    # normilize the data_set.   Note:  data_labels is not nessary to normlize
    norm_set, ranges, min_val = KNN.normalize(data_set)

    all_rows = norm_set.shape[0]   # number of all rows
    test_rows = int(ratio * all_rows)  # number of test rows
    error_num = 0
    for i in range(test_rows):
        # return the predict labels
        label_res = KNN.knn_classify(norm_set[i, :], norm_set[test_rows: all_rows, :],\
                                     data_labels[test_rows: all_rows, :], 3)
        print 'Classifier predict: %d, real result is: %d' % (label_res, data_labels[i])
        if label_res != data_labels[i]:
            error_num += 1
    print 'total error rate is: %f ' % (error_num * 1.0 / float(test_rows))
开发者ID:cristalezx,项目名称:Machine-Learning-in-Action,代码行数:19,代码来源:testKNN.py

示例14: gameRecommendations

def gameRecommendations(u_name):
    # Get API key
    all_api_keys1 = get_keys("./num1.txt")
    all_api_keys2 = get_keys("./num2.txt")
    api_key = str(all_api_keys1[0]) + str(all_api_keys2[0])

    if len(api_key) != 32:
        print("Uh-oh, don't forget to enter your API key!")
        return

    # Set up a requests session to allow retries when a request fails
    session = reqGet.Session()
    session.mount("http://", reqGet.adapters.HTTPAdapter(max_retries=10))

    games_response_json = getUserGames(u_name, api_key)

    all_games = loadGameIDs("./data/id_header.csv")

    # Get all of the game names and IDs from steam and save them in a dictionary for easy usage
    game_list = json.loads(session.get(url="http://api.steampowered.com/ISteamApps/GetAppList/v2").text)['applist']['apps']
    game_dict = {}
    for game in game_list:
        game_dict[game['appid']] = game

    user_game_array = ["0"] * len(all_games)

    if not games_response_json:
        return

    for game in games_response_json:
        if game['appid'] in all_games:
            game_index = all_games.index(game['appid'])
            user_game_array[game_index] = "1"

    all_games = [game_dict[x]['name'] for x in all_games]

    game_bit_string = int(''.join(user_game_array), 2)
    dataset = KNN.loadDataset("./data/games_by_username_all.csv")
    closest = KNN.findClosest(dataset, game_bit_string, 100)
    return KNN.getTopGames(KNN.getVotes(all_games, closest, game_bit_string), 5)
开发者ID:aaronkarp123,项目名称:steam-game-recommender,代码行数:40,代码来源:runKnn.py

示例15: handwriteDigitTest

def handwriteDigitTest():
    trainData, trainLabel = loadTrainData()
    testData = loadTestData()
    m, n = shape(testData)
    testLabel = loadTestResult() 
    resultList = []
    k = 5
    # predict every testData row's label 
    for i in xrange(m):
        classifyClassResult = KNN.classify0(testData[i], trainData, trainLabel.transpose(), k)
        resultList.append(classifyClassResult)
        print "the classifier calcute is: %d, the real answer is : %d" %(classifyClassResult, testLabel[0,i])
    saveResult(resultList)
开发者ID:beyondliyang,项目名称:KNN,代码行数:13,代码来源:KNNstudy.py


注:本文中的KNN类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。