# Import NumPy and matplotlib.pyplot
import numpy as np
import matplotlib.pyplot as plt
# Read in the data for age, bmi and insurance charges in separate arrays.
# Print out to check
#
filename='insurance.csv'
age = np.loadtxt ('insurance.csv', delimiter = ',', skiprows = 1, usecols = 0)
print ('age data')
print (age)
bmi = np.loadtxt ('insurance.csv', delimiter = ',', skiprows = 1, usecols = 2)
print ('bmi data')
print (bmi)
insurance = np.loadtxt ('insurance.csv', delimiter = ',', skiprows = 1, usecols = 6)
print ('insurance costs')
print (insurance)
# print out the number of data instances
print (f" There are {len (age)}) data entries")
print (f" There are {len (bmi)}) data entries")
print (f" There are {len (insurance)}) data entries")
# plot charges vs age and add labels to axis and title
insurance = np.loadtxt ('insurance.csv', delimiter = ',', skiprows = 1, usecols = 6)
print ('insurance costs')
print (insurance)
age = np.loadtxt ('insurance.csv', delimiter = ',', skiprows = 1, usecols = 0 )
print ('age data')
print (age)
x = np.array (age)
y = np.array (insurance)
plt.scatter (x, y)
plt.xlabel ('age')
plt.ylabel ('charges')
plt.title ('Age vs Charges ')
# plot charges vs BMI; add axis labels and plot title
insurance = np.loadtxt ('insurance.csv', delimiter = ',', skiprows = 1, usecols = 6)
print ('insurance costs')
print (insurance)
bmi = np.loadtxt ('insurance.csv', delimiter = ',', skiprows = 1, usecols = 2)
print ('bmi data')
print (bmi)
x = np.array (bmi)
y = np.array (insurance )
plt.scatter (x, y)
plt.xlabel ('bmi')
plt.ylabel ('charges')
plt.title ('charges vs bmi')
# Import NumPy and matplotlib.pyplot
import numpy as np
import matplotlib.pyplot as plt
# Read in the data for bmi and insurance charges in separate arrays.
# Print out to check
charges = np.loadtxt (filename, usecols = 6, skiprows = 1, delimiter = ',')
print (bmi)
print (charges)
# Fit a line using linear regression
linecoefficent = np.polyfit (bmi, charges, 1)
print (f' the equation is {linecoefficent [0]} x + {linecoefficent [1]}')
# Plot data and line on same graph
plt.plot (bmi, charges, 'bo')
plt.plot (x, y , 'r')
plt.show ()
# Fit a parabola to data
poly_coeff = np.polyfit (bmi, charges, 2)
print (f' {poly_coeff [0]} + {poly_coeff[1]}x + {poly_coeff[2]}')
# Plot data and parabola on same graph
max_bmi = max (bmi)
min_bmi = min (bmi)
x = np.linespace(min_bmi, max_bmi, 100)
f = np.poly1d(poly_coeff)
plt.scatter (bmi, charges)
plt.ylabel ('charges')
plt.xlabel ('bmi')
plt.title ('charges vs bmi')
plt.plot (x, 'r')