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

  • 相关阅读:
    2021年2月13
    2021年2月12
    2021年2月11
    2021年2月10
    2021年2月9
    下载优化
    20180301越努力越轻松
    2018-03-01继续完善2
    2018-03-01继续完善
    2018-02-16 GetSameTypeQuestion
  • 原文地址:https://www.cnblogs.com/keepSmile/p/7794204.html
Copyright © 2011-2022 走看看