• 基于python的咖啡数据集分析


    工具:jupyter notebook

    启动界面:

    代码分析:

    1. # -*- coding='utf-8' -*-
    2. '''
    3. 功能:基于咖啡数据集的python(引导法)数据分析技术
    4. 作者:pegasus
    5. 时间:2022/11/22
    6. '''
    7. # 导入数据分析所需要的库文件
    8. import numpy as np
    9. import pandas as pd
    10. import matplotlib.pyplot as plt
    11. # %matplotlib inline # %matplotlib inline加在语句块的第一句,作用是图象出现在Notebook里面,而不是一个新窗口里
    12. # 设定一个随机种子
    13. np.random.seed(42)
    14. # 读入CSV格式的数据集
    15. coffee_full = pd.read_csv('coffee_dataset.csv')
    16. # 数据采样,这是你在现实世界中可能得到的唯一数据。
    17. coffee_red = coffee_full.sample(200)
    18. # 打印采样数据
    19. print('现实样本数据:', coffee_red)
    20. # 计算样本中喝咖啡的比例和不喝咖啡的比例
    21. drinks_coffee_scale=coffee_red.groupby(by = 'drinks_coffee').size() / coffee_red.shape[0]
    22. print('喝咖啡和不喝咖啡的比列为:', drinks_coffee_scale)
    23. #在喝咖啡的人中,平均身高是多少?
    24. drinks_coffee_height = coffee_red[coffee_red['drinks_coffee'] == True]['height'].mean()
    25. print('喝咖啡的平均身高是:',drinks_coffee_height)
    26. # 不喝咖啡的人的平均身高是多少?
    27. drinks_not_coffee_height = coffee_red[coffee_red['drinks_coffee'] == False]['height'].mean()
    28. print('不喝咖啡的人的平均身高是:',drinks_not_coffee_height )
    29. # 从你最初的200个样本中模拟出200个“新”个体成为"引导样本"。不喝咖啡的人呢?
    30. coffee_new = coffee_red.sample(200, replace = True)
    31. print('引导样本是:',coffee_new)
    32. # 引导样本中喝咖啡的比例是多少?
    33. new_drinks_coffee_scale = coffee_new.groupby(by = 'drinks_coffee').size() / coffee_new.shape[0]
    34. print('引导样本中喝咖啡和不喝咖啡的比列为:', new_drinks_coffee_scale)
    35. '''
    36. 现在模拟你的自举样本10000次,并在每个样本中测量不喝咖啡的人的平均身高。
    37. 每个引导样本应该来自200个数据点的第一个样本。绘制分布图,并提取95%置信
    38. 区间所需的值。你注意到这个例子中平均值的抽样分布吗?
    39. '''
    40. boot_means = []
    41. for n in range(10000):
    42. bootsample = coffee_red.sample(200, replace=True)
    43. bootsample_mean = bootsample[bootsample['drinks_coffee'] == False]['height'].mean()
    44. boot_means.append(bootsample_mean)
    45. boot_means = np.array(boot_means)
    46. print('模拟样本平均值:',boot_means)
    47. print('模拟样本平均值的平均值:',boot_means.mean())
    48. # 找到一组数的分位数,便于设置置信区间
    49. print('得到的不同分位数是:',np.percentile(boot_means, 2.5), np.percentile(boot_means, 97.5))
    50. # 绘制直方图
    51. plt.hist(boot_means, alpha=0.7);
    52. plt.axvline(np.percentile(boot_means, 2.5), color='red', linewidth=2) ;
    53. plt.axvline(np.percentile(boot_means, 97.5), color='red', linewidth=2) ;
    54. #你的时间间隔是否记录了人群中不喝咖啡的人的实际平均身高?查看总体平均值和95%置信区间提供的两个界限,然后回答下面的最后一个测验问题。
    55. aa = coffee_full.query('drinks_coffee == False')['height'].mean()
    56. print(aa)
    57. import statsmodels.stats.api as sm
    58. X1 = coffee_red[coffee_red['drinks_coffee']==True]['height']
    59. X2 = coffee_red[coffee_red['drinks_coffee']==False]['height']
    60. cm = sm.CompareMeans(sm.DescrStatsW(X1), sm.DescrStatsW(X2))
    61. print (cm.tconfint_diff(usevar='unequal'))
    62. print(cm.summary())

    结果:

    绘制混淆矩阵

    1. import sklearn
    2. import pandas as pd
    3. print('sklearn version: ', sklearn.__version__)
    4. print('pandas version: ', pd.__version__)
    5. from sklearn.metrics import confusion_matrix
    6. y_true = [0, 1, 0, 1, 0, 1, 0]
    7. y_pred = [1, 1, 1, 0, 1, 0, 1]
    8. cm = confusion_matrix(y_true, y_pred)
    9. print ('混淆矩阵是:',cm)
    10. cm = confusion_matrix(y_true, y_pred, labels=[1,0])
    11. print ('混淆矩阵2是:',cm)
    12. tp = cm[0][0]
    13. fp = cm[1][0]
    14. tn = cm[1][1]
    15. fn = cm[0][1]
    16. print("各项指标为:",tp,fp,tn,fn)

    结果:

    数据集格式内容:

     数据集下载地址:

    https://download.csdn.net/download/mzl_18353516147/87516856

  • 相关阅读:
    Linux磁盘分区中物理卷(PV)、卷组(VG)、逻辑卷(LV)创建和(LVM)管理
    改进 hibernate-validator,新一代校验框架 validator 使用介绍 v0.4
    微服务9:服务治理来保证高可用
    晶振与晶体
    C# 看懂这100+行代码,你就真正入门了(经典)
    Tesseract OCR安装与简单使用
    Redis面试
    Azure AKS集群监控告警表达式配置
    解析java中的return关键字
    批量替换WordPress文章内图片链接
  • 原文地址:https://blog.csdn.net/mzl_18353516147/article/details/127980100