Python 将随机变量传递给sklearn随机搜索(RandomizedSearchCV)
在下面的代码中,Python 将随机变量传递给sklearn随机搜索(RandomizedSearchCV),python,scikit-learn,scipy,scipy.stats,Python,Scikit Learn,Scipy,Scipy.stats,在下面的代码中,interactive()和expon()有什么用途 svm_grid_R = {'kernel':["linear","rbf"], 'C': reciprocal(20,200000), "gamma" : expon(scale=1.0)} 为什么我们不能直接使用range()?expon(比例=1.0)和倒数(20200000)表示什么范围 对于上下文,使用这些参数的代码如下所示: svm_reg = SVR(
interactive()
和expon()
有什么用途
svm_grid_R = {'kernel':["linear","rbf"], 'C': reciprocal(20,200000), "gamma" : expon(scale=1.0)}
为什么我们不能直接使用range()
?expon(比例=1.0)
和倒数(20200000)
表示什么范围
对于上下文,使用这些参数的代码如下所示:
svm_reg = SVR()
rnd_search = RandomizedSearchCV(svm_reg, param_distributions=svm_grid_R,
n_iter=50, cv=5, scoring='neg_mean_squared_error',
verbose=2, random_state=42)
rnd_search.fit(housing_prepared, housing_labels)
我建议您检查脚本中导入函数的部分,以了解它们是什么。根据你的问题,我推断如下:
# Import the distribution
from scipy.stats import expon
# Initialize a random variable with lambda=1 (scale=1)
exponential_rv = expon(scale=1)
# Draw a random sample from this distribution
exponential_rv.rvs()
> 0.780028923390962
应该来自scipy.stats import倒数,这将为您提供一个倒数随机变量倒数
应该来自scipy.stats import expon的expon
,它将为您提供一个指数随机变量
C
和gamma
参数传递给随机搜索。这意味着搜索使用的随机参数将从这两个分布中采样
从技术上讲,您还可以使用range
命令搜索从给定序列中随机抽取数字。另一种方法是向搜索传递一个随机变量,从中对随机参数进行采样。您的代码采用第二种方法
为了更好地理解第二种方法的意义,请尝试以下方法:
# Import the distribution
from scipy.stats import expon
# Initialize a random variable with lambda=1 (scale=1)
exponential_rv = expon(scale=1)
# Draw a random sample from this distribution
exponential_rv.rvs()
> 0.780028923390962
在这种情况下,您的搜索将把
C=0.780028923390962
传递给支持向量机。谢谢,所以倒数
从逆/倒数分布中给出随机数,expon
从λ(刻度)为1.0的指数分布中给出随机数?正是!这就是代码的作用。谢谢,这帮了大忙。