十个超有用的 Python 的库
2024-02-01 12:43:30 软件 195观看
摘要PandasPandas 是 Python 中最流行的数据操作和分析库之一。它提供了一个强大的数据结构,称为 DataFrame,它允许你轻松存储和操作结构化数据。import pandas as pd# Create a DataFramedata = {'Name': ['Alice', 'Bob',

Pandas

Pandas 是 Python 中最流行的数据操作和分析库之一。它提供了一个强大的数据结构,称为 DataFrame,它允许你轻松存储和操作结构化数据。MGH28资讯网——每日最新资讯28at.com

import pandas as pd# Create a DataFramedata = {'Name': ['Alice', 'Bob', 'Charlie'],        'Age': [25, 30, 35],        'Occupation': ['Engineer', 'Teacher', 'Designer']}df = pd.DataFrame(data)print(df)

NumPy

NumPy 是 Python 中科学计算的基础库。它提供对大型多维数组和矩阵的支持,以及对这些数组进行操作的数学函数集合。MGH28资讯网——每日最新资讯28at.com

arr = np.array([[1, 2, 3], [4, 5, 6]])print(arr)

Matplotlib

Matplotlib 是一个绘图库,允许你创建各种类型的绘图,包括线图、条形图、直方图和散点图。MGH28资讯网——每日最新资讯28at.com

import matplotlib.pyplot as plt# Create a line plotx = [1, 2, 3, 4, 5]y = [1, 4, 9, 16, 25]plt.plot(x, y)plt.show()

RequestsMGH28资讯网——每日最新资讯28at.com

Requests 是一个用于在 Python 中发出 HTTP 请求的库。它简化了发送 HTTP 请求和处理响应的过程。MGH28资讯网——每日最新资讯28at.com

import requests# Send a GET requestresponse = requests.get('https://www.example.com')print(response.text)

BeautifulSoup

BeautifulSoup 是一个用于解析 HTML 和 XML 文档的库。它可以轻松地从网页中提取数据并导航文档树结构。MGH28资讯网——每日最新资讯28at.com

from bs4 import BeautifulSoup# Parse an HTML documenthtml = '<html><body><h1>Example</h1></body></html>'soup = BeautifulSoup(html, 'html.parser')print(soup.h1.text)

SQLAlchemy

SQLAlchemy 是 Python 的对象关系映射 (ORM) 库。它提供了一种使用 Python 对象与数据库交互的方式,使得管理数据库操作变得更加容易。MGH28资讯网——每日最新资讯28at.com

from sqlalchemy import create_engine, Column, Integer, Stringfrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy.orm import sessionmaker# Define a database modelBase = declarative_base()class User(Base):    __tablename__ = 'users'    id = Column(Integer, primary_key=True)    name = Column(String)# Create a database sessionengine = create_engine('sqlite:///example.db')Session = sessionmaker(bind=engine)session = Session()# Add a new useruser = User(name='Alice')session.add(user)session.commit()# Query the users tableusers = session.query(User).all()for user in users:    print(user.name)

Scikit-learn

Scikit-learn 是 Python 中的机器学习库。它提供了一系列用于数据挖掘、数据分析和预测建模的算法和工具。MGH28资讯网——每日最新资讯28at.com

from sklearn.ensemble import RandomForestClassifierfrom sklearn.datasets import load_iris# Load the Iris datasetdata = load_iris()# Train a random forest classifierclassifier = RandomForestClassifier()classifier.fit(data.data, data.target)# Make predictionspredictions = classifier.predict([[5.1, 3.5, 1.4, 0.2], [6.2, 2.9, 4.3, 1.3]])print(predictions)

TensorFlow

TensorFlow 是一个用于数值计算和机器学习的库。它为构建和训练各种类型的机器学习模型提供了灵活的框架。MGH28资讯网——每日最新资讯28at.com

import tensorflow as tf# Create a TensorFlow constanta = tf.constant(1)b = tf.constant(2)# Perform a computationc = tf.add(a, b)# Run the computationwith tf.Session() as sess:    result = sess.run(c)    print(result)

Django

Django 是 Python 的高级 Web 框架。它提供了一种干净、高效的方式来构建 Web 应用程序、处理 URL 路由、数据库管理和表单处理等任务。MGH28资讯网——每日最新资讯28at.com

from django.urls import pathfrom django.http import HttpResponse# Define a viewdef hello(request):    return HttpResponse('Hello, World!')# Define URLsurlpatterns = [    path('hello/', hello),]# Configure and run the Django applicationfrom django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()

Pytest

Pytest 是 Python 的测试框架。它简化了编写测试的过程,并提供了强大的功能,例如测试发现、测试参数化和固定装置。MGH28资讯网——每日最新资讯28at.com

import pytest# Define a test functiondef test_addition():    result = 1 + 2    assert result == 3# Run the testspytest.main()

本文链接:http://www.28at.com/showinfo-26-70390-0.html十个超有用的 Python 的库

声明:本网页内容旨在传播知识,不代表本站观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。

显示全文

上一篇:前端Async和Await的原理、流程、使用方法及注意事项你知道多少?

下一篇:分享 15 个 HTML 新特性,大多数人可能不知道,建议尽早使用上

最新热点