- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从 pandas DataFrame 开始,d_train
(774 行):
这个想法是遵循示例 here研究岭系数路径。
在该示例中,以下是变量类型:
X, y, w = make_regression(n_samples=10, n_features=10, coef=True,
random_state=1, bias=3.5)
print X.shape, type(X), y.shape, type(y), w.shape, type(w)
>> (10, 10) <type 'numpy.ndarray'> (10,) <type 'numpy.ndarray'> (10,) <type'numpy.ndarray'>
为了避免 this stackoverflow discussion 中提到的问题,我将所有内容都转换为 numpy 数组:
predictors = ['p1', 'p2', 'p3', 'p4']
target = ['target_bins']
X = d_train[predictors].as_matrix()
### X = np.transpose(d_train[predictors].as_matrix())
y = d_train['target_bins'].as_matrix()
w = numpy.full((774,), 3, dtype=float)
print X.shape, type(X), y.shape, type(y), w.shape, type(w)
>> (774, 4) <type 'numpy.ndarray'> y_shape: (774,) <type 'numpy.ndarray'> w_shape: (774,) <type 'numpy.ndarray'>
然后我就跑了(a) 示例中的确切代码,(b) 将参数 fit_intercept = True, normalize = True
添加到 ridge 调用中(我的数据未缩放)得到相同的错误消息:
my_ridge = Ridge()
coefs = []
errors = []
alphas = np.logspace(-6, 6, 200)
for a in alphas:
my_ridge.set_params(alpha=a, fit_intercept = True, normalize = True)
my_ridge.fit(X, y)
coefs.append(my_ridge.coef_)
errors.append(mean_squared_error(my_ridge.coef_, w))
>> ValueError: Found input variables with inconsistent numbers of samples: [4, 774]
正如代码的注释部分所示,我也尝试了“相同”的代码,但使用了转置的 X 矩阵。在创建 X 矩阵
之前,我还尝试了缩放数据。收到相同的错误消息。
最后,我使用“RidgeClassifier”做了同样的事情,并设法得到不同的错误消息。
>> Found input variables with inconsistent numbers of samples: [1, 774]
问题:我不知道这里发生了什么 - 你能帮忙吗?
在 Canopy 1.7.4.3348(64 位)上使用 python 2.7 以及 scikit-learn 18.01-3 和 pandas 0.19.2-2
谢谢。
最佳答案
您需要拥有与特征数量一样多的权重w
(因为您为每个特征学习一个权重),但在您的代码中,权重向量的维度为 774(即训练数据集中的行数),这就是它不起作用的原因。将代码修改为以下内容(改为使用 4 个权重),一切都会正常工作:
w = np.full((4,), 3, dtype=float) # number of features = 4, namely p1, p2, p3, p4
print X.shape, type(X), y.shape, type(y), w.shape, type(w)
#(774L, 4L) <type 'numpy.ndarray'> (774L,) <type 'numpy.ndarray'> (4L,) <type 'numpy.ndarray'>
现在您可以运行 http://scikit-learn.org/stable/auto_examples/linear_model/plot_ridge_coeffs.html#sphx-glr-auto-examples-linear-model-plot-ridge-coeffs-py 中的其余代码通过网格搜索查看权重和误差如何随正则化参数 alpha
变化,并获得以下数字
关于python - scikit-learn 中岭回归的系数路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42031681/
基本上,我的问题是,由于无监督学习是机器学习的一种,是否需要机器“学习”的某些方面并根据其发现进行改进?例如,如果开发了一种算法来获取未标记的图像并找到它们之间的关联,那么它是否需要根据这些关联来改进
生成模型和判别模型似乎可以学习条件 P(x|y) 和联合 P(x,y) 概率分布。但从根本上讲,我无法说服自己“学习概率分布”意味着什么。 最佳答案 这意味着您的模型要么充当训练样本的分布估计器,要么
是否有类似于 的 scikit-learn 方法/类元成本 在 Weka 或其他实用程序中实现的算法以执行常量敏感分析? 最佳答案 不,没有。部分分类器提供 class_weight和 sample_
是否Scikit-learn支持迁移学习?请检查以下代码。 型号 clf由 fit(X,y) 获取 jar 头型号clf2在clf的基础上学习和转移学习 fit(X2,y2) ? >>> from s
我发现使用相同数据的两种交叉验证技术之间的分类性能存在差异。我想知道是否有人可以阐明这一点。 方法一:cross_validation.train_test_split 方法 2:分层折叠。 具有相同
我正在查看 scikit-learn 文档中的这个示例:http://scikit-learn.org/0.18/auto_examples/model_selection/plot_nested_c
我想训练一个具有很多标称属性的数据集。我从一些帖子中注意到,要转换标称属性必须将它们转换为重复的二进制特征。另外据我所知,这样做在概念上会使数据集稀疏。我也知道 scikit-learn 使用稀疏矩阵
我正在尝试在 scikit-learn (sklearn.feature_selection.SelectKBest) 中通过卡方方法进行特征选择。当我尝试将其应用于多标签问题时,我收到此警告: 用户
有几种算法可以构建决策树,例如 CART(分类和回归树)、ID3(迭代二分法 3)等 scikit-learn 默认使用哪种决策树算法? 当我查看一些决策树 python 脚本时,它神奇地生成了带有
我正在尝试在 scikit-learn (sklearn.feature_selection.SelectKBest) 中通过卡方方法进行特征选择。当我尝试将其应用于多标签问题时,我收到此警告: 用户
有几种算法可以构建决策树,例如 CART(分类和回归树)、ID3(迭代二分法 3)等 scikit-learn 默认使用哪种决策树算法? 当我查看一些决策树 python 脚本时,它神奇地生成了带有
有没有办法让 scikit-learn 中的 fit 方法有一个进度条? 是否可以包含自定义的类似 Pyprind 的内容? ? 最佳答案 如果您使用 verbose=1 初始化模型调用前 fit你应
我正在使用基于 rlglue 的 python-rl q 学习框架。 我的理解是,随着情节的发展,算法会收敛到一个最优策略(这是一个映射,说明在什么状态下采取什么行动)。 问题 1:这是否意味着经过若
我正在尝试使用 grisSearchCV 在 scikit-learn 中拟合一些模型,并且我想使用“一个标准错误”规则来选择最佳模型,即从分数在 1 以内的模型子集中选择最简约的模型最好成绩的标准误
我正在尝试离散数据以进行分类。它们的值是字符串,我将它们转换为数字 0,1,2,3。 这就是数据的样子(pandas 数据框)。我已将数据帧拆分为 dataLabel 和 dataFeatures L
每当我开始拥有更多的类(1000 或更多)时,MultinominalNB 就会变得非常慢并且需要 GB 的 RAM。对于所有支持 .partial_fit()(SGDClassifier、Perce
我需要使用感知器算法来研究一些非线性可分数据集的学习率和渐近误差。 为了做到这一点,我需要了解构造函数的一些参数。我花了很多时间在谷歌上搜索它们,但我仍然不太明白它们的作用或如何使用它们。 给我带来更
我知道作为功能 ordinal data could be assigned arbitrary numbers and OneHotEncoding could be done for catego
这是一个示例,其中有逐步的过程使系统学习并对输入数据进行分类。 它对给定的 5 个数据集域进行了正确分类。此外,它还对停用词进行分类。 例如 输入:docs_new = ['上帝就是爱', '什么在哪
我有一个 scikit-learn 模型,它简化了一点,如下所示: clf1 = RandomForestClassifier() clf1.fit(data_training, non_binary
我是一名优秀的程序员,十分优秀!