import numpy as np
from matplotlib import pyplot as plt

X_train = np.array([
[158, 64],
[170, 66],
[183, 84],
[191, 80],
[155, 49],
[163, 59],
[180, 67],
[158, 54],
[178, 77]
])
y_train = ["male", "male", "male", "male", "female", "female", "female", "female", "female"]

plt.figure()
plt.title("Human Heights and Weights by Sex")
plt.xlabel("Height in cm")
plt.ylabel("Weight in kg")

for i, x in enumerate(X_train):
plt.scatter(x[0], x[1], c="k", marker="x" if y_train[i] == "male" else "D")

plt.grid(True)
plt.show()
代码结果:

from collections import Counter

import numpy as np

X_train = np.array([
[158, 64],
[170, 66],
[183, 84],
[191, 80],
[155, 49],
[163, 59],
[180, 67],
[158, 54],
[178, 77]
])
y_train = ["male", "male", "male", "male", "female", "female", "female", "female", "female"]

# 预测数据
x = np.array([[155, 70]])

# 两点之间的距离
distances = np.sqrt(np.sum((X_train - x) ** 2, axis=1))
"""[ 6.70820393 15.5241747 31.30495168 37.36308338 21. 13.60147051 25.17935662 16.2788206 24.04163056]"""

# 距离由小到大的索引排序(取前三个)
nearest_neighbor_indices = distances.argsort()[:3]
"""[0 5 1]"""

# 通过索引,获取y_train中相应的内容
nearest_neighbor_genders = np.take(y_train, nearest_neighbor_indices)
"""['male' 'female' 'male']"""

# 统计nearest_neighbor_genders中每个数据出现的次数
b = Counter(nearest_neighbor_genders)
"""Counter({'male': 2, 'female': 1})"""

# 获取出现次数最多的一个数据(1代表获取一个数据)
gender = b.most_common(1)[0][0]
print(gender)
"""male"""

# 标签二进制化
from sklearn.preprocessing import LabelBinarizer
from sklearn.neighbors import KNeighborsClassifier

import numpy as np

X_train = np.array([
[158, 64],
[170, 66],
[183, 84],
[191, 80],
[155, 49],
[163, 59],
[180, 67],
[158, 54],
[178, 77]
])
y_train = ["male", "male", "male", "male", "female", "female", "female", "female", "female"]

# 预测数据
x = np.array([[155, 70]])

# 实例化标签二进制化
lb = LabelBinarizer()
# 将y_train转化为二进制
y_train_binarized = lb.fit_transform(y_train)
"""[[1] [1] [1] [1] [0] [0] [0] [0] [0]]"""

K = 3

# 实例化KNeighborsClassifier类
clf = KNeighborsClassifier(n_neighbors=K)

# 调用fit方法
clf.fit(X_train, y_train_binarized.reshape(-1))

# 预测x的标签(二进制)
predicted_binarized = clf.predict(x)
"""[1]"""

# 将二进制转换为标签
predicted_label = lb.inverse_transform(predicted_binarized)
print(predicted_label)
"""['male']"""

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, matthews_corrcoef, \
classification_report
from sklearn.preprocessing import LabelBinarizer
from sklearn.neighbors import KNeighborsClassifier

import numpy as np

X_train = np.array([
[158, 64],
[170, 66],
[183, 84],
[191, 80],
[155, 49],
[163, 59],
[180, 67],
[158, 54],
[178, 77]
])
y_train = ["male", "male", "male", "male", "female", "female", "female", "female", "female"]

# 预测数据
x_text = np.array([
[168, 65],
[180, 96],
[160, 52],
[169, 67]
])

y_test = ['female', 'male', 'female', 'female']

# 实例化标签二进制化
lb = LabelBinarizer()
# 将y_train转化为二进制
y_train_binarized = lb.fit_transform(y_train)
"""[[1] [1] [1] [1] [0] [0] [0] [0] [0]]"""

y_test_binarized = lb.transform(y_test)
"""[[0] [1] [0] [0]]"""

K = 3

# 实例化KNeighborsClassifier类
clf = KNeighborsClassifier(n_neighbors=K)

# 调用fit方法
clf.fit(X_train, y_train_binarized.reshape(-1))

# 预测x的标签(二进制)
predicted_binarized = clf.predict(x_text)
"""[1 1 0 0]"""

# 将二进制转换为标签
predicted_label = lb.inverse_transform(predicted_binarized)
print(predicted_label)
"""['male' 'male' 'female' 'female']"""

# 3.5计算准确率
# gender_accuracy_score = accuracy_score(y_test, predicted_label)
gender_accuracy_score = accuracy_score(y_test_binarized, predicted_binarized)
print(gender_accuracy_score)

# 3.6计算精准率(只能使用二进制)
gender_precision_score = precision_score(y_test_binarized, predicted_binarized)
print(gender_precision_score)

# 3.7计算召回率(只能使用二进制)
gender_recall_score = recall_score(y_test_binarized, predicted_binarized)
print(gender_recall_score)

# 3.8计算F1统计变量(精准率和召回率的调和平均值)
gender_f1_score = f1_score(y_test_binarized, predicted_binarized)
print(gender_f1_score)

# 3.9计算马修斯相关系数MCC
gender_mcc_score = matthews_corrcoef(y_test_binarized, predicted_binarized)
print(gender_mcc_score)

# 3.10生成精准率、召回率、F1得分
# gender_report = classification_report(y_test_binarized, predicted_binarized, target_names=["male"], labels=[1])
gender_report = classification_report(y_test_binarized, predicted_binarized)
print(gender_report)
代码结果:

['male' 'male' 'female' 'female']
0.75
0.5
1.0
0.6666666666666666
0.5773502691896258
precision recall f1-score support

0 1.00 0.67 0.80 3
1 0.50 1.00 0.67 1

micro avg 0.75 0.75 0.75 4
macro avg 0.75 0.83 0.73 4
weighted avg 0.88 0.75 0.77 4
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
from sklearn.neighbors import KNeighborsRegressor

import numpy as np

X_train = np.array([
[158, 1],
[170, 1],
[183, 1],
[191, 1],
[155, 0],
[163, 0],
[180, 0],
[158, 0],
[170, 0]
])
y_train = [64, 86, 84, 80, 49, 59, 67, 54, 67]

# 预测数据
x_text = np.array([
[168, 1],
[180, 1],
[160, 0],
[169, 0]
])

y_test = [65, 96, 52, 67]

K = 3

# 实例化KNeighborsRegressor类
clf = KNeighborsRegressor(n_neighbors=K)

# 调用fit方法
clf.fit(X_train, y_train)

# 预测x的体重
predictions = clf.predict(x_text)
print("Predicted wights: %s" % predictions)

# 计算确定系数
wieghts_r2_score = r2_score(y_test, predictions)
print("Coefficient of determination: %s" % wieghts_r2_score)

# 计算平均绝对误差
wieghts_mean_absolute_error = mean_absolute_error(y_test, predictions)
print("Mean absolute error: %s" % wieghts_mean_absolute_error)

# 计算均方误差
wieghts_mean_squared_error = mean_squared_error(y_test, predictions)
print("Mean squared error: %s" % wieghts_mean_squared_error)
代码结果:

Predicted wights: [70.66666667 79. 59. 70.66666667]
Coefficient of determination: 0.6290565226735438
Mean absolute error: 8.333333333333336
Mean squared error: 95.8888888888889
import numpy as np
from scipy.spatial.distance import euclidean

# heights in millimeters
X_train = np.array([
[1700, 1],
[1600, 0]
])
x_test = np.array([1640, 1]).reshape(1, -1)

