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()
机器学习一般的数据集会划分为两个部分:
划分比例:
数据集划分api
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)