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

  • 相关阅读:
    keras.Sequential.compile(loss='目标函数 ', optimizer='adam', metrics=['accuracy'])
    Linux下安装tensorflow
    【拿来主义】Shell 中的4种引字符号:单引号、双引号、反引号、反斜杠的用法
    ResourceBundle类:读取配置文件
    Linux三剑客之——awk用法入门
    Javascript学习笔记-基本概念-语句
    Javascript学习笔记-基本概念-操作符
    Javascript学习笔记-基本概念-语法、关键字和保留字、变量
    django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11
    当在安装包过程中出现python setup.py build_ext --pg-config /path/to/pg_config build ... or with t
  • 原文地址:https://www.cnblogs.com/keepSmile/p/7794204.html
Copyright © 2011-2022 走看看