Python sklearn 交叉驗證

2021-10-05 23:16:49 字數 1215 閱讀 1596

from sklearn.datasets import load_boston

from sklearn.model_selection import cross_val_score

from sklearn.tree import decisiontreeregressor

boston = load_boston()

regressor = decisiontreeregressor(random_state=0) # 例項化

# 交叉驗證有5個引數

# 第乙個引數:可以是任何例項化後的演算法模型;

# 第二個引數:不需要劃分測試集和驗證集的資料;第三個引數:完整的不需要劃分的標籤

# 第四個引數:把資料分為10份,預設是5,通常也選擇5

# 第五個引數:scoring 返回的結果的型別,對於回歸預設為r的平方,可選:neg_mean_squared_error

result = cross_val_score(regressor, boston.data, boston.target, cv=10)

print(result)

輸出結果:

分析:對於decisiontreeregressor,回歸樹介面 score 預設返回的是

from sklearn.datasets import load_boston

from sklearn.model_selection import cross_val_score

from sklearn.tree import decisiontreeregressor

boston = load_boston()

regressor = decisiontreeregressor(random_state=0) # 例項化

result = cross_val_score(regressor, boston.data, boston.target, cv=10, scoring='neg_mean_squared_error')

print(result)

結果:

python sklearn庫實現簡單邏輯回歸

import xlrd import matplotlib.pyplot as plt import numpy as np from sklearn import model selection from sklearn.linear model import logisticregression...

Python sklearn 中的SVM示例

coding utf 8 import pandas as pd from numpy.random import shuffle from sklearn import svm import joblib from sklearn import metrics inputfile data mom...

python sklearn實現中文簡訊垃圾分類

資料讀取 import pandas as pd import jieba data pd.read csv r e 資料 實驗data messages.csv encoding gbk header 0,names id label text print data.head 簡訊分詞 print...