题目:根据数据,求出会员和非会员的占比
import os
import numpy as np
import matplotlib.pyplot as plt
# 创建输出文件夹,保存结果图
output_path = './output'
if not os.path.exists(output_path):
os.makedirs(output_path)
def collect_and_process_data():
member_type_list = []
for data_filename in data_filenames:
data_file = os.path.join(data_path,data_filename)
#用numpy的loadtxt读取数据
data_arr = np.loadtxt(data_file,delimiter=",",dtype="str",skiprows=1)
# print(data_arr)
#将最后一列会员信息,去除 ""
member_type_col = np.core.defchararray.replace(data_arr[:,-1],'"',"")
# print(member_type_col)
#如果不确定有多少行,用-1去代替,numpy会自动计算出有多少行
member_type_col = member_type_col.reshape(-1,1)
# print(member_type_col)
member_type_list.append(member_type_col)
# 数组拼接
year_member_type = np.concatenate(member_type_list)
print(year_member_type)
return year_member_type
def analyze_data(year_member_type):
# 筛选出来有多少Member 多少Casual
member_number = year_member_type[year_member_type == "Member"].shape[0]
casual_number = year_member_type[year_member_type == "Casual"].shape[0]
users_number = [member_number,casual_number]
print(users_number)
return users_number
def save_show_result(users_number):
# 初始化画布
plt.figure()
# 绘制饼状图
#explode=(0.05,0),将某部分爆炸出来, 使用括号,将第一块分割出来,数值的大小是分割出来的与其他两块的间隙
#autopct="%.2f%%" 小数点位数
plt.pie(users_number,labels=['Member','Casual'],autopct="%.2f%%",shadow=False,explode=(0.05,0))
# 紧凑输出,控制画布以内
plt.tight_layout()
plt.savefig(os.path.join(output_path,'pie.png'))
plt.show()
结果如图: