Waleed Naveed
Waleed Naveed

Reputation: 2381

Importing training data from local directory in python

I need to import some training data from my local directory into a python program. Currently I am following a tutorial and in this tutorial the data is imported with the help of the following code:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True) 

But my problem is that my data is present in my local directory so I cannot use this approach. Your help will be highly appreciated. My local directory contains multiple files and I have to import them all through one variable.

Upvotes: 4

Views: 9078

Answers (2)

Waleed Naveed
Waleed Naveed

Reputation: 2381

I solved the problem with the help of scikit. First install it and then use the below code for reading the files from local directory

 import sklearn.datasets
 data = sklearn.datasets.load_files(path, shuffle='False')

Upvotes: 1

Mohamed Ali JAMAOUI
Mohamed Ali JAMAOUI

Reputation: 14689

create a folder data in your local directory where you put all your data, then you can refer to it using ./data. Then, to access the local data folder, the following should work:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./data/", one_hot = True) 

You can also get the current directory programmatically, and then construct the path to the data directory as follows

import os 

# get the current directory 
current_directory = os.path.join(os.path.dirname(__file__))

# create the path to the data directory within the current directory
data_directory = os.path.join(current_directory, "data")

Then, edit your code as follows:

import os 
from tensorflow.examples.tutorials.mnist import input_data

# get the current directory 
current_directory = os.path.join(os.path.dirname(__file__))

# create the path to the data directory within the current directory
data_directory = os.path.join(current_directory, "data")

mnist = input_data.read_data_sets(data_directory, one_hot = True) 

EDIT: based on your comment, you are asking about how to load your own data in tensorflow:

As recommended in the documentation, if you are new to TF it's better to start with this tutorial:

Upvotes: 1

Related Questions