机器学习实战笔记 k-近邻算法

P 18 改变当前目录

import os
os.getcwd() # 查看当前工作目录
os.chdir("F:\\Python") # 改变目录 注意双下划线


P 19 代码

# -*- coding: utf-8 -*-
from numpy import *
import operator

def createDataSet():
	group = array ([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
	labels = ['A','A','B','B']
	return group, labels

def classify0(inX, dataSet, labels, k):
	#If Y has n rows and m columns, then Y.shape is (n,m). So Y.shape[0] is n.
	dataSetSize = dataSet.shape[0]
	#Construct an array by repeating inX the number of times given by reps.
	#dataSetsize rows and one column there
	diffMat = tile(inX, (dataSetSize, 1)) - dataSet
	#Matrix square calculation
	sqDiffMat = diffMat ** 2
	#perform a sum over each row
	sqDistances = sqDiffMat.sum(axis= 1)
	#root
	distances = sqDistances ** 0.5
	#sort the index  eg.
	#>>> x = np.array([3, 1, 2])
	#>>> np.argsort(x)
	#array([1, 2, 0])
	sortedDistIndicies = distances.argsort()
	classCount = {}
	for i in range(k):
		voteIlabel = labels[sortedDistIndicies[i]]
		#if (classCount[voteIlabel] exists)
		#	classCount.get(voteIlabel,0) = classCount[voteIlabel];
		#else classCount.get(voteIlabel,0) = 0;
		classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1
	#Parameter 'reverse' is using to flag descending sorts
	sortedClassCount = sorted(classCount.iteritems(), 
		key = operator.itemgetter(1), reverse = True)
	#return the most likely label
	return sortedClassCount[0][0]

P 21 代码

def file2matrix(filename):
	#open the file
	fr = open(filename)
	#readlines() reads until EOF using readline() and returns a list containing the lines.
	arrayOLines = fr.readlines()
	# get the number of lines
	numberOfLines = len(arrayOLines)
	# 创建m行n列的零矩阵 
	returnMat = zeros((numberOfLines,3))
	 # define a dictionary (can be append element)  
	classLabelVector = []
	index = 0
	for line in arrayOLines:
		line = line.strip()
		listFromLine = line.split('\t')
		returnMat[index,:] = listFromLine[0 : 3]
		classLabelVector.append(int(listFromLine[-1]))
		index += 1
	return returnMat, classLabelVector




                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值