numpy、scipy、pandas、matplotlib的讀書報告:
阿新 • • 發佈:2020-11-22
1.
Numpy:為大型多維陣列和矩陣新增 Python 支援,並提供高階的數學函式來運算這些陣列。
SciPy:基於 Numpy,彙集了一系列的數學演算法和便捷的函式。它可以向開發者提供用於資料操作與視覺化的高階命令和類,是構建互動式 Python 會話的強大工具。
Pandas:面向資料操作和分析的 Python 庫,提供用於處理數字圖表和時序資料的資料結構和操作功能。
Matplotlib:Python 中常用的繪相簿,能在跨平臺的互動式環境生成高質量圖形。後來在它的基礎上又衍生了更為高階的繪相簿 Seaborn。
總的來說,如果你想理解和處理手頭的資料,就用 Pandas;如果你想執行一些複雜的計算,就用 Numpy 和 SciPy;如果你想將資料視覺化,就用 Matplotlib。
2.
2.1numpy的操作。
import numpy
# 建立數一維陣列組
# numpy.array([元素1,元素2,......元素n])
x = numpy.array(['a', '9', '8', '1'])
# 建立二維陣列格式
# numpy.array([[元素1,元素2,......元素n],[元素1,元素2,......元素n],[元素1,元素2,......元素n]])
y = numpy.array([[3,5,7],[9,2,6],[5,3,0]])
# 排序
x.sort()
y.sort()
# 取最大值
y1 = y.max()
# 取最小值
y2 = y.main()
# 切片
2.2pandas的操作。
import pandas as pda
# 使用pandas生成資料
# Series代表某一串資料 index指定行索引名稱,Series索引預設從零開始
# DataFrame代表行列整合出來的資料框,columns 指定列名
a = pda.Series([8, 9, 2, 1], index=['one', 'two', 'three', 'four'])
# 以列表的格式建立資料框
b = pda.DataFrame([[5,6,2,3],[3,5,1,4],[7,9,3,5]], columns=['one', 'two', 'three', 'four'],index=['one', 'two', 'three'])
# 以字典的格式建立資料框
c = pda.DataFrame({
'one':4, # 會自動補全
'two':[6,2,3],
'three':list(str(982))
})
# b.head(行數)# 預設取前5行頭
# b.tail(行數)# 預設取後5行尾
# b.describe() 統計資料的情況 count mean std min 25% max
e = b.head()
f = b.describe()
# 資料的轉置,及行變成列,列變成行
g = b.T
2.3 matplotlib的使用
# 折線圖/散點圖用plot
# 直方圖用hist
import matplotlib.pylab as pyl
import numpy as npy
x = [1,2,4,6,8,9]
y = [5,6,7,8,9,0]
pyl.plot(x, y) #plot(x軸資料,y軸資料,展現形式)
# o散點圖,預設是直線 c cyan青色 r red紅色 m magente品紅色 g green綠色 b blue藍色 y yellow黃色 w white白色
# -直線 --虛線 -. -.形式 :細小虛線
# s方形 h六角形 *星星 + 加號 x x形式 d菱形 p五角星
pyl.plot(x, y, 'D')
pyl.title('name') #名稱
pyl.xlabel('xname') #x軸名稱
pyl.ylabel('yname') #y軸名稱
pyl.xlim(0,20) #設定x軸的範圍
pyl.ylim(2,22) #設定y軸的範圍
pyl.show()
# 隨機數的生成
data = npy.random.random_integers(1,20,100) #(最小值,最大值,個數)
# 生成具有正態分佈的隨機數
data2 = npy.random.normal(10.0, 1.0, 10000) #(均值,西格瑪,個數)
# 直方圖hist
pyl.hist(data)
pyl.hist(data2)
# 設定直方圖的上限下限
sty = npy.arange(2,20,2) #步長也表示直方圖的寬度
pyl.hist(data, sty, histtype='stepfilled') #