# 网格搜索优化参数
from pandas import read_csv
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV
filename = 'D:/0520代码+数据/第3、4次课:代码+数据/pima_data.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
array = data.values
X = array[:, 0:8]
Y = array[:, 8]
model = Ridge()
# 设置遍历参数
param_grid = {'alpha' : [1, 0.1, 0.01, 0.001, 0]}
grid = GridSearchCV(estimator=model, param_grid=param_grid)
grid.fit(X, Y)
# 结果
print('最高得分: %.3f' %grid.best_score_)
print('最优参数: %s' %grid.best_estimator_.alpha)
# 随机优化参数
from pandas import read_csv
from sklearn.linear_model import Ridge
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform
filename = 'D:/0520代码+数据/第3、4次课:代码+数据/pima_data.csv'
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
data = read_csv(filename, names=names)
array = data.values
X = array[:, 0:8]
Y = array[:, 8]
model = Ridge()
# 设置遍历参数
param_grid = {'alpha': uniform()}
grid = RandomizedSearchCV(estimator=model, param_distributions=param_grid, n_iter=100,random_state=7)
grid.fit(X, Y)
print('最高得分: %.3f' %grid.best_score_)
print('最优参数: %s' %grid.best_estimator_.alpha)
机械学系之-搜索_算法调参
最新推荐文章于 2024-11-06 11:05:01 发布