python - 构建特征矩阵和标签向量:

标签 python matplotlib scikit-learn

我有一个数据集“Digit”。该数据集包含 1797 张小图像(8x8 像素),每张图像都包含一个手写数字(0-9)。每个图像被视为以像素为特征的数据样本。因此,要构建特征表,您必须将每个 8x8 图像转换为一行特征矩阵,其中 64 个特征列代表 64 个像素。如何为其构建特征矩阵和标签向量???

最佳答案

您可以按照有关监督学习的 scikit-learn 教程进行操作,其中他们使用的是 Digit 数据集

http://scikit-learn.org/stable/tutorial/basic/tutorial.html#loading-an-example-dataset

更多详细信息here 。如果按照示例加载数据集,则可以简单地 reshape 图像:

from sklearn import datasets
digits = datasets.load_digits()
# To apply a classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))

这使得 data 成为一个 2D 矩阵,具有 n_samples 行和任意数量的列,以适应扁平图像。

关于python - 构建特征矩阵和标签向量:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47425356/

相关文章:

python - Braintree paypal 实现到 django-python

python - 在 neo4j 中创建关系的有效方法

python - 如何hstack几个稀疏矩阵(特征矩阵)?

python - 完整性错误: column user_id is not unique in django tastypie test unit

python - 如何计算两个相似的 pandas 列之间的索引交集?

python - Pandas Matplotlib : How to change shape and size of the legend in scatter plot?

python - 如何使用 matplotlib 在 python 中绘制时间戳?

Python 保存空图

python - 向后差异编码如何用于测试集?

python - 如何指定决策树的 graphviz 表示形式的 figsize?