from numpy import * import operator def create_data_set(): # 训练集与标签 group = array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]]) labels = ['A', 'A', 'B', 'B'] return group, labels group, labels = create_data_set() def classify0(inX, data_set, labels, k): # inX 待分类向量 data_set训练集 labels标签向量 k最相近邻居的数目 计算距离 # for 循环前步骤用于计算距离 距离公式:d = ((xA - xB)**2 + (yA - yB)**2)**0.5 data_set_size = data_set.shape[0] # 阵列的行数 diff_mat = tile(inX, (data_set_size, 1)) - data_set # 待分类向量 - 训练集中每行阵列 相当于计xA - xB,yA - yB sq_diff_mat = diff_mat ** 2 # 阵列平方,就是阵列每个对应数字平方 ,相当于将上一步的差平方(xA - xB)**2 sq_distances = sq_diff_mat.sum(axis=1) # 求和(xA - xB)**2 + (yA - yB)**2 distances = sq_distances ** 0.5 # 开方,得到距离 ((xA - xB)**2 + (yA - yB)**2)**0.5 sorted_dist_indicies = distances.argsort() # 根据距离从小到大排序排序,显示为对应索引 class_count = {} for i in range(k): # 选择距离最小的k个点 vote_ilabel = labels[sorted_dist_indicies[i]] # 从距离最近的开始取对应的索引,根据标签[索引]得到对应标签 class_count[vote_ilabel] = class_count.get(vote_ilabel, 0) + 1 # 字典中有该标签,则count+1,没有就新建 sorted_class_count = sorted(class_count.items(), key=operator.itemgetter(1), reverse=True) # 降序排序 return sorted_class_count
def file2matrix(filename): # 文本记录转换为numpy解析程序 fr = open(filename) array_of_lines = fr.readlines() number_of_lines = len(array_of_lines) # 得到文件行数 return_mat = zeros((number_of_lines, 3)) # 创建用零填充的矩阵 class_label_vector = [] for index, line in enumerate(array_of_lines): line = line.strip() list_fromline = line.split(' ') return_mat[index, :] = list_fromline[0:3] class_label_vector.append(int(list_fromline[-1])) return return_mat, class_label_vector def autoNorm(data_set): # 数据归一化(不归一化处理会使数据值大的对结果的影响远远大于其他值) min_vals = data_set.min(0) # 取列的最小值 max_vals = data_set.max(0) # 取最大值 ranges = max_vals - min_vals m = data_set.shape[0] # 行数 norm_data_set = data_set - tile(min_vals, (m, 1)) norm_data_set = norm_data_set / tile(ranges, (m, 1)) # 特征值相除 return norm_data_set, ranges, min_vals def datingClassTest(): # 测试,得出错误率 ho_ratio = 0.10 dating_data_mat, dating_labels = file2matrix('datingTestSet2.txt') # 读取文档生成训练集和标签 norm_mat, ranges, min_vals = autoNorm(dating_data_mat) # 进行归一化,生成①新矩阵,②max-min ③min m = norm_mat.shape[0] # 行数 num_test_vecs = int(m * ho_ratio) error_count = 0 for i in range(num_test_vecs): classifier_result = classify0(norm_mat[i, :], norm_mat[num_test_vecs:m, :], dating_labels[num_test_vecs:m], 4) print('the classifier came back with:%s,the real answer is : %s' % (classifier_result, dating_labels[i])) if (classifier_result != dating_labels[i]): error_count += 1.0 print('the total error rate is :%f' % (error_count / float(num_test_vecs))) def classifyPerson(): # 用户交互的预测函数 result_list = ['not at all', 'in small doses', 'in large doses'] percent_tats = float(input('玩电子游戏的时间百分比?')) ff_miles = float(input('每年的飞行里程?')) ice_cream = float(input('每年消费的冰淇淋量?')) dating_data_mat,dating_labels = file2matrix('datingTestSet2.txt') # 读取文档生成训练集和标签 norm_mat, ranges, minvals = autoNorm(dating_data_mat) # 进行归一化,生成①新矩阵,②max-min ③min in_arr = array([ff_miles,percent_tats, ice_cream]) # 根据用户输入建立矩阵 classifier_result = classify0((in_arr-minvals)/ranges,norm_mat,dating_labels,3) print('You will probably like this person:',result_list[classifier_result-1])
# 识别手写数字
def img2vector(filename): # 将图像转换成向量 return_vect = zeros((1, 1024)) # 创建用零填充的矩阵 fr = open(filename) for i in range(32): line_str = fr.readline() for j in range(32): return_vect[0, 32 * i + j] = int(line_str[j]) return return_vect def handwritingClassTest(): hw_labels =[] training_file_list = listdir('trainingDigits') # 获取训练目录内容 m = len(training_file_list) # 目录文件数 training_mat = zeros((m, 1024)) # 用零填充m行 1024列的矩阵 for i in range(m): file_name_str = training_file_list[i] # 取出目录内的文件名 file_str = file_name_str.split('.')[0] class_num_str = int(file_str.split('_')[0]) # 根据文件名提取出标签类型 hw_labels.append(class_num_str) training_mat[i,:] = img2vector('trainingDigits\%s' % file_name_str) # 利用上面的函数将该文件转换为向量并复制给矩阵 test_file_list = listdir('testDigits') # 获取测试文件内容 error_count = 0.0 m_test = len(test_file_list) # 获取测试文件数目 for i in range(m_test): file_name_str = test_file_list[i] file_str = file_name_str.split('.')[0] class_num_str = int(file_str.split('_')[0]) vector_under_test = img2vector('testDigits\%s' % file_name_str) classifier_result = classify0(vector_under_test,training_mat,hw_labels,3) print('the classifier came back with : %s,the real answer is : %s'% (classifier_result,class_num_str)) if classifier_result != class_num_str: error_count += 1.0 print(' the total number of errors is : %s '% error_count) print(' the total error rate is : %s' % (error_count/float(m_test)))