Python机器学习与深度学习之四:机器学习算法评估

一、数据集分成训练集与评估集

1、按2:1把数据分成训练集与评估集合

import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

iris  = datasets.load_iris()

names = ['separ-length','separ-width','petal-length','petal-width','class']
data  = pd.read_csv(r'iris.csv',names = names)

array = data.values
X     = array[:,0:4]
Y     = array[:,4]

test_size = 0.33
seed = 7

X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size = test_size, random_state = seed)
model = LogisticRegression()
model.fit(X_train,Y_train)

result = model.score(X_test,Y_test)

print("算法评估结果: %.3f%%" %(result * 100))

运行结果:

2、按4:1比例分离数据

import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

iris  = datasets.load_iris()

names = ['separ-length','separ-width','petal-length','petal-width','class']
data  = pd.read_csv(r'iris.csv',names = names)

array = data.values
X     = array[:,0:4]
Y     = array[:,4]

test_size = 0.2
seed = 7

X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size = test_size, random_state = seed)
model = LogisticRegression()
model.fit(X_train,Y_train)

result = model.score(X_test,Y_test)

print("算法评估结果: %.3f%%" %(result * 100))

运行结果:

二、K折交叉验证分离

import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression

iris  = datasets.load_iris()

names = ['separ-length','separ-width','petal-length','petal-width','class']
data  = pd.read_csv(r'iris.csv',names = names)

array = data.values
X     = array[:,0:4]
Y     = array[:,4]

num_folds = 10
seed = 7

kfold = KFold(n_splits = num_folds, random_state = seed)
model = LogisticRegression()
result = cross_val_score(model,X,Y,cv = kfold)

print("算法评估结果:%.2f%% (%.2f%%)" %(result.mean()*100,result.std()*100))

运行结果:

三、弃一交叉验证分离

import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.model_selection import LeaveOneOut
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression

iris  = datasets.load_iris()

names = ['separ-length','separ-width','petal-length','petal-width','class']
data  = pd.read_csv(r'iris.csv',names = names)

array = data.values
X     = array[:,0:4]
Y     = array[:,4]

loocv = LeaveOneOut()
model = LogisticRegression()

result = cross_val_score(model,X,Y,cv = loocv)

print("算法评估结果:%.2f%% (%.2f%%)" %(result.mean()*100,result.std()*100))

运行结果:

四、重复随机分离

import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression

iris  = datasets.load_iris()

names = ['separ-length','separ-width','petal-length','petal-width','class']
data  = pd.read_csv(r'iris.csv',names = names)

array = data.values
X     = array[:,0:4]
Y     = array[:,4]

n_splits = 10
test_size = 0.33
seed = 7

kfold = ShuffleSplit(n_splits = n_splits, test_size = test_size, random_state = seed)
model = LogisticRegression()

result = cross_val_score(model,X,Y,cv = kfold)

print("算法评估结果:%.2f%% (%.2f%%)" %(result.mean()*100,result.std()*100))

运行结果:

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值