In [4]:
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
%matplotlib inline
import statsmodels.formula.api as smf
df = pd.read_csv('heights_weights_genders.csv')
In [5]:
df.head(3)
Out[5]:
In [12]:
female_df = df[df['Gender'] == 'Female']
male_df = df[df['Gender'] == 'Male']
In [17]:
female_df.describe()
Out[17]:
In [25]:
lm = smf.ols(formula="Weight~Height",data=female_df).fit()
lm.params
Out[25]:
In [26]:
female_intercept, female_slope = lm.params
In [36]:
female_df.plot(kind="scatter",x="Height",y="Weight")
plt.plot(female_df["Height"],female_slope*female_df["Height"]+female_intercept,"-",color="darkgrey")
plt.title('Correlation between height and weight for females')
plt.xlabel('Height (inches)')
plt.ylabel('Weight (lbs)')
Out[36]:
In [19]:
male_df.describe()
Out[19]:
In [20]:
lm = smf.ols(formula="Weight~Height",data=male_df).fit()
lm.params
Out[20]:
In [23]:
male_intercept, male_slope = lm.params
In [35]:
male_df.plot(kind="scatter",x="Height",y="Weight")
plt.plot(male_df["Height"],male_slope*male_df["Height"]+male_intercept,"-",color="darkgrey")
plt.title('Correlation between height and weight for males')
plt.xlabel('Height (inches)')
plt.ylabel('Weight (lbs)')
Out[35]:
In [89]:
gender = input('Male or female? ')
if gender == 'female' or gender == 'Female':
height = int(input('Height (in inches): '))
weight = female_slope * height + female_intercept
print('If a person is female and ' + str(height) + ' inches tall, they probably weigh ' + str(round(weight,2)) + ' pounds.')
elif gender == 'male' or gender == 'Male':
height = int(input('Height (in inches): '))
weight = male_slope * height + male_intercept
print('If a person is male and ' + str(height) + ' inches tall, they probably weigh ' + str(round(weight,2)) + ' pounds.')
else:
print("Input error.")
In [90]:
gender = input('Male or female? ')
if gender == 'female' or gender == 'Female':
height = int(input('Height (in inches): '))
weight = 5.994047 * height - 246.013266
print('If a person is female and ' + str(height) + ' inches tall, they probably weigh ' + str(round(weight,2)) + ' pounds.')
elif gender == 'male' or gender == 'Male':
height = int(input('Height (in inches): '))
weight = 5.961774 * height - 224.498841
print('If a person is male and ' + str(height) + ' inches tall, they probably weigh ' + str(round(weight,2)) + ' pounds.')
else:
print("Input error.")