# 计算欧氏距离(Euclidean Distance)
print(euclidean(X_train[0, :], x_test))
print(euclidean(X_train[1, :], x_test))

# heights in meters
X_train = np.array([
[1.7, 1],
[1.6, 0]
])
x_test = np.array([1.64, 1]).reshape(1, -1)

print(euclidean(X_train[0, :], x_test))
print(euclidean(X_train[1, :], x_test))
代码结果:

60.0
40.01249804748511
0.06000000000000005
1.0007996802557444
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
from sklearn.neighbors import KNeighborsRegressor

import numpy as np
from sklearn.preprocessing import StandardScaler

X_train = np.array([
[158, 1],
[170, 1],
[183, 1],
[191, 1],
[155, 0],
[163, 0],
[180, 0],
[158, 0],
[170, 0]
])
y_train = [64, 86, 84, 80, 49, 59, 67, 54, 67]

# 预测数据
x_test = np.array([
[168, 1],
[180, 1],
[160, 0],
[169, 0]
])

y_test = [65, 96, 52, 67]

# 实例化StandardScaler类
ss = StandardScaler()
# 调用fit_transform方法
X_train_scaled = ss.fit_transform(X_train)
print(X_train)
print(X_train_scaled)

x_test_scaled = ss.fit_transform(x_test)
print(x_test)
print(x_test_scaled)

K = 3

# 实例化KNeighborsRegressor类
clf = KNeighborsRegressor(n_neighbors=K)

# 调用fit方法
clf.fit(X_train_scaled, y_train)

# 预测x的体重
predictions = clf.predict(x_test_scaled)
print("Predicted wights: %s" % predictions)

# 计算确定系数
wieghts_r2_score = r2_score(y_test, predictions)
print("Coefficient of determination: %s" % wieghts_r2_score)

# 计算平均绝对误差
wieghts_mean_absolute_error = mean_absolute_error(y_test, predictions)
print("Mean absolute error: %s" % wieghts_mean_absolute_error)

# 计算均方误差
wieghts_mean_squared_error = mean_squared_error(y_test, predictions)
print("Mean squared error: %s" % wieghts_mean_squared_error)
代码结果:

[[158 1]
[170 1]
[183 1]
[191 1]
[155 0]
[163 0]
[180 0]
[158 0]
[170 0]]
[[-0.9908706 1.11803399]
[ 0.01869567 1.11803399]
[ 1.11239246 1.11803399]
[ 1.78543664 1.11803399]
[-1.24326216 -0.89442719]
[-0.57021798 -0.89442719]
[ 0.86000089 -0.89442719]
[-0.9908706 -0.89442719]
[ 0.01869567 -0.89442719]]
[[168 1]
[180 1]
[160 0]
[169 0]]
[[-0.17557375 1. ]
[ 1.50993422 1. ]
[-1.29924573 -1. ]
[-0.03511475 -1. ]]
Predicted wights: [78. 83.33333333 54. 64.33333333]
Coefficient of determination: 0.6706425961745109
Mean absolute error: 7.583333333333336
Mean squared error: 85.13888888888893

---------------------

