1 Star 0 Fork 0

AIpi-machine/decesiontree

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
sklearntest.py 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
吴官骏 提交于 2021-12-21 01:45 . decesiontree
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import mglearn
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
class solution:
def datareading(self):
pd.set_option("display.max_columns",1000000)
pd.set_option('display.width',10000)
self.data=pd.read_csv('sonar.csv')
print(self.data)
def datasplit(self):
self.target=self.data.iloc[:,-1].copy()
self.dataset=self.data.iloc[:,0:self.data.shape[1]-1].copy()
print(self.target)
print(self.dataset)
self.x_train,self.x_test,self.y_train,self.y_test=train_test_split(self.dataset,self.target,stratify=self.target,random_state=42)
def treeapply(self):
tree=DecisionTreeClassifier(random_state=0)
tree.fit(self.x_train,self.y_train)
self.tree=tree
print("Accuracy on training set:{:.3f}".format(tree.score(self.x_train,self.y_train)))
print("Accuracy on test set:{:.3f}".format(tree.score(self.x_test,self.y_test)))
def look(self):
from sklearn.tree import export_graphviz
export_graphviz(self.tree,out_file="tree.dot",class_names=["R","L"],impurity=False,filled=True)
import graphviz
with open("tree.dot") as f:
dot_graph=f.read()
graphviz.Source(dot_graph)
plt.show()
s=solution()
s.datareading()
s.datasplit()
s.treeapply()
s.look()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aipi-machine/decesiontree.git
git@gitee.com:aipi-machine/decesiontree.git
aipi-machine
decesiontree
decesiontree
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385