zoukankan      html  css  js  c++  java
  • Intro to Python for Data Science Learning 8

    NumPy: Basic Statistics

    from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-4-numpy?ex=13

    • Average versus median

    You now know how to use numpy functions to get a better feeling for your data. It basically comes down to importingnumpy and then calling several simple functions on the numpyarrays:

    import numpy as np
    x = [1, 4, 8, 10, 12]
    np.mean(x)
    np.median(x)

    # np_baseball is available

    # Import numpy
    import numpy as np

    # Create np_height from np_baseball
    np_height = np.array(np_baseball)[:,0]

    # Print out the mean of np_height
    print(np.mean(np_height))

    # Print out the median of np_height
    print(np.median(np_height))

    • Explore the baseball data

    # np_baseball is available

    # Import numpy
    import numpy as np

    # Print mean height (first column)
    avg = np.mean(np_baseball[:,0])
    print("Average: " + str(avg))

    # Print median height. Replace 'None'
    med = np.median(np_baseball[:,0])
    print("Median: " + str(med))

    # Print out the standard deviation on height. Replace 'None'
    stddev = np.std(np_baseball[:,0])
    print("Standard Deviation: " + str(stddev))

    # Print out correlation between first and second column. Replace 'None'
    corr = np.corrcoef(np_baseball[:,0],np_baseball[:,1])
    print("Correlation: " + str(corr))

    • Blend it all together

    You've contacted FIFA for some data and they handed you two lists. The lists are the following:

    positions = ['GK', 'M', 'A', 'D', ...]
    heights = [191, 184, 185, 180, ...]

    Each element in the lists corresponds to a player. The first list,positions, contains strings representing each player's position. The possible positions are: 'GK' (goalkeeper), 'M' (midfield),'A' (attack) and 'D' (defense). The second list, heights, contains integers representing the height of the player in cm. The first player in the lists is a goalkeeper and is pretty tall (191 cm).

    You're fairly confident that the median height of goalkeepers is higher than that of other players on the soccer field. Some of your friends don't believe you, so you are determined to show them using the data you received from FIFA and your newly acquired Python skills.

    # heights and positions are available as lists

    # Import numpy
    import numpy as np

    # Convert positions and heights to numpy arrays: np_positions, np_heights
    np_positions = np.array(positions)
    np_heights = np.array(heights)

    # Heights of the goalkeepers: gk_heights
    gk_heights = np_heights[np_positions == "GK"]

    # Heights of the other players: other_heights
    other_heights = np_heights[np_positions != "GK"]


    # Print out the median height of goalkeepers. Replace 'None'
    print("Median height of goalkeepers: " + str(np.median(gk_heights)))

    # Print out the median height of other players. Replace 'None'
    print("Median height of other players: " + str(np.median(other_heights)))

  • 相关阅读:
    【分享】HTML5附件拖拽上传drop & google.gears
    【分享】return false,对阻止事件默认动作的一些测试
    【记录】随笔分类汇总
    【分享】微博 @ 符号的用户名提示效果。(想@到谁?)
    【记录】File, FileReader 和 Ajax 文件上传
    【动态】简单的JS动态加载单体
    【分享】简单页面提示插件第二版表单验证很简单
    【记录】GIT 常用命令
    【分享】jQuery animate自定义动画的简单实现
    【分享】 封装js操作textarea 方法集合(兼容很好)。
  • 原文地址:https://www.cnblogs.com/keepSmile/p/7794204.html
Copyright © 2011-2022 走看看