
上QQ阅读APP看书,第一时间看更新
How to do it...
This section walks through the steps to predict gender based on height and weight.
- Create a Python function called input_normalize to input new values for height and weight and output a normalized height and weight, as seen in the following script:
def input_normalize(height, weight):
inputHeight = (height - x_mean[0])/x_std[0]
inputWeight = (weight - x_mean[1])/x_std[1]
return inputHeight, inputWeight
- Assign a variable called score to the function for the values of 70 inches for the height and 180 lbs for the weight, as seen in the following script:
score = input_normalize(70, 180)
- Create another Python function, called predict_gender, to output a probability score, gender_score, between 0 and 1, as well as a gender description, by applying the summation with w1, w2, and b as well as the sigmoid function, as seen in the following script:
def predict_gender(raw_score):
gender_summation = raw_score[0]*w1 + raw_score[1]*w2 + b
gender_score = sigmoid(gender_summation)
if gender_score <= 0.5:
gender = 'Female'
else:
gender = 'Male'
return gender, gender_score