Python学习笔记¶

第八章 程序设计方法学¶

至此,Python基础语法体系已经全部结束!简要回顾一下

  • 数字类型和操作
  • 字符串类型和操作
  • 程序的分支结构
  • 程序的循环结构
  • 函数的定义和使用
  • 代码复用和函数递归
  • 组合数据类型(集合、序列(元组,列表,字符串)、字典)
  • 文件的使用
  • 数据的维度及格式化和处理

8.1 体育竞技分析¶

8.1.1 需求分析¶

  • 科学分析体育竞技比赛?
  • 输出球员水平,输出可预测的比赛成绩
  • 计算思维:抽象+自动化
  • 模拟:抽象比赛过程+自动化执行N场比赛
  • N越大,比赛结果越科学

  • 假设新的比赛规则(早年女排规则):

    • A&B两人比赛
    • 开始一方发球,直至判分,接下来胜者发球
    • 15分胜一局
    • 有发球权才能得分

8.1.2 自顶向下和自底向上¶

  • 自顶向下
    • 解决复杂问题的有效方法
    • 一个总的问题表达成若干个小的问题的组成
    • 使用同样的方法进一步分解小的问题
    • 自顶向下设计
  • 自底向上
    • 自底向上执行
    • 逐步组建复杂系统的有效测试方法
    • 分单元测试,逐步组装
    • 按照自顶向下的相反路径
    • 直到系统的各部分以组装的思路都经过测试和验证

8.1.3 代码编写¶

整体步骤:打印介绍信息->获取参数->模拟->输出结果

通过四个函数实现这件事

In [ ]:
import random

def print_info():
    print("欢迎来到体育竞技模拟器")
    print("请输入A和B队员的能力(0到1之间的小数)")

def get_inputs():
    a = eval(input("请输入A的能力(0-1):"))
    b = eval(input("请输入B的能力(0-1):"))
    n = eval(input("请输入要模拟的次数:"))
    return a,b,n

def simulate_N(a,b,n):
    win_a, win_b = (0, 0)
    for i in range(n):
        score_a ,score_b = simulate_one(a,b)
        if score_a > score_b:
            win_a+=1
        else:
            win_b+=1
    return win_a,win_b

def simulate_one(a,b):
    score_a,score_b = (0,0)
    ball_control_list = {"A","B"}
    ball_control =  ball_control_list.pop() # 假定A或者B先发球
    while not game_over(score_a,score_b):
        # 根据能力判定谁赢了这一局
        # 假定发球和能力十分相关
        if ball_control== "A":
            if random.random()<a:
                score_a+=1
            else:
                ball_control = "B"
        else:
            if random.random()<b:
                score_b+=1
            else:
                ball_control = "A"
    return score_a,score_b

def game_over(score_a,score_b):
    return score_a==15 or score_b==15

def print_summary(wins_a,wins_b):
    n = wins_a+wins_b
    print("共模拟比赛{}场".format(n))
    print("A获胜{}场,胜率为{}".format(wins_a,wins_a/n))
    print("B获胜{}场,胜率为{}".format(wins_b,wins_b/n))

print_info()
a,b,n = get_inputs()
win_a,win_b = simulate_N(a,b,n)
print_summary(win_a,win_b)

初步结论:差之毫厘,谬以千里。一点点能力的不同会导致悬殊的差距。

但是如果水平相近,那么才是比分变化微妙的时刻。

8.2 Python程序设计思维¶

8.2.1 计算思维与程序设计¶

  • 第三种人类思维特征
  • 逻辑思维:推理和演义,数学为代表,A->B B->C => A->C
  • 实证思维:实验和验证,物理为代表,引力波<-实验
  • 计算思维:设计和构造,计算机为代表,汉诺塔递归

  • 计算思维的特征:抽象和自动化

  • 计算思维:Computational Thinking
  • 抽象问题的计算过程,利用计算机自动化求解
  • 计算思维是基于计算机的思维方式

例1:计算1+2+...+100

  • 逻辑思维:高斯求和公式
  • 计算思维:写个循环就行了

例2:圆周率的计算

  • 逻辑思维:无限级数求和
  • 计算思维:蒙特卡罗方法,无限撒点,逼近

例3:汉诺塔思维

  • 逻辑思维:2^n-1
  • 计算思维:写个递归就行

例4:天气预报

  • 以前:实证思维+逻辑思维 利用经验来预测
  • 现在:有了超级计算机。利用MM5模型,利用全球传感器的值,经过迭代和计算

