# 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', usecols = (0), skiprows = 1, delimiter = ',')
print("The ages are:")
print("",age)
bmi = np.loadtxt('insurance.csv',usecols = (2), skiprows = 1, delimiter = ',')
print("The BMI's are:")
print("",bmi)
charges = np.loadtxt('insurance.csv', usecols = (6), skiprows = 1, delimiter = ',')
print("The charges are:")
print(" ",charges)
# print out the number of data instances
insurance = np.loadtxt('insurance.csv',skiprows=1, delimiter = ',', dtype = 'U20')
print(insurance)
# plot charges vs age and add labels to axis and title
plt.plot(charges,age, 'rx')
plt.xlabel('Charges')
plt.ylabel('Age')
plt.title('Scatter plot of Age & Charges')
# plot charges vs BMI; add axis labels and plot title
plt.plot(charges,bmi, 'rx')
plt.xlabel('Charges')
plt.ylabel('BMI')
plt.title('Scatter plot of BMI & Charges')
# 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('insurance.csv',usecols = (2), skiprows = 1, delimiter = ',')
print(bmi)
charges = np.loadtxt('insurance.csv', usecols = (6), skiprows = 1, delimiter = ',')
print(charges)
# Fit a line using linear regression
insuranceline = np.polyfit (bmi,charges,1)
np.polyfit(bmi,charges,1)
print (f"equation of the line is {insuranceline[0]}x + {insuranceline[1]}" )
# Plot data and line on same graph
plt.plot(bmi,charges, 'bo')
plt.xlabel("BMI")
plt.ylabel("Charges in $")
f = np.poly1d(insuranceline)
bmimin = min(bmi)
bmimax = max(bmi)
x = np.linspace(bmimin, bmimax,100)
plt.plot(x, f(x),'r')
plt.text(15,50000,"line: y=393.87 x + 1192.94") # add text to plot
plt.title("Linear regression fit to data")
plt.show()
# Fit a parabola to data
insuranceparabola = np.polyfit (bmi,charges,2)
print (f"Quadratic polynomial is {insuranceparabola[2]}*x^2 +{ insuranceparabola[1]}*x +\
{ insuranceparabola[0]}")
# Plot data and parabola on same graph
g = np.poly1d(insuranceparabola)
# set points to draw parabola
x_eval = np.linspace(bmimin,bmimax ); y_eval=g(x_eval)
#
plt.plot(bmi,charges,'ro') # plot data points
plt.plot(x_eval,y_eval)
plt.title ("Quadratic Polynomial fit to data")
plt.xlabel("BMI")
plt.ylabel("Charges in $")
plt.text (15,55000,"parabola: y = 5177.03x^2 + 813.97x - 6.66")