Python最常用的20 個(gè)包總結(jié)
numpy(數(shù)據(jù)處理和科學(xué)計(jì)算)
代碼示例:
arr = np.array([1, 2, 3, 4, 5]) print(arr)
pandas(數(shù)據(jù)處理和分析)
代碼示例:
data = {'name': ['John', 'Bob', 'Alice'], 'age': [20, 35, 25]} df = pd.DataFrame(data) print(df)
matplotlib(數(shù)據(jù)可視化)
代碼示例:
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [4, 2, 7, 5, 9] plt.plot(x, y) plt.show()
scikit-learn(機(jī)器學(xué)習(xí)工具)
代碼示例:
from sklearn.linear_model import LinearRegression X = [[1, 4], [2, 5], [3, 6]] y = [8, 10, 12] model = LinearRegression().fit(X, y) print(model.predict([[4, 7]]))
tensorflow(深度學(xué)習(xí)框架)
代碼示例:
import tensorflow as tf x = tf.constant([1, 2, 3, 4]) y = tf.constant([5, 6, 7, 8]) z = tf.add(x, y) sess = tf.Session() print(sess.run(z))
keras(深度學(xué)習(xí)框架)
代碼示例:
from keras.models import Sequential from keras.layers import Dense model = Sequential() model.add(Dense(10, input_dim=5, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam')
requests(HTTP 庫)
代碼示例:
import requests response = requests.get('https://www.baidu.com') print(response.text)
flask(Web 框架)
代碼示例:
from flask import Flask, render_template app = Flask(**name**) @app.route('/') def index(): return render_template('index.html') if **name** == '**main**': app.run(debug=True)
scrapy(網(wǎng)絡(luò)爬蟲框架)
代碼示例:
import scrapy class MySpider(scrapy.Spider): name = 'myspider' start_urls = ['http://quotes.toscrape.com'] def parse(self, response): for quote in response.css('div.quote'): yield {'text': quote.css('span.text::text').get(), 'author': quote.css('span small::text').get()}
beautifulsoup(HTML 解析器)
代碼示例:
from bs4 import BeautifulSoup html = '<html><head><title>這是標(biāo)題</title></head><body><p>這是一個(gè)段落。</p ></body></html>' soup = BeautifulSoup(html, 'html.parser') print(soup.title.text)
selenium(Web 自動化測試)
代碼示例:
from selenium import webdriver driver = webdriver.Chrome() driver.get('https://www.baidu.com') search_box = driver.find_element_by_name('wd') search_box.send_keys('Python') search_box.submit()
ctypes(調(diào)用 C 語言庫)
代碼示例:
import ctypes lib = ctypes.cdll.LoadLibrary('libexample.so') lib.add(1, 2)
wxPython(GUI 開發(fā))
代碼示例:
import wx app = wx.App() frame = wx.Frame(None, title='Hello, wxPython!') frame.Show() app.MainLoop()
pillow(圖像處理)
代碼示例:
from PIL import Image im = Image.open('test.jpg') im.show()
openpyxl(處理 Excel 文件)
代碼示例:
import openpyxl wb = openpyxl.load_workbook('example.xlsx') sheet = wb['Sheet1'] cell = sheet['A1'] print(cell.value)
nltk(自然語言處理)
代碼示例:
import nltk sent = ‘This is a sentence.' tokens = nltk.word_tokenize(sent) print(tokens)
jieba(中文分詞)
代碼示例:
import jieba text = '我愛中文分詞' words = jieba.cut(text) for word in words: print(word)
re(正則表達(dá)式)
代碼示例:
import re text = 'The quick brown fox jumps over the lazy dog.' pattern = re.compile('fox') print(pattern.findall(text))
datetime(日期時(shí)間處理)
代碼示例:
import datetime dt = datetime.datetime.now() print(dt)
random(隨機(jī)數(shù)生成)
代碼示例:
import random print(random.randint(1, 10))
到此這篇關(guān)于Python最常用的20 個(gè)包總結(jié)的文章就介紹到這了,更多相關(guān)Python常用包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python unittest如何生成HTMLTestRunner模塊
這篇文章主要介紹了Python unittest如何生成HTMLTestRunner模塊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09python連接MySQL數(shù)據(jù)庫實(shí)例分析
這篇文章主要介紹了python連接MySQL數(shù)據(jù)庫,實(shí)例分析了Python操作MySQL的相關(guān)技巧,需要的朋友可以參考下2015-05-05Python+radar實(shí)現(xiàn)隨機(jī)日期時(shí)間的生成
Python有廣泛豐富的第三方庫,在沒有特殊定制下,避免了重復(fù)造輪子。本文將利用radar庫實(shí)現(xiàn)生成隨機(jī)的日期或時(shí)間,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05Python實(shí)現(xiàn)提高運(yùn)行速度的技巧分享
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)提高運(yùn)行速度的相關(guān)技巧,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-06-06Python實(shí)現(xiàn)的多線程端口掃描工具分享
這篇文章主要介紹了Python實(shí)現(xiàn)的多線程端口掃描工具分享,工具實(shí)現(xiàn)了掃單IP和掃IP段功能,本文給出運(yùn)行效果和實(shí)現(xiàn)源碼,需要的朋友可以參考下2015-01-01python3實(shí)現(xiàn)帶多張圖片、附件的郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)帶多張圖片、附件的郵件發(fā)送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08