算法练习-KNN分类预测

所需文件如下:,提取码: uy9a
文件获取

KNN分类

#导入需要的包
import numpy as np
import pandas as pd
#读取文件
data = pd.read_csv("./iris.csv")
#清洗数据,将Species列转化为数字
data["Species"] = data["Species"].map({"virginica":0,"setosa":1,"versicolor":2})
#删除Id列并替换data中的数据
data.drop("Id",axis=1,inplace=True)
#删除重复行并替换data
data.drop_duplicates(inplace=True)
#KNN算法类
class KNN:
    """实现KNN邻近算法 (分类) """
    def __init__(self, k):
        """构造方法k:int  拿k个"""
        self.k = k
    def fit(self, X, y):
        # X:类数组类型
        # 训练样本特征(属性) 7.2,3.2,6,1.8
        # y:类数组类型,形状为[标签数量]
        # 每个样本的目标值 (标签) 0 1 2
        pass
        self.X = np.asarray(X)
        self.y = np.asarray(y)
    def predict(self, X):
        # 根据样本特征,预测特征 result:数组类型 预测结果
        X = np.asarray(X)
        result = []
        # 计算距离
        for x in X:
            dis = np.sum((x - self.X) ** 2, axis=1)
            # 排序取下标
            index = dis.argsort()
            #取k个
            index = index[:self.k]
            # 算次数将次数最多的加入结果集
            count = np.bincount(self.y[index])
            result.append(count.argmax())
        return np.asarray(result)
#筛选
t0 = data[data["Species"]==0]
t1 = data[data["Species"]==1]
t2 = data[data["Species"]==2]
#取样
t0 = t0.sample(len(t0),random_state=0)
t1 = t1.sample(len(t1),random_state=0)
t2 = t2.sample(len(t2),random_state=0)
#构造真实数据
#训练集
train_X = pd.concat([t0.iloc[:40,:-1],t1.iloc[:40,:-1],t2.iloc[:40,:-1]],axis=0)
train_y = pd.concat([t0.iloc[:40,-1],t1.iloc[:40,-1],t2.iloc[:40,-1]],axis=0)
#测试集
test_X = pd.concat([t0.iloc[40:,:-1],t1.iloc[40:,:-1],t2.iloc[40:,:-1]],axis=0)
test_y = pd.concat([t0.iloc[40:,-1],t1.iloc[40:,-1],t2.iloc[40:,-1]],axis=0)
#使用knn算出结果
knn = KNN(k=80)
knn.fit(train_X,train_y)
result = knn.predict(test_X)
#计算预测算法的准确率
print(np.sum(result == test_y) / len(test_y))


#可视化
import matplotlib as mpl
#import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
#设置中文
mpl.rcParams["font.family"] = "SimHei"
mpl.rcParams["axes.unicode_minus"]=False
#训练图
plt.figure(figsize=(10,10))
plt.scatter(x=t0["SepalLengthCm"][:40],y=t0["PetalLengthCm"][:40],color="r",label="virginica")
plt.scatter(x=t1["SepalLengthCm"][:40],y=t1["PetalLengthCm"][:40],color="g",label="setosa")
plt.scatter(x=t2["SepalLengthCm"][:40],y=t2["PetalLengthCm"][:40],color="b",label="versicolo")

#测试图 真实test_y 预测result
right = test_X[test_y == result]
wrong = test_X[test_y != result]
plt.scatter(x=right["SepalLengthCm"],y=right["PetalLengthCm"],color="c",label="right",marker="x")
plt.scatter(x=wrong["SepalLengthCm"],y=wrong["PetalLengthCm"],color="m",label="wrong",marker=">")
plt.legend(loc="best")
plt.title("KNN分类算法显示")
plt.xlabel("花萼")
plt.ylabel("花瓣")
plt.show()

KNN回归

class KNN2:
    #回归算法
    def __init__(self,k):
        self.k=k
    def fit(self,X,y):
        self.X = np.asarray(X)
        self.y = np.asarray(y)
    def predict(self,X):
        X = np.asarray(X)
        result = []
        for x in X:
            dis = np.sum((x-self.X)**2,axis=1)
            
            index = dis.argsort()
            index = index[:self.k]
            
            result.append(np.mean(self.y[index]))
        return np.asarray(result)
    def predict2(self,X):
        
        X = np.asarray(X)
        result = []
        for x in X:
            dis = np.sum((x-self.X)**2,axis=1)
            
            index = dis.argsort()
            index = index[:self.k]
            #,倒数和避免除0加一个很小很小的数
            daoshu = 1/dis[index]+0.000000001
            he = np.sum(daoshu)
            #权重
            weight = daoshu/he
            result.append(np.mean(self.y[index]*weight))
        return np.asarray(result)

data = pd.read_csv("./iris.csv")
len(data)
data.drop(["Id","Species"],axis=1,inplace=True)
data.drop_duplicates(inplace=True)
X = data.sample(len(data),random_state=0)
train_X = X.iloc[:120,:-1]
train_y = X.iloc[:120,-1]
test_X = X.iloc[120:,:-1]
test_y = X.iloc[120:,-1]
X = data.sample(len(data),random_state=0)
train_X = X.iloc[:120,:-1]
train_y = X.iloc[:120,-1]
test_X = X.iloc[120:,:-1]
test_y = X.iloc[120:,-1]
np.sum((result-test_y.values)**2)/len(result)
import matplotlib as mpl
#import matplotlib.pyplot as plt
from matplotlib import pyplot as plt

#设置中文
mpl.rcParams["font.family"] = "SimHei"
mpl.rcParams["axes.unicode_minus"]=False
plt.figure(figsize=(8,8))
plt.plot(result,"ro-",label="预测值")
plt.plot(test_y.values,"go--",label="真实值")
plt.title("KNN回归算法")
plt.xlabel("序号")
plt.ylabel("度量值")
plt.legend()
plt.show()

最小二乘法求线性回归方程

import numpy as np
import pandas as pd 
class LinearRegression:
    def __init__(self,X,y):
        X = np.asmatrix(X.copy())
        y = np.asmatrix(y).reshape(-1,1)
#         T转置 I求逆
        self.w_ = ((X.T) * X).I * (X.T) * y
    def predict(self,X):
        X = np.asmatrix(X.copy())
        result = X * self.w_
        return np.array(result).ravel()
data = pd.read_csv("./boston.csv")
t = data.sample(len(data),random_state=0)
train_X = t.iloc[:400,:-1]
train_y = t.iloc[:400,-1]
test_X = t.iloc[400:,:-1]
test_y = t.iloc[400:,-1]
lr = LinearRegression(X=train_X,y=train_y)
res = lr.predict(test_X)
np.sum((res-test_y.values)**2) /len(res)
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值