使用k-近鄰算法改進約會網站的配對效果(kNN)

目錄

谷歌筆記本(可選)

準備數據:從文本文件中解析數據

編寫算法:編寫kNN算法

分析數據:使用Matplotlib創建散點圖

準備數據:歸一化數值

測試算法:作為完整程序驗證分類器

使用算法:構建完整可用系統


谷歌筆記本(可選)


from?google.colab?import?drive
drive.mount("/content/drive")

Mounted at /content/drive


準備數據:從文本文件中解析數據


def?file2matrix(filename):fr?=?open(filename)arrayOfLines?=?fr.readlines()numberOfLines?=?len(arrayOfLines)returnMat?=?zeros((numberOfLines,?3))classLabelVector?=?[]index?=?0for?line?in?arrayOfLines:line?=?line.strip()listFromLine?=?line.split('\t')returnMat[index,?:]?=?listFromLine[0:3]classLabelVector.append(int(listFromLine[-1]))index?+=?1return?returnMat,?classLabelVector
datingDataMat,?datingLabels?=?file2matrix('/content/drive/MyDrive/MachineLearning/機器學習/k-近鄰算法/使用k-近鄰算法改進約會網站的配對效果/datingTestSet2.txt')
datingDataMat

array([[4.0920000e+04, 8.3269760e+00, 9.5395200e-01], [1.4488000e+04, 7.1534690e+00, 1.6739040e+00], [2.6052000e+04, 1.4418710e+00, 8.0512400e-01], ..., [2.6575000e+04, 1.0650102e+01, 8.6662700e-01], [4.8111000e+04, 9.1345280e+00, 7.2804500e-01], [4.3757000e+04, 7.8826010e+00, 1.3324460e+00]])

datingLabels[:10]

[3, 2, 1, 1, 1, 1, 3, 3, 1, 3]


編寫算法:編寫kNN算法


from?numpy?import?*
import?operatordef?classify0(inX,?dataSet,?labels,?k):dataSetSize?=?dataSet.shape[0]diffMat?=?tile(inX,?(dataSetSize,?1))?-?dataSetsqDiffMat?=?diffMat?**?2sqDistances?=?sqDiffMat.sum(axis=1)distances?=?sqDistances**0.5sortedDistIndicies?=?distances.argsort()classCount?=?{}for?i?in?range(k):voteIlabel?=?labels[sortedDistIndicies[i]]classCount[voteIlabel]?=?classCount.get(voteIlabel,?0)?+?1sortedClassCount?=?sorted(classCount.items(),?key=operator.itemgetter(1),?reverse=True)return?sortedClassCount[0][0]

分析數據:使用Matplotlib創建散點圖


import?matplotlib
import?matplotlib.pyplot?as?plt
fig?=?plt.figure()
ax?=?fig.add_subplot(111)
ax.scatter(datingDataMat[:,?1],?datingDataMat[:,?2])
plt.show()

?

import?matplotlib
import?matplotlib.pyplot?as?plt
fig?=?plt.figure()
ax?=?fig.add_subplot(111)
ax.scatter(datingDataMat[:,?1],?datingDataMat[:,?2],15.0*array(datingLabels),?15.0*array(datingLabels))
plt.show()

import?matplotlib
import?matplotlib.pyplot?as?plt
fig?=?plt.figure()
ax?=?fig.add_subplot(111)
ax.scatter(datingDataMat[:,?0],?datingDataMat[:,?1],15.0*array(datingLabels),?15.0*array(datingLabels))
plt.show()


準備數據:歸一化數值


def?autoNorm(dataSet):minVals?=?dataSet.min(0)maxVals?=?dataSet.max(0)ranges?=?maxVals?-?minValsnormDataSet?=?zeros(shape(dataSet))m?=?dataSet.shape[0]normDataSet?=?dataSet?-?tile(minVals,?(m,1))normDataSet?=?normDataSet/tile(ranges,?(m,1))return?normDataSet,?ranges,?minVals
normMat,?ranges,?minVals?=?autoNorm(datingDataMat)
normMat
array([[0.44832535, 0.39805139, 0.56233353],[0.15873259, 0.34195467, 0.98724416],[0.28542943, 0.06892523, 0.47449629],...,[0.29115949, 0.50910294, 0.51079493],[0.52711097, 0.43665451, 0.4290048 ],[0.47940793, 0.3768091 , 0.78571804]])
ranges
array([9.1273000e+04, 2.0919349e+01, 1.6943610e+00])
minVals
array([0.      , 0.      , 0.001156])

測試算法:作為完整程序驗證分類器


def?datingClassTest():hoRatio?=?0.1datingDataMat,?datingLabels?=?file2matrix('/content/drive/MyDrive/MachineLearning/機器學習/k-近鄰算法/使用k-近鄰算法改進約會網站的配對效果/datingTestSet2.txt')normMat,?ranges,?minVals?=?autoNorm(datingDataMat)m?=?normMat.shape[0]numTestVecs?=?int(m*hoRatio)errorCount?=?0for?i?in?range(numTestVecs):classifierResult?=?classify0(normMat[i,:],?normMat[numTestVecs:m,:],datingLabels[numTestVecs:m],3)print("the?classifierResult?came?back?with:?%d,\the?real?answer?is:?%d"?%?(classifierResult,?datingLabels[i]))if?(classifierResult?!=?datingLabels[i]):errorCount?+=?1print("the?total?error?rate?is:?%f"?%?(errorCount/float(numTestVecs)))

datingClassTest()
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 3,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 2,    the real answer is: 3
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 3,    the real answer is: 3
the classifierResult came back with: 2,    the real answer is: 2
the classifierResult came back with: 1,    the real answer is: 1
the classifierResult came back with: 3,    the real answer is: 1
the total error rate is: 0.050000

使用算法:構建完整可用系統


def?classifyPerson():resultList?=?['not?at?all','in?small?doses','in?large?doses',]percentTats?=?float(input("percentage?of?time?spent?playing?video?games?"))ffMiles?=?float(input("frequent?flier?miles?earned?per?year?"))iceCream?=?float(input("liters?of?ice?cream?consumed?per?year?"))datingDataMat,?datingLabels?=?file2matrix('/content/drive/MyDrive/MachineLearning/機器學習/k-近鄰算法/使用k-近鄰算法改進約會網站的配對效果/datingTestSet2.txt')normMat,?ranges,?minVals?=?autoNorm(datingDataMat)inArr?=?array([ffMiles,?percentTats,?iceCream])classifierResult?=?classify0((inArr?-?minVals)/ranges,?normMat,?datingLabels,?3)print("You?will?probably?like?this?person:",?resultList[classifierResult?-?1])
classifyPerson()
percentage of time spent playing video games?10
frequent flier miles earned per year?10000
liters of ice cream consumed per year?0.5
You will probably like this person: in small doses

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/697436.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/697436.shtml
英文地址,請注明出處:http://en.pswp.cn/news/697436.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

js過濾取出對象中改變的屬性和值

朋友公司的面試題 ,取出對象中被改變的屬性和值 const obj1 { a: 1, b: 2, c: 4 }; const obj2 { a: 1, b: 2, c: 5 }; 方法1 function testFun(obj1, obj2) {const diff {};const keys1 Object.keys(obj1);const keys2 Object.keys(obj2);const allKyes keys…

【深度學習】Gemini 1.0 Pro 如何讓chatGPT扮演stable diffusion的提示詞工程師

google也出了一個chatGPT,免費申請使用: https://aistudio.google.com/app/prompts/new_chat https://github.com/google/generative-ai-docs/blob/main/site/en/tutorials/rest_quickstart.ipynb 模型信息: $ curl https://generativelan…

SpringCloud(14)之SpringCloud Consul

我們知道 Eureka 2.X 遇到困難停止開發了,所以我們需要尋找其他的替代技術替代Eureka,這一小 節我們就講解一個新的組件Consul。 一、Consul介紹 Consul 是 HashiCorp 公司推出的開源工具,用于實現分布式系統的服務發現與配置。與其它分布式…

kali xrdp

Kali Linux 使用遠程桌面連接——xrdp&xfce_kali xfce桌面-CSDN博客 Ubuntu/Debian/Kali xrdp遠程桌面黑屏/空屏/無畫面解決辦法 - 知乎 (zhihu.com) sudo apt-get install xrdp -y sudo apt-get install xfce4 -ysudo systemctl enable xrdp --now systemctl status xrd…

中級.NET開發工程師面試經歷

文章目錄 前言面試題目(只記錄了還記得的部分)一.簡單說下.NETCORE的生命周期?二.C#如何保證在并發情況下接口不會被重復觸發?三.引用類型和值類型有什么區別?四.那怎樣能讓引用類型和值類型一樣,在賦值的時…

【Latex】TeXstudio編譯器選項修改

1、動機 編譯國科大博士畢業答辯論文latex時報錯 Package ctable Error: You must load ctable after tikz. 2、方法 經過搜索發現是因為這是中文模板,編譯的選項不對,需要從 PDFLaTeX 調整到 XeLaTeX。于是操作如下 1)點擊選項 2&#xf…

linux 文件目錄操作命令【重點】

目錄 ls cd cat more tail【工作中使用多】 mkdir rmdir rm ls 作用: 顯示指定目錄下的內容 語法: ls [-al] [dir] 說明: -a 顯示所有文件及目錄 (. 開頭的隱藏文件也會列出) -l 除文件名稱外,同時將文件型態(d表示目錄,-表示文件)、權限…

SpringMVC POST請求傳參 屬性名字母大寫注入失敗解決方案

問題描述: 我現在有一個接口通過一個實體(RequestBody)去接收一系列的參數,前端傳參為一個JSON字符串,但是當我的屬性名以大寫字母開頭(有的中間還有下劃線),或者第二個字母是大寫字母的時候,我發現后端接收不到參數值…

