# 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(filename,usecols=0,skiprows=1,delimiter=',',dtype=int)
bmi = np.loadtxt(filename,usecols=2,skiprows=1,delimiter=',')
charges = np.loadtxt(filename,usecols=6,skiprows=1,delimiter=',')
print (age)
print (bmi)
print (charges)
# print out the number of data instances
print (f'There are {len(age)} data instances.')
# plot charges vs age and add labels to axis and title
plt.scatter(age,charges)
plt.xlabel('Age')
plt.ylabel("Charges")
plt.title("Age vs. Charges")
# plot charges vs BMI; add axis labels and plot title
plt.scatter(age,bmi)
plt.xlabel('Age')
plt.ylabel("BMI")
plt.title("Age 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
#
filename='insurance.csv'
bmi = np.loadtxt(filename,usecols=2,skiprows=1,delimiter=',')
charges = np.loadtxt(filename,usecols=6,skiprows=1,delimiter=',')
print (bmi)
print (charges)
# Fit a line using linear regression
line_coeff = np.polyfit(bmi,charges,1)
print (f'The line equation is {line_coeff[0]}x + {line_coeff[1]}')
# Plot data and line on same graph
min_bmi = min(bmi)
max_bmi = max(bmi)
x = np.linspace(min_bmi,max_bmi,100)
f = np.poly1d(line_coeff)
plt.scatter(bmi,charges)
plt.plot(x,f(x),'r')
plt.xlabel("BMI")
plt.ylabel("Charges")
plt.title("BMI vs. Charges")
# Fit a parabola to data
poly_coeff = np.polyfit(bmi,charges,2)
print (f'{poly_coeff[0]}x^2 + {poly_coeff[1]}x + {poly_coeff[2]}')
# Plot data and parabola on same graph
min_bmi = min(bmi)
max_bmi = max(bmi)
x = np.linspace(min_bmi,max_bmi,100)
f = np.poly1d(poly_coeff)
plt.scatter(bmi,charges)
plt.plot(x,f(x),'r')
plt.xlabel("BMI")
plt.ylabel("Charges")
plt.title("BMI vs. Charges")