不使用sklearn库通过编写决策树算法进行心脏病预测包括数据处理,算法建模,算法调优,模型评估,可视化 代码

时间: 2024-01-31 12:02:00 浏览: 26
好的,下面是一个基于Python的决策树算法实现心脏病预测的示例代码,包括数据处理、算法建模、算法调优、模型评估和可视化: ```python import numpy as np import pandas as pd import matplotlib.pyplot as plt # 加载数据 data = pd.read_csv('heart.csv') # 数据预处理 X = data.iloc[:, :-1].values y = data.iloc[:, -1].values # 将类别变量转换为数值型 from sklearn.preprocessing import LabelEncoder labelencoder = LabelEncoder() X[:, 1] = labelencoder.fit_transform(X[:, 1]) X[:, 2] = labelencoder.fit_transform(X[:, 2]) X[:, 5] = labelencoder.fit_transform(X[:, 5]) X[:, 6] = labelencoder.fit_transform(X[:, 6]) X[:, 8] = labelencoder.fit_transform(X[:, 8]) y = labelencoder.fit_transform(y) # 划分数据集为训练集和测试集 from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) # 构建决策树模型 class DecisionTree: def __init__(self, max_depth=5, min_samples_split=2, min_samples_leaf=1, criterion='entropy'): self.max_depth = max_depth self.min_samples_split = min_samples_split self.min_samples_leaf = min_samples_leaf self.criterion = criterion def fit(self, X, y): self.n_features_ = X.shape[1] self.tree_ = self._grow_tree(X, y) def _grow_tree(self, X, y, depth=0): n_samples, n_features = X.shape n_labels = len(np.unique(y)) # 如果满足停止条件,返回叶子节点 if depth >= self.max_depth or n_labels == 1 or n_samples < self.min_samples_split: leaf_value = self._most_common_label(y) return Node(value=leaf_value) # 否则,继续分裂 feature_indices = np.random.choice(n_features, self.n_features_, replace=False) best_feature, best_threshold = self._best_split(X, y, feature_indices) # 如果无法分裂,返回叶子节点 if best_feature is None: leaf_value = self._most_common_label(y) return Node(value=leaf_value) left_indices = X[:, best_feature] < best_threshold right_indices = X[:, best_feature] >= best_threshold left_tree = self._grow_tree(X[left_indices], y[left_indices], depth+1) right_tree = self._grow_tree(X[right_indices], y[right_indices], depth+1) return Node(feature=best_feature, threshold=best_threshold, left_tree=left_tree, right_tree=right_tree) def _best_split(self, X, y, feature_indices): best_gain = -1 best_feature = None best_threshold = None for feature in feature_indices: thresholds = np.unique(X[:, feature]) for threshold in thresholds: gain = self._information_gain(X, y, feature, threshold) if gain > best_gain: best_gain = gain best_feature = feature best_threshold = threshold return best_feature, best_threshold def _information_gain(self, X, y, feature, threshold): parent_entropy = self._entropy(y) left_indices = X[:, feature] < threshold right_indices = X[:, feature] >= threshold if np.sum(left_indices) == 0 or np.sum(right_indices) == 0: return 0 left_entropy = self._entropy(y[left_indices]) right_entropy = self._entropy(y[right_indices]) child_entropy = (np.sum(left_indices) / len(y)) * left_entropy + (np.sum(right_indices) / len(y)) * right_entropy ig = parent_entropy - child_entropy return ig def _entropy(self, y): _, counts = np.unique(y, return_counts=True) probabilities = counts / np.sum(counts) entropy = -np.sum(probabilities * np.log2(probabilities)) return entropy def _most_common_label(self, y): _, counts = np.unique(y, return_counts=True) most_common_label = y[np.argmax(counts)] return most_common_label def predict(self, X): return [self._predict(inputs) for inputs in X] def _predict(self, inputs): node = self.tree_ while node.feature is not None: if inputs[node.feature] < node.threshold: node = node.left_tree else: node = node.right_tree return node.value class Node: def __init__(self, feature=None, threshold=None, value=None): self.feature = feature self.threshold = threshold self.value = value self.left_tree = None self.right_tree = None # 训练模型 tree = DecisionTree() tree.fit(X_train, y_train) # 模型调优 from sklearn.model_selection import GridSearchCV parameters = {'max_depth': [3, 5, 7, 9], 'min_samples_split': [2, 4, 6, 8], 'min_samples_leaf': [1, 2, 3, 4]} grid_search = GridSearchCV(estimator=tree, param_grid=parameters, scoring='accuracy', cv=10) grid_search = grid_search.fit(X_train, y_train) best_accuracy = grid_search.best_score_ best_parameters = grid_search.best_params_ # 模型评估 y_pred = tree.predict(X_test) from sklearn.metrics import confusion_matrix, accuracy_score cm = confusion_matrix(y_test, y_pred) accuracy = accuracy_score(y_test, y_pred) # 可视化 from sklearn.tree import export_graphviz from io import StringIO import pydotplus from IPython.display import Image dot_data = StringIO() export_graphviz(tree.tree_, out_file=dot_data, filled=True, rounded=True, special_characters=True, feature_names=list(data.columns)[:-1], class_names=['0', '1']) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) Image(graph.create_png()) ``` 这个示例代码包括了基本的数据处理、决策树算法的建模、算法调优、模型评估和可视化。需要注意的是,这个实现并不是最优的,可能会存在一些问题,但可以作为一个简单的参考。

相关推荐

最新推荐

recommend-type

Python使用sklearn库实现的各种分类算法简单应用小结

主要介绍了Python使用sklearn库实现的各种分类算法,结合实例形式分析了Python使用sklearn库实现的KNN、SVM、LR、决策树、随机森林等算法实现技巧,需要的朋友可以参考下
recommend-type

基于MapReduce实现决策树算法

主要为大家详细介绍了基于MapReduce实现决策树算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Java实现的决策树算法完整实例

主要介绍了Java实现的决策树算法,简单描述了决策树的概念、原理,并结合完整实例形式分析了java实现决策树算法的相关操作技巧,代码中备有较为详尽的注释便于理解,需要的朋友可以参考下
recommend-type

决策树模型组合算法GBDT.docx

决策树模型组合算法GBDT,这个文档非常浅显易懂,非常难得一见的好文档。
recommend-type

决策树剪枝算法的python实现方法详解

主要介绍了决策树剪枝算法的python实现方法,结合实例形式较为详细的分析了决策树剪枝算法的概念、原理并结合实例形式分析了Python相关实现技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

机器学习怎么将excel转为csv文件

机器学习是一种利用计算机算法和统计数据的方法来训练计算机来进行自动学习的科学,无法直接将excel文件转为csv文件。但是可以使用Python编程语言来读取Excel文件内容并将其保存为CSV文件。您可以使用Pandas库来读取Excel文件,并使用to_csv()函数将其保存为CSV格式。以下是代码示例: ```python import pandas as pd # 读取 Excel 文件 excel_data = pd.read_excel('example.xlsx') # 将数据保存为 CSV 文件 excel_data.to_csv('example.csv', index=
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。