Flask——基于python完整實現客戶端和服務器后端流式請求及響應

文章目錄 本地客戶端Flask服務器后端客戶端/服務器端流式接收[打字機]效果 看了很多相關博客,但是都沒有本地客戶端和服務器后端的完整代碼示例,有的也只說了如何流式獲取后端結果,基本沒有講兩端如何同時實現流式輸入輸出,特此整…

C++字符串類

C中有兩種主要的字符串類&#xff1a;std::string 和 std::wstring。 std::string std::string 是 C 標準庫中用于處理 ASCII 字符串的類。它提供了豐富的方法來操作字符串&#xff0c;包括插入、刪除、查找子串、比較等功能。使用 std::string 需要包含頭文件 <string>…

8.CSS層疊繼承規則總結

CSS 層疊繼承規則總結 經典真題 請簡述一下 CSS 中的層疊規則 CSS 中的層疊繼承規則 在前面《CSS屬性的計算過程》中&#xff0c;我們介紹了每一個元素都有都有所有的屬性&#xff0c;每一個屬性都會通過一系列的計算過程得到最終的值。 這里來回顧一下計算過程&#xff0…

Node.js中如何處理異步編程

在Node.js中&#xff0c;處理異步編程是至關重要的技能。由于Node.js的單線程執行模型&#xff0c;異步編程可以極大地提高程序的性能和響應速度。本文將介紹幾種常見的異步編程處理方式&#xff0c;并附上示例代碼&#xff0c;幫助您更好地理解和應用異步編程技術。 回調函數…

家政小程序開發,引領家庭服務新時代的科技革命

隨著科技的飛速發展&#xff0c;人們的生活方式正在發生深刻的變化。其中&#xff0c;家政服務作為日常生活的重要組成部分&#xff0c;也在經歷著一場由小程序技術引領的科技革命。本文將探討家政小程序的發展趨勢、功能特點以及對家庭服務的深遠影響。 一、家政小程序的發展…

Linux命令-chattr命令(用來改變文件屬性)

說明 chattr命令 用來改變文件屬性。這項指令可改變存放在ext2文件系統上的文件或目錄屬性&#xff0c;這些屬 性共有以下8種模式。 語法 chattr(選項)選項 a&#xff1a;讓文件或目錄僅供附加用途&#xff1b; b&#xff1a;不更新文件或目錄的最后存取時間&#xff1b; c…

NFTScan Labs,一個聚焦在 NFT 領域的開發者組織

NFTScan Labs 是一個聚焦在 NFT 領域的開發者組織&#xff0c;成立于 2021 年 3 月份。NFTScan Labs 核心成員從 2016 年開始涉足區塊鏈領域&#xff0c;有多年開發經驗和前沿行業認知&#xff0c;對加密錢包、區塊鏈安全、鏈上數據追蹤、DeFi、預言機、NFT 等領域有深入的研究…

2/22作業

1.按位置插入 void insert_pos(seq_p L,datetype value,int pos) { if(LNULL) { printf("入參為空\n"); return; } if(seq_full(L)) { printf("表已滿\n"); return; } if(pos>L->len|…

Jenkins的使用GIT(4)

Jenkins的使用GIT 20211002 我們使用 Jenkins 集成外部 Git 倉庫&#xff0c;實現對真實代碼的拉取和構建。在這里&#xff0c;我們選用 Coding/Github/Gitee 等都可以作為我們的代碼源 1 生成公鑰私鑰 首先&#xff0c;我們先來配置公鑰和私鑰。這是 Jenkins 訪問 Git 私有庫…

【nvm】下載安裝及使用(包含windows和Linux)

目錄 1、Windows版本下載及安裝 2、Linux下載及安裝 下載 安裝 3、使用 在不借助第三方工具的情況下切換node版本&#xff0c;只能卸載現有版本&#xff0c;安裝需要的版本&#xff0c;這樣顯然很麻煩。而nvm就很好的幫我們解決了這個問題。 nvm&#xff08;node.js vers…

QT中調用python

一.概述 1.Python功能強大&#xff0c;很多Qt或者c/c開發不方便的功能可以由Python編碼開發&#xff0c;尤其是一些算法庫的應用上&#xff0c;然后Qt調用Python。 2.在Qt調用Python的過程中&#xff0c;必須要安裝python環境&#xff0c;并且Qt Creator中編譯器與Python的版…

OpenCV:計算機視覺領域的瑞士軍刀

摘要 本文將深入探索OpenCV&#xff08;開源計算機視覺庫&#xff09;的基本概念、應用領域、主要功能和未來發展。通過本文&#xff0c;讀者將能夠理解OpenCV在計算機視覺中的重要性&#xff0c;并掌握其基本使用方法。 一、引言 隨著人工智能和機器學習技術的飛速發展&…