如何将我的csv文件转换为此scikit学习数据集

时间:2018-10-31 19:41:16

标签: python scikit-learn dataset

很抱歉,如果我在这里使用的术语不正确。我有一个带有自己数据的csv文件。我首先需要将其转换为另一个format,以便可以将其加载到另一个Python code中。我在下面显示了一个格式示例,它是虹膜数据集的子集,示例通过该数据集进行加载:

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列的随机数,其中包含标题和几行,仅供测试。

1 个答案:

答案 0 :(得分: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()

我同意可以使代码更加智能,但是它可以工作,您只需要进行以下调整:

  • 您的文件名
  • 数据行数,不计标题
  • 要素的列数,不计算最后一个目标列
  • 列出功能名称
  • 列出目标名称