代码拉取完成,页面将自动刷新
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。