Python計算美國總統的身高並實現數據可視化


代碼如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


data=pd.read_csv('president_heights.csv')
heights=np.array(data['height(cm)'])
print(heights)
#print(data)
print("總統身高的平均值是",heights.mean())
print("總統身高的平均值是",np.mean(heights))#這兩種方法用來計算平均值都是可以的
print("總統身高的標准差是",heights.std())
print("總統身高的最低值是",heights.min())
print("總統身高的最高值是",heights.max())
print("總統身高的前百分之二十五是",np.percentile(heights,25))
print("總統身高的后百分之二十五是",np.percentile(heights,75))
print("總統身高的中位數是",np.percentile(heights,50))
plt.hist(heights)
plt.title("USA presidents' heights")
plt.xlabel('heights(cm)')
plt.ylabel("People's number")
plt.show()

csv文件在github上:https://github.com/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/data/president_heights.csv

輸出結果是:

[189 170 189 163 183 171 185 168 173 183 173 173 175 178 183 193 178 173
 174 183 183 168 170 178 182 180 183 178 182 188 175 179 183 193 182 183
 177 185 188 188 182 185]
總統身高的平均值是 179.73809523809524
總統身高的平均值是 179.73809523809524
總統身高的標准差是 6.931843442745892
總統身高的最低值是 163
總統身高的最高值是 193
總統身高的前百分之二十五是 174.25
總統身高的后百分之二十五是 183.0
總統身高的中位數是 182.0

圖像結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM