# 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'{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
lineCoefficient = np.polyfit(bmi,charges,1)
print (f'The line equation is {lineCoefficient[0]}x + {lineCoefficient[1]}')
# Plot data and line on same graph
minbmi = np.min(bmi)
maxbmi = np.max(bmi)
x = np.linspace(minbmi,maxbmi,100)
y = np.poly1d(lineCoefficient)
plt.scatter(bmi,charges)
plt.plot(x,y(x),'r')
plt.xlabel("BMI")
plt.ylabel("Charges")
plt.title("BMI vs. Charges")
# Fit a parabola to data
polyCoefficient = np.polyfit(bmi,charges,2)
print (f'{polyCoefficient[0]}x^2 + {polyCoefficient[1]}x + {polyCoefficient[2]}')
# Plot data and parabola on same graph
minbmi2 = np.min(bmi)
maxbmi2 = np.max(bmi)
x = np.linspace(minbmi2,maxbmi2,100)
y = np.poly1d(polyCoefficient)
plt.scatter(bmi,charges)
plt.plot(x,y(x),'r')
plt.xlabel("BMI")
plt.ylabel("Charges")
plt.title("BMI vs Charges")