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])))

"""
Explanation: View in Colaboratory
Colab and Jupyter Notebooks Quick Tour
Welcome
First we are going to visit a notebbok from Colab.
Loading  and saving data
There are several options for Google external sources:

Local files
Google Drive
Google Sheets
Google Cloud Storage

Upload local files
With this snippet we can choose a file to upload from our local system:
End of explanation
"""


!ls

"""
Explanation: We can check to see if it was uploaded correctly by using a system call:
End of explanation
"""


!pip install -U -q PyDrive

"""
Explanation: Upload a file from Google Drive using  PyDrive
We must install the PyDrive library first:
End of explanation
"""


from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

"""
Explanation: We need to import the libraries we need
End of explanation
"""


auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

"""
Explanation: We must first authenticate with google and create the GoogleDrive client.
End of explanation
"""


data = drive.CreateFile({'id': '1pi40wcRtqCdqwbBx9ISrAZVLHF-MMQox'})
print('Downloaded content "{}"'.format(data.GetContentString()))

"""
Explanation: Now  we can download the file and print its contents.
If an output is to long we can delete it with the corresponding option in the code cell menu.
End of explanation
"""


import requests
r = requests.get('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data')
print(r.text)

"""
Explanation: Upload a file from the web
Another option is to download the file from the UCI ML Repository. We can use the requests library.
End of explanation
"""


iris_rows = r.text.split('\n')
print (iris_rows[:5])


"""
Explanation: Python + Numpy + Matplotlib
We are going to use python to read the file. We need to split the file into lines first:
End of explanation
"""


iris_rows[-4:]

"""
Explanation: We use slicing to get fragments from the list.
End of explanation
"""


[row.split(',')[:-1] for row in iris_rows[:-2]]


"""
Explanation: We use list comprehensions to get only what we need:
End of explanation
"""


[map(float, row.split(',')[:-1]) for row in iris_rows[:-2]]

"""
Explanation: Finally by using the function map we can get the array we need.
End of explanation
"""


[list(map(float, row.split(',')[:-1])) for row in iris_rows[:-2]]

"""
Explanation: In Python 3, map is a generator object, but we can extract a list.
End of explanation
"""


import numpy
iris_data = [list(map(float, row.split(',')[:-1])) for row in iris_rows[:-2]]
iris = numpy.array(iris_data)
iris[:5]

"""
Explanation: Let's create a NumPy array:
End of explanation
"""


import matplotlib.pyplot as plt
x = iris[:50,0]
y = iris[:50,1]
plt.plot(x, y, 'r.')
plt.show()

"""
Explanation: We can explore the data with matplotlib:
End of explanation
"""


plt.plot( iris[:50,0], iris[:50,1], 'r.')
plt.plot( iris[51:100,0], iris[51:100,1], 'b.')


"""
Explanation: We can plot two types of flowers:
End of explanation
"""


plt.plot( iris[:50,0], iris[:50,1], 'r.')
plt.plot( iris[51:100,0], iris[51:100,1], 'b.')
plt.plot( iris[101:,0], iris[101:,1], 'g.')

"""
Explanation: Let's do three
End of explanation
"""


