老师,课上您说到如果在实际应用中逻辑回归的超参数是利用网格搜索来找的,我自己试了一下,下面是代码,您帮我看看哪里不对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #您课上举得例子 import numpy as np import matplotlib.pyplot as plt np.random.seed( 666 ) X = np.random.normal( 0 , 1 , size = ( 200 , 2 )) y = np.array((X[:, 0 ] * * 2 + X[:, 1 ])< 1.5 , dtype = 'int' ) for _ in range ( 20 ): y[np.random.randint( 200 )] = 1 #数据集分成训练数据集和测试数据集 from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 666 ) #使用逻辑回归 from sklearn.linear_model import LogisticRegression log_reg = LogisticRegression() log_reg.fit(X_train, y_train) log_reg.score(X_test, y_test) ##########这里已经能够看到SCORE是多少############## #这里是重点,我想使用网格搜索来查找多项式回归和逻辑回归的超参数 C_PARM = [ 0.1 , 0.2 , 0.3 , 0.4 , 0.5 ] param_grid = [ { 'degree' : [i for i in range ( 1 , 11 )], 'C' : [i for i in C_PARM] } ] from sklearn.model_selection import GridSearchCV grid_search = GridSearchCV(log_reg, param_grid) grid_search.fit(X_train,y_train) #这一步进行fit的时候已经报错,我觉得是因为逻辑回归没有degree这个超参数 from sklearn.preprocessing import PolynomialFeatures from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler def PolynomialLogisticRegression(degree,C): return Pipeline([ ( 'poly' , PolynomialFeatures(degree = degree)), ( 'std_scaler' , StandardScaler()), ( 'log_reg' , LogisticRegression(C = C)) ]) ####老师,我最想表达的是怎么用网格搜索来搜索degree和C这两个超参数,让其跟后面的pipeline一起使用 |
1 | <br><br><br> |