首页 > 其他分享 >随机森林+SVM+参数调优

随机森林+SVM+参数调优

时间:2022-11-02 15:44:59浏览次数:54  
标签:10 auc SVM df train grid 随机 iloc 调优

注意事项

  1. 最好使用回归而不是分类,回归可以打分,可以认为划定阈值,从而调整灵敏和特异

参数调优-以SVM为例

from sklearn import svm

from sklearn.model_selection import GridSearchCV

from sklearn.svm import SVC

train_x = sample_feature_train_df.iloc[:,1:-1]

train_y = sample_feature_train_df.iloc[:,-1]

test_x = sample_feature_test_df.iloc[:,1:-1]

test_y = sample_feature_test_df.iloc[:,-1]


param_grid=[{"kernel":["rbf"],"C":[0.1, 1, 10,15,20], "gamma": [10,5,1, 0.1, 0.01]},

            {"kernel":["poly"],"C": [0.1, 1, 10,15,20], "gamma": [10,5,1, 0.1, 0.01],"degree":[3,5,10,15,20],"coef0":[0,0.1,1,5,10,15]},

            {"kernel":["sigmoid"], "C": [0.1, 1, 10,15,20], "gamma": [10,5,1, 0.1, 0.01],"coef0":[0,0.1,1,5,10,15]}]

  
  
grid = GridSearchCV(SVC(), param_grid=param_grid, cv=4)

grid.fit(train_x,train_y)

print('grid_best_params:',  grid.best_params_)

print('grid.best_score_:', grid.best_score_)

随机森林

from sklearn.model_selection import GridSearchCV

train_x = sample_feature_train_df.iloc[:,1:-1]

train_y = sample_feature_train_df.iloc[:,-1]

test_x = sample_feature_test_df.iloc[:,1:-1]

test_y = sample_feature_test_df.iloc[:,-1]


# 参数搜索
param_grid=[{"n_estimators":[10,50,80,100,150,200,300],"criterion":['gini','entropy'], "max_depth": [None,1,3,5,10,15,20,40]}]

grid = GridSearchCV(RandomForestClassifier(), param_grid=param_grid, cv=5)

grid.fit(train_x,train_y)

print('grid_best_params:',  grid.best_params_)

print('grid.best_score_:', grid.best_score_)

  
  
  
  

# 训练模型,并利用auc评估模型性能

rskf = RepeatedStratifiedKFold(n_splits=10, n_repeats=1, random_state=0)

auc_list=[]

for train_index, valid_index in rskf.split(train_x, train_y):

    X_train1, y_train1 = train_x.iloc[train_index, :], train_y.iloc[train_index]

    X_valid1, y_valid1 = train_x.iloc[valid_index, :], train_y.iloc[valid_index]

    clf = RandomForestClassifier(n_estimators=100, max_depth=None,min_samples_split=2, random_state=0)

    clf.fit(X_train1, y_train1)

    predict_value = clf.predict(X_valid1)

    auc = roc_auc_score(y_valid1,predict_value)

    # print(auc)

    auc_list.append(auc)

print(np.mean(auc_list))

SVM


from sklearn import svm

from sklearn.model_selection import GridSearchCV

from sklearn.svm import SVC

train_x = sample_feature_train_df.iloc[:,1:-1]

train_y = sample_feature_train_df.iloc[:,-1]

test_x = sample_feature_test_df.iloc[:,1:-1]

test_y = sample_feature_test_df.iloc[:,-1]



# 训练模型,并利用auc评估模型性能
rskf = RepeatedStratifiedKFold(n_splits=10, n_repeats=1, random_state=0)
auc_list=[]

for train_index, valid_index in rskf.split(train_x, train_y):

    X_train1, y_train1 = train_x.iloc[train_index, :], train_y.iloc[train_index]

    X_valid1, y_valid1 = train_x.iloc[valid_index, :], train_y.iloc[valid_index]

    clf = svm.SVC(C=10)  

    clf.fit(X_train1, y_train1)

    predict_value = clf.predict(X_valid1)

    auc = roc_auc_score(y_valid1,predict_value)

    # print(auc)

    auc_list.append(auc)

print(np.mean(auc_list))

标签:10,auc,SVM,df,train,grid,随机,iloc,调优
From: https://www.cnblogs.com/kang1010/p/16851188.html

相关文章

  • Bagging与随机森林
    1.集成学习三大领域集成学习(Ensemblelearning)方法会训练多个弱评估器(baseestimators),并将他们的输出结果以某种方式结合起来解决一个问题。模型融合投票法Voting、......
  • 随机变量函数-余丙森
    https://www.bilibili.com/video/BV1te4y1S7zY?p=13&vd_source=18c316629aa624cb452f9acf73595c09......
  • 笔记:java如何获取,指定范围的随机数?
    一、需求:如何获取一个指定范围的随机数,进行业务操作? 二、代码示例://传入指定的数值区间publicstaticintgetRandom(intmin,intmax){Randomrandom=newR......
  • 【经验】通过JVM调优,让凯哥个人博客响应速度提升了不少
    为什么你的个人博客访问慢?不知道大家有没有注意到,在22.10.3121点之后,凯哥的个人博客站点(凯哥Java:www.kaigejava.com)访问速度提升了不少。那是因为凯哥对站点做了优化......
  • 【经验】通过JVM调优,让凯哥个人博客响应速度提升了不少
    为什么你的个人博客访问慢?​不知道大家有没有注意到,在22.10.3121点之后,凯哥的个人博客站点(凯哥Java:www.kaigejava.com)访问速度提升了不少。那是因为凯哥对站点做了优化......
  • jQuery制作随机色弹幕墙
    jQuery功能确实强大,虽然用起来不熟练但是在百度大法的帮助下还是挺好使的,今天便用它实现了一个弹幕墙。上张成果图,页面没用心写,有点辣眼睛界面制作就不啰嗦了,后面会放上......
  • 随机化算法解决圆排列问题 - python解法
    问题描述给定n个大小不等的圆,现要将这n个圆排进一个矩形框中,且要求各圆与矩形框的底边相切。圆排列问题要求从n个圆的所有排列中找出有最小长度的圆排列。例如,当n=3,且所给......
  • 第11章 支撑向量机 SVM
     11-1什么是支持向量机           11-2支持向量机的效用函数推导               ......
  • 四、Spark性能调优
    目录​​1.常规性能调优​​​​常规性能调优一:最优资源配置​​​​常规性能调优二:RDD优化​​​​RDD复用​​​​RDD持久化​​​​ RDD尽可能早的filter操作​​​​常......
  • js常用方法和一些封装(2) -- 随机数生成
    任何编程语言,随机数都是必不可少的,我在开发过程中,尤其在自己做一些小玩意的时候,就经常使用随机数,后来发现每次使用都懒得写,直接去网上搜一个,拿过来就用了。可是时间一长,发现......