【问题标题】:How to transform my csv file into this scikit learn dataset如何将我的 csv 文件转换成这个 scikit learn 数据集
【发布时间】:2019-04-05 01:27:57
【问题描述】:

对不起,如果我在这里没有使用正确的术语。我有一个包含我自己数据的 csv 文件。我首先需要将它转换成另一个format,这样我就可以将它加载到另一个Python code 中。我展示了以下格式的示例,它是示例加载的 Iris 数据集的子集:

from sklearn import datasets
data = datasets.load_iris()
print(data)

这给了我(我截断了一些部分以保持可读性):

{'data': array([[5.1, 3.5, 1.4, 0.2],
       [4.9, 3. , 1.4, 0.2],
       [4.7, 3.2, 1.3, 0.2],
       [4.6, 3.1, 1.5, 0.2],
       ...
       [6.5, 3. , 5.2, 2. ],
       [6.2, 3.4, 5.4, 2.3],
       [5.9, 3. , 5.1, 1.8]]), 'target': array([0, 0, 0, ... 2, 2, 2]), 'target_names': array(['setosa', 'versicolor', 'virginica'], dtype='<U10'), 'DESCR': 'Iris Plants Database\n====================\n\nNotes\n-----\nData Set Characteristics:\n    :Number of Instances: 150 (50 in each of three classes)\n    :Number of Attributes: 4 numeric, predictive attributes and the class\n    :Attribute Information:\n        - sepal length in cm\n        - sepal width in cm\n        - petal length in cm\n        - petal width in cm\n        - class:\n                - Iris-Setosa\n                - Iris-Versicolour\n                - Iris-Virginica\n    :Summary Statistics:\n\n    ============== ==== ==== ======= ===== ====================\n                    Min  Max   Mean    SD   Class Correlation\n    ============== ==== ==== ======= ===== ====================\n    sepal length:   4.3  7.9   5.84   0.83    0.7826\n    sepal width:    2.0  4.4   3.05   0.43   -0.4194\n    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)\n    petal width:    0.1  2.5   1.20  0.76     0.9565  (high!)\n    ============== ==== ==== ======= ===== ====================\n\n    :Missing Attribute Values: None\n    :Class Distribution: 33.3% for each of 3 classes.\n    :Creator: R.A. Fisher\n    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n    :Date: July, 1988\n\nThis is a copy of UCI ML iris datasets.\nhttp://archive.ics.uci.edu/ml/datasets/Iris\n\nThe famous Iris database, first used by Sir R.A Fisher\n\nThis is perhaps the best known database to be found in the\npattern recognition literature.  Fisher\'s paper is a classic in the field and\nis referenced frequently to this day.  (See Duda & Hart, for example.)  The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant.  One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\nReferences\n----------\n   - Fisher,R.A. "The use of multiple measurements in taxonomic problems"\n     Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n     Mathematical Statistics" (John Wiley, NY, 1950).\n   - Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis.\n     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.\n   - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n     Structure and Classification Rule for Recognition in Partially Exposed\n     Environments".  IEEE Transactions on Pattern Analysis and Machine\n     Intelligence, Vol. PAMI-2, No. 1, 67-71.\n   - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactions\n     on Information Theory, May 1972, 431-433.\n   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS II\n     conceptual clustering system finds 3 classes in the data.\n   - Many, many more ...\n', 'feature_names': ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']}

我可以生成第一个“数据”数组和第二个“目标”数组。但我正在努力处理文件的最后一部分,其中包含一些字典标签,如“target_names”、“feature_names”、“mean”等等。

我在其余代码中需要这些标签,可以在这里找到: https://github.com/gaurav-kaushik/Data-Visualizations-Medium/blob/master/pca_feature_correlation.py

数据集信息在这里: http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_iris.html

理想情况下寻找一段代码来从我的 csv 文件中生成这种格式。

到目前为止我的代码:

from numpy import genfromtxt
data = genfromtxt('myfile.csv', delimiter=',')
features = data[:, :3]
targets = data[:, 3]

myfile.csv 只是 4 列中的随机数,带有标题和几行,只是为了测试。

【问题讨论】:

    标签: python scikit-learn dataset


    【解决方案1】:

    好的。在这篇文章的帮助下,我找到了一种方法: How to create my own datasets using in scikit-learn?

    我的 iris.csv 文件如下所示:

    f1,f2,f3,f4,t
    5.1,3.5,1.4,0.2,0
    4.9,3,1.4,0.2,0
    ....(150 rows)
    

    以及以我在我的 OP 中描述的格式转换此 .csv 的代码:

    import numpy as np
    import csv
    from sklearn.datasets.base import Bunch
    
    def load_my_dataset():
        with open('iris.csv') as csv_file:
            data_file = csv.reader(csv_file)
            temp = next(data_file)
            n_samples = 150 #number of data rows, don't count header
            n_features = 4 #number of columns for features, don't count target column
            feature_names = ['f1','f2','f3','f4'] #adjust accordingly
            target_names = ['t1','t2','t3'] #adjust accordingly
            data = np.empty((n_samples, n_features))
            target = np.empty((n_samples,), dtype=np.int)
    
            for i, sample in enumerate(data_file):
                data[i] = np.asarray(sample[:-1], dtype=np.float64)
                target[i] = np.asarray(sample[-1], dtype=np.int)
    
        return Bunch(data=data, target=target, feature_names = feature_names, target_names = target_names)
    
    data = load_my_dataset()
    

    我同意代码可以变得更聪明一点,但它可以工作,你只需要适应:

    • 你的文件名
    • 数据行数,不计算表头
    • 特征的列数,不计算最后一个目标列
    • 列出功能名称
    • 列出目标名称

    【讨论】:

      猜你喜欢
      • 2016-11-01
      • 2012-06-16
      • 2012-11-29
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2015-03-07
      • 2014-04-29
      • 2015-10-23
      相关资源
      最近更新 更多