- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用this sklearn 文档中的示例。我不太确定代码在做什么,尽管我假设我以错误的方式输入数据集,但我最近收到了此错误:
<ipython-input-26-3c3c0763766b> in <module>()
49 for ds in datasets:
50 # preprocess dataset, split into training and test part
---> 51 X, y = ds
52 X = StandardScaler().fit_transform(X)
53 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4)
ValueError:太多值无法解压关于如何修改代码以处理我的数据集(这是来自 pandas 数据帧的多维 numpy 数组)并修复错误,有什么想法吗?
dataURL = "peridotites_clean_complete.csv"
pd_data = pd.read_csv(dataURL)
rock_names = pd_data['ROCK NAME']
rock_compositions = pd_data.columns[1:]
rock_data = np.vstack([pd_data[x] for x in rock_compositions])
classifiers = [
KNeighborsClassifier(3),
SVC(kernel="linear", C=0.025),
SVC(gamma=2, C=1),
DecisionTreeClassifier(max_depth=5),
RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
AdaBoostClassifier(),
GaussianNB(),
LDA(),
QDA()]
X, y = make_classification(n_features=2, n_redundant=0, n_informative=2,
random_state=1, n_clusters_per_class=1)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(size=X.shape)
linearly_separable = (X, y)
datasets = [rock_data]
figure = plt.figure(figsize=(27, 9))
i = 1
# iterate over datasets
for ds in datasets:
# preprocess dataset, split into training and test part
X, y = ds
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4)
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
# just plot the dataset first
cm = plt.cm.RdBu
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
# Plot the training points
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
# and testing points
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
i += 1
# iterate over classifiers
for name, clf in zip(names, classifiers):
ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, m_max]x[y_min, y_max].
if hasattr(clf, "decision_function"):
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
else:
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
# Put the result into a color plot
Z = Z.reshape(xx.shape)
ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)
# Plot also the training points
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
# and testing points
ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright,
alpha=0.6)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
ax.set_title(name)
ax.text(xx.max() - .3, yy.min() + .3, ('%.2f' % score).lstrip('0'),
size=15, horizontalalignment='right')
i += 1
figure.subplots_adjust(left=.02, right=.98)
plt.show()
最佳答案
问题是 ds
是一个包含两个以上值的列表,如下所示:
>>> ds=['rockatr1','rockatr2','rockatr','rocktype']
>>> X,y=ds
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
您必须指定哪个部分是X
,哪个部分是y
,如下所示。通常在分类数据中,最后一列用作标签,这就是我在这里假设的。
>>> X,y=ds[:-1],ds[-1]
>>> X
['rockatr1', 'rockatr2', 'rockatr']
>>> y
'rocktype'
关于python - 在 Scikit-Learn 中设置多个算法试验时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599495/
来自文档: sklearn.preprocessing.MinMaxScaler.min_ : ndarray, shape (n_features,) Per feature adjustment
这是我的数据:(我重置了索引。日期应该是索引) Date A B C D 0 2013-10-07 -0.002
我正在构建一个分类器,通过贷款俱乐部数据,选择最好的 X 笔贷款。我训练了一个随机森林,并创建了通常的 ROC 曲线、混淆矩阵等。 混淆矩阵将分类器的预测(森林中树木的多数预测)作为参数。但是,我希望
是否有类似于 的 scikit-learn 方法/类元成本 在 Weka 或其他实用程序中实现的算法以执行常量敏感分析? 最佳答案 不,没有。部分分类器提供 class_weight和 sample_
我发现使用相同数据的两种交叉验证技术之间的分类性能存在差异。我想知道是否有人可以阐明这一点。 方法一: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你应
我正在尝试使用 grisSearchCV 在 scikit-learn 中拟合一些模型,并且我想使用“一个标准错误”规则来选择最佳模型,即从分数在 1 以内的模型子集中选择最简约的模型最好成绩的标准误
我有一个预定义的决策树,它是根据基于知识的拆分构建的,我想用它来进行预测。我可以尝试从头开始实现决策树分类器,但那样我就无法在 Scikit 函数中使用 predict 等内置函数。有没有办法将我的树
我正在使用随机森林解决分类问题。为此,我决定使用 Python 库 scikit-learn。但我对随机森林算法和这个工具都很陌生。我的数据包含许多因子变量。我用谷歌搜索,发现像我们在线性回归中所做的
我使用 Keras 回归器对数据进行回归拟合。我使用 Scikit-learn wrapper 和 Pipeline 来首先标准化数据,然后将其拟合到 Keras 回归器上。有点像这样: from s
在 scikit-learn ,有一个 的概念评分函数 .如果我们有一些预测标签和真实标签,我们可以通过调用 scoring(y_true, y_predict) 来获得分数。 .这种评分函数的一个例
我知道 train_test_split 方法将数据集拆分为随机训练和测试子集。并且使用 random_state=int 可以确保每次调用该方法时我们对该数据集都有相同的拆分。 我的问题略有不同。
我正在使用 scikit-learn 0.18.dev0。我知道之前有人问过完全相同的问题 here .我尝试了那里提供的答案,但出现以下错误 >>> def mydist(x, y): ...
我试图在 scikit-learn 中结合递归特征消除和网格搜索。正如您从下面的代码(有效)中看到的那样,我能够从网格搜索中获得最佳估计量,然后将该估计量传递给 RFECV。但是,我宁愿先进行 RFE
我是一名优秀的程序员,十分优秀!