用K-近邻算法分类和回归的更多相关文章

  1. 分类算法----k近邻算法

    K最近邻(k-Nearest Neighbor,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一.该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的 ...

  2. 第4章 最基础的分类算法-k近邻算法

    思想极度简单 应用数学知识少 效果好(缺点?) 可以解释机器学习算法使用过程中的很多细节问题 更完整的刻画机器学习应用的流程 distances = [] for x_train in X_train ...

  3. 基本分类方法——KNN(K近邻)算法

    在这篇文章 http://www.cnblogs.com/charlesblc/p/6193867.html 讲SVM的过程中,提到了KNN算法.有点熟悉,上网一查,居然就是K近邻算法,机器学习的入门 ...

  4. 机器学习(四) 机器学习(四) 分类算法--K近邻算法 KNN (下)

    六.网格搜索与 K 邻近算法中更多的超参数 七.数据归一化 Feature Scaling 解决方案:将所有的数据映射到同一尺度 八.scikit-learn 中的 Scaler preprocess ...

  5. 机器学习(四) 分类算法--K近邻算法 KNN (上)

    一.K近邻算法基础 KNN------- K近邻算法--------K-Nearest Neighbors 思想极度简单 应用数学知识少 (近乎为零) 效果好(缺点?) 可以解释机器学习算法使用过程中 ...

  6. 02-19 k近邻算法(鸢尾花分类)

    [TOC] 更新.更全的<机器学习>的更新网站,更有python.go.数据结构与算法.爬虫.人工智能教学等着你:https://www.cnblogs.com/nickchen121/ ...

  7. python 机器学习(二)分类算法-k近邻算法

      一.什么是K近邻算法? 定义: 如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别. 来源: KNN算法最早是由Cover和Hart提 ...

  8. 机器学习PR:k近邻法分类

    k近邻法是一种基本分类与回归方法.本章只讨论k近邻分类,回归方法将在随后专题中进行. 它可以进行多类分类,分类时根据在样本集合中其k个最近邻点的类别,通过多数表决等方式进行预测,因此不具有显式的学习过 ...

  9. 从K近邻算法谈到KD树、SIFT+BBF算法

    转自 http://blog.csdn.net/v_july_v/article/details/8203674 ,感谢july的辛勤劳动 前言 前两日,在微博上说:“到今天为止,我至少亏欠了3篇文章 ...

  10. k近邻算法

    k 近邻算法是一种基本分类与回归方法.我现在只是想讨论分类问题中的k近邻法.k近邻算法的输入为实例的特征向量,对应于特征空间的点,输出的为实例的类别.k邻近法假设给定一个训练数据集,其中实例类别已定. ...

随机推荐

  1. C#递归加载目录树

    /// 获取目录管理信息集合 /// </summary> /// <returns></returns> public List<CatalogTree&g ...

  2. EF - order by specific values

    List<string> s = new List<string>() { "A", "家", "帮", " ...

  3. Struts2之动态方法调用

    1.感叹号 前台页面 <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...

  4. pjhp实现使用redis来存储session数据

    #设置php处理session的方式为redis,并配置redis的访问地址(因为在redis中绑定的访问地址为127.0.0.1,所以就没有设置访问密码,如果需要请自行搜索)#因为不想修改php.i ...

  5. hdu3664 Permutation Counting(dp)

    hdu3664 Permutation Counting 题目传送门 题意: 在一个序列中,如果有k个数满足a[i]>i:那么这个序列的E值为k,问你 在n的全排列中,有多少个排列是恰好是E值为 ...

  6. 在win7下面清除samba用户的登录状态

    相信会有一部分刚开始测试samba服务器的人会有过这样的疑惑? 在win7下面使用一个samba用户的username和passwd登录过后,之后每次进去都是以这样的username和passwd进去 ...

  7. nginx+lua+redis实现灰度发布_test

    nginx+lua+redis实现灰度发布: 灰度发布是指在黑白之间能够平滑过渡的一种方式 AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见, ...

  8. 2019年 Java 面试题解析

    2019年 Java 面试题解析 转载地址:https://www.cnblogs.com/Zz-maker/p/11193930.html 作者: Zz_maker 包含的模块: 本文分为十九个模块 ...

  9. python爬虫:抓取下载视频文件,合并ts文件为完整视频

    1.获取m3u8文件 2.代码 """@author :Eric-chen@contact :sygcrjgx@163.com@time :2019/6/16 15:32 ...

  10. openstack创建一个虚拟机的过程

      为什要用云? 一.简单的说就是对资源更加合理的分配,使用,比如硬件的数量,带宽等等这些,因为你不能机器买来不需要了再卖掉(当然也可以),带宽跟机房签合同得来一年的,中间不够了也不能加,超了也不退钱 ...