例5:量化分析

  • 以前:看K线图,猜测,实证思维+预测思维
  • 现在:历史大量数据,进行机器学习,完成预测,自动交易

  • 计算思维基于计算机强大的算力和海量的数据

  • 抽象计算过程,关注设计和构造,而非因果关系
  • 计算机程序设计是实现计算思维的主要手段
  • 编程是将计算思维变成现实的手段

8.2.3 计算生态与Python语言¶

  • 开源运动
    • 1983 Richard Stallman GNU项目启动
      • 雇佣有经验的开发人员,让人们无偿使用
    • 1989 GNU通用协议诞生,自由软件时代到来
    • 1991 Linus发布了Linux内核
      • 分布式,大家都可以贡献
    • 1998 网景公司开源浏览器,产生了Mozilla,开源生态逐步建立
    • Richard代表了一种大教堂模式 精英开发,用户使用
    • Linus代表集市模式,软件大众开发,服务于大众,这个成为了主流
  • 计算生态
    • 开源思想的深入演化和发展
    • 没有顶层设计、以功能为单位、有三个特点
      • 竞争发展、相互依存、迅速更迭
  • Python语言与计算生态
    • Python语言库提供了多于13W个第三方库
    • 库的建设经过了野蛮生长和自然选择
    • 同一个功能,至少有2个及以上的第三方库
    • 库之间相互关联使用,依存发展
    • Python库之间广泛联系,逐级封装
    • 社区庞大,新技术更迭迅速
    • AlphaGo就是用Python写的
  • API != 生态
    • API是设计出来的
    • 生态是生长发展出来的
  • 创新:跟随创新、集成创新、原始创新
    • 计算生态是加速科技类创新的重要支撑
    • 发展科技产品商业价值的重要模式
    • 国家科技体系安全和稳固的基础
  • 计算生态的应用
    • 刀耕火种 -> 站在巨人的肩膀上
    • 编程的起点不是算法而是系统
    • 如同搭积木,利用计算生态为主要模式
    • 编程的目标是快速解决问题

8.4.3 用户体验与软件产品¶

  • 用户体验:用户对产品建立的主观感受和认识
  • 从功能实现到用户体验
  • 编程是手段,不是目的,程序最终为人类服务

一些可能的方法:

  • 方法1:进度展示、步骤提示、循环提示
  • 方法2:异常处理,输入合规性检查、读写文件判断、输入输出运算结果判断
  • 方法3:打印输出:在特定位置输出过程
  • 方法4:日志文件:及时记录异常和用户使用
  • 方法5:帮助信息:打印帮助信息,告诉用户该干什么

用户体验是产品到用户的关键环节,也决定了产品能不能服务更多用户。

8.4.4 基本的程序设计模式¶

  • IPO:输入输出和处理
  • 自顶向下设计:细分问题
  • 模块化设计:很多函数
  • 配置化设计:程序引擎+配置文件
    • 可选参数配置化
    • 这样程序开发变成了配置文件编写,扩展功能同时不修改程序
    • 接口设计应该清晰明了、灵活可扩展

开发的四个步骤:

  1. 产品定义:对应用需求充分理解和明确定义,还要考虑到商业模式
  2. 系统架构:以系统方式来思考实现,关注数据流、模块化、体系架构
  3. 设计与实现:结合架构完成关键设计及系统实现,结合可扩展性、灵活性进行优化
  4. 用户体验:从用户角度思考应用效果,用户至上、体验为先,用户是中心

8.3 Python第三方库安装¶

8.3.1 看见更大的Python世界¶

PyPI网站

PyPI: Python Package Index

由PSF进行维护

在里面可以充分浏览检索第三方库,找到合适的第三方库开发程序

安装Python库一般用三种方法:

  1. pip3命令
  2. 集成安装方法
  3. 文件安装方法

8.3.2 第三方库的pip3安装方法¶

