# 决策树

1138-魏同学

, ,

## 决策树的使用

#%%

import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn import tree
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=1024)

clf = DecisionTreeClassifier(criterion="entropy")
clf.fit(X_train,y_train)
y_ = clf.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy_score(y_test,y_)

#%%



sklearn使用的步骤
1.数据清洗
2.特征工程
3.使用模型进行训练
4.模型参数调优

## 随机森林

import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
import pandas as pd
from sklearn.model_selection import train_test_split

wine

X = wine['data']
y = wine['target']
X.shape

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)
clf = RandomForestClassifier()
clf.fit(X_train,y_train)
y_= clf.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy_score(y_test,y_)


Vieu3.3主题

Q Q 登 录