# 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
#
import numpy as np
import matplotlib.pyplot as plt
filename='insurance.csv'
age=np.loadtxt(filename,usecols=0,delimiter=',',skiprows=1)
bmi=np.loadtxt(filename,usecols=2,delimiter=',',skiprows=1)
charges=np.loadtxt(filename,usecols=6,delimiter=',',skiprows=1)
print(age)
print(bmi)
print(charges)
# print out the number of data instances
import numpy as np
import matplotlib.pyplot as plt
filename='insurance.csv'
age=np.loadtxt(filename,usecols=0,delimiter=',',skiprows=1)
bmi=np.loadtxt(filename,usecols=2,delimiter=',',skiprows=1)
charges=np.loadtxt(filename,usecols=6,delimiter=',',skiprows=1)
print(f"The number of data instances for age in the file is {len(age)}.")
print(f"The number of data instances for bmi in the file is {len(bmi)}.")
print(f"The number of data instances for insurance charges in the file is {len(charges)}.")
# 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)
bmi=np.loadtxt(filename,usecols=2,delimiter=',',skiprows=1)
charges=np.loadtxt(filename,usecols=6,delimiter=',',skiprows=1)
plt.plot(age,charges,"r")
plt.xlabel("Age")
plt.ylabel("Charges")
plt.title("Charges vs. Age")
# plot charges vs BMI; add axis labels and plot title
import numpy as np
import matplotlib.pyplot as plt
filename='insurance.csv'
age=np.loadtxt(filename,usecols=0,delimiter=',',skiprows=1)
bmi=np.loadtxt(filename,usecols=2,delimiter=',',skiprows=1)
charges=np.loadtxt(filename,usecols=6,delimiter=',',skiprows=1)
plt.plot(bmi,charges,"r")
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'
import numpy as np
import matplotlib.pyplot as plt
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
filename='insurance.csv'
import numpy as np
import matplotlib.pyplot as plt
bmi=np.loadtxt(filename,usecols=2,delimiter=',',skiprows=1)
charges=np.loadtxt(filename,usecols=6,delimiter=',',skiprows=1)
coeff1=np.polyfit(bmi,charges,1)
f=np.poly1d(coeff1)
print(f)
# Plot data and line on same graph
filename='insurance.csv'
import numpy as np
import matplotlib.pyplot as plt
bmi=np.loadtxt(filename,usecols=2,delimiter=',',skiprows=1)
charges=np.loadtxt(filename,usecols=6,delimiter=',',skiprows=1)
coeff1=np.polyfit(bmi,charges,1)
f=np.poly1d(coeff1)
plt.plot(bmi,charges)
bmi_min=np.min(bmi)
bmi_max=np.max(bmi)
xx=np.linspace(bmi_min,bmi_max,4)
yy=f(xx)
plt.plot(xx,yy)
plt.xlabel("BMI")
plt.ylabel("Charges")
plt.title("Charges vs. BMI")
plt.show()
# Fit a parabola to data
filename='insurance.csv'
import numpy as np
import matplotlib.pyplot as plt
bmi=np.loadtxt(filename,usecols=2,delimiter=',',skiprows=1)
charges=np.loadtxt(filename,usecols=6,delimiter=',',skiprows=1)
coeff1=np.polyfit(bmi,charges,2)
f=np.poly1d(coeff1)
print(f)
# Plot data and parabola on same graph
filename='insurance.csv'
import numpy as np
import matplotlib.pyplot as plt
bmi=np.loadtxt(filename,usecols=2,delimiter=',',skiprows=1)
charges=np.loadtxt(filename,usecols=6,delimiter=',',skiprows=1)
coeff1=np.polyfit(bmi,charges,2)
f=np.poly1d(coeff1)
plt.plot(bmi,charges)
bmi_min=np.min(bmi)
bmi_max=np.max(bmi)
xx=np.linspace(bmi_min,bmi_max,4)
yy=f(xx)
plt.plot(xx,yy)
plt.xlabel("BMI")
plt.ylabel("Charges")
plt.title("Charges vs. BMI")
plt.show()