sklearn學習筆記之決策樹分類和線性回歸

2021-08-18 09:53:42 字數 2776 閱讀 2441

decisoin tree:

# -*- coding: utf-8 -*-

importsklearn

fromsklearnimporttree

importmatplotlib.pyplotasplt

fromsklearn.model_selectionimporttrain_test_split

fromsklearnimportdatasets

importpandasaspd

importnumpy

defgetdata_1():

iris = datasets.load_iris()

x = iris.data #樣本特徵矩陣,150*4矩陣,每行乙個樣本,每個樣本維度是4

y = iris.target #樣本類別矩陣,150維行向量,每個元素代表乙個樣本的類別

df1=pd.dataframe(x, columns =['sepallengthcm','sepalwidthcm','petallengthcm','petalwidthcm'])

df1['target']=y

returndf1

df=getdata_1()

x_train, x_test, y_train, y_test = train_test_split(df.iloc[:,0:3],df['target'], test_size=0.3, random_state=42)

printx_train, x_test, y_train, y_test

model = tree.decisiontreeclassifier(criterion='gini') #cart樹

model.fit(x_train, y_train)

model2= tree.decisiontreeclassifier(criterion='entropy') #c4.5樹

model2.fit(x_train, y_train)

print'cart樹:'.format(model.score(x_test, y_test)) # 決策樹

print'c4.5樹::'.format(model2.score(x_test, y_test))

結果:輸出的準確度

# -*- coding: utf-8 -*-

importsklearn

fromsklearn.datasets.samples_generatorimportmake_classification

fromsklearn.linear_modelimportlinearregression

importmatplotlib.pyplotasplt

fromsklearn.model_selectionimporttrain_test_split

x, y = make_classification(n_samples=2400, n_features=5, n_informative=2,

n_redundant=2, n_classes=2, n_clusters_per_class=2, scale=1.0,

random_state=20)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)

model = linearregression(fit_intercept=true, normalize=false,

copy_x=true, n_jobs=1)

model.fit(x_train, y_train)

print'finish'printmodel.score(x_train, y_train) # 線性回歸:r square; 分類問題: acc

printmodel.score(x_test, y_test)

printx_train,y_train

printx_test,y_test

sklearn 決策樹學習筆記

遍歷眾多特徵,計算每一次分類後的資訊增益,選取分類後熵值最小的特徵作為當前分類節點 防止過擬合,當每個資料都是乙個葉結點的時候,分類正確率是100 但是樹過於龐大。from sklearn.datasets.california housing import fetch california hou...

SKlearn之決策樹

決策樹是一種非引數的監督學習方法。模組 sklearn.tree sklearn建模的步驟 1 選擇並建立模型 例 clf tree.decisiontreeclassifier 2 提供資料訓練模型 例 clf clf.fit x train,y train 3 獲取需要的資訊 例 result ...

sklearn機器學習 決策樹

tree.decisiontreeclassifier 分類樹 tree.decisiontreeregressor 回歸樹 tree.export graphviz 將生成的決策樹匯出為dot格式,畫圖專用 from sklearn import tree 匯入需要的模組 clf tree.dec...