当前位置: 首页 > news >正文

python中数据处理库,机器学习库以及自动化与爬虫

Python 在数据处理、机器学习和自动化任务方面非常强大,它的库生态系统几乎涵盖了所有相关领域。我们将从以下几个部分来介绍 Python 中最常用的库:

  1. 数据处理库:Pandas、NumPy 等
  2. 机器学习库:Scikit-learn、TensorFlow、Keras 等
  3. 自动化与爬虫:Selenium、Requests、BeautifulSoup、Scrapy 等

一、Python 中的数据处理库

1.1 Pandas

Pandas 是 Python 最流行的数据处理库之一,专门用于处理结构化数据(如表格、CSV 文件等)。它引入了两种主要的数据结构:SeriesDataFrame,可以高效地进行数据操作。

Pandas 基本用法
  • 安装 Pandas

    pip install pandas
    
  • 创建 DataFrame

    import pandas as pddata = {'Name': ['Alice', 'Bob', 'Charlie'],'Age': [25, 30, 35],'Salary': [50000, 60000, 70000]}df = pd.DataFrame(data)
    print(df)
    
  • 读取和写入 CSV 文件

    # 读取 CSV 文件
    df = pd.read_csv('data.csv')# 写入 CSV 文件
    df.to_csv('output.csv', index=False)
    
  • 常见数据操作

    # 查看前几行数据
    print(df.head())# 过滤数据
    df_filtered = df[df['Age'] > 30]# 添加新列
    df['Bonus'] = df['Salary'] * 0.1# 分组并聚合
    grouped = df.groupby('Age').mean()# 缺失值处理
    df.fillna(0, inplace=True)  # 用 0 填充缺失值
    

1.2 NumPy

NumPy 是 Python 的数值计算库,专门用于处理大规模的数组和矩阵运算。Pandas 底层数据结构基于 NumPy。

NumPy 基本用法
  • 安装 NumPy

    pip install numpy
    
  • 创建数组

    import numpy as np# 创建一维数组
    arr = np.array([1, 2, 3])# 创建二维数组
    matrix = np.array([[1, 2], [3, 4]])
    
  • 数组运算

    # 数组元素相加
    arr_sum = arr + 2# 矩阵乘法
    mat_mul = np.dot(matrix, matrix)
    
  • 数组统计

    # 求和
    total = np.sum(arr)# 均值
    mean = np.mean(arr)# 标准差
    std_dev = np.std(arr)
    

1.3 数据可视化库:Matplotlib 与 Seaborn

Matplotlib 是一个基础的数据可视化库,Seaborn 则是在 Matplotlib 之上构建的更高级别的库,提供了更简洁美观的绘图接口。

  • 安装 Matplotlib 和 Seaborn
    pip install matplotlib seaborn
    
Matplotlib 示例
import matplotlib.pyplot as plt# 生成简单的折线图
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]plt.plot(x, y)
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Simple Line Plot')
plt.show()
Seaborn 示例
import seaborn as sns# 加载示例数据集
tips = sns.load_dataset("tips")# 生成一个散点图
sns.scatterplot(x="total_bill", y="tip", data=tips)
plt.show()

二、Python 中的机器学习库

2.1 Scikit-learn

Scikit-learn 是一个功能强大的机器学习库,包含了经典的机器学习算法、数据预处理工具和模型评估功能。它特别适合用来构建和训练传统机器学习模型,如回归、分类、聚类等。

  • 安装 Scikit-learn
    pip install scikit-learn
    
Scikit-learn 基本用法
  • 加载数据集

    from sklearn.datasets import load_irisiris = load_iris()
    X = iris.data
    y = iris.target
    
  • 训练模型

    from sklearn.model_selection import train_test_split
    from sklearn.ensemble import RandomForestClassifier# 分割数据集
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)# 创建模型并训练
    clf = RandomForestClassifier()
    clf.fit(X_train, y_train)# 预测
    y_pred = clf.predict(X_test)
    
  • 评估模型

    from sklearn.metrics import accuracy_scoreaccuracy = accuracy_score(y_test, y_pred)
    print(f"Accuracy: {accuracy}")
    

2.2 TensorFlow 和 Keras

TensorFlow 是一个流行的开源深度学习框架,Keras 是一个基于 TensorFlow 的高级神经网络库,提供了更加简洁的 API。它们被广泛用于构建和训练深度神经网络模型。

  • 安装 TensorFlow 和 Keras
    pip install tensorflow
    
TensorFlow/Keras 基本用法
  • 构建简单的神经网络模型
    import tensorflow as tf
    from tensorflow.keras import layers# 构建模型
    model = tf.keras.Sequential([layers.Dense(64, activation='relu', input_shape=(4,)),layers.Dense(64, activation='relu'),layers.Dense(3, activation='softmax')
    ])# 编译模型
    model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])# 训练模型
    model.fit(X_train, y_train, epochs=10)# 评估模型
    loss, accuracy = model.evaluate(X_test, y_test)
    print(f"Test accuracy: {accuracy}")
    

2.3 PyTorch

PyTorch 是另一个流行的深度学习框架,因其动态计算图和灵活性而受到研究人员的青睐。

  • 安装 PyTorch
    pip install torch
    
