Python之(matplotlib、numpy、pandas)數據分析

  一、Matplotlib 是一個 Python 的 2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互式環境生成出版質量級別的圖形。

    它主要用來回事圖形,用來展現一些數據,更加直觀的展示,讓你第一眼就只要數據的呈現趨勢

  二、Matplotlib 的基本用法

# !/usr/bin/python
# -*- coding: UTF-8 -*-
import matplotlib
from matplotlib import pyplot

x = [1, 2, 3, 4, 7, 5, 6, 7, 4, 6, 9, 6, 2, 5, 3, 9, 1, 7]
y_1 = [10, 15, 7, 6, 13, 17, 19, 1, 5, 2, 15, 11, 12, 16, 8, 3, 5, 17]
y_2 = [17, 5, 3, 8, 16, 12, 11, 15, 2, 5, 1, 19, 17, 13, 6, 7, 15, 10]

pyplot.figure(figsize=(20, 12), dpi=50)

# 調整字體
matplotlib.rc("font", family="MicroSoft YaHei",weight="bold", size=20)

# 改變刻度
# pyplot.xticks([ i + 1 for i in range(max(x))], [ "time" + str(i + 1) for i in range(max(x))], rotation=45)
# 第一個參數x軸 第二個展示的內容 rotation 旋轉

# 描述
pyplot.xlabel("時間")
pyplot.ylabel("溫度")
pyplot.title("折線圖")

# 折線圖
pyplot.plot(x, y_1)
# pyplot.plot(x, y_2)
# 散點圖
# pyplot.scatter(x, y_1)
# pyplot.scatter(x, y_2)
# 柱狀圖
# pyplot.bar(x, y_1)
# pyplot.bar(x, y_2)
# 橫版柱狀圖
# pyplot.barh(range(len(x)), y_1, height=0.3)
# pyplot.barh(range(len(x)), y_2, height=0.3)
# 直方圖
# pyplot.hist(x, (max(x)-min(x))//1)
pyplot.xticks(range(min(x), max(x) + 1, 1))
# pyplot.grid()
# 保存圖片
# pyplot.savefig("link.png")

pyplot.show()

  效果:

  三、NumPy系統是Python的一種開源的數值計算擴展。這種工具可用來存儲和處理大型矩陣,比Python自身的嵌套列表(nested list structure)結構要高效的多(該結構也可以用來表示矩陣(matrix))。

    一個用python實現的科學計算,包括:1、一個強大的N維數組對象Array;2、比較成熟的(廣播)函數庫;3、用於整合C/C++和Fortran代碼的工具包;4、實用的線性代數、傅里葉變換和隨機數生成函數。numpy和稀疏矩陣運算包scipy配合使用更加方便。

    個人感覺和MATLAB很像,在數據結構和使用上面

  四、基本使用方式

# !/usr/bin/python
# -*- coding: UTF-8 -*-

import numpy

# 生成數組
n = numpy.arange(10)
print(n)
print("*"*20)

# 生成數組,並做2行3列的分隔
m = numpy.array([0,1,2,3,4,5]).reshape(2, 3)
print(m)
print("*"*20)

# 生成數據,分隔成3位數組
t = numpy.arange(27).reshape(3, 3, 3)
print(t)
print("*"*20)

# 加載文本,爲int方式
tx1 = numpy.loadtxt("numpy.txt", delimiter=",", dtype="int")
# 橫列替換
tx2 = numpy.loadtxt("numpy.txt", delimiter=",", dtype="int", unpack=True)
print(tx1)
print(tx2)
# 1:2橫截取,[1,2]爲選取
tx3 = tx1[1:2,[1,2]]
print(tx3)
print("*"*20)

# 豎拼接
tx4 = numpy.vstack((tx1, tx2))
print(tx4)
# 橫拼接
tx5 = numpy.hstack((tx1, tx2))
print(tx5)
print("*"*20)
numpy.txt
1,2,3
4,5,6
7,8,9

 

  效果:

[0 1 2 3 4 5 6 7 8 9]
********************
[[0 1 2]
 [3 4 5]]
********************
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
********************
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[1 4 7]
 [2 5 8]
 [3 6 9]]
[[5 6]]
********************
[[1 2 3]
 [4 5 6]
 [7 8 9]
 [1 4 7]
 [2 5 8]
 [3 6 9]]
[[1 2 3 1 4 7]
 [4 5 6 2 5 8]
 [7 8 9 3 6 9]]
********************

  五、pandas 是基於NumPy 的一種工具,該工具是爲了解決數據分析任務而創建的。Pandas 納入了大量庫和一些標準的數據模型,提供了高效地操作大型數據集所需的工具。pandas提供了大量能使我們快速便捷地處理數據的函數和方法。你很快就會發現,它是使Python成爲強大而高效的數據分析環境的重要因素之一。

    強大的工具哦,主要用來做數據處理,可以分析各種數據,然後通過其他方式呈現出來。

  六、基本使用方式:

# !/usr/bin/python
# -*- coding: UTF-8 -*-
import pandas
from matplotlib import pyplot

# 讀取文件
df = pandas.read_csv("BeijingPM20100101_20151231.csv")
# 展示
# print(df.head())
# print(df.info())

# 拼接時間
period = pandas.PeriodIndex(year=df["year"], month=df["month"], day=df["day"], hour=df["hour"], freq="H")
# 將時間數據賦值
df["dataTime"] = period
# 設置索引
df.set_index("dataTime", inplace=True)
# # print(period)
# print(df.head())

# 通過月份統計
df = df.resample("M").mean()

# (統計)缺失
data = df["PM_US Post"].dropna()

# pylot展示
x = data.index
y = data.values

pyplot.figure(figsize=(20, 8), dpi=80)
pyplot.plot(range(len(x)), y)
pyplot.xticks(range(0, len(x), 3), x[::3])
pyplot.show() 

  BeijingPM20100101_20151231.csv
  數據來源:https://www.kaggle.com/uciml/pm25-data-for-five-chinese-cities

  效果:

  七、數據分析纔是數據作用的根本出發點,matplotlib只是輔助工具,numpy纔是核心,這也是做機器學習、深度學習的基礎。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章