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)))

  • 相关阅读:
    UVALive-8077 Brick Walls 找规律
    UVALive-8079 Making a Team 排列组合公式化简
    UVALive-8072 Keeping On Track 树形dp 联通块之间缺失边的个数
    HDU-5534 Partial Tree 完全背包 设定初始选择
    HDU-2844 Coins 多重背包 物品数量二进制优化
    CodeForces-366C Dima and Salad 对01背包的理解 多个背包问题
    HDU-2955 Robberies 浮点数01背包 自变量和因变量位置互换
    UVALive-7197 Axles 动态规划 多个背包问题
    广义表(C++实现)
    稀疏矩阵及稀疏矩阵的压缩存储
  • 原文地址:https://www.cnblogs.com/keepSmile/p/7794204.html
Copyright © 2011-2022 走看看