Skip to content

How to Import Files to Google Collab

Mateus de Assis Silva edited this page May 29, 2020 · 1 revision

How to import Github Folder to Google Collab

Sometimes is useful to import gihub folder to google collab, because our code is already on github. Suppose we want to use MtxslvNode() class. Then you must copy and paste the next cell:

#this is for the purpose of getting MtxslvNode file
%cd /content/
%rm -r StudyingMachineLearning/
!git clone https://github.com/mtxslv/StudyingMachineLearning.git
% cd StudyingMachineLearning/PaulGAllenSchool/DecisionTrees/codes

Notice I also erase previous folders with same name. It is important to do that to avoid importing the same folder repeatedly.

  • %cd /content/: open the content folder (imported files stay there, when you're using collab)
  • %rm -r StudyingMachineLearning/: remove non-empty StudyingMachineLearning previous directory
  • % cd StudyingMachineLearning/PaulGAllenSchool/DecisionTrees/codes: open folder with desired file

How to get a file from your local file system

The following code is used to upload material from your local file system, as seen here:

from google.colab import files
 
uploaded = files.upload()

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))

Browse to your local file system and upload the data you want to. After doing this, the data will be available in the Collab file System. Let's suppose it is a txt database. You can use numpy.loadtxt. Let's imagine your data looks like this:

17693,608538,3.0
17693,769235,3.0
17693,2593882,1.0
17693,2167828,2.0
17693,2020448,3.0
17693,1630419,4.0
17693,1410683,3.0
17693,263291,4.0
17693,165288,3.0

then you can use:

import numpy as np

training_ratings = np.loadtxt("TrainingRatings.txt",delimiter=',')
testing_ratings = np.loadtxt("TestingRatings.txt",delimiter=',')