PyTorch 示例
import torch
import torch.nn as nn
import torch.optim as optim# 构建一个简单的线性模型
class SimpleModel(nn.Module):def __init__(self):super(SimpleModel, self).__init__()self.linear = nn.Linear(1, 1)def forward(self, x):return self.linear(x)# 初始化模型、损失函数和优化器
model = SimpleModel()
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)# 训练模型(假设你有数据 X 和 y)
for epoch in range(100):optimizer.zero_grad()outputs = model(torch.tensor([[1.0]]))  # 输入为 1loss = criterion(outputs, torch.tensor([[2.0]]))  # 期望输出为 2loss.backward()optimizer.step()print("模型训练完成")

三、自动化与爬虫

3.1 自动化工具

Selenium

Selenium 是一个自动化 Web 浏览器的工具,广泛用于自动化测试和 Web 爬虫。

  • 安装 Selenium

    pip install selenium
    
  • 使用 Selenium 自动化浏览器操作

    from selenium import webdriver# 启动浏览器
    driver = webdriver.Chrome()# 打开网页
    driver.get("https://www.example.com")# 查找元素并进行操作
    element = driver.find_element_by_name("q")
    element.send_keys("Selenium")
    element.submit()# 关闭浏览器
    driver.quit()
    

3.2 网络请求库:Requests

Requests 是一个简单且功能强大的 HTTP 请求库,适合进行 API 请求和基本的 Web 爬取任务。

  • 安装 Requests

    pip install requests
    
  • 发送 HTTP 请求

    import requests# 发送 GET 请求
    response = requests.get('https://api.example.com/data')# 解析 JSON 数据
    data = response.json()
    print(data)
    

3.3 BeautifulSoup

**Beautiful

Soup** 是一个用于解析 HTML 和 XML 的库,通常与 Requests 搭配使用,适合抓取网页数据。

  • 安装 BeautifulSoup

    pip install beautifulsoup4
    
  • 解析网页并提取数据

    from bs4 import BeautifulSoup
    import requests# 发送请求
    response = requests.get('https://example.com')# 解析 HTML
    soup = BeautifulSoup(response.content, 'html.parser')# 提取标题
    title = soup.title.string
    print(f"页面标题: {title}")
    

3.4 Scrapy

Scrapy 是一个用于构建强大 Web 爬虫的框架,适合大规模数据抓取任务。

  • 安装 Scrapy

    pip install scrapy
    
  • Scrapy 基本示例

    scrapy startproject myspider
    

    进入项目目录后,编辑 spiders 目录中的爬虫脚本。

    import scrapyclass QuotesSpider(scrapy.Spider):name = "quotes"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('small.author::text').get(),}next_page = response.css('li.next a::attr(href)').get()if next_page is not None:yield response.follow(next_page, self.parse)
    
    • 运行爬虫
      scrapy crawl quotes
      

总结

Python 拥有强大的库生态,涵盖了数据处理、机器学习、自动化以及 Web 爬虫等多个领域。你可以通过 Pandas 和 NumPy 高效处理数据,用 Scikit-learn 和 TensorFlow 构建机器学习模型,并通过 Selenium 和 Requests 等库实现 Web 自动化和爬虫任务。结合这些工具,可以轻松完成从数据采集到分析、建模和自动化的全流程。

如果你想进一步探索这些库,可以尝试更多实战项目,并结合具体的需求来选择合适的工具。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • flume系列之:出现数据堆积时临时增大sink端消费能力
  • [spring]应用分层 及 Spring IoCDI
  • Leetcode3289. 数字小镇中的捣蛋鬼
  • Unity 百度AI实现无绿幕拍照抠像功能(详解版)
  • 人工智能之计算机视觉的发展历程与相关技术内容,相应的模型介绍
  • 树上差分+lca 黑暗的锁链
  • 【C#生态园】构建你的C#操作系统:框架选择与实践
  • <刷题笔记> 力扣236题——二叉树的公共祖先
  • VS code 查看 ${workspaceFolder} 目录指代路径
  • nginx服务介绍
  • 密集行人数据集 CrowdHumanvoc和yolo两种格式,yolo可以直接使用train val test已经划分好有yolov8训练200轮模型
  • 全栈开发(四):使用springBoot3+mybatis-plus+mysql开发restful的增删改查接口
  • VSCode开发ros程序无法智能提示的解决方法(二)
  • 【计网面试真题】If-Modified-Since和Etag有什么区别
  • 【SSM-Day2】创建SpringBoot项目
  • [deviceone开发]-do_Webview的基本示例
  • Angular 响应式表单之下拉框
  • axios 和 cookie 的那些事
  • ES6语法详解(一)
  • gcc介绍及安装
  • golang 发送GET和POST示例
  • JavaScript中的对象个人分享
  • Python - 闭包Closure
  • python_bomb----数据类型总结
  • vagrant 添加本地 box 安装 laravel homestead
  • vue:响应原理
  • Vue实战(四)登录/注册页的实现
  • 试着探索高并发下的系统架构面貌
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (1)虚拟机的安装与使用,linux系统安装
  • (26)4.7 字符函数和字符串函数
  • (42)STM32——LCD显示屏实验笔记
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (delphi11最新学习资料) Object Pascal 学习笔记---第5章第5节(delphi中的指针)
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (PADS学习)第二章:原理图绘制 第一部分
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (zt)最盛行的警世狂言(爆笑)
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (含笔试题)深度解析数据在内存中的存储
  • (总结)(2)编译ORB_SLAM2遇到的错误
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .NET 4.0中的泛型协变和反变
  • .NET COER+CONSUL微服务项目在CENTOS环境下的部署实践
  • .Net Core 微服务之Consul(二)-集群搭建
  • .NET Framework 3.5安装教程
  • .NET Standard 的管理策略
  • 。。。。。
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • @hook扩展分析
  • @Responsebody与@RequestBody