knn算法的使用分析

knn算法怎么实现分类,怎么对相关的数据进行分析,怎么得出相关的结果。。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
#中文乱码解决办法
mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 指定默认字体:解决plot不能显示中文问题
mpl.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题


df = pd.read_excel("石家庄贝壳二手房清洗后数据.xlsx")

####数据处理
df['修建时间']=df['修建时间'].str.replace('年建','')#修改时间转为数字列
df['修建时间']=df['修建时间'].astype('int')
#print(df.info())



newdf=df[(df['修建时间']>=2010) & (df['修建时间']<=2022)]#只绘制2016~2022年的总面积
newdf = newdf.groupby([df['修建时间']]).agg(sum).rename_axis(['年份'])#.reset_index().rename(columns={'y':'sum'})#按年份统计统计所有数字列
####数据处理
print(newdf)

#下面为面积(平方米)列绘制的图片,要绘制什么样的图片取消对应代码的注释即可
##更多图形类型参考https://blog.csdn.net/weixin_34236672/article/details/112522103
#newdf.boxplot(column='面积(平米)',by='年份')#绘制箱型图
#newdf['面积(平米)'].plot()#曲线图
#newdf['面积(平米)'].plot(kind='bar')#柱状图
#newdf['面积(平米)'].plot.area()#块型图

newdf.plot.scatter(x='面积(平米)', y='价格(万)')

plt.title("2010-2022年房屋建筑面积(平米)")
plt.show()

  • 建议你看下这篇博客👉 :KNN算法实现图像分类
  • 除此之外, 这篇博客: knn算法中的 数据集划分 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 机器学习一般的数据集会划分为两个部分:

    • 训练数据:用于训练,构建模型
    • 测试数据:在模型检验时使用,用于评估模型是否有效

    划分比例:

    • 训练集:70% 80% 75%
    • 测试集:30% 20% 25%

    数据集划分api

    • sklearn.model_selection.train_test_split(arrays, *options)
      • 参数:
        • x 数据集的特征值
        • y 数据集的标签值
        • test_size 测试集的大小,一般为float
        • random_state 随机数种子,不同的种子会造成不同的随机采样结果。相同的种子采样结果相同。
      • return
        • x_train, x_test, y_train, y_test
    from sklearn.datasets import load_iris
    from sklearn.model_selection import train_test_split
    # 1、获取鸢尾花数据集
    iris = load_iris()
    # 对鸢尾花数据集进行分割
    # 训练集的特征值x_train 测试集的特征值x_test 训练集的目标值y_train 测试集的目标值y_test
    x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
    print("x_train:\n", x_train.shape)
    # 随机数种子
    x_train1, x_test1, y_train1, y_test1 = train_test_split(iris.data, iris.target, random_state=6)
    x_train2, x_test2, y_train2, y_test2 = train_test_split(iris.data, iris.target, random_state=6)
    print("如果随机数种子不一致:\n", x_train == x_train1)
    print("如果随机数种子一致:\n", x_train1 == x_train2)
    
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632