我是哭哭我还是苦苦

import numpy as np
import pandas as pd
from sklearn.model_selection import  train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import precision_score, recall_score, roc_curve, accuracy_score
import matplotlib.pyplot as plt

def drop_duplicates(path):
    data = pd.read_csv(path)
    data = data.drop_duplicates()  # 去重
    y = data.iloc[:, -1]
    y = y.drop_duplicates()
    print(y)
# 9 10 8 20 16 19 14 11 12 18 13 5 4 6 21 17 22 1 3 26 23 29 2 27 25 24

def process_data(path):
    data = pd.read_csv(path)
    data = data.drop_duplicates()  # 去重
    # print(data)
    # 定义一个字典 将M 作为键 ;0 作为值。。。
    class_dict = {"M": 0, "F": 1, "I": 2}
    # 将species 下的 替换 成 字典规则
    data["Sex "] = data["Sex "].map(class_dict)  # 将字典的键映射为值 即完成转换
    # print(data)
    data = data.astype(float)
    X = data.iloc[:, :-1]
    y = data.iloc[:, -1] # 9 10 8 20 16 19 14 11 12 18 13 5 4 6 21 17 22 1 3 26 23 29 2 27 25 24
                        # 1 2 3 4 5 6 8 9 10        11 12 13 14 16 17 18 19 20      21 22 23 24 25 26 27 29

    y[y == 1] = 0
    y[y == 2] = 0
    y[y == 3] = 0
    y[y == 4] = 0
    y[y == 5] = 0
    y[y == 6] = 0
    y[y == 7] = 0
    y[y == 8] = 0
    y[y == 9] = 0
    y[y == 10] = 0
    y[y == 11] = 1
    y[y == 12] = 1
    y[y == 13] = 1
    y[y == 14] = 1
    y[y == 16] = 1
    y[y == 17] = 1
    y[y == 18] = 1
    y[y == 19] = 1
    y[y == 20] = 1
    y[y == 21] = 2
    y[y == 22] = 2
    y[y == 23] = 2
    y[y == 24] = 2
    y[y == 25] = 2
    y[y == 26] = 2
    y[y == 27] = 2
    y[y == 29] = 2
    # print(X)
    # print(y)
    # 特征标准化  自变量数据的归一化
    mu = X.mean(0)
    std = X.std(0)
    X = (X - mu) / std  # 归一化  使其范围缩小到(-1,1)
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size = 0.33, random_state = 42)
    return  X_train, X_test, y_train, y_test

def decisionTree( X_train, X_test, y_train, y_test):
    # 默认为gini系数方法 需要指定信息增益的方法-即ID3决策树
    clf = DecisionTreeClassifier(criterion="entropy")# ID3决策树
    clf.fit(X_train,y_train)
    y_predict = clf.predict(X_test)
    # print("预测值-pridict", y_predict)
    print("自己计算-模型准确率", np.sum(y_test == y_predict) / len(y_test))  # 手动计算模型准确率
    # 模型评价
    print("precision_score:", precision_score(y_test, y_predict, average='micro'))
    print("recall_score:", recall_score(y_test, y_predict, average='micro'))
    print("accuracy_score:", accuracy_score(y_test, y_predict))
    # roc曲线绘制
    fpr, tpr, thresholds = roc_curve(y_test, y_predict,pos_label=2)
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.plot(fpr, tpr, color="blue")
    plt.show()

    print("accuracy on training set:",clf.score(X_train, y_train))# 训练集上的精度是 100%,这是因为叶结点都是纯的,树的深度很大
    print("accuracy on test set:{:.3f}".format(clf.score(X_test, y_test)))# 线性模型的精度
    # y_pred = clf.predict(X_test)
    # return y_pred

if __name__ == '__main__':
    path = r"F:\\desktop\\abalone.csv"
    X_train, X_test, y_train, y_test =  process_data(path)
    decisionTree(X_train, X_test, y_train, y_test)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
循环神经网络(RNN)是一种神经网络结构,用于处理序列数据。与传统的前馈神经网络不同,RNN具有循环连接,使得信息可以在网络中传递并保持记忆。RNN的每个时间步都接收一个输入和一个隐藏状态,然后根据当前输入和前一个时间步的隐藏状态计算当前时间步的输出和新的隐藏状态。这种循环结构使得RNN能够对序列数据进行建模和预测。\[1\] RNN的结构可以通过展开图来表示,其中每个时间步都对应一个神经元。在标准的RNN结构中,隐藏层的神经元之间也存在权重连接,使得前面的隐藏状态可以影响后面的隐藏状态。这种权值共享的特点使得RNN能够处理不同长度的序列数据,并且能够捕捉到序列中的时间依赖关系。\[3\] RNN的训练过程通常使用误差值的反向传播和梯度下降算法来更新权重。然而,与前馈神经网络不同,RNN的训练过程需要考虑时间上的依赖关系,因此标准的反向传播算法无法直接应用于RNN。\[2\] 总之,循环神经网络(RNN)是一种具有循环连接的神经网络结构,用于处理序列数据,并能够捕捉到序列中的时间依赖关系。它的训练过程需要考虑时间上的依赖关系,并使用误差值的反向传播和梯度下降算法来更新权重。 #### 引用[.reference_title] - *1* *3* [[深度学习-原理篇]什么是循环神经网络RNN与LSTM](https://blog.csdn.net/keeppractice/article/details/107373069)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [新手你还在苦苦学习神经网络?看完本文相信你必会恍然大悟](https://blog.csdn.net/m0_37971088/article/details/81167475)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值