當前位置: 首頁>>代碼示例>>Python>>正文


Python KNN.file2matrix方法代碼示例

本文整理匯總了Python中KNN.file2matrix方法的典型用法代碼示例。如果您正苦於以下問題:Python KNN.file2matrix方法的具體用法?Python KNN.file2matrix怎麽用?Python KNN.file2matrix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在KNN的用法示例。


在下文中一共展示了KNN.file2matrix方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: plotwithlable

# 需要導入模塊: import KNN [as 別名]
# 或者: from KNN import file2matrix [as 別名]
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,代碼行數:57,代碼來源:my1stplot.py

示例2: classifyPerson

# 需要導入模塊: import KNN [as 別名]
# 或者: from KNN import file2matrix [as 別名]
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,代碼行數:14,代碼來源:test.py

示例3:

# 需要導入模塊: import KNN [as 別名]
# 或者: from KNN import file2matrix [as 別名]
from numpy import *
import KNN
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
datingDataMat,datingLabels,datingLabelsInt = KNN.file2matrix(
    '../SourceCode2/Ch02/datingTestSet.txt')
ax.scatter(datingDataMat[:,0], datingDataMat[:,1], s=15.0*datingLabelsInt
    ,marker='^', c=datingLabelsInt)
# ax.axis([-2, 60000, -0.2, 16])
plt.xlabel('Percentage of Time Spent Playing Video Games')
plt.ylabel('Liters of Ice Cream Consumed Per Week')
plt.legend()
plt.show()
開發者ID:racekiller,項目名稱:MachineLearningInAction,代碼行數:17,代碼來源:plot.py

示例4: array

# 需要導入模塊: import KNN [as 別名]
# 或者: from KNN import file2matrix [as 別名]
"""
Created on Oct 27, 2010

@author: Peter
"""
from numpy import *
import KNN
import matplotlib
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
datingDataMat, datingLabels = KNN.file2matrix("datingTestSet.txt")
# ax.scatter(datingDataMat[:,1], datingDataMat[:,2])
ax.scatter(datingDataMat[:, 1], datingDataMat[:, 2], 15.0 * array(datingLabels), 15.0 * array(datingLabels))
ax.axis([-2, 25, -0.2, 2.0])
plt.xlabel("Percentage of Time Spent Playing Video Games")
plt.ylabel("Liters of Ice Cream Consumed Per Week")
plt.show()
開發者ID:peiying,項目名稱:Machine_Learning_python,代碼行數:21,代碼來源:createFirstPlot.py


注:本文中的KNN.file2matrix方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。