First, upload the data file from your computer using the Colab file upload interface.
(Skip this if you are working locally in Python or Jupyter notebook)
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])))
Examine what this variable uploaded is
uploaded.keys()
Import numpy for math and number related functions, and matplotlib for plotting
import numpy as np
from matplotlib import pyplot as plt
Open the file, loop through and build a dictionary with each planet's information
planets = {}
f = open('planets.txt')
f.readline()
for line in f:
try:
name, mass, radius, period, nothing = line.split(',')
planets[name]={}
planets[name]['Mass'] = float(mass.strip('""'))
planets[name]['Radius'] = float(radius.strip('""'))
planets[name]['Period']= float(period.strip('""'))
except ValueError:
print(line)
masses = []
radii = []
for planet in planets:
try:
masses.append(planets[planet]['Mass'])
radii.append(planets[planet]['Radius'])
except:
continue
planets[name]['Mass']
plt.plot(radii,masses,'o')
plt.show()
plt.plot(radii, masses, 'o')
plt.xlabel('Radius')
plt.ylabel('Mass')
np.median(masses)
np.median(radii)
np.polyfit?
p = np.polyfit(radii,masses,1)
print(p)
fit_x = np.arange(0,2,0.1)
fit_y = p[0] + p[1]*fit_x
plt.plot(fit_x, fit_y)
plt.plot(fit_x,fit_y, label=str('Fit: m = %.2f * r + %.2f' % (p[1], p[0])))
plt.plot(radii,masses,'o')
plt.xlabel('Radius')
plt.ylabel('Mass')
plt.legend(loc='best')
import pandas as pd
data = pd.read_csv('planets.txt')
data = pd.
data.describe()
data.columns
plt.plot(data['Pl. Radius'],data['Pl. Mass'], 'o')