2017-08-18 16 views
-1

csvからデータを読み取るサンプルコードを入手できますか? 私の要件は、TensorFlowでCSVから列車とテストデータを生成する必要があります。TensorFlowでcsvファイルからデータを読み取る方法

トレインデータとテストデータの両方を含む1つのCSV。私は列車のために最初の10行を意味し、次は試験のために10を意味します。 ありがとうございました

+0

の可能性のある重複した[実際* \する方法\ * TensorFlowでCSVデータを読み込む?](https://でのstackoverflow。 com/questions/37091899/how-to-actually-csv-data-in-tensorflow) – Rthomas529

+0

csvのカラムはどのように分散されていますか?あなたは何人のcsvの話をしていますか? – DarkCygnus

+0

TrainとTestの両方のデータを含む1つのCSV。私は列車のために最初の10列を意味し、次に試験のために10列を意味する –

答えて

0

皆さんはTensorFlowでexcellent tutorialを作成しました。 csvから国勢調査データを読み込み、それをテンソルに変換し、高度な見積もりAPIを使用して機械学習モデルに適合させ評価する方法について説明します。

しかし、urllib関数を試したときにエラーが発生しました。コードを少し変更して、pandasを使って直接データを読み込むようにしました。

オリジナルコード

import tempfile 
import urllib 
train_file = tempfile.NamedTemporaryFile() 
test_file = tempfile.NamedTemporaryFile() 
urllib.urlretrieve("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data", train_file.name) 
urllib.urlretrieve("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.test", test_file.name) 

import pandas as pd 
CSV_COLUMNS = [ 
    "age", "workclass", "fnlwgt", "education", "education_num", 
    "marital_status", "occupation", "relationship", "race", "gender", 
    "capital_gain", "capital_loss", "hours_per_week", "native_country", 
    "income_bracket"] 
df_train = pd.read_csv(train_file.name, names=CSV_COLUMNS, skipinitialspace=True) 
df_test = pd.read_csv(test_file.name, names=CSV_COLUMNS, skipinitialspace=True, skiprows=1) 

修正コード

import pandas as pd 
COLUMNS = ["age", "workclass", "fnlwgt", "education", "education_num", 
      "marital_status", "occupation", "relationship", "race", "gender", 
      "capital_gain", "capital_loss", "hours_per_week", "native_country", 
      "income_bracket"] 

df_train = pd.read_csv('http://mlr.cs.umass.edu/ml/machine-learning-databases/adult/adult.data' 
         , names=COLUMNS 
         , skipinitialspace=True) 
df_test = pd.read_csv('http://mlr.cs.umass.edu/ml/machine-learning-databases/adult/adult.test' 
         , names=COLUMNS 
         , skipinitialspace=True 
         , skiprows=1) 
関連する問題