In [3]:
!pip3 -h
Usage:   
  pip3 <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  cache                       Inspect and manage pip's wheel cache.
  index                       Inspect information available from package indexes.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  debug                       Show information useful for debugging.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --debug                     Let unhandled exceptions propagate outside the
                              main subroutine, instead of logging them to
                              stderr.
  --isolated                  Run pip in an isolated mode, ignoring
                              environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be
                              used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be
                              used up to 3 times (corresponding to WARNING,
                              ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --no-input                  Disable prompting for input.
  --proxy <proxy>             Specify a proxy in the form
                              [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should
                              attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists:
                              (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
  --trusted-host <hostname>   Mark this host or host:port pair as trusted,
                              even though it does not have valid or any HTTPS.
  --cert <path>               Path to PEM-encoded CA certificate bundle. If
                              provided, overrides the default. See 'SSL
                              Certificate Verification' in pip documentation
                              for more information.
  --client-cert <path>        Path to SSL client certificate, a single file
                              containing the private key and the certificate
                              in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine
                              whether a new version of pip is available for
                              download. Implied with --no-index.
  --no-color                  Suppress colored output.
  --no-python-version-warning
                              Silence deprecation warnings for upcoming
                              unsupported Pythons.
  --use-feature <feature>     Enable new functionality, that may be backward
                              incompatible.
  --use-deprecated <feature>  Enable deprecated functionality, that will be
                              removed in the future.

pip3命令简要介绍:

  • pip3 install 第三方库名:安装一个第三方库
  • pip3 install -U 第三方库名:升级一个第三方库
  • pip3 uninstall 第三方库名:卸载一个第三方库
  • pip3 download 第三方库名:下载但是不安装第三方库
  • pip3 show 库名:列出某个第三方库的详细信息
  • pip3 search 关键词:查找名称/介绍中出现的第三方库。即将被废弃,应该用pip_search库来完成检索。
  • pip3 list:列出所有的第三方库

pip3是最主要的方法,需要使用互联网连接

In [4]:
! pip3 search blockchain # 即将被废弃
! pip_search blockchain # 正确用法
ERROR: XMLRPC request failed [code: -32500]
RuntimeError: PyPI's XMLRPC API is currently disabled due to unmanageable load and will be deprecated in the near future. See https://status.python.org/ for more information.

8.3.3 第三方库的集成安装方法¶

结合特定的Python开发工具进行安装:如Anaconda。

内含800+数据分析的第三方库,并且支持多种开发环境,如Jupyter。

8.3.4 第三方库的文件安装方法¶

有些第三方库只提供源代码,需要编译后才能安装。

如果系统没有编译环境,那么pip3后无法安装。

所以可以下载别人提供好的可执行文件,这样下载完之后,使用pip3 install即可安装。

8.4 os库的使用¶

8.4.1 os库基本介绍¶

os库提供通用的、基本的操作系统交互功能。

  • 是Python标准库,包含几百个函数
  • 常用操作路径、进程管理、环境参数等
  • 支持Windows、macOS、Linux等各大平台

8.4.2 路径操作¶

使用os.path子库进行路径操作

用于处理文件路径。常见方法如下:

  • os.path.abspath(path) 返回path在当前系统中的绝对路径
  • os.path.normpath(path) 归一化表示,用/统一分隔路径
  • os.path.relpath(path) 返回程序当前路径与文件之间的相对路径 relative path
  • os.path.dirname(path) 返回path中的目录名称
  • os.path.basename(path) 返回path中最后的文件名称
  • os.path.join(path,*paths) 组合path与paths,形成一个符合路径标准的路径字符串
  • os.path.exists(path) 判断path对应的文件或者目录是否存在,返回True或者False,不会真正打开,只是判断是否存在
  • os.path.isfile(path) 判断path路径对应的文件是否为已存在的文件,返回True或者False
  • os.path.isdir(path) 判断path路径对应的文件是否为已存在的目录,返回True或者False
In [24]:
import os.path
os.chdir("/Users/caiguu/PycharmProjects/pythonLearn")
print(os.path.abspath("data.csv"))
print(os.path.normpath(os.path.abspath("data.csv")))
print(os.path.relpath("data.csv"))
print(os.path.relpath("/System/Applications/Messages.app"))
print(os.path.dirname("/System/Applications/Messages.app"))
print(os.path.basename("/System/Applications/Messages.app"))
print(os.path.join("/System","Applications","Messages.app"))
print(os.path.exists("/System/Applications/Messages.app"))
print(os.path.exists("/System/Applications/QQ.app"))
print(os.path.isfile("/System/Applications/Messages.app"))
print(os.path.isdir("/System/Applications/Messages.app")) #很显然macOS中的.app都是目录
print(os.path.isfile('data.csv'))
/Users/caiguu/PycharmProjects/pythonLearn/data.csv
/Users/caiguu/PycharmProjects/pythonLearn/data.csv
data.csv
../../../../System/Applications/Messages.app
/System/Applications
Messages.app
/System/Applications/Messages.app
True
False
False
True
True

下面是几个与文件访问时间相关的方法:

  • os.path.getatime(path) 返回path对应文件或目录上一次访问的时间。文件不存在会抛出异常
  • os.path.getmtime(path) 返回path对应文件或目录上一次修改的时间。文件不存在会抛出异常
  • os.path.getctime(path) 返回path对应文件或目录上一次创建的时间。文件不存在会抛出异常
In [27]:
import os.path
print(os.path.getatime("data.csv")) #access
print(os.path.getmtime("data.csv")) #modify
print(os.path.getctime("data.csv")) #create
1645429359.6352324
1645354605.9654377
1645354605.9654377

最后一个:

  • os.path.getsize(path) 返回文件大小,以字节为单位
In [28]:
print(os.path.getsize("data.csv"))
232

8.4.3 进程管理¶

  • os.system(command) 执行系统的命令行

比如我们调用一个云天气APP:

In [30]:
os.system("/Applications/云天气.app/Contents/MacOS/云天气")
2022-02-22 16:11:16.493 云天气[2563:2007491] *** WARNING: Textured window <AtomNSWindow: 0x7f800af2e0b0> is getting an implicitly transparent titlebar. This will break when linking against newer SDKs. Use NSWindow's -titlebarAppearsTransparent=YES instead.
https://mtapi.ruanmei.com/api/calendar/getfestival?appver=1.02&platform=macos&abestmorale=true Error: 404 Not Found
    at /Applications/云天气.app/Contents/Resources/app.asar/src/utils/fetch.js:1:550
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
Out[30]:
0

返回了一个0,表示程序正确运行。

image.png

同时可以看到,这个程序弹出的警告也可以被输出。

也可以指定一些参数。

掌握了这个os.system,就具备了调用其他程序的能力。

8.4.4 环境参数¶

  • os.chdir(path) 修改当前程序操作的路径
  • os.getcwd() 返回当前程序的路径
  • os.getlogin() 获取当前系统登录用户的名称
  • os.cpu_count() 获得当前系统CPU数量
  • os.urandom(n) 产生n字节长的随机字符串,常用语加密解密运算
In [34]:
os.chdir("/Users/caiguu/PycharmProjects/pythonLearn")
print(os.getcwd())
print(os.getlogin())
print(os.cpu_count())
print(os.urandom(12))
/Users/caiguu/PycharmProjects/pythonLearn
caiguu
10
b'+Y\xca\x9b\x0fT\x0cXV\xb1\xc2b'

可以看出我当前路径,登录名,我的CPU是10核心的。

随机字符串由于有的字符不能被有效打印,所以不能打印的是16进制的形式,能打印的是字符的形式。

一个字符占用一个字节。

8.5 第三方库自动安装脚本¶

  • 需求:批量安装第三方库,不需要人工干预,全自动安装!
  • 自动执行批量pip指令。
  • 想要安装10个第三方库:
    • NumPy Matplotlib PIL Scikit-Learn Requests
    • jieba Beautiful-Soup Wheel SymPy Pandas
In [36]:
import os
libs = {"numpy","matplotlib","pillow","sklearn","requests","jieba"\
        ,"beautifulsoup4","wheel","sympy","pandas"}
try:
    for lib in libs:
        os.system("pip3 install "+lib)
    print("Finished")
except:
    print("Failed!")
Requirement already satisfied: wheel in /opt/homebrew/lib/python3.10/site-packages (0.37.0)
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.10/bin/python3.10 -m pip install --upgrade pip' command.
Requirement already satisfied: beautifulsoup4 in /opt/homebrew/lib/python3.10/site-packages (4.10.0)
Requirement already satisfied: soupsieve>1.2 in /opt/homebrew/lib/python3.10/site-packages (from beautifulsoup4) (2.3.1)
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.10/bin/python3.10 -m pip install --upgrade pip' command.
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x1055da5c0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /simple/pandas/
Collecting pandas
  Downloading pandas-1.4.1-cp310-cp310-macosx_11_0_arm64.whl (10.5 MB)
Requirement already satisfied: python-dateutil>=2.8.1 in /opt/homebrew/lib/python3.10/site-packages (from pandas) (2.8.2)
Requirement already satisfied: numpy>=1.21.0 in /opt/homebrew/lib/python3.10/site-packages (from pandas) (1.22.2)
Collecting pytz>=2020.1
  Downloading pytz-2021.3-py2.py3-none-any.whl (503 kB)
Requirement already satisfied: six>=1.5 in /opt/homebrew/lib/python3.10/site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)
Installing collected packages: pytz, pandas
Successfully installed pandas-1.4.1 pytz-2021.3
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.10/bin/python3.10 -m pip install --upgrade pip' command.
Requirement already satisfied: jieba in /opt/homebrew/lib/python3.10/site-packages (0.42.1)
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.10/bin/python3.10 -m pip install --upgrade pip' command.
Requirement already satisfied: requests in /opt/homebrew/lib/python3.10/site-packages (2.27.1)
Requirement already satisfied: charset-normalizer~=2.0.0 in /opt/homebrew/lib/python3.10/site-packages (from requests) (2.0.12)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/homebrew/lib/python3.10/site-packages (from requests) (1.26.8)
Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.10/site-packages (from requests) (2021.10.8)
Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.10/site-packages (from requests) (3.3)
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.10/bin/python3.10 -m pip install --upgrade pip' command.
Requirement already satisfied: matplotlib in /opt/homebrew/lib/python3.10/site-packages (3.5.1)
Requirement already satisfied: cycler>=0.10 in /opt/homebrew/lib/python3.10/site-packages (from matplotlib) (0.11.0)
Requirement already satisfied: packaging>=20.0 in /opt/homebrew/lib/python3.10/site-packages (from matplotlib) (21.3)
Requirement already satisfied: numpy>=1.17 in /opt/homebrew/lib/python3.10/site-packages (from matplotlib) (1.22.2)
Requirement already satisfied: pillow>=6.2.0 in /opt/homebrew/lib/python3.10/site-packages (from matplotlib) (9.0.1)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/homebrew/lib/python3.10/site-packages (from matplotlib) (1.3.2)
Requirement already satisfied: pyparsing>=2.2.1 in /opt/homebrew/lib/python3.10/site-packages (from matplotlib) (3.0.6)
Requirement already satisfied: python-dateutil>=2.7 in /opt/homebrew/lib/python3.10/site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: fonttools>=4.22.0 in /opt/homebrew/lib/python3.10/site-packages (from matplotlib) (4.29.1)
Requirement already satisfied: six>=1.5 in /opt/homebrew/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.10/bin/python3.10 -m pip install --upgrade pip' command.
Collecting sympy
  Downloading sympy-1.9-py3-none-any.whl (6.2 MB)
Collecting mpmath>=0.19
  Downloading mpmath-1.2.1-py3-none-any.whl (532 kB)
Installing collected packages: mpmath, sympy
  WARNING: The script isympy is installed in '/opt/homebrew/opt/python@3.10/Frameworks/Python.framework/Versions/3.10/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.10/bin/python3.10 -m pip install --upgrade pip' command.
Successfully installed mpmath-1.2.1 sympy-1.9
Requirement already satisfied: pillow in /opt/homebrew/lib/python3.10/site-packages (9.0.1)
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.10/bin/python3.10 -m pip install --upgrade pip' command.
Collecting sklearn
  Using cached sklearn-0.0.tar.gz (1.1 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Collecting scikit-learn
  Downloading scikit_learn-1.0.2-cp310-cp310-macosx_12_0_arm64.whl (6.9 MB)
Requirement already satisfied: scipy>=1.1.0 in /opt/homebrew/lib/python3.10/site-packages (from scikit-learn->sklearn) (1.8.0)
Requirement already satisfied: numpy>=1.14.6 in /opt/homebrew/lib/python3.10/site-packages (from scikit-learn->sklearn) (1.22.2)
Collecting threadpoolctl>=2.0.0
  Downloading threadpoolctl-3.1.0-py3-none-any.whl (14 kB)
Collecting joblib>=0.11
  Using cached joblib-1.1.0-py2.py3-none-any.whl (306 kB)
Building wheels for collected packages: sklearn
  Building wheel for sklearn (setup.py): started
  Building wheel for sklearn (setup.py): finished with status 'done'
  Created wheel for sklearn: filename=sklearn-0.0-py2.py3-none-any.whl size=1309 sha256=56b44a1a6fff8d864a17fd1ea785329c20765ff7e6e784430acb56237c6f9600
  Stored in directory: /Users/caiguu/Library/Caches/pip/wheels/9b/13/01/6f3a7fd641f90e1f6c8c7cded057f3394f451f340371c68f3d
Successfully built sklearn
Installing collected packages: threadpoolctl, joblib, scikit-learn, sklearn
Successfully installed joblib-1.1.0 scikit-learn-1.0.2 sklearn-0.0 threadpoolctl-3.1.0
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.10/bin/python3.10 -m pip install --upgrade pip' command.
Requirement already satisfied: numpy in /opt/homebrew/lib/python3.10/site-packages (1.22.2)
Finished
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.
You should consider upgrading via the '/opt/homebrew/opt/python@3.10/bin/python3.10 -m pip install --upgrade pip' command.

根据输出,我们看到成功安装了。

其实用pip3 install -r requirements.txt更为常见,直接安装全部依赖。