-
大小: 8KB文件类型: .py金币: 2下载: 1 次发布日期: 2021-06-06
- 语言: Python
- 标签: coursera Assignment
资源简介
2020最新版coursera mooc Introduction to Data Science in Python
用于学习交流
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Fri Sep 18 21:56:15 2020
@author: Ray
@email: 1324789704@qq.com
@wechat: RayTing0305
“““
‘‘‘
Question 1
Write a function called proportion_of_education which returns the proportion of children in the dataset who had a mother with the education levels equal to less than high school (<12) high school (12) more than high school but not a college graduate (>12) and college degree.
This function should return a dictionary in the form of (use the correct numbers do not round numbers):
{“less than high school“:0.2
“high school“:0.4
“more than high school but not college“:0.2
“college“:0.2}
‘‘‘
import scipy.stats as stats
import numpy as np
import pandas as pd
df = pd.read_csv(“./assets/NISPUF17.csv“)
def proportion_of_education():
# your code goes here
# YOUR CODE HERE
df_edu = df.EDUC1
edu_list = [1 2 3 4]
zero_df = pd.Dataframe(np.zeros((df_edu.shape[0] len(edu_list))) columns=edu_list)
for edu in edu_list:
zero_df[edu][df_edu==edu]=1
#zero_df
sum_ret = zero_df.sum(axis=0)
name_l = [“less than high school“ “high school“ “more than high school but not college“ “college“]
rat = sum_ret.values/sum(sum_ret.values)
dic = dict()
for i in range(4):
dic[name_l[i]] = rat[i]
return dic
raise NotImplementedError()
assert type(proportion_of_education())==type({}) “You must return a dictionary.“
assert len(proportion_of_education()) == 4 “You have not returned a dictionary with four items in it.“
assert “less than high school“ in proportion_of_education().keys() “You have not returned a dictionary with the correct keys.“
assert “high school“ in proportion_of_education().keys() “You have not returned a dictionary with the correct keys.“
assert “more than high school but not college“ in proportion_of_education().keys() “You have not returned a dictionary with the correct keys.“
assert “college“ in proportion_of_education().keys() “You have not returned a dictionary with the correct“
‘‘‘
Question 2
Let‘s explore the relationship between being fed breastmilk as a child and getting a seasonal influenza vaccine from a healthcare provider. Return a tuple of the average number of influenza vaccines for those children we know received breastmilk as a child and those who know did not.
This function should return a tuple in the form (use the correct numbers:
(2.5 0.1)
‘‘‘
def average_influenza_doses():
# YOUR CODE HERE
#是否喂养母乳
fed_breastmilk = list(df.groupby(by=‘CBF_01‘))
be_fed_breastmilk = fed_breastmilk[0][1]
not_fed_breastmilk = fed_breastmilk[1][1]
#喂养母乳的influenza数目
be_fed_breastmilk_influenza = be_fed_breastmilk.P_NUMFLU
num_be_fed_breastmilk_influenza = be_fed_breastmilk_influenza.dropna().mean()
#未喂养母乳的influenza数目
not_be_fed_breastmilk_influenza = not_fed_breastmilk.P_NUMFLU
num_not_be_fed_breastm
评论
共有 条评论