# 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)
age data
[19. 18. 28. ... 18. 21. 61.]
bmi data
[27.9 33.77 33. ... 36.85 25.8 29.07]
insurance costs
[16884.924 1725.5523 4449.462 ... 1629.8335 2007.945 29141.3603]
# 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")
There are 1338) data entries
There are 1338) data entries
There are 1338) 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 ')
insurance costs
[16884.924 1725.5523 4449.462 ... 1629.8335 2007.945 29141.3603]
age data
[19. 18. 28. ... 18. 21. 61.]
# 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')
insurance costs
[16884.924 1725.5523 4449.462 ... 1629.8335 2007.945 29141.3603]
bmi data
[27.9 33.77 33. ... 36.85 25.8 29.07]
# 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)
[27.9 33.77 33. ... 36.85 25.8 29.07]
[16884.924 1725.5523 4449.462 ... 1629.8335 2007.945 29141.3603]
# Fit a line using linear regression
linecoefficent = np.polyfit (bmi, charges, 1)
print (f' the equation is {linecoefficent [0]} x + {linecoefficent [1]}')
the equation is 393.8730307973952 x + 1192.937208961153
# 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]}')
-6.662322630063166 + 813.9749575731079x + -5177.034125706157
# 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')
There’s an error in this block
Try running the app again, or contact the app’s creator