# 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, delimiter= ',' , skiprows= 1)
sex = np.loadtxt(filename, usecols = 1, delimiter= ',' , skiprows= 1, dtype = 'U10')
bmi = np.loadtxt(filename, usecols= 2, delimiter= ',' , skiprows= 1)
charges = np.loadtxt(filename, usecols = 6, delimiter= ',', skiprows= 1)
print(age)
print(sex)
print(bmi)
print(charges)
#I added in age too late, decided to keep in
# print out the number of data instances
print(f"In age, there are {len(age)} data instances" )
print(f"In BMI, there are {len(bmi)} data instances" )
print(f"In charges, there are {len(charges)} data instances" )
# plot charges vs age and add labels to axis and title
import numpy as np
import matplotlib.pyplot as plt
filename='insurance.csv'
age = np.loadtxt(filename, usecols= 0, delimiter= ',' , skiprows= 1)
coef_line = np.polyfit(age, charges, 1)
f = np.poly1d(coef_line)
plt.plot(age, charges, 'ro')
plt.xlabel('age')
plt.ylabel('charges')
plt.title('Charges vs. Age')
#I had to repeat the import code as well as the others because it would give me an error message saying it was not defined if not.
# plot charges vs BMI; add axis labels and plot title
import numpy as np
import matplotlib.pyplot as plt
filename='insurance.csv'
bmi = np.loadtxt(filename, usecols= 2, delimiter= ',' , skiprows= 1)
charges = np.loadtxt(filename, usecols = 6, delimiter= ',', skiprows= 1)
coef_line = np.polyfit(bmi, charges, 1)
f = np.poly1d(coef_line)
plt.plot(age, charges, 'ro')
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
#
filename='insurance.csv'
bmi = np.loadtxt(filename, usecols= 2, delimiter= ',' , skiprows= 1)
charges = np.loadtxt(filename, usecols = 6, delimiter= ',', skiprows= 1)
print(bmi)
print(charges)
# Fit a line using linear regression
coef_line = np.polyfit(charges,bmi , 1)
f = np.poly1d(coef_line)
charges_min = np.min(charges)
charges_max = np.max(charges)
#print(charges_min, charges_max))
xx = np.linspace(bmi_min, bmi_max, 2)
yy = f(xx)
# Plot data and line on same graph
plt.plot(charges, bmi, 'bo')
plt.plot(xx, yy, 'r')
plt.show()
# Fit a parabola to data
coef_line2 = np.polyfit(charges, bmi, 2)
f = np.poly1d(coef_line2)
charges_min = np.min(charges)
charges_max = np.max(charges)
#print(charges_min, charges_max))
xx = np.linspace(charges_min, charges_max, 1000)
yy = f(xx)
plt.plot(charges, bmi, 'bo')
plt.plot(xx, yy, 'r')
plt.show()
# Plot data and parabola on same graph