【Python】Python相关库的使用说明

Python库

数据分析

  1. Numpy:表达N维数组的最基础库
    使用普通for循环计算:
#两个一维数组的计算
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())
  1. Pandas:Python数据分析高层次应用库
    Series = 索引 + 一位数据
    DataFrame = 行列索引 + 二维数据
  2. SciPy:数学、科学和工程计算功能库

数据可视化

  1. Matplotlib:高质量的二维数据可视化功能库
    在这里插入图片描述

  2. Seaborn:统计类数据可视化功能库
    在这里插入图片描述

  3. Mayavi:三维科学数据可视化功能库
    在这里插入图片描述

文本处理

  1. PyPDF2:用来处理PDF文件的工具集
  2. NLTK:自然语言文本处理第三方库
from nltk.corpus import treebank
t = treebank.parsed_sents('wsj_0001.mrg')[0]
t.draw()
  1. Python-docx:创建或更新Microsoft Word文件的第三方库
from docx import Document
document = Document()
document.add_heading('Document Title',0)
p = document.add_paragraph('A plain paragraph having some ')
document.add_page_break()
document.save('demo.dock')

机器学习

  1. Scikit-learn:机器学习方法工具集
  2. TensorFlow:AlphaGo背后的机器学习计算框架
  3. MXNet:基于神经网络的深度学习计算框架

示例:霍兰德人格分析

#霍兰德人格分析
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)
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.jpg')
plt.show()

在这里插入图片描述

网络爬虫

  1. Requests:最友好的网络爬虫功能库
  2. Scrapy:优秀的网络爬虫框架,Python数据分析高层次应用库
  3. pyspider:强大的Web页面爬取系统

Web信息提取

  1. Beartiful Soup:HTMLhe XML的解析库
  2. Re:正则表达式解析和处理功能库
  3. Python-Goose:提取文章类型Web页面的功能库

Web网站开发

  1. Django:最流行的Web应用框架
  2. Pyramid:规模适中的Web应用框架
  3. Flask:Web应用开发微框架

示例:Pyramid

#Pyramid:规模适中的Web应用框架
#从wsgiref.simple_server模块中导入make_server函数,该函数用于创建一个WSGI服务器对象。
from wsgiref.simple_server import make_server
#从pyramid模块中分别导入Configurator和Response类,这两个类用于配置应用程序和生成HTTP响应。
from pyramid.config import Configurator
from pyramid.response import Response
#定义一个名为hello_world的视图函数,它接收一个request对象作为参数,
# 并返回一个带有"Hello World!"文本内容的Response对象。
def hello_world(request):
    return Response('Hello World!')
#检查当前模块是否是主程序。
#使用with语句创建一个Configurator对象并将其赋值给config变量,
# 然后在其中添加一个路由,将/路径映射到hello_world视图函数上。
# 最后,使用config.make_wsgi_app()方法创建一个WSGI应用程序对象,并将其赋值给app变量。
if __name__ == '__main__':
    with Configurator() as config:
        config.add_route('hello','/')
        config.add_view(hello_world,route_name='hello')
        app = config.make_wsgi_app()
#使用make_server()函数创建一个WSGI服务器对象,并指定在端口6543上监听所有IP地址。
    # 然后,调用服务器对象的serve_forever()方法,启动服务器并开始接收请求。
    server = make_server('0.0.0.0',6543,app)
    server.serve_forever()

通过访问 http://localhost:6543/ 或 http://127.0.0.1:6543/,会在浏览器中看到 “Hello World!” 的文本。
在这里插入图片描述

网络应用开发

  1. WePoBot:微信公众号开发框架
  2. api:百度AI开放平台接口
  3. MyQR:二维码生成第三方库

图形用户界面

  1. PyQt5:Qt开发框架的Python接口
  2. wxPython:跨平台GUI开发框架
  3. PyGObject:使用GTK+开发GUI的功能库

游戏开发

  1. PyGame:简单的游戏开发功能库
  2. Panda3D:开源、跨平台的3D渲染和游戏开发库
  3. cocos2D:构建2D游戏和图形界面交互式应用的框架

虚拟现实

  1. VR Zero:在树莓派上开发VR应用的Python库
  2. pyovr:Oculus Rift的Python开发接口
  3. Vizard:基于Python的通用VR开发引擎

图形艺术

  1. Quads:迭代的艺术
  2. ascii_art:ASCII艺术库
  3. turtle:海龟绘制体系

示例:玫瑰花的绘制

#玫瑰花的绘制
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()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值