李明梓

  • 首页
  • 文档
  • 关于我
Star
  • bigdata
    • bigdata
    • 读[数据分析实战45讲]
  • go
    • go
    • GO SDK
    • K8S
    • 读[GO语言核心36讲]
    • 读[GO语言编程]
  • JAVA
    • JAVA
    • maven-wrapper基础使用
    • 位运算基础知识
  • python
    • python基础
  • DB
    • canal-mysql binlog日志解析
    • db
    • mysql master-slave
  • ai
    • gpt-agent
  • 读书
    • <<大风歌>>王立群
    • 概览
    • 高级信息系统项目管理师
  • 工具
    • jemeter基本使用
    • sonarqube基础使用
    • 工具
  • 数学
    • math
    • 数学基础概念
  • 英语
    • english
    • 英语自然拼读
    • 英语音节划分
  • 其他
    • Hexo基础使用
    • Hugo基本使用
    • Markdown基本使用
    • Yarn相关
    • 关于调音声卡
    • 其他
    • 开源监控软件对比_zabbix
    • 开源项目索引
    • 正则表达式30分钟入门教程
  • 源码探究

python基础

python基础

pytyon与数据分析

Python 已经成为发展最快的主流编程语言,从众多开发语言中脱颖而出,深受开发者喜爱。其次,在数据分析领域中,使用 Python 的开发者是最多的,远超其他语言之和。最后,Python 语言简洁,有大量的第三方库,功能强大,能解决数据分析的大部分问题。

Python 语言最大的优点是简洁,它虽然是 C 语言写的,但是摒弃了 C 语言的指针,这就让代码非常简洁明了。

Python 还有强大的开发者工具。在数据科学领域,Python 有许多非常著名的工具库:比如科学计算工具 NumPy 和 Pandas 库,深度学习工具 Keras 和 TensorFlow,以及机器学习工具 Scikit-learn,使用率都非常高。

如果想在数据分析、机器学习等数据科学领域有所作为,那么掌握Python 语言的使用是非常有必要的,熟练掌握相关工具会让你事半功倍。

python版本选择

Python 主要有两个版本: 2.7.x 和 3.x。两个版本之间存在一些差异,但并不大,它们语法不一样的地方不到 10%。

另一个事实就是:大部分 Python 库都同时支持 Python 2.7.x 和 3.x 版本。虽然官方称 Python2.7 只维护到 2020 年,但它的寿命远不止到 2020 年,一份调查显示:在 2017 年的商业项目中 2.7 版本依然是主流,占到了 63.7%,即使这两年 Python3.x 版本使用的增速较快,但实际上 Python3.x 在 2008 年就已经有了。

版本选择的标准就是看你的项目是否会依赖于 Python2.7 的包,如果有依赖的就只能使用 Python2.7,否则你可以用 Python 3.x 开始全新的项目。

python ide

  1. PyCharm 这是一个跨平台的 Python 开发工具,可以帮助用户在使用 Python 时提升效率,比如:调试、语法高亮、代码跳转、自动完成、智能提示等。
  2. Sublime Text SublimeText 是个著名的编辑器,Sublime Text3 基本上可以 1 秒即启动,反应速度很快。同时它对 Python 的支持也很到位,具有代码高亮、语法提示、自动完成等功能。
  3. Vim Vim 是一个简洁、高效的工具,速度很快,可以做任何事,从来不崩溃。不过 Vim 相比于 Sublime Text 上手有一定难度,配置起来有些麻烦。
  4. Eclipse+PyDev 习惯使用 Java 的人一定对 Eclipse 这个 IDE 不陌生,那么使用 Eclipse+PyDev 插件会是一个很好的选择,这样熟悉 Eclipse 的开发者可以轻易上手。

如果上面这些 IDE 你之前都没有怎么用过,那么推荐你使用 Sublime Text,上手简单,反应速度快。

python基础语法

python三方库

Numpy

Python 中一个非常重要的第三方库 NumPy。 它不仅是 Python 中使用最多的第三方库,而且还是 SciPy、Pandas 等数据科学的基础库。

为什么要用 NumPy 数组结构而不是 Python 本身的列表 list?这是因为列表 list 的元素在系统内存中是分散存储的,而 NumPy 数组存储在一个均匀连续的内存块中。另外在内存访问模式中,缓存会直接把字节块从 RAM 加载到 CPU 寄存器中。因为数据连续的存储在内存中,NumPy 直接利用现代 CPU 的矢量化指令计算,加载寄存器中的多个连续浮点数。另外 NumPy 中的矩阵计算可以采用多线程的方式,充分利用多核 CPU 计算资源,大大提升了计算效率。

NumPy 里有两个重要的对象:

1.ndarray(N-dimensional array object)解决了多维数组问题

2.ufunc(universal function object)则是解决对数组进行处理的函数

Pandas

