09 python基礎--常用庫
阿新 • • 發佈:2018-12-18
09.1 資料處理->人工智慧
簡介
資料表示->資料清洗->資料統計->資料視覺化->資料探勘->人工智慧
資料表示:採用合適方式用程式表達資料
資料清理:資料歸一化、資料轉換、異常值處理
資料統計:資料的概要理解,數量、分佈、中位數等
資料視覺化:直觀展示資料內涵的方式
資料探勘:從資料分析獲得知識,產生資料外的價值
人工智慧:資料/語言/影象/視覺等方面深度分析與決策
庫介紹
資料處理: Numpy: 表達N維陣列的最基礎庫,C語言實現,計算速度優異(http://www.numpy.org); Pandas: Python資料分析高層次應用庫基於Numpy開發(http://pandas.pydata.org); SciPy: 數學、科學和工程計算功能庫(http://www.scipy.org) 視覺化: Matplotlib: 高質量的二維資料視覺化功能庫(http://matplotlib.org); Seaborn: 統計類資料視覺化功能庫; Mayavi:三維科學資料視覺化功能庫(http://docs.enthought.com/mayavi/mayavi/) 文字處理: PyPDF2:用來處理pdf檔案的工具集; NLTK:自然語言文字處理第三方庫; Python-docx:建立或更新Microsoft Word檔案的第三方庫 機器學習: Scikit-learn:機器學習方法工具集; TensorFlow:AlphaGo背後的機器學習計算框架; MXNet:基於神經網路的深度學習計算框架
def pysum(): a = [0,1,2,3,4] b = [9,8,7,6,5] c = [] for i in range(len(a)): c.append(a[i]**2+b[i]**3) return c print(pysum()) ## numpy庫 import numpy as np def npsum(): a = np.array([0,1,2,3,4]) b = np.array([9,8,7,6,5]) c = a**2 + b**3 return c print(npsum()) >[729 513 347 225 141] ## "霍蘭德人格分析雷達圖"例項展示 import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams['font.family'] = 'SimHei' ## 黑體 radar_labels = np.array(['研究型(I)','藝術型(A)','社會型(S)','企業型(E)','常規型(C)','現實型(R)']) data = np.array([[0.40, 0.32, 0.35, 0.30, 0.30, 0.88], [0.85, 0.35, 0.30, 0.40, 0.40, 0.30], [0.43, 0.89, 0.30, 0.28, 0.22, 0.30], [0.30, 0.25, 0.48, 0.85, 0.45, 0.40], [0.20, 0.38, 0.87, 0.45, 0.32, 0.28], [0.34, 0.31, 0.38, 0.40, 0.92, 0.28]]) #資料值 data_labels = ('藝術家','實驗員','工程師','推銷員','社會工作者','記事員') angles = np.linspace(0, 2*np.pi, 6, endpoint=False) data = np.concatenate((data, [data[0]])) angles = np.concatenate((angles, [angles[0]])) fig = plt.figure(facecolor="white") plt.subplot(111, polar=True) plt.plot(angles,data,'o-', linewidth=1, alpha=0.2) plt.fill(angles,data, alpha=0.25) plt.thetagrids(angles*180/np.pi, radar_labels,frac = 1.2) plt.figtext(0.52, 0.95, '霍蘭德人格分析', ha='center', size=20) legend = plt.legend(data_labels, loc=(0.94, 0.80), labelspacing=0.1) plt.setp(legend.get_texts(), fontsize='large') plt.grid(True) plt.savefig('holland_radar.png') plt.show()
09.2 Web解析->網路空間
庫介紹
網路爬蟲: Requests: 最友好的網路爬蟲功能庫(http://www.python-requests.org/); Scrapy: 優秀的網路爬蟲框架(https://scrapy.org); pyspider: 強大的Web頁面爬取系統 資訊提取: Beautiful Soup: HTML和XML的解析庫(https://www.crummy.com/software/BeautifulSoup/bs4); Re: 正則表示式解析和處理功能庫(https://docs.python.org/3.6/library/re.html); Python-Goose: 提取文章型別Web頁面的功能庫(https://github.com/grangier/python-goose) 網站開發: Django: 最流行的Web應用框架(MTV)(https://www.djangoproject.com); Pyramid: 規模適中的Web應用框架(https://trypyramid.com/); Flask: Web應用開發微框架(http://flask.pocoo.org) 應用開發: WeRoBot: 微信公眾號開發框架(https://github.com/offu/WeRoBot); aip: 百度AI開放平臺介面(https://github.com/Baidu-AIP/python-sdk); MyQR: 二維碼生成第三方庫(https://github.com/sylnsfar/qrcode)
## Pyramid
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('Hello World!')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('hello', '/')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()
## Flask
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
## WeRoBot
import werobot
robot = werobot.WeRoBot(token='tokenhere')
@robot.handler
def hello(message):
return 'Hello World!'
09.3 人機互動->藝術設計
庫介紹
圖形使用者介面:
PyQt5: Qt開發框架的Python介面(https://www.riverbankcomputing.com/software/pyqt);
wxPython: 跨平臺GUI開發框架
PyGObject: 使用GTK+開發GUI的功能庫
遊戲開發:
PyGame: 簡單的遊戲開發功能庫(http://www.pygame.org);
Panda3D: 開源、跨平臺的3D渲染和遊戲開發庫;
cocos2d: 構建2D遊戲和圖形介面互動式應用的框架
虛擬現實:
VR Zero: 在樹莓派上開發VR應用的Python庫;
pyovr: Oculus Rift的Python開發介面;
Vizard: 基於Python的通用VR開發引擎(http://www.worldviz.com/vizard-virtual-reality-software);
圖形藝術:
Quads: 迭代的藝術(https://github.com/fogleman/Quads);
ascii_art: ASCII藝術庫(https://github.com/jontonsoup4/ascii_art);
turtle: 海龜繪圖體系(https://docs.python.org/3/library/turtle.html);
## PyGObject
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
window = Gtk.Window(title="Hello World")
window.show()
window.connect("destroy", Gtk.main_quit)
Gtk.main()
## 玫瑰花
import turtle as t
# 定義一個曲線繪製函式
def DegreeCurve(n, r, d=1):
for i in range(n):
t.left(d)
t.circle(r, abs(d))
# 初始位置設定
s = 0.2 # size
t.setup(450*5*s, 750*5*s)
t.pencolor("black")
t.fillcolor("red")
t.speed(100)
t.penup()
t.goto(0, 900*s)
t.pendown()
# 繪製花朵形狀
t.begin_fill()
t.circle(200*s,30)
DegreeCurve(60, 50*s)
t.circle(200*s,30)
DegreeCurve(4, 100*s)
t.circle(200*s,50)
DegreeCurve(50, 50*s)
t.circle(350*s,65)
DegreeCurve(40, 70*s)
t.circle(150*s,50)
DegreeCurve(20, 50*s, -1)
t.circle(400*s,60)
DegreeCurve(18, 50*s)
t.fd(250*s)
t.right(150)
t.circle(-500*s,12)
t.left(140)
t.circle(550*s,110)
t.left(27)
t.circle(650*s,100)
t.left(130)
t.circle(-300*s,20)
t.right(123)
t.circle(220*s,57)
t.end_fill()
# 繪製花枝形狀
t.left(120)
t.fd(280*s)
t.left(115)
t.circle(300*s,33)
t.left(180)
t.circle(-300*s,33)
DegreeCurve(70, 225*s, -1)
t.circle(350*s,104)
t.left(90)
t.circle(200*s,105)
t.circle(-500*s,63)
t.penup()
t.goto(170*s,-30*s)
t.pendown()
t.left(160)
DegreeCurve(20, 2500*s)
DegreeCurve(220, 250*s, -1)
# 繪製一個綠色葉子
t.fillcolor('green')
t.penup()
t.goto(670*s,-180*s)
t.pendown()
t.right(140)
t.begin_fill()
t.circle(300*s,120)
t.left(60)
t.circle(300*s,120)
t.end_fill()
t.penup()
t.goto(180*s,-550*s)
t.pendown()
t.right(85)
t.circle(600*s,40)
# 繪製另一個綠色葉子
t.penup()
t.goto(-150*s,-1000*s)
t.pendown()
t.begin_fill()
t.rt(120)
t.circle(300*s,115)
t.left(75)
t.circle(300*s,100)
t.end_fill()
t.penup()
t.goto(430*s,-1070*s)
t.pendown()
t.right(30)
t.circle(-600*s,35)
t.done()