pandas的使用频率是很高的,一方面是因为 Pandas 提供的基础数据结构 DataFrame 与 json 的契合度很高,转换起来就很方便。另一方面,如果我们日常的数据清理工作不是很复杂的话,你通常用几句 Pandas 代码就可以对数据进行规整。

Pandas 可以说是基于 NumPy 构建的含有更高级数据结构和分析能力的工具包。在 NumPy 中数据结构是围绕 ndarray 展开的,而Pandas 中的核心数据结构是 Series 和 DataFrame 这两个核心数据结构,他们分别代表着一维的序列和二维的表结构。

数据结构:Series 和 DataFrame

  • Series 是个定长的字典序列,有两个基本属性:index 和 values。在 Series 结构中,index 默认是 0,1,2,……递增的整数序列,当然我们也可以自己来指定索引,比如 index=[‘a’, ‘b’, ‘c’, ‘d’]。
  • DataFrame 类型数据结构类似数据库表。 它包括了行索引和列索引,我们可以将 DataFrame 看成是由相同索引的 Series 组成的字典类型。

requests

pip install requests

命令

pip list #这将列出所有已安装的包,并显示它们的版本号
pip show numpy #显示 numpy 包的版本号和其他相关信息
pip install numpy==  #查看 numpy 可用版本并安装

参考

https://github.com/OpenMathLib/OpenBLAS

问题记录

在 Mac M1 上安装 numpy 时出错

由于MacOS内置的BLAS与numpy不匹配,因此必须更换为另一个BLAS库。让我们用 openblas 替换 BLAS 库

limingzi@limz-mac ~ % pip install numpy
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting numpy
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b7/6f/24647f014eef9b67a24adfcbcd4f4928349b4a0f8393b3d7fe648d4d2de3/numpy-1.16.6.zip (5.1MB)
     |████████████████████████████████| 5.2MB 261kB/s 
Installing collected packages: numpy
  Running setup.py install for numpy ... error
    ERROR: Command errored out with exit status 1:
    clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
    clang: error: the clang compiler does not support 'faltivec', please use -maltivec and include altivec.h explicitly
    ----------------------------------------
ERROR: Command errored out with exit status 1:  
WARNING: You are using pip version 19.2.3, however version 20.3.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
解决:
# openblas
brew install openblas
OPENBLAS="$(brew --prefix openblas)" pip install numpy
CFLAGS='-Wno-implicit-function-declaration'  pip install pandas

参考
https://github.com/numpy/numpy/issues/17784#issuecomment-729950525
https://stackoverflow.com/questions/74113427/install-numpy-with-pyhon-3-7-on-macbook-m1

在 Mac M1 上安装 pandas时出错

pandas/_libs/src/parse_helper.h:141:26: error: call to undeclared function 'tolower_ascii'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        for (; *p; ++p) *p = tolower_ascii(*p);
                             ^
    1 warning and 1 error generated.
    error: command 'clang' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /Users/limingzi/.pyenv/versions/2.7.18/bin/python2.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/12/9k1vclks6l72j8y59_t_xl500000gn/T/pip-install-zc303S/pandas/setup.py'"'"'; __file__='"'"'/private/var/folders/12/9k1vclks6l72j8y59_t_xl500000gn/T/pip-install-zc303S/pandas/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/12/9k1vclks6l72j8y59_t_xl500000gn/T/pip-record-iCPYOQ/install-record.txt --single-version-externally-managed --compile --install-headers /Users/limingzi/.pyenv/versions/2.7.18/include/python2.7/pandas Check the logs for full command output.

解决:
CFLAGS='-Wno-implicit-function-declaration'  pip install pandas
参考:
https://github.com/termux/termux-packages/issues/12412

ImportError: Install xlrd >= 1.0.0 for Excel support

缺少 x lrd模块,手工安装pip install xlrd -i https://pypi.tuna.tsinghua.edu.cn/simple

xlrd.biffh.XLRDError: Excel xlsx file; not supported

limingzi@limz-mac ~ % pip list 
Package         Version
--------------- ------------
certifi         2021.10.8
chardet         4.0.0
et-xmlfile      1.0.1
idna            2.10
jdcal           1.4.1
numpy           1.16.6
pandas          0.24.2
pip             20.3.4
python-dateutil 2.8.2
pytz            2023.3.post1
requests        2.27.1
setuptools      41.2.0
six             1.16.0
urllib3         1.26.17
xlrd            2.0.1

xlrd版本太高
pip uninstall xlrd
pip install xlrd==1.2.0

See also

  • python基础
  • Hugo基本使用
  • Hugo基本使用

On this page

  • pytyon与数据分析
  • python版本选择
  • python三方库
  • 参考
  • 问题记录
Last updated: July 30, 2023
Improve this page
李明梓-BLOG
Hugo Logo
  本文仅为个人笔记,作为学习使用,如有雷同请联系作者 mingzi.li 处理,mail: qiaomingzi100@sina.com