简洁易懂,初学者挑战学习Python编程30天 (四)

目录

  • 第 21 天 - 类和对象
    • 21.1创建一个类
    • 21.2创建对象
    • 21.3类构造函数
    • 21.4对象方法
    • 21.5对象默认方法
    • 21.6修改类默认值的方法
    • 21.7继承
    • 21.8Overriding parent method
  • 第 22 天 - 网页抓取
    • 22.1什么是网页抓取
  • 第 23 天 - 虚拟环境
    • 23.1设置虚拟环境
  • 第 24 天 - 统计
    • 24.1统计数据
    • 24.2什么是数据?
    • 24.3统计模块
    • 24.4NumPy
    • 24.5导入 NumPy
    • 24.6使用创建 numpy 数组
    • 24.7创建 float numpy 数组
    • 24.8创建布尔 numpy 数组
    • 24.9使用numpy创建多维数组
    • 24.10将 numpy 数组转换为列表
    • 24.11从元组创建numpy数组
    • 24.12numpy 数组的形状
    • 24.13numpy数组的数据类型
    • 24.14numpy 数组的大小
    • 24.15使用numpy进行数学运算
    • 24.16添加
    • 24.17减法
    • 24.18乘法
    • 24.19分配
    • 24.20模数;找到余数
    • 24.21楼层划分
    • 24.22指数
    • 24.23检查数据类型
    • 24.24转换类型
    • 24.25多维数组
    • 24.26从 numpy 数组中获取项目
    • 24.27切片 Numpy 数组
    • 24.28如何反转行和整个数组?
    • 24.29反转行列位置
    • 24.30如何表示缺失值?
    • 24.31生成随机数
    • 24.32生成随机数
    • 24.33Numpy 和统计
    • 24.34numpy中的矩阵
    • 24.35numpy numpy.arange()
    • 24.36使用 linspace 创建数字序列
    • 24.37NumPy 统计函数与示例
    • 24.38如何创建重复序列?
    • 24.39如何生成随机数?
    • 24.40线性代数
    • 24.41NumPy 矩阵乘法与 np.matmul()
  • 第 25 天 - Pandas
    • 25.1安装pandas
    • 导入pandas
    • 25.2使用默认索引创建 Pandas 系列
    • 25.3使用自定义索引创建 Pandas 系列
    • 25.4从字典创建 Pandas 系列
    • 25.5创建一个常量 Pandas 系列
    • 25.6使用 Linspace 创建 Pandas 系列
    • 25.7数据帧
      • 从列表列表创建数据帧
    • 25.8使用字典创建 DataFrame
    • 25.9从字典列表创建数据帧
    • 25.10使用 Pandas 读取 CSV 文件
    • 25.11数据探索
    • 25.12修改数据帧
    • 25.13创建数据帧
    • 25.14添加新列
    • 25.15修改列值
    • 25.16格式化 DataFrame 列
    • 25.17检查列值的数据类型
    • 25.18布尔索引

简洁易懂,初学者挑战学习Python编程30天 (四)_第1张图片

你们的三连(点赞,收藏,评论)是我持续输出的动力,感谢。
在兴趣中学习,效益超乎想象,有趣的源码与学习经验,工具安装包,欢迎加我的微信:bobin1124,一起交流学习与分享。

第 21 天 - 类和对象

Python 是一种面向对象的编程语言。Python 中的一切都是一个对象,有它的属性和方法。程序中使用的数字、字符串、列表、字典、元组、集合等是相应内置类的对象。我们创建类来创建一个对象。一个类就像一个对象构造函数,或者是创建对象的“蓝图”。我们实例化一个类来创建一个对象。类定义了对象的属性和行为,而另一方面,对象代表了类。

从这个挑战一开始,我们就在不知不觉中处理类和对象。Python 程序中的每个元素都是一个类的对象。让我们检查一下python中的所有东西是否都是一个类:

asabeneh @ Asabeneh:~ $ python 
Python  3.96(默认,2021628 日 ,152621[11.0 0.0(铛- 1100.033.8)在 达尔文
式 的“帮助”,“版权”,“信用” 或 “许可” 的 更多 信息。
>> >  num  =  10 
>> >  type ( num )
 < class  'int' > 
>> >  string  =  'string' 
>> >'STR' > 
>> > 布尔 =>> > 类型(布尔)
 <'布尔' > 
>> >  LST  = []
 >> > 型(LST)
 <'列表' > 
>> >  TPL  =)
 >> > 类型( tpl )
 < class  'tuple' > 
>> > SET1  = 集()
>> > 类型(set1)
 < class  'set' > 
>> >  dct  = {
     }
 >> > 类型(dct)
 < class  'dict' >

21.1创建一个类

要创建一个类,我们需要关键字类,后跟名称和冒号。类名应该是CamelCase。

#语法
类类名:
  代码在这里

例子:

类 人:
  通过
打印(人)
< __main__.Person 对象在 0x10804e 510>

21.2创建对象

我们可以通过调用类来创建一个对象。

p  =()
打印( p )

21.3类构造函数

在上面的例子中,我们从 Person 类创建了一个对象。然而,没有构造函数的类在实际应用中并没有真正的用处。让我们使用构造函数使我们的类更有用。与Java或JavaScript中的构造函数一样,Python也有内置的init ()构造函数。的初始化构造函数有自参数这对类的当前实例的引用
实施例:

class  Person :
       def  __init__ ( self , name ):
         # self 允许将参数附加到类
          self。姓名 =姓名

p  =  Person ( 'Asabeneh' )
打印( p . name )
打印( p )
#输出
阿萨贝内
< __main__.Person 对象在 0x2abf46907e 80>

让我们向构造函数添加更多参数。

class  Person :
       def  __init__ ( self , firstname , lastname , age , country , city ):
           self。名字 = 名字
          自我。姓氏 = 姓氏
          自我。年龄 = 年龄
          自我。国家 = 国家
          自我。城市 = 城市


p  =  Person ( 'Asabeneh' , 'Yetayeh' , 250 , 'Finland' , 'Helsinki' )
 print ( p . firstname )
 print ( p . lastname )
 print ( p . age )
 print ( p . country )
 print ( p .城市)
#输出
阿萨贝内
耶塔耶
250
芬兰
赫尔辛基

21.4对象方法

对象可以有方法。方法是属于对象的函数。

例子:

class  Person :
       def  __init__ ( self , firstname , lastname , age , country , city ):
           self。名字 = 名字
          自我。姓氏 = 姓氏
          自我。年龄 = 年龄
          自我。国家 = 国家
          自我。city  =  city 
      def  person_info ( self ):
        返回 f' {
       自我。名字}  {
       自我。姓氏}{
        self。年龄}岁。他住在{
       自我。城市}{
       自我。国家} '

p  = 人('Asabeneh''Yetayeh'250'芬兰''赫尔辛基')
打印(p。person_info())
#输出
Asabeneh Yetayeh 已经 250 岁了。他住在芬兰赫尔辛基

21.5对象默认方法

有时,您可能希望为对象方法设置默认值。如果我们在构造函数中给参数赋予默认值,就可以避免在不带参数的情况下调用或实例化我们的类时出错。让我们看看它的外观:

例子:

class  Person :
       def  __init__ ( self , firstname = 'Asabeneh' , lastname = 'Yetayeh' , age = 250 , country = 'Finland' , city = 'Helsinki' ):
           self。名字 = 名字
          自我。姓氏 = 姓氏
          自我。年龄 = 年龄
          自我。国家 = 国家
          自我. 城市 = 城市

      def  person_info ( self ):
        返回 f' {
        self . 名字}  {
       自我。姓氏}{
        self。年龄}岁。他住在{
       自我。城市}{
       自我。国家} .'

P1  = 人()
打印(P1。person_info())
 P2  = 人('约翰''李四'30'Nomanland' , “诺曼城市)
打印(P2。person_info())
#输出
Asabeneh Yetayeh 已经 250 岁了。他住在芬兰赫尔辛基。
约翰·多伊今年 30 岁。他住在诺曼兰的诺曼城。

21.6修改类默认值的方法

在下面的例子中,person 类,所有的构造函数参数都有默认值。除此之外,我们还有技能参数,我们可以使用方法访问它。让我们创建 add_skill 方法来将技能添加到技能列表中。

class  Person :
       def  __init__ ( self , firstname = 'Asabeneh' , lastname = 'Yetayeh' , age = 250 , country = 'Finland' , city = 'Helsinki' ):
           self。名字 = 名字
          自我。姓氏 = 姓氏
          自我。年龄 = 年龄
          自我。国家 = 国家
          自我. 城市 = 城市
          自我。技能 = []

      def  person_info ( self ):
        返回 f' {
        self . 名字}  {
       自我。姓氏}{
        self。年龄}岁。他住在{
       自我。城市}{
       自我。国家} .' 
      def  add_skill(自我,技能):
          自我。技能。追加(技能)

p1  =  Person ()
打印( p1 . person_info ())
 p1 . add_skill ( 'HTML' )
 p1。add_skill ( 'CSS' )
 p1。add_skill('的JavaScript' )
 P2  = 人('约翰''李四'30'Nomanland' , “诺曼城市)
打印(P2。person_info())
印刷(P1. 技能)
打印(p2。技能)
#输出
Asabeneh Yetayeh 已经 250 岁了。他住在芬兰赫尔辛基。
约翰·多伊今年 30 岁。他住在诺曼兰的诺曼城。
[ ' HTML '' CSS '' JavaScript ' ]
[]

21.7继承

使用继承,我们可以重用父类代码。继承允许我们定义一个继承父类的所有方法和属性的类。父类或超类或基类是提供所有方法和属性的类。子类是从另一个类或父类继承的类。让我们通过继承person类来创建一个student类。

班级 学生(人):
通过

S1  = 学生('Eyob''Yetayeh'30'芬兰''赫尔辛基')
 S2  = 学生('了Lidiya''Teklemariam'28'芬兰''埃斯波')
印刷(S1。person_info( ))
 s1。add_skill ( 'JavaScript' )
 s1。add_skill ( '反应' )
 s1。'Python' )
打印( s1 .技能)

打印(S2。person_info())
 S2。add_skill ( '组织' )
 s2。add_skill ( '营销' )
 s2。add_skill ( '数字营销' )
打印( s2 . Skill )
输出
Eyob Yetayeh 30 岁。他住在芬兰赫尔辛基。
[ ' JavaScript '' React '' Python ' ]
Lidiya Teklemariam 28 岁。他住在芬兰的埃斯波。
[ “组织”、“营销”、“数字营销” ]

我们没有在子类中调用init ()构造函数。如果我们没有调用它,那么我们仍然可以从父级访问所有属性。但是如果我们确实调用了构造函数,我们就可以通过调用super来访问父属性。
我们可以向子类添加新方法,也可以通过在子类中创建相同的方法名称来覆盖父类方法。当我们添加init ()函数时,子类将不再继承父类的init ()函数。

21.8Overriding parent method

类 学生(人):
    高清 __init__(自我,名字= 'Asabeneh' ,姓氏= 'Yetayeh' ,年龄= 250,全国= '芬兰,城市= '赫尔辛基',性别= '男'):
        自我。性别 = 性别
        超()。__init__(名字,姓氏,年龄,country , city )
     def  person_info ( self ):
        性别 =  'He'  if  self。性别 == '男' 否则 '她'
        返回 f' {
        self . 名字}  {
       自我。姓氏}{
        self。年龄}岁。{
       性别}生活在{
       自我。城市} , {
       自己. 国家} .'

s1  =  Student ( 'Eyob' , 'Yetayeh' , 30 , 'Finland' , 'Helsinki' , 'male' )
 s2  =  Student ( 'Lidiya' , 'Teklemariam' , 28 , 'Finland' , 'Espoo' , 'female' ' )
打印( s1 . person_info ())
 s1 . add_skill ( 'JavaScript' )
 s1。s1。add_skill('Python的)
印刷(S1,技能)

打印(S2。person_info())
 S2。add_skill ( '组织' )
 s2。add_skill ( '营销' )
 s2。add_skill ( '数字营销' )
打印( s2 . Skill )
Eyob Yetayeh 30 岁。他住在芬兰赫尔辛基。
[ ' JavaScript '' React '' Python ' ]
Lidiya Teklemariam 28 岁。她住在芬兰的埃斯波。
[ “组织”、“营销”、“数字营销” ]

我们可以使用 super() 内置函数或父名 Person 来自动继承其父级的方法和属性。在上面的例子中,我们Overriding parent method的方法。child 方法有一个不同的特点,它可以识别性别是男性还是女性并指定适当的代词(他/她)

第 22 天 - 网页抓取

22.1什么是网页抓取

互联网充满了可用于不同目的的大量数据。为了收集这些数据,我们需要知道如何从网站上抓取数据。

网页抓取是从网站中提取和收集数据并将其存储在本地机器或数据库中的过程。

在本节中,我们将使用 beautifulsoup 和 requests 包来抓取数据。我们使用的包版本是beautifulsoup 4。

要开始抓取网站,您需要请求、beautifoulSoup4和网站。

pip 安装请求
pip 安装 beautifulsoup4

要从网站抓取数据,需要对 HTML 标签和 CSS 选择器有基本的了解。我们使用 HTML 标签、类或/和 ID 定位来自网站的内容。让我们导入 requests 和 BeautifulSoup 模块

进口 请求
从 BS4 进口 BeautifulSoup

让我们为要抓取的网站声明 url 变量。

 来自bs4 的导入请求
import BeautifulSoup url = 'https://archive.ics.uci.edu/ml/datasets.php'   
  

# 让我们使用 requests 的 get 方法从 url 中获取数据

响应 = 请求。get ( url )
 # 让我们检查状态
status  =  response。status_code 
print ( status ) # 200 表示获取成功
200

使用beautifulSoup解析页面内容

 来自bs4 的导入请求
import BeautifulSoup url = 'https://archive.ics.uci.edu/ml/datasets.php'   
  

响应 = 请求。获取(网址)
内容 = 响应。content  # 我们从网站上获取所有内容
soup  =  BeautifulSoup ( content , 'html.parser' ) # beautiful 
Soup将有机会解析print ( soup . title ) # UCI Machine Learning Repository: Data Sets</标题></span>
打印(汤。标题。get_text())#UCI机器学习库:数据集
打印(汤。体)#给网站上的整个页面
打印(响应。STATUS_CODE)

桌子 <span class="token operator">=</span> 汤。find_all <span class="token punctuation">(</span> <span class="token string">'table'</span> <span class="token punctuation">,</span> <span class="token punctuation">{
     </span> <span class="token string">'cellpadding'</span> <span class="token punctuation">:</span> <span class="token string">'3'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
 <span class="token comment"># 我们的目标是 cellpadding 属性值为 3 的表格</span>
<span class="token comment"># 我们可以选择使用 id、class 或 HTML 标签,更多信息请查看beautifulsoup doc </span>
table  <span class="token operator">=</span> 表<span class="token punctuation">[</span> <span class="token number">0</span> <span class="token punctuation">]</span> #,结果是一个列表,我们是从它取出数据
为 TD 在 表。找到(<span class="token string">'tr'</span>)。find_all <span class="token punctuation">(</span> <span class="token string">'td'</span> <span class="token punctuation">)</span><span class="token punctuation">:</span>
    打印<span class="token punctuation">(</span> td <span class="token punctuation">.</span> text<span class="token punctuation">)</span>
</code></pre> 
  <p>如果你运行这段代码,你可以看到提取已经完成了一半。</p> 
  <p>你很特别,每天都在进步。您距离通往伟大的道路只剩下八天了。<br> 恭喜! </p> 
  <h1>第 23 天 - 虚拟环境</h1> 
  <h2>23.1设置虚拟环境</h2> 
  <p>从项目开始,最好有一个虚拟环境。虚拟环境可以帮助我们创建一个孤立或分离的环境。这将帮助我们避免跨项目的依赖冲突。如果您在终端上编写 pip freeze ,您将在计算机上看到所有已安装的软件包。如果我们使用 virtualenv,我们将只访问特定于该项目的包。打开终端并安装 virtualenv</p> 
  <blockquote> 
   <p>asabeneh@Asabeneh: ~ $ pip install virtualenv</p> 
  </blockquote> 
  <p>在 30DaysOfPython 文件夹中创建一个 flask_project 文件夹。</p> 
  <p>安装 virtualenv 包后,转到您的项目文件夹并通过编写以下内容创建一个虚拟环境:</p> 
  <p>对于 Mac/Linux:</p> 
  <pre><code class="prism language-python">asabeneh@Asabeneh<span class="token punctuation">:</span> <span class="token operator">~</span> <span class="token operator">/</span>Desktop<span class="token operator">/</span>30DaysOfPython<span class="token operator">/</span>flask_project \$ virtualenv venv
</code></pre> 
  <p>对于 Windows:</p> 
  <pre><code class="prism language-python">C:\Ú SERS \Ú SER \ d ocuments \ <span class="token number">3</span> 0DaysOfPython \˚F lask_project <span class="token operator">></span>蟒<span class="token operator">-</span>m VENV VENV
</code></pre> 
  <p>我更喜欢将新项目称为 venv,但可以随意使用不同的名称。让我们检查 venv 是否是通过使用 ls(或 dir 用于 Windows 命令提示符)命令创建的。</p> 
  <pre><code class="prism language-python">asabeneh@Asabeneh:<span class="token operator">~</span> <span class="token operator">/</span>Desktop<span class="token operator">/</span>30DaysOfPython<span class="token operator">/</span>flask_project$ ls


静脉<span class="token operator">/</span>
</code></pre> 
  <p>让我们通过在我们的项目文件夹中编写以下命令来激活虚拟环境。</p> 
  <p>对于 Mac/Linux:</p> 
  <pre><code class="prism language-python">asabeneh@Asabeneh<span class="token punctuation">:</span> <span class="token operator">~</span> <span class="token operator">/</span>Desktop<span class="token operator">/</span>30DaysOfPython<span class="token operator">/</span>flask_project$ source venv<span class="token operator">/</span><span class="token builtin">bin</span><span class="token operator">/</span>activate
</code></pre> 
  <p>在 Windows 中激活虚拟环境可能非常依赖于 Windows Power shell 和 git bash。</p> 
  <p>对于 Windows 电源外壳:</p> 
  <pre><code class="prism language-python">C:\Ú SERS \Ú SER \ d ocuments \ <span class="token number">3</span> 0DaysOfPython \˚F lask_project <span class="token operator">></span> VENV \ S cripts \一个ctivate
</code></pre> 
  <p>对于 Windows Git bash:</p> 
  <pre><code class="prism language-python">C:\Ú SERS \Ú SER \ d ocuments \ <span class="token number">3</span> 0DaysOfPython \˚F lask_project <span class="token operator">></span> VENV \ S cripts \。启用
</code></pre> 
  <p>编写激活命令后,您的项目目录将以 venv 开头。请参阅下面的示例。</p> 
  <p>(venv) asabeneh@Asabeneh: ~ /Desktop/30DaysOfPython/flask_project$<br> 现在,让我们通过编写 pip freeze 来检查这个项目中的可用包。您将看不到任何包。</p> 
  <p>我们将要做一个Flask小项目,所以让我们将Flask包安装到这个项目中。</p> 
  <pre><code class="prism language-python"><span class="token punctuation">(</span>venv<span class="token punctuation">)</span> asabeneh@Asabeneh<span class="token punctuation">:</span> <span class="token operator">~</span> <span class="token operator">/</span>Desktop<span class="token operator">/</span>30DaysOfPython<span class="token operator">/</span>flask_project$ pip install Flask
</code></pre> 
  <p>现在,让我们编写 pip freeze 来查看项目中已安装包的列表:</p> 
  <pre><code class="prism language-python"><span class="token punctuation">(</span>venv<span class="token punctuation">)</span> asabeneh@Asabeneh<span class="token punctuation">:</span> <span class="token operator">~</span> <span class="token operator">/</span>Desktop<span class="token operator">/</span>30DaysOfPython<span class="token operator">/</span>flask_project$ pip freeze
点击<span class="token operator">==</span><span class="token number">7.0</span>
Flask<span class="token operator">==</span><span class="token number">1.1</span><span class="token number">.1</span>
它的危险<span class="token operator">==</span><span class="token number">1.1</span><span class="token number">.0</span>
Jinja2<span class="token operator">==</span><span class="token number">2.10</span><span class="token number">.3</span>
标记安全<span class="token operator">==</span><span class="token number">1.1</span><span class="token number">.1</span>
Werkzeug<span class="token operator">==</span><span class="token number">0.16</span><span class="token number">.0</span>
</code></pre> 
  <p>完成后,您应该使用deactivate 停用活动项目。</p> 
  <pre><code class="prism language-python"><span class="token punctuation">(</span>venv<span class="token punctuation">)</span> asabeneh@Asabeneh<span class="token punctuation">:</span> <span class="token operator">~</span> <span class="token operator">/</span>Desktop<span class="token operator">/</span>30DaysOfPython$ 停用
</code></pre> 
  <p>安装了使用Flask的必要模块。现在,您的项目目录已准备好用于Flask项目。<br> 恭喜! </p> 
  <h1>第 24 天 - 统计</h1> 
  <h2>24.1统计数据</h2> 
  <p>统计学是研究数据的收集、组织、显示、分析、解释和呈现的学科。统计学是数学的一个分支,建议作为数据科学和机器学习的先决条件。统计学是一个非常广泛的领域,但我们将在本节中只关注最相关的部分。完成此挑战后,您可以进入 Web 开发、数据分析、机器学习和数据科学路径。无论您走哪条路,在您职业生涯的某个阶段,您都会获得可以处理的数据。拥有一些统计知识将帮助您根据数据做出决策,数据如他们所说。</p> 
  <h2>24.2什么是数据?</h2> 
  <p>数据是为某种目的(通常是分析)收集和翻译的任何字符集。它可以是任何字符,包括文本和数字、图片、声音或视频。如果数据没有放在上下文中,它对人或计算机没有任何意义。为了让数据有意义,我们需要使用不同的工具处理数据。</p> 
  <p>数据分析、数据科学或机器学习的工作流程始于数据。可以从某个数据源提供数据,也可以创建数据。有结构化和非结构化数据。</p> 
  <p>可以以小格式或大格式找到数据。我们将获得的大多数数据类型已在文件处理部分中介绍。</p> 
  <h2>24.3统计模块</h2> 
  <p>Python统计模块提供了计算数值数据的数理统计的函数。该模块无意成为第三方库(如 NumPy、SciPy)或面向专业统计学家(如 Minitab、SAS 和 Matlab)的专有全功能统计软件包的竞争对手。它针对图形和科学计算器的级别。</p> 
  <h2>24.4NumPy</h2> 
  <p>在第一部分中,我们将 Python 本身定义为一种出色的通用编程语言,但在其他流行库(numpy、scipy、matplotlib、pandas 等)的帮助下,它成为了一个强大的科学计算环境。</p> 
  <p>NumPy 是 Python 科学计算的核心库。它提供了一个高性能的多维数组对象,以及用于处理数组的工具。</p> 
  <p>到目前为止,我们一直在使用 vscode,但从现在开始我会推荐使用 Jupyter Notebook。要访问 jupyter notebook,让我们安装anaconda。如果您使用的是 anaconda,则大多数常用软件包都已包含在内,如果您安装了 anaconda,则您没有安装软件包。</p> 
  <pre><code class="prism language-python">asabeneh@Asabeneh<span class="token punctuation">:</span> <span class="token operator">~</span> <span class="token operator">/</span>Desktop<span class="token operator">/</span>30DaysOfPython$ pip install numpy
</code></pre> 
  <h2>24.5导入 NumPy</h2> 
  <p>如果您支持 jupyter notebook,则可以使用Jupyter notebook</p> 
  <pre><code class="prism language-python">  <span class="token comment"># 如何导入 numpy </span>
    <span class="token keyword">import</span>  numpy  <span class="token keyword">as</span>  np 
    <span class="token comment"># 如何检查 numpy 包的版本</span>
    <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'numpy:'</span> <span class="token punctuation">,</span> np <span class="token punctuation">.</span> __version__ <span class="token punctuation">)</span>
     <span class="token comment"># 检查可用方法</span>
    <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token builtin">dir</span> <span class="token punctuation">(</span> np <span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> 
  <h2>24.6使用创建 numpy 数组</h2> 
  <p><strong>创建 int numpy 数组</strong></p> 
  <pre><code class="prism language-python">  <span class="token comment"># 创建 python 列表</span>
    python_list  <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span>

    <span class="token comment"># 检查数据类型</span>
    <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'Type:'</span> <span class="token punctuation">,</span> <span class="token builtin">type</span> <span class="token punctuation">(</span> python_list <span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># <class 'list'> </span>
    <span class="token comment"># </span>
    <span class="token keyword">print</span> <span class="token punctuation">(</span> python_list <span class="token punctuation">)</span> <span class="token comment"># [1, 2, 3, 4, 5]</span>

    二维列表 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">6</span> <span class="token punctuation">,</span> <span class="token number">7</span> <span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>

    打印(二维列表)   <span class="token comment"># [[0, 1, 2], [3, 4, 5], [6, 7, 8]]</span>

    <span class="token comment"># 从 python 列表创建 Numpy(Numerical Python) 数组</span>

    numpy_array_from_list  <span class="token operator">=</span>  np。array <span class="token punctuation">(</span> python_list <span class="token punctuation">)</span>
     <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token builtin">type</span> <span class="token punctuation">(</span> numpy_array_from_list <span class="token punctuation">)</span><span class="token punctuation">)</span>    <span class="token comment"># <class 'numpy.ndarray'> </span>
    <span class="token keyword">print</span> <span class="token punctuation">(</span> numpy_array_from_list <span class="token punctuation">)</span> <span class="token comment"># array([1, 2, 3, 4, 5])</span>
</code></pre> 
  <h2>24.7创建 float numpy 数组</h2> 
  <p>使用浮点数据类型参数从列表创建浮点 numpy 数组</p> 
  <pre><code class="prism language-python">  <span class="token comment"># Python 列表</span>
    python_list  <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span>

    numy_array_from_list2  <span class="token operator">=</span>  np。阵列(python_list,D型细胞<span class="token operator">=</span>浮动)
    打印(numy_array_from_list2)#阵列(<span class="token punctuation">[</span><span class="token number">1</span>,<span class="token number">2</span>,<span class="token number">3</span>,<span class="token number">4</span>,<span class="token number">5</span><span class="token punctuation">]</span>)
</code></pre> 
  <h2>24.8创建布尔 numpy 数组</h2> 
  <p>从列表创建一个布尔值 numpy 数组</p> 
  <p>numpy_bool_array = np。数组([ 0 , 1 , - 1 , 0 , 0 ], dtype = bool )<br> 打印( numpy_bool_array ) # 数组([假, 真, 真, 假, 假])</p> 
  <h2>24.9使用numpy创建多维数组</h2> 
  <p>一个 numpy 数组可能有一个或多个行和列</p> 
  <pre><code class="prism language-python">  two_Dimension_list  <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">6</span> <span class="token punctuation">,</span> <span class="token number">7</span> <span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>
     numpy_two_dimensional_list  <span class="token operator">=</span>  np。数组(二维列表)
    打印(类型(numpy_two_dimensional_list))
    打印(numpy_two_dimensional_list)
</code></pre> 
  <p><类’ numpy.ndarray ’ ><br> [[0 1 2]<br> [3 4 5]<br> [6 7 8]]</p> 
  <h2>24.10将 numpy 数组转换为列表</h2> 
  <pre><code class="prism language-python"><span class="token comment"># 我们总是可以使用 tolist() 将数组转换回 Python 列表。</span>
np_to_list  <span class="token operator">=</span>  numpy_array_from_list。tolist()
打印(类型(np_to_list))
打印(<span class="token string">'一个维阵列:'</span>,np_to_list)
打印(<span class="token string">'二维阵列:'</span>,numpy_two_dimensional_list。tolist())
</code></pre> 
  <pre><code class="prism language-python">   <span class="token operator"><</span>类<span class="token string">'列表'</span> <span class="token operator">></span>
    一维数组:<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
    二维数组:<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</code></pre> 
  <h2>24.11从元组创建numpy数组</h2> 
  <pre><code class="prism language-python"><span class="token comment"># Numpy array from tuple </span>
<span class="token comment"># 在 Python 中创建元组</span>
python_tuple  <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">)</span>
 <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token builtin">type</span> <span class="token punctuation">(</span> python_tuple <span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># <class 'tuple'> </span>
<span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'python_tuple: '</span> <span class="token punctuation">,</span> python_tuple <span class="token punctuation">)</span> <span class="token comment"># python_tuple: ( 1, 2, 3, 4, 5)</span>

numpy_array_from_tuple  <span class="token operator">=</span>  np。array <span class="token punctuation">(</span> python_tuple <span class="token punctuation">)</span>
 <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token builtin">type</span> <span class="token punctuation">(</span> numpy_array_from_tuple <span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># <class 'numpy.ndarray'> </span>
<span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'numpy_array_from_tuple: '</span> <span class="token punctuation">,</span> numpy_array_from_tuple <span class="token punctuation">)</span> <span class="token comment"># numpy_array_from_tuple: [1 2 3 4 5]</span>
</code></pre> 
  <h2>24.12numpy 数组的形状</h2> 
  <p>shape 方法以元组的形式提供数组的形状。第一个是行,第二个是列。如果数组只是一维,则返回数组的大小。</p> 
  <pre><code class="prism language-python">   数字 <span class="token operator">=</span>  np。阵列(<span class="token punctuation">[</span> <span class="token number">1</span>,<span class="token number">2</span>,<span class="token number">3</span>,<span class="token number">4</span>,<span class="token number">5</span> <span class="token punctuation">]</span>)
    打印(NUMS)
    打印(<span class="token string">'NUMS的形状:'</span>,NUMS。形状)
    打印(numpy_two_dimensional_list)
    打印(<span class="token string">'numpy_two_dimensional_list的形状:'</span>,numpy_two_dimensional_list。形状)
     three_by_four_array  <span class="token operator">=</span>  NP。数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token punctuation">[</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">6</span> <span class="token punctuation">,</span> <span class="token number">7</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token punctuation">[</span> <span class="token number">8</span>,<span class="token number">9</span>,<span class="token number">10</span>,<span class="token number">11</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>)
    打印(three_by_four_array。形状)
</code></pre> 
  <pre><code class="prism language-python">   <span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span> <span class="token number">4</span> <span class="token number">5</span><span class="token punctuation">]</span>
    数字的形状:<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token punctuation">)</span>
    <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span> <span class="token number">1</span> <span class="token number">2</span><span class="token punctuation">]</span>
     <span class="token punctuation">[</span><span class="token number">3</span> <span class="token number">4</span> <span class="token number">5</span><span class="token punctuation">]</span>
     <span class="token punctuation">[</span><span class="token number">6</span> <span class="token number">7</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
    numpy_two_dimensional_list 的形状:<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
    <span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
</code></pre> 
  <h2>24.13numpy数组的数据类型</h2> 
  <p>数据类型类型:str、int、float、complex、bool、list、None</p> 
  <pre><code class="prism language-python">int_lists  <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token operator">-</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token operator">-</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token operator">-</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span>
 int_array  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 数组(int_lists)
 float_array  <span class="token operator">=</span>  np。阵列(int_lists,D型细胞<span class="token operator">=</span>浮动)

打印(INT_ARRAY)
打印(INT_ARRAY。D型细胞)
打印(float_array)
打印(float_array。D型)
</code></pre> 
  <p>[-3 -2 -1 0 1 2 3]<br> int64<br> [-3。-2. -1. 0. 1. 2. 3.]<br> 浮动64</p> 
  <h2>24.14numpy 数组的大小</h2> 
  <p>在 numpy 中要知道 numpy 数组列表中的项目数,我们使用 size</p> 
  <pre><code class="prism language-python">numpy_array_from_list  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
 two_dimensional_list  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
                              <span class="token punctuation">[</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
                              <span class="token punctuation">[</span> <span class="token number">6</span> <span class="token punctuation">,</span> <span class="token number">7</span> <span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

<span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'The size:'</span> <span class="token punctuation">,</span> numpy_array_from_list <span class="token punctuation">.</span> size <span class="token punctuation">)</span> <span class="token comment"># 5 </span>
<span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'The size:'</span> <span class="token punctuation">,</span> two_dimensional_list <span class="token punctuation">.</span> size <span class="token punctuation">)</span>   <span class="token comment"># 3</span>
</code></pre> 
  <pre><code class="prism language-python">  尺寸:<span class="token number">5</span>
    尺寸:<span class="token number">9</span>
</code></pre> 
  <h2>24.15使用numpy进行数学运算</h2> 
  <p>NumPy 数组并不完全像 python 列表。要在 Python 列表中进行数学运算,我们必须遍历项目,但 numpy 可以允许在不循环的情况下进行任何数学运算。数学运算:</p> 
  <ol> 
   <li>加法 (+)</li> 
   <li>减法 (-)</li> 
   <li>乘法 (*)</li> 
   <li>分配 (/)</li> 
   <li>模块 (%)</li> 
   <li>楼层划分(//)</li> 
   <li>指数(**)</li> 
  </ol> 
  <h2>24.16添加</h2> 
  <pre><code class="prism language-python"><span class="token comment"># 数学运算</span>
<span class="token comment"># 加法</span>
numpy_array_from_list  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> <span class="token string">'原始数组: '</span> <span class="token punctuation">,</span> numpy_array_from_list <span class="token punctuation">)</span>
 ten_plus_original  <span class="token operator">=</span>  numpy_array_from_list   <span class="token operator">+</span>  <span class="token number">10</span>
打印<span class="token punctuation">(</span> ten_plus_original <span class="token punctuation">)</span>
</code></pre> 
  <p>原始数组:[1 2 3 4 5]<br> [11 12 13 14 15]</p> 
  <h2>24.17减法</h2> 
  <pre><code class="prism language-python"><span class="token comment"># 减法</span>
numpy_array_from_list  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> <span class="token string">'原始数组: '</span> <span class="token punctuation">,</span> numpy_array_from_list <span class="token punctuation">)</span>
 ten_minus_original  <span class="token operator">=</span>  numpy_array_from_list   <span class="token operator">-</span>  <span class="token number">10</span>
打印<span class="token punctuation">(</span> ten_minus_original <span class="token punctuation">)</span>
</code></pre> 
  <p>原始数组:[1 2 3 4 5]<br> [-9 -8 -7 -6 -5]</p> 
  <h2>24.18乘法</h2> 
  <pre><code class="prism language-python">numpy_array_from_list  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> <span class="token string">'原始数组: '</span> <span class="token punctuation">,</span> numpy_array_from_list <span class="token punctuation">)</span>
 ten_times_original  <span class="token operator">=</span>  numpy_array_from_list  <span class="token operator">*</span>  <span class="token number">10</span>
打印<span class="token punctuation">(</span> ten_times_original <span class="token punctuation">)</span>
</code></pre> 
  <p>原始数组:[1 2 3 4 5]<br> [10 20 30 40 50]</p> 
  <h2>24.19分配</h2> 
  <pre><code class="prism language-python"><span class="token comment"># 除法</span>
numpy_array_from_list  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> <span class="token string">'原始数组: '</span> <span class="token punctuation">,</span> numpy_array_from_list <span class="token punctuation">)</span>
 ten_times_original  <span class="token operator">=</span>  numpy_array_from_list  <span class="token operator">/</span>  <span class="token number">10</span>
打印<span class="token punctuation">(</span> ten_times_original <span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python"> 原始数组:<span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span> <span class="token number">4</span> <span class="token number">5</span><span class="token punctuation">]</span>
    <span class="token punctuation">[</span><span class="token number">0.1</span> <span class="token number">0.2</span> <span class="token number">0.3</span> <span class="token number">0.4</span> <span class="token number">0.5</span><span class="token punctuation">]</span>
</code></pre> 
  <h2>24.20模数;找到余数</h2> 
  <pre><code class="prism language-python">numpy_array_from_list  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> <span class="token string">'原始数组: '</span> <span class="token punctuation">,</span> numpy_array_from_list <span class="token punctuation">)</span>
 ten_times_original  <span class="token operator">=</span>  numpy_array_from_list  <span class="token operator">%</span>  <span class="token number">3</span>
打印<span class="token punctuation">(</span> ten_times_original <span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python">   原始数组:<span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span> <span class="token number">4</span> <span class="token number">5</span><span class="token punctuation">]</span>
    <span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">0</span> <span class="token number">1</span> <span class="token number">2</span><span class="token punctuation">]</span>
</code></pre> 
  <h2>24.21楼层划分</h2> 
  <pre><code class="prism language-python"><span class="token comment"># 楼层除法:没有余数的除法结果</span>
numpy_array_from_list  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> <span class="token string">'原始数组: '</span> <span class="token punctuation">,</span> numpy_array_from_list <span class="token punctuation">)</span>
 ten_times_original  <span class="token operator">=</span>  numpy_array_from_list  <span class="token operator">//</span>  <span class="token number">10</span>
打印<span class="token punctuation">(</span> ten_times_original <span class="token punctuation">)</span>
</code></pre> 
  <h2>24.22指数</h2> 
  <pre><code class="prism language-python"><span class="token comment"># Exponential 是找到某个数字的另一个幂:</span>
numpy_array_from_list  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> <span class="token string">'原始数组: '</span> <span class="token punctuation">,</span> numpy_array_from_list <span class="token punctuation">)</span>
 ten_times_original  <span class="token operator">=</span>  numpy_array_from_list   <span class="token operator">**</span>  <span class="token number">2</span>
打印<span class="token punctuation">(</span> ten_times_original <span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python">   原始数组:<span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span> <span class="token number">4</span> <span class="token number">5</span><span class="token punctuation">]</span>
    <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token number">4</span> <span class="token number">9</span> <span class="token number">16</span> <span class="token number">25</span><span class="token punctuation">]</span>
</code></pre> 
  <h2>24.23检查数据类型</h2> 
  <pre><code class="prism language-python"><span class="token comment">#Int, 浮点数</span>
numpy_int_arr  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
 numpy_float_arr  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1.1</span> <span class="token punctuation">,</span> <span class="token number">2.0</span> <span class="token punctuation">,</span> <span class="token number">3.2</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
 numpy_bool_arr  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token operator">-</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token operator">-</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> dtype <span class="token operator">=</span> <span class="token string">'bool'</span> <span class="token punctuation">)</span>

打印(numpy_int_arr。D型细胞)
打印(numpy_float_arr。D型细胞)
打印(numpy_bool_arr。D型)
</code></pre> 
  <pre><code class="prism language-python">int64
浮动<span class="token number">64</span>
布尔值
</code></pre> 
  <h2>24.24转换类型</h2> 
  <p>我们可以转换numpy数组的数据类型</p> 
  <p>1.整数到浮动</p> 
  <pre><code class="prism language-python">numpy_int_arr  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> dtype  <span class="token operator">=</span>  <span class="token string">'float'</span> <span class="token punctuation">)</span>
 numpy_int_arr
</code></pre> 
  <pre><code class="prism language-python">array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <p>2.浮动到整数</p> 
  <pre><code class="prism language-python">numpy_int_arr  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> dtype  <span class="token operator">=</span>  <span class="token string">'int'</span> <span class="token punctuation">)</span>
 numpy_int_arr
</code></pre> 
  <pre><code class="prism language-python">   数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <p>3.整数或布尔值</p> 
  <pre><code class="prism language-python">NP。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token operator">-</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token operator">-</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> dtype <span class="token operator">=</span> <span class="token string">'bool'</span> <span class="token punctuation">)</span>
</code></pre> 
  <p>数组([真,真,假,真,真,真])</p> 
  <p>4.整数到 str</p> 
  <pre><code class="prism language-python">numpy_float_list。astype <span class="token punctuation">(</span> <span class="token string">'int'</span> <span class="token punctuation">)</span>。astype(<span class="token string">'str'</span>)
</code></pre> 
  <p>数组([ ’ 1 ’ , ’ 2 ’ , ’ 3 ’ ], dtype= ’ <U21 ’ )</p> 
  <h2>24.25多维数组</h2> 
  <pre><code class="prism language-python"><span class="token comment"># 2 维数组</span>
two_dimension_array  <span class="token operator">=</span>  np <span class="token punctuation">.</span> array <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">(</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">6</span> <span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span> <span class="token number">7</span> <span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">,</span> <span class="token number">9</span> <span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
 <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token builtin">type</span> <span class="token punctuation">(</span> two_dimension_array <span class="token punctuation">)</span><span class="token punctuation">)</span>
 <span class="token keyword">print</span> <span class="token punctuation">(</span> two_dimension_array <span class="token punctuation">)</span>
 <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'Shape: '</span> <span class="token punctuation">,</span> two_dimension_array <span class="token punctuation">.</span> shape <span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> <span class="token string">'大小:'</span>,two_dimension_array。尺寸)
的打印(<span class="token string">'数据类型:'</span>,two_dimension_array。D型)
</code></pre> 
  <p><类’ numpy.ndarray ’ ><br> [[1 2 3]<br> [4 5 6]<br> [7 8 9]]<br> 形状:(3, 3)<br> 尺寸:9<br> 数据类型:int64</p> 
  <h2>24.26从 numpy 数组中获取项目</h2> 
  <pre><code class="prism language-python"><span class="token comment"># 2 维数组</span>
two_dimension_array  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 阵列(<span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span>,<span class="token number">2</span>,<span class="token number">3</span> <span class="token punctuation">]</span>,<span class="token punctuation">[</span> <span class="token number">4</span>,<span class="token number">5</span>,<span class="token number">6</span> <span class="token punctuation">]</span>,<span class="token punctuation">[</span> <span class="token number">7</span>,<span class="token number">8</span>,<span class="token number">9</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>)
 FIRST_ROW  <span class="token operator">=</span>  two_dimension_array <span class="token punctuation">[</span> <span class="token number">0</span> <span class="token punctuation">]</span>
 second_row  <span class="token operator">=</span>  two_dimension_array <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">]</span>
 third_row  <span class="token operator">=</span>  two_dimension_array <span class="token punctuation">[</span> <span class="token number">2</span> <span class="token punctuation">]</span>
打印(“第一行:' <span class="token punctuation">,</span> first_row <span class="token punctuation">)</span>
打印(<span class="token string">'第二行:'</span>,second_row)
打印(<span class="token string">'第三行:'</span>,third_row)
</code></pre> 
  <p>第一行:[1 2 3]<br> 第二行:[4 5 6]<br> 第三行:[7 8 9]</p> 
  <pre><code class="prism language-python">first_column <span class="token operator">=</span>  two_dimension_array <span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span> <span class="token punctuation">]</span>
 second_column  <span class="token operator">=</span>  two_dimension_array <span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">]</span>
 third_column  <span class="token operator">=</span>  two_dimension_array <span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">]</span>
 <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'First column:'</span> <span class="token punctuation">,</span> first_column <span class="token punctuation">)</span>
 <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'Second column:'</span> <span class="token punctuation">,</span> second_column <span class="token punctuation">)</span>
 <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'第三列: '</span> <span class="token punctuation">,</span> third_column <span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> two_dimension_array <span class="token punctuation">)</span>
</code></pre> 
  <p>第一列:[1 4 7]<br> 第二列:[2 5 8]<br> 第三列:[3 6 9]<br> [[1 2 3]<br> [4 5 6]<br> [7 8 9]]</p> 
  <h2>24.27切片 Numpy 数组</h2> 
  <p>在 numpy 中切片类似于在 python list 中切片</p> 
  <pre><code class="prism language-python">two_dimension_array  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">6</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">7</span> <span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">,</span> <span class="token number">9</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
 first_two_rows_and_columns  <span class="token operator">=</span>  two_dimension_array <span class="token punctuation">[</span> <span class="token number">0</span> <span class="token punctuation">:</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">0</span> <span class="token punctuation">:</span> <span class="token number">2</span> <span class="token punctuation">]</span>
打印<span class="token punctuation">(</span> first_two_rows_and_columns <span class="token punctuation">)</span>
</code></pre> 
  <p>[[1 2]<br> [4 5]]</p> 
  <h2>24.28如何反转行和整个数组?</h2> 
  <pre><code class="prism language-python">二维数组<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
</code></pre> 
  <p>数组([[1, 2, 3],<br> [4, 5, 6],<br> [7, 8, 9]])</p> 
  <h2>24.29反转行列位置</h2> 
  <pre><code class="prism language-python">two_dimension_array  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">6</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">7</span> <span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">,</span> <span class="token number">9</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
 two_dimension_array <span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span> <span class="token operator">-</span> <span class="token number">1</span> <span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token punctuation">:</span> <span class="token operator">-</span> <span class="token number">1</span> <span class="token punctuation">]</span>
</code></pre> 
  <p>数组([[9, 8, 7],<br> [6, 5, 4],<br> [3, 2, 1]])</p> 
  <h2>24.30如何表示缺失值?</h2> 
  <pre><code class="prism language-python">打印<span class="token punctuation">(</span> two_dimension_array <span class="token punctuation">)</span>
 two_dimension_array <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  <span class="token number">55</span> 
two_dimension_array <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">44</span>
打印<span class="token punctuation">(</span> two_dimension_array <span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python"><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span><span class="token number">4</span> <span class="token number">5</span> <span class="token number">6</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span><span class="token number">7</span> <span class="token number">8</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span> <span class="token number">4</span> <span class="token number">55</span> <span class="token number">44</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span> <span class="token number">7</span> <span class="token number">8</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</code></pre> 
  <pre><code class="prism language-python"> <span class="token comment"># Numpy </span>
    Zeroes <span class="token comment"># numpy.zeros(shape, dtype=float, order='C') </span>
    numpy_zeroes  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 零<span class="token punctuation">(</span><span class="token punctuation">(</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">)</span><span class="token punctuation">,</span> dtype <span class="token operator">=</span> <span class="token builtin">int</span> <span class="token punctuation">,</span> order <span class="token operator">=</span> <span class="token string">'C'</span> <span class="token punctuation">)</span>
     numpy_zeroes
</code></pre> 
  <pre><code class="prism language-python">数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python"><span class="token comment"># Numpy</span>
归零 numpy_ones  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 个<span class="token punctuation">(</span><span class="token punctuation">(</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">)</span><span class="token punctuation">,</span> dtype <span class="token operator">=</span> <span class="token builtin">int</span> <span class="token punctuation">,</span> order <span class="token operator">=</span> <span class="token string">'C'</span> <span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> numpy_ones <span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python"><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">1</span> <span class="token number">1</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">1</span> <span class="token number">1</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">1</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</code></pre> 
  <pre><code class="prism language-python">两个 <span class="token operator">=</span>  numpy_ones  <span class="token operator">*</span>  <span class="token number">2</span>
</code></pre> 
  <pre><code class="prism language-python"><span class="token comment"># 重塑</span>
<span class="token comment"># numpy.reshape(), numpy.flatten() </span>
first_shape   <span class="token operator">=</span>  np <span class="token punctuation">.</span> 阵列(<span class="token punctuation">[</span>(<span class="token number">1</span>,<span class="token number">2</span>,<span class="token number">3</span>),(<span class="token number">4</span>,<span class="token number">5</span>,<span class="token number">6</span>)<span class="token punctuation">]</span>)
打印(first_shape)
重构 <span class="token operator">=</span>  first_shape。重塑(<span class="token number">3</span>,<span class="token number">2</span>)
打印(重塑)
    <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span><span class="token punctuation">]</span>
     <span class="token punctuation">[</span><span class="token number">4</span> <span class="token number">5</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
    <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span><span class="token punctuation">]</span>
     <span class="token punctuation">[</span><span class="token number">3</span> <span class="token number">4</span><span class="token punctuation">]</span>
     <span class="token punctuation">[</span><span class="token number">5</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</code></pre> 
  <pre><code class="prism language-python">扁平 <span class="token operator">=</span> 重塑。flatten <span class="token punctuation">(</span><span class="token punctuation">)</span>
扁平化
</code></pre> 
  <pre><code class="prism language-python">数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment">## 水平堆栈</span>
np_list_one  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
 np_list_two  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">6</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>

打印(np_list_one  <span class="token operator">+</span>  np_list_two)

打印(<span class="token string">'水平附加:'</span>,NP。hstack((np_list_one,np_list_two)))
<span class="token punctuation">[</span><span class="token number">5</span> <span class="token number">7</span> <span class="token number">9</span><span class="token punctuation">]</span>
水平追加:<span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span> <span class="token number">4</span> <span class="token number">5</span> <span class="token number">6</span><span class="token punctuation">]</span>
</code></pre> 
  <p>##垂直叠<br> 打印(‘垂直附加:’,NP。vstack((np_list_one,np_list_two)))<br> 垂直附加:[[1 2 3]<br> [4 5 6]]</p> 
  <h2>24.31生成随机数</h2> 
  <pre><code class="prism language-python">  <span class="token comment"># 生成一个随机浮点数</span>
    random_float  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 随机的。随机()
     random_float
    <span class="token number">0.018929887384753874</span>
    <span class="token comment"># 生成一个随机浮点数</span>
    random_floats  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 随机的。随机<span class="token punctuation">(</span> <span class="token number">5</span> <span class="token punctuation">)</span>
     random_floats
    数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0.26392192</span><span class="token punctuation">,</span> <span class="token number">0.35842215</span><span class="token punctuation">,</span> <span class="token number">0.87908478</span><span class="token punctuation">,</span> <span class="token number">0.41902195</span><span class="token punctuation">,</span> <span class="token number">0.78926418</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token comment"># 生成 0 到 10 之间的随机整数</span>

    random_int  <span class="token operator">=</span>  np。随机的。randint <span class="token punctuation">(</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">11</span> <span class="token punctuation">)</span>
     random_int
    <span class="token number">4</span>
    <span class="token comment"># 生成一个 2 到 11 之间的随机整数,并创建一个</span>
    单行 数组random_int <span class="token operator">=</span>  np <span class="token punctuation">.</span> 随机的。randint <span class="token punctuation">(</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">10</span> <span class="token punctuation">,</span> size <span class="token operator">=</span> <span class="token number">4</span> <span class="token punctuation">)</span>
     random_int
    数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token comment"># 生成 0 到 10 之间的随机整数</span>
    random_int  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 随机的。randint <span class="token punctuation">(</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">10</span> <span class="token punctuation">,</span> size <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">)</span><span class="token punctuation">)</span>
     random_int
    数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
           <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
           <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <h2>24.32生成随机数</h2> 
  <pre><code class="prism language-python"> <span class="token comment"># np.random.normal(mu, sigma, size) </span>
    normal_array  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 随机的。正常<span class="token punctuation">(</span> <span class="token number">79</span> <span class="token punctuation">,</span> <span class="token number">15</span> <span class="token punctuation">,</span> <span class="token number">80</span> <span class="token punctuation">)</span>
     normal_array
</code></pre> 
  <pre><code class="prism language-python"> 数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">89.49990595</span><span class="token punctuation">,</span> <span class="token number">82.06056961</span><span class="token punctuation">,</span> <span class="token number">107.21445842</span><span class="token punctuation">,</span> <span class="token number">38.69307086</span><span class="token punctuation">,</span>
            <span class="token number">47.85259157</span>、<span class="token number">93.07381061</span>、<span class="token number">76.40724259</span>、<span class="token number">78.55675184</span>、
            <span class="token number">72.17358173</span>、<span class="token number">47.9888899</span>、<span class="token number">65.10370622</span>、<span class="token number">76.29696568</span>、
            <span class="token number">95.58234254</span>、<span class="token number">68.14897213</span>、<span class="token number">38.75862686</span>、<span class="token number">122.5587927</span>、
            <span class="token number">67.0762565</span>、<span class="token number">95.73990864</span>、<span class="token number">81.97454563</span>、<span class="token number">92.54264805</span>、
            <span class="token number">59.37035153</span>、<span class="token number">77.76828101</span>、<span class="token number">52.30752166</span>、<span class="token number">64.43109931</span>、
            <span class="token number">62.63695351</span>、<span class="token number">90.04616138</span>、<span class="token number">75.70009094</span>、<span class="token number">49.87586877</span>、
            <span class="token number">80.22002414</span>、<span class="token number">68.56708848</span>、<span class="token number">76.27791052</span>、<span class="token number">67.24343975</span>、
            <span class="token number">81.86363935</span>、<span class="token number">78.22703433</span>、<span class="token number">102.85737041</span>、<span class="token number">65.15700341</span>、
            <span class="token number">84.87033426</span>、<span class="token number">76.7569997</span>、<span class="token number">64.61321853</span>、<span class="token number">67.37244562</span>、
            <span class="token number">74.4068773</span>、<span class="token number">58.65119655</span>、<span class="token number">71.66488727</span>、<span class="token number">53.42458179</span>、
            <span class="token number">70.26872028</span>、<span class="token number">60.96588544</span>、<span class="token number">83.56129414</span>、<span class="token number">72.14255326</span>、
            <span class="token number">81.00787609</span>、<span class="token number">71.81264853</span>、<span class="token number">72.64168853</span>、<span class="token number">86.56608717</span>、
            <span class="token number">94.94667321</span>、<span class="token number">82.32676973</span>、<span class="token number">70.5165446</span>、<span class="token number">85.43061003</span>、
            <span class="token number">72.45526212</span>、<span class="token number">87.34681775</span>、<span class="token number">87.69911217</span>、<span class="token number">103.02831489</span>、
            <span class="token number">75.28598596</span>、<span class="token number">67.17806893</span>、<span class="token number">92.41274447</span>、<span class="token number">101.06662611</span>、
            <span class="token number">87.70013935</span>、<span class="token number">70.73980645</span>、<span class="token number">46.40368207</span>、<span class="token number">50.17947092</span>、
            <span class="token number">61.75618542</span>、<span class="token number">90.26191397</span>、<span class="token number">78.63968639</span>、<span class="token number">70.84550744</span>、
            <span class="token number">88.91826581</span>、<span class="token number">103.91474733</span>、<span class="token number">66.3064638</span>、<span class="token number">79.49726264</span>、
            <span class="token number">70.81087439</span><span class="token punctuation">,</span> <span class="token number">83.90130623</span><span class="token punctuation">,</span> <span class="token number">87.58555972</span><span class="token punctuation">,</span> <span class="token number">59.95462521</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <h2>24.33Numpy 和统计</h2> 
  <pre><code class="prism language-python">导入 matplotlib。pyplot  <span class="token keyword">as</span>  plt
将 seaborn 作为 sns 
sns导入。设置<span class="token punctuation">(</span><span class="token punctuation">)</span>
 plt。hist <span class="token punctuation">(</span> normal_array <span class="token punctuation">,</span> color <span class="token operator">=</span> <span class="token string">"grey"</span> <span class="token punctuation">,</span> bins <span class="token operator">=</span> <span class="token number">50</span> <span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python"> <span class="token punctuation">(</span>数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span>
            <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span> <span class="token punctuation">,</span>
            <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
     数组(<span class="token punctuation">[</span> <span class="token number">38.69307086</span><span class="token punctuation">,</span> <span class="token number">40.37038529</span><span class="token punctuation">,</span> <span class="token number">42.04769973</span><span class="token punctuation">,</span> <span class="token number">43.72501417</span><span class="token punctuation">,</span>
             <span class="token number">45.4023286</span>、<span class="token number">47.07964304</span>、<span class="token number">48.75695748</span>、<span class="token number">50.43427191</span>、
             <span class="token number">52.11158635</span>、<span class="token number">53.78890079</span>、<span class="token number">55.46621523</span>、<span class="token number">57.14352966</span>、
             <span class="token number">58.8208441</span>、<span class="token number">60.49815854</span>、<span class="token number">62.17547297</span>、<span class="token number">63.85278741</span>、
             <span class="token number">65.53010185</span>、<span class="token number">67.20741628</span>、<span class="token number">68.88473072</span>、<span class="token number">70.56204516</span>、
             <span class="token number">72.23935959</span>、<span class="token number">73.91667403</span>、<span class="token number">75.59398847</span>、<span class="token number">77.27130291</span>、
             <span class="token number">78.94861734</span>、<span class="token number">80.62593178</span>、<span class="token number">82.30324622</span>、<span class="token number">83.98056065</span>、
             <span class="token number">85.65787509</span>、<span class="token number">87.33518953</span>、<span class="token number">89.01250396</span>、<span class="token number">90.6898184</span>、
             <span class="token number">92.36713284</span>、<span class="token number">94.04444727</span>、<span class="token number">95.72176171</span>、<span class="token number">97.39907615</span>、
             <span class="token number">99.07639058</span>、<span class="token number">100.75370502</span>、<span class="token number">102.43101946</span>、<span class="token number">104.1083339</span>、
            <span class="token number">105.78564833</span>、<span class="token number">107.46296277</span>、<span class="token number">109.14027721</span>、<span class="token number">110.81759164</span>、
            <span class="token number">112.49490608</span>、<span class="token number">114.17222052</span>、<span class="token number">115.84953495</span>、<span class="token number">117.52684939</span>、
            <span class="token number">119.20416383</span><span class="token punctuation">,</span> <span class="token number">120.88147826</span><span class="token punctuation">,</span> <span class="token number">122.5587927</span> <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
     <span class="token operator"><</span> <span class="token number">50</span> 个 Patch 对象的列表<span class="token operator">></span> <span class="token punctuation">)</span>
</code></pre> 
  <h2>24.34numpy中的矩阵</h2> 
  <pre><code class="prism language-python">Four_by_four_matrix  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 矩阵(NP。者((<span class="token number">4</span>,<span class="token number">4</span>),D型细胞<span class="token operator">=</span>浮动))
Four_by_four_matrix
矩阵<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
NP。asarray(four_by_four_matrix)<span class="token punctuation">[</span> <span class="token number">2</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  <span class="token number">2</span>个
four_by_four_matrix
矩阵<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <h2>24.35numpy numpy.arange()</h2> 
  <p>有时,您希望创建在定义的间隔内均匀分布的值。例如,您想创建从 1 到 10 的值;你可以使用 numpy.arange() 函数</p> 
  <pre><code class="prism language-python"><span class="token comment"># 使用 range(starting, stop, step) 创建列表</span>
lst  <span class="token operator">=</span>  <span class="token builtin">range</span> <span class="token punctuation">(</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">11</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span>
 lst
范围<span class="token punctuation">(</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">11</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span>
<span class="token keyword">for</span>  l  <span class="token keyword">in</span>  lst <span class="token punctuation">:</span>
    打印<span class="token punctuation">(</span> l <span class="token punctuation">)</span>
    <span class="token number">2</span>
    <span class="token number">4</span>
    <span class="token number">6</span>
    <span class="token number">8</span>
    <span class="token number">10</span>
</code></pre> 
  <pre><code class="prism language-python"><span class="token comment"># 类似于范围 arange numpy.arange(start, stop, step) </span>
whole_numbers  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 范围<span class="token punctuation">(</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">20</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">)</span>
整数
数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span>
           <span class="token number">17</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
natural_numbers  <span class="token operator">=</span>  np。arange <span class="token punctuation">(</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">20</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">)</span>
 natural_numbers
奇数 <span class="token operator">=</span>  np。arange <span class="token punctuation">(</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">20</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span>
奇数
    数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
even_numbers  <span class="token operator">=</span>  np。arange <span class="token punctuation">(</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">20</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span>
 even_numbers
    数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <h2>24.36使用 linspace 创建数字序列</h2> 
  <pre><code class="prism language-python"><span class="token comment"># numpy.linspace() </span>
<span class="token comment"># numpy.logspace() in Python with Example </span>
<span class="token comment"># 例如,它可以用来创建从 1 到 5 的 10 个均匀间隔的值。</span>
NP。linspace <span class="token punctuation">(</span> <span class="token number">1.0</span> <span class="token punctuation">,</span> <span class="token number">5.0</span> <span class="token punctuation">,</span> num <span class="token operator">=</span> <span class="token number">10</span> <span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python">    数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1.44444444</span><span class="token punctuation">,</span> <span class="token number">1.88888889</span><span class="token punctuation">,</span> <span class="token number">2.33333333</span><span class="token punctuation">,</span> <span class="token number">2.77777778</span><span class="token punctuation">,</span>
           <span class="token number">3.22222222</span><span class="token punctuation">,</span> <span class="token number">3.66666667</span><span class="token punctuation">,</span> <span class="token number">4.11111111</span><span class="token punctuation">,</span> <span class="token number">4.55555556</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment"># 不包括区间</span>
np 中的最后一个值。linspace <span class="token punctuation">(</span> <span class="token number">1.0</span> <span class="token punctuation">,</span> <span class="token number">5.0</span> <span class="token punctuation">,</span> num <span class="token operator">=</span> <span class="token number">5</span> <span class="token punctuation">,</span>端点<span class="token operator">=</span> <span class="token boolean">False</span> <span class="token punctuation">)</span>
array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">1.8</span><span class="token punctuation">,</span> <span class="token number">2.6</span><span class="token punctuation">,</span> <span class="token number">3.4</span><span class="token punctuation">,</span> <span class="token number">4.2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python"><span class="token comment"># LogSpace </span>
<span class="token comment"># LogSpace 返回对数刻度上的偶数间隔数。Logspace 具有与 np.linspace 相同的参数。</span>

# 句法:

<span class="token comment"># numpy.logspace(开始,停止,数量,端点)</span>

NP。日志空间<span class="token punctuation">(</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">4.0</span> <span class="token punctuation">,</span> num <span class="token operator">=</span> <span class="token number">4</span> <span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python">数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">100</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">464.15888336</span><span class="token punctuation">,</span> <span class="token number">2154.43469003</span><span class="token punctuation">,</span> <span class="token number">10000</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment"># 检查数组的大小</span>
x  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 阵列(<span class="token punctuation">[</span> <span class="token number">1</span>,<span class="token number">2</span>,<span class="token number">3</span> <span class="token punctuation">]</span>,D型细胞<span class="token operator">=</span> NP。complex128)
X
    数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token operator">+</span><span class="token number">0.j</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token operator">+</span><span class="token number">0.j</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token operator">+</span><span class="token number">0.j</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
×。项目大小
<span class="token number">16</span>
<span class="token comment"># 在 Python 中索引和切片 NumPy 数组</span>
np_list  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">(</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">6</span> <span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
 np_list
    数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
           <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
打印(<span class="token string">'第一行:'</span>,np_list <span class="token punctuation">[</span> <span class="token number">0</span> <span class="token punctuation">]</span>)
打印(<span class="token string">'第二行:'</span>,np_list <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">]</span>)
    第一行:<span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span><span class="token punctuation">]</span>
    第二行:<span class="token punctuation">[</span><span class="token number">4</span> <span class="token number">5</span> <span class="token number">6</span><span class="token punctuation">]</span>
打印(<span class="token string">'第一列:'</span>,np_list<span class="token punctuation">[</span> :,<span class="token number">0</span> <span class="token punctuation">]</span>)
打印(<span class="token string">'第二列:'</span>,np_list<span class="token punctuation">[</span> :,<span class="token number">1</span> <span class="token punctuation">]</span>)
打印(<span class="token string">'第三列:'</span>,np_list<span class="token punctuation">[</span> :,<span class="token number">2</span> <span class="token punctuation">]</span>)
    第一列:<span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">4</span><span class="token punctuation">]</span>
    第二列:<span class="token punctuation">[</span><span class="token number">2</span> <span class="token number">5</span><span class="token punctuation">]</span>
    第三列:<span class="token punctuation">[</span><span class="token number">3</span> <span class="token number">6</span><span class="token punctuation">]</span>
</code></pre> 
  <h2>24.37NumPy 统计函数与示例</h2> 
  <p>NumPy 具有非常有用的统计函数,用于从数组中的给定元素中查找最小值、最大值、平均值、中位数、百分位数、标准偏差和方差等。函数解释如下 - 统计函数 Numpy 配备了如下所列的稳健统计函数</p> 
  <ul> 
   <li>Numpy 函数</li> 
  </ul> 
  <ul> 
   <li>最小 np.min()</li> 
   <li>最大 np.max()</li> 
   <li>平均 np.mean()</li> 
   <li>中位数 np.median()</li> 
   <li>差异</li> 
   <li>百分位</li> 
   <li>标准差 np.std()</li> 
  </ul> 
  <pre><code class="prism language-python">np_normal_dis  <span class="token operator">=</span>  np。随机的。正常(<span class="token number">5</span>,<span class="token number">0.5</span>,<span class="token number">100</span>)
 np_normal_dis 
<span class="token comment">##最小值,最大值,平均值,中位数,SD</span>
打印(<span class="token string">'分钟:'</span>,two_dimension_array。分钟())
的打印(<span class="token string">'最大:'</span>,two_dimension_array。最大())
的打印(“平均: ' two_dimension_array。意思是())
#打印('中位数”,two_dimension_array<span class="token punctuation">.</span>median())
打印(<span class="token string">'SD:'</span><span class="token punctuation">,</span> two_dimension_array。标准<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python"><span class="token builtin">min</span><span class="token punctuation">:</span>  <span class="token number">1</span>
<span class="token builtin">max</span><span class="token punctuation">:</span>  <span class="token number">55</span>
mean<span class="token punctuation">:</span>  <span class="token number">14.777777777777779</span>
sd<span class="token punctuation">:</span>  <span class="token number">18.913709183069525</span>
最小值:   <span class="token number">1</span>
最大值:   <span class="token number">55</span>
平均值:   <span class="token number">14.777777777777779</span> 标准
差:   <span class="token number">18.913709183069525</span>
</code></pre> 
  <pre><code class="prism language-python">打印(two_dimension_array)
打印(<span class="token string">'列具有最小:'</span>,NP。阿明(two_dimension_array,轴<span class="token operator">=</span> <span class="token number">0</span>))
的打印(<span class="token string">'列具有最大:'</span>,NP。AMAX(two_dimension_array,轴<span class="token operator">=</span> <span class="token number">0</span>))
的打印(“<span class="token operator">==</span><span class="token operator">=</span>行<span class="token operator">==</span>”)
打印(<span class="token string">'行用最小的:'</span>,NP。阿明(two_dimension_array,轴<span class="token operator">=</span> <span class="token number">1</span>))
的打印(<span class="token string">'行用最大:'</span>,NP。AMAX(two_dimension_array,轴<span class="token operator">=</span> <span class="token number">1</span>))


<span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span>  <span class="token number">2</span>  <span class="token number">3</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span> <span class="token number">4</span> <span class="token number">55</span> <span class="token number">44</span><span class="token punctuation">]</span>
 <span class="token punctuation">[</span> <span class="token number">7</span>  <span class="token number">8</span>  <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
Column <span class="token keyword">with</span> minimum<span class="token punctuation">:</span>  <span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span><span class="token punctuation">]</span>
Column <span class="token keyword">with</span> maximum<span class="token punctuation">:</span>  <span class="token punctuation">[</span> <span class="token number">7</span> <span class="token number">55</span> <span class="token number">44</span><span class="token punctuation">]</span>
<span class="token operator">==</span><span class="token operator">=</span> Row <span class="token operator">==</span>
Row <span class="token keyword">with</span> minimum<span class="token punctuation">:</span>  <span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">4</span> <span class="token number">7</span><span class="token punctuation">]</span>
Row <span class="token keyword">with</span> maximum<span class="token punctuation">:</span>  <span class="token punctuation">[</span> <span class="token number">3</span> <span class="token number">55</span>  <span class="token number">9</span><span class="token punctuation">]</span>
</code></pre> 
  <h2>24.38如何创建重复序列?</h2> 
  <pre><code class="prism language-python">a  <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span>

#整个重复的<span class="token string">'A'</span>两次
打印(<span class="token string">'平铺:'</span>,NP。瓷砖(一,<span class="token number">2</span>))

#重复的<span class="token string">'A'</span>两次各元件
打印(<span class="token string">'重复:'</span>,NP。重复(一,<span class="token number">2</span>))
Tile<span class="token punctuation">:</span>    <span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span> <span class="token number">1</span> <span class="token number">2</span> <span class="token number">3</span><span class="token punctuation">]</span>
Repeat<span class="token punctuation">:</span>  <span class="token punctuation">[</span><span class="token number">1</span> <span class="token number">1</span> <span class="token number">2</span> <span class="token number">2</span> <span class="token number">3</span> <span class="token number">3</span><span class="token punctuation">]</span>
</code></pre> 
  <h2>24.39如何生成随机数?</h2> 
  <pre><code class="prism language-python"><span class="token comment"># [0,1) </span>
one_random_num  <span class="token operator">=</span>  np之间的一个随机数。随机的。随机<span class="token punctuation">(</span><span class="token punctuation">)</span>
 one_random_in  <span class="token operator">=</span>  np。随机
打印(one_random_num)
<span class="token number">0.6149403282678213</span>
<span class="token number">0.4763968133790438</span>
<span class="token number">0.4763968133790438</span>
<span class="token comment"># 形状为 2,3 的 [0,1) 之间的随机数</span>
r  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 随机的。随机<span class="token punctuation">(</span> size <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> r <span class="token punctuation">)</span>
<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0.13031737</span> <span class="token number">0.4429537</span>  <span class="token number">0.1129527</span> <span class="token punctuation">]</span>
 <span class="token punctuation">[</span><span class="token number">0.76811539</span> <span class="token number">0.88256594</span> <span class="token number">0.6754075</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>
打印<span class="token punctuation">(</span> np <span class="token punctuation">.</span> random <span class="token punctuation">.</span> choice <span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token string">'a'</span> <span class="token punctuation">,</span> <span class="token string">'e'</span> <span class="token punctuation">,</span> <span class="token string">'i'</span> <span class="token punctuation">,</span> <span class="token string">'o'</span> <span class="token punctuation">,</span> <span class="token string">'u'</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> size <span class="token operator">=</span> <span class="token number">10</span> <span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">[</span><span class="token string">'u'</span> <span class="token string">'o'</span> <span class="token string">'o'</span> <span class="token string">'i'</span> <span class="token string">'e'</span> <span class="token string">'e'</span> <span class="token string">'u'</span> <span class="token string">'o'</span> <span class="token string">'u'</span> <span class="token string">'a'</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span> <span class="token string">'i'</span>  <span class="token string">'u'</span>  <span class="token string">'e'</span>  <span class="token string">'o'</span>  <span class="token string">'a'</span>  <span class="token string">'i'</span>  <span class="token string">'e'</span>  <span class="token string">'u'</span>  <span class="token string">'o'</span>  <span class="token string">'i'</span> <span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token string">'iueoaieuoi'</span><span class="token punctuation">]</span>
<span class="token comment">## 形状 2, 2 的 [0, 1] 之间的随机数</span>
rand  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 随机的。兰特<span class="token punctuation">(</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span>
兰特
array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0.97992598</span><span class="token punctuation">,</span> <span class="token number">0.79642484</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">0.65263629</span><span class="token punctuation">,</span> <span class="token number">0.55763145</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
rand2  <span class="token operator">=</span>  np。随机的。randn <span class="token punctuation">(</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span>
 rand2
array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1.65593322</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.52326621</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span> <span class="token number">0.39071179</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2.03649407</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment"># 形状为 2,5 的 [0, 10) 之间的随机整数</span>
rand_int  <span class="token operator">=</span>  np <span class="token punctuation">.</span> 随机的。randint <span class="token punctuation">(</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">10</span> <span class="token punctuation">,</span> size <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
 rand_int
array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <pre><code class="prism language-python">从 scipy 导入 统计
np_normal_dis  <span class="token operator">=</span>  np。随机的。正常(<span class="token number">5</span>,<span class="token number">0.5</span>,<span class="token number">1000</span>)#平均值,标准偏差,样本数
np_normal_dis 
<span class="token comment">##最小值,最大值,平均值,中位数,SD</span>
打印(<span class="token string">'分钟:'</span>,NP。分钟(np_normal_dis))
打印(<span class="token string">'最大:'</span>,np <span class="token punctuation">.</span> <span class="token builtin">max</span> <span class="token punctuation">(</span> np_normal_dis <span class="token punctuation">)</span><span class="token punctuation">)</span>
 <span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'mean: '</span> <span class="token punctuation">,</span> np <span class="token punctuation">.</span>均值(np_normal_dis))
打印(<span class="token string">'中位数:'</span>,NP。中值(np_normal_dis))
打印(<span class="token string">'模式:'</span>,统计信息。模式(np_normal_dis))
打印(<span class="token string">'SD:'</span>,NP。STD(np_normal_dis))
    分钟:<span class="token number">3.557811005458804</span>
    最大:<span class="token number">6.876317743643499</span>
    平均值:<span class="token number">5.035832048106663</span>
    中位数:<span class="token number">5.020161980441937</span>
    模式:模式结果(模式<span class="token operator">=</span>数组(<span class="token punctuation">[</span><span class="token number">3.55781101</span><span class="token punctuation">]</span>),计数<span class="token operator">=</span>数组(<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>))
    标准差:<span class="token number">0.489682424165213</span>
PLT。hist <span class="token punctuation">(</span> np_normal_dis <span class="token punctuation">,</span> color <span class="token operator">=</span> <span class="token string">"grey"</span> <span class="token punctuation">,</span> bins <span class="token operator">=</span> <span class="token number">21</span> <span class="token punctuation">)</span>
 plt。显示<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/c9fbd233a88643dbb62d85f2f7d57c73.png" target="_blank"><img src="https://img.it610.com/image/info8/c9fbd233a88643dbb62d85f2f7d57c73.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第2张图片" width="388" height="264" style="border:1px solid black;"></a></p> 
  <pre><code class="prism language-python"><span class="token comment"># numpy.dot(): Python 中使用 Numpy 的</span>
Dot Product 
<span class="token comment"># Dot Product # Numpy 是强大的矩阵计算库。例如,您可以使用 np.dot 计算点积</span>

# 句法

<span class="token comment"># numpy.dot(x, y, out=None)</span>
</code></pre> 
  <h2>24.40线性代数</h2> 
  <p>点积</p> 
  <pre><code class="prism language-python"><span class="token comment">## 线性代数</span>
<span class="token comment">### 点积:两个数组的乘积</span>
f  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
 g  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
 <span class="token comment">### 1*4+2*5 + 3*6 </span>
np <span class="token punctuation">.</span> 点<span class="token punctuation">(</span> f <span class="token punctuation">,</span> g <span class="token punctuation">)</span>   <span class="token comment"># 23</span>
</code></pre> 
  <h2>24.41NumPy 矩阵乘法与 np.matmul()</h2> 
  <pre><code class="prism language-python"><span class="token comment">### Matmul:两个数组的矩阵乘积</span>
h  <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>
 i  <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">6</span> <span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span> <span class="token number">7</span> <span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>
 <span class="token comment">### 1*5+2*7 = 19</span>
纳米。matmul <span class="token punctuation">(</span> h <span class="token punctuation">,</span> i <span class="token punctuation">)</span>
    数组<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">19</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
           <span class="token punctuation">[</span><span class="token number">43</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment">##行列式2*2矩阵</span>
<span class="token comment">###5*8-7*6np.linalg.det(i)</span>
NP。linalg。检测<span class="token punctuation">(</span> i <span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token number">1.999999999999999</span>
Z  <span class="token operator">=</span>  np。零<span class="token punctuation">(</span><span class="token punctuation">(</span> <span class="token number">8</span> <span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">)</span><span class="token punctuation">)</span>
 Z <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">:</span><span class="token punctuation">:</span> <span class="token number">2</span> <span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token punctuation">:</span> <span class="token number">2</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  <span class="token number">1</span> 
Z <span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">:</span><span class="token punctuation">:</span> <span class="token number">2</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  <span class="token number">1</span>
Z
</code></pre> 
  <pre><code class="prism language-python">array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
new_list  <span class="token operator">=</span> <span class="token punctuation">[</span> x  <span class="token operator">+</span>  <span class="token number">2</span>  <span class="token keyword">for</span>  x  <span class="token keyword">in</span>  <span class="token builtin">range</span> <span class="token punctuation">(</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">11</span> <span class="token punctuation">)</span><span class="token punctuation">]</span>
</code></pre> 
  <p>新列表</p> 
  <pre><code class="prism language-python"><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">]</span>
<span class="token punctuation">[</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">6</span> <span class="token punctuation">,</span> <span class="token number">7</span> <span class="token punctuation">,</span> <span class="token number">8</span> <span class="token punctuation">,</span> <span class="token number">9</span> <span class="token punctuation">,</span> <span class="token number">10</span> <span class="token punctuation">,</span> <span class="token number">11</span> <span class="token punctuation">,</span> <span class="token number">12</span> <span class="token punctuation">]</span>
<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">]</span>
np_arr  <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span>范围<span class="token punctuation">(</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">11</span> <span class="token punctuation">)</span><span class="token punctuation">)</span>
 np_arr  <span class="token operator">+</span>  <span class="token number">2</span>
</code></pre> 
  <p>数组([ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])</p> 
  <p>对于具有线性关系的量,我们使用线性方程。让我们看看下面的例子:</p> 
  <pre><code class="prism language-python">温度 <span class="token operator">=</span>  np。数组<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
 pressure  <span class="token operator">=</span>  temp  <span class="token operator">*</span>  <span class="token number">2</span>  <span class="token operator">+</span>  <span class="token number">5</span> 
pressure
</code></pre> 
  <p>数组([ 7, 9, 11, 13, 15])</p> 
  <pre><code class="prism language-python">PLT。绘图(温度,压力)
 plt。xlabel(<span class="token string">'摄氏温度'</span>)
 plt。ylabel(<span class="token string">'atm 中的压力'</span>)
 plt。标题(<span class="token string">'温度与压力'</span>)
 plt。xticks(NP。人气指数(<span class="token number">0</span>,<span class="token number">6</span>,步骤<span class="token operator">=</span> <span class="token number">0.5</span>))
 PLT。显示<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/18e82ed8cbd74f4d9af8cd63bbac5b62.jpg" target="_blank"><img src="https://img.it610.com/image/info8/18e82ed8cbd74f4d9af8cd63bbac5b62.jpg" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第3张图片" width="386" height="278" style="border:1px solid black;"></a></p> 
  <p>使用 numpy 绘制高斯正态分布。如下所示,numpy 可以生成随机数。要创建随机样本,我们需要均值(mu)、sigma(标准差)、数据点数。</p> 
  <pre><code class="prism language-python">mu  <span class="token operator">=</span>  <span class="token number">28</span>
西格玛 <span class="token operator">=</span>  <span class="token number">15</span> 个
样本 <span class="token operator">=</span>  <span class="token number">100000</span>

x  <span class="token operator">=</span>  np。随机的。正常(mu <span class="token punctuation">,</span> sigma <span class="token punctuation">,</span>样本)
 ax  <span class="token operator">=</span>  sns。分布图<span class="token punctuation">(</span> x <span class="token punctuation">)</span><span class="token punctuation">;</span>
斧头。设置<span class="token punctuation">(</span> xlabel <span class="token operator">=</span> <span class="token string">"x"</span> <span class="token punctuation">,</span> ylabel <span class="token operator">=</span> <span class="token string">'y'</span> <span class="token punctuation">)</span>
 plt。显示<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/4beaadeb30ef4f14b25c73b5e0cbcdd3.jpg" target="_blank"><img src="https://img.it610.com/image/info8/4beaadeb30ef4f14b25c73b5e0cbcdd3.jpg" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第4张图片" width="400" height="261" style="border:1px solid black;"></a></p> 
  <p>总而言之,与 python 列表的主要区别是:</p> 
  <ol> 
   <li>数组支持向量化操作,而列表不支持。</li> 
   <li>一旦创建了数组,就不能更改其大小。您将不得不创建一个新阵列或覆盖现有阵列。</li> 
   <li>每个数组都有一个且只有一个 dtype。其中的所有项目都应该是那个 dtype。</li> 
   <li>等效的 numpy 数组比 Python 列表占用的空间少得多。</li> 
   <li>numpy 数组支持布尔索引。</li> 
  </ol> 
  <h1>第 25 天 - Pandas</h1> 
  <p>Pandas 是一种开源、高性能、易于使用的 Python 编程语言的数据结构和数据分析工具。Pandas 添加了数据结构和工具,旨在处理类似表格的数据,即Series和Data Frames。Pandas 提供了用于数据操作的工具:</p> 
  <ol> 
   <li>重塑</li> 
   <li>合并</li> 
   <li>排序</li> 
   <li>切片</li> 
   <li>聚合</li> 
   <li>插补。如果您使用的是 anaconda,则无需安装 pandas。</li> 
  </ol> 
  <h2>25.1安装pandas</h2> 
  <p>对于 Mac:</p> 
  <blockquote> 
   <p>pip 安装 conda<br> conda 安装 pandas</p> 
  </blockquote> 
  <p>对于 Windows:</p> 
  <blockquote> 
   <p>pip 安装 conda<br> pip 安装 熊猫</p> 
  </blockquote> 
  <p>Pandas 数据结构基于Series和DataFrames。</p> 
  <p>一个系列是一列和一个数据帧是一个多维表的集合组成系列。为了创建一个pandas系列,我们应该使用numpy来创建一个一维数组或一个python列表。让我们看一个系列的例子:</p> 
  <p>命名pandas系列</p> 
  <p><a href="https://img.it610.com/image/info8/19e0a0c8fd64436bb4a872272a9c5091.png" target="_blank"><img src="https://img.it610.com/image/info8/19e0a0c8fd64436bb4a872272a9c5091.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第5张图片" width="231" height="194" style="border:1px solid black;"></a></p> 
  <p>国家系列<br> <a href="https://img.it610.com/image/info8/8db922e9c7fb4b5298e62c103fa19cef.png" target="_blank"><img src="https://img.it610.com/image/info8/8db922e9c7fb4b5298e62c103fa19cef.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第6张图片" width="216" height="187" style="border:1px solid black;"></a></p> 
  <p>城市系列<br> <a href="https://img.it610.com/image/info8/235d1178d6cb4fcfb62a0abc43f0621a.png" target="_blank"><img src="https://img.it610.com/image/info8/235d1178d6cb4fcfb62a0abc43f0621a.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第7张图片" width="218" height="193" style="border:1px solid black;"></a></p> 
  <p>如您所见,pandas 系列只是一列数据。如果我们想要多列,我们使用数据框。下面的示例显示了 Pandas DataFrames。</p> 
  <p>让我们看看一个 Pandas 数据框的例子:</p> 
  <p><a href="https://img.it610.com/image/info8/f0e4c86ffb9c464fbab8280d8f3784d6.png" target="_blank"><img src="https://img.it610.com/image/info8/f0e4c86ffb9c464fbab8280d8f3784d6.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第8张图片" width="575" height="187" style="border:1px solid black;"></a></p> 
  <p>数据框是行和列的集合。看下表;它比上面的例子有更多的列:</p> 
  <p><a href="https://img.it610.com/image/info8/19c548f8dfd64630a886bac657c6ac34.jpg" target="_blank"><img src="https://img.it610.com/image/info8/19c548f8dfd64630a886bac657c6ac34.jpg" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第9张图片" width="650" height="141" style="border:1px solid black;"></a></p> 
  <p>接下来,我们将看到如何导入pandas以及如何使用pandas创建Series和DataFrames</p> 
  <h2>导入pandas</h2> 
  <pre><code class="prism language-python"><span class="token keyword">import</span>  pandas  <span class="token keyword">as</span>  pd  <span class="token comment"># 将pandas 导入为pd </span>
<span class="token keyword">import</span>  numpy   <span class="token keyword">as</span>  np  <span class="token comment"># 将numpy 导入为np</span>
</code></pre> 
  <h2>25.2使用默认索引创建 Pandas 系列</h2> 
  <pre><code class="prism language-python">nums  <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span>
 s  <span class="token operator">=</span>  pd。系列<span class="token punctuation">(</span> nums <span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> s <span class="token punctuation">)</span>
    <span class="token number">0</span> <span class="token number">1</span>
    <span class="token number">1</span> <span class="token number">2</span>
    <span class="token number">2</span> <span class="token number">3</span>
    <span class="token number">3</span> <span class="token number">4</span>
    <span class="token number">4</span> <span class="token number">5</span>
    数据类型:int64
</code></pre> 
  <h2>25.3使用自定义索引创建 Pandas 系列</h2> 
  <pre><code class="prism language-python">nums  <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span>
 s  <span class="token operator">=</span>  pd。系列<span class="token punctuation">(</span> nums <span class="token punctuation">,</span> index <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> s <span class="token punctuation">)</span>
    <span class="token number">1</span> <span class="token number">1</span>
    <span class="token number">2</span> <span class="token number">2</span>
    <span class="token number">3</span> <span class="token number">3</span>
    <span class="token number">4</span> <span class="token number">4</span>
    <span class="token number">5</span> <span class="token number">5</span>
    数据类型:int64
水果 <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token string">'Orange'</span> <span class="token punctuation">,</span> <span class="token string">'Banana'</span> <span class="token punctuation">,</span> <span class="token string">'Mango'</span> <span class="token punctuation">]</span>
水果 <span class="token operator">=</span>  pd <span class="token punctuation">.</span> 系列(水果,指数<span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">1</span>,<span class="token number">2</span>,<span class="token number">3</span> <span class="token punctuation">]</span>)
打印(水果)
    <span class="token number">1</span> 橙色
    <span class="token number">2</span> 香蕉
    <span class="token number">3</span> 芒果
    数据类型:对象
</code></pre> 
  <h2>25.4从字典创建 Pandas 系列</h2> 
  <pre><code class="prism language-python">dct  <span class="token operator">=</span> <span class="token punctuation">{
     </span> <span class="token string">'name'</span> <span class="token punctuation">:</span> <span class="token string">'Asabeneh'</span> <span class="token punctuation">,</span> <span class="token string">'country'</span> <span class="token punctuation">:</span> <span class="token string">'芬兰'</span> <span class="token punctuation">,</span> <span class="token string">'city'</span> <span class="token punctuation">:</span> <span class="token string">'赫尔辛基'</span> <span class="token punctuation">}</span>
s  <span class="token operator">=</span>  pd。系列<span class="token punctuation">(</span> dct <span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> s <span class="token punctuation">)</span>
    姓名 Asabeneh
    国家芬兰
    赫尔辛基市
    数据类型:对象
</code></pre> 
  <h2>25.5创建一个常量 Pandas 系列</h2> 
  <pre><code class="prism language-python">s  <span class="token operator">=</span>  pd。系列<span class="token punctuation">(</span> <span class="token number">10</span> <span class="token punctuation">,</span> index  <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> s <span class="token punctuation">)</span>
    <span class="token number">1</span> <span class="token number">10</span>
    <span class="token number">2</span> <span class="token number">10</span>
    <span class="token number">3</span> <span class="token number">10</span>
    数据类型:int64
</code></pre> 
  <h2>25.6使用 Linspace 创建 Pandas 系列</h2> 
  <pre><code class="prism language-python">s  <span class="token operator">=</span>  pd。系列<span class="token punctuation">(</span> np <span class="token punctuation">.</span> linspace <span class="token punctuation">(</span> <span class="token number">5</span> <span class="token punctuation">,</span> <span class="token number">20</span> <span class="token punctuation">,</span> <span class="token number">10</span> <span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># linspace(starting, end, items)</span>
打印<span class="token punctuation">(</span> s <span class="token punctuation">)</span>
    <span class="token number">0</span> <span class="token number">5.000000</span>
    <span class="token number">1</span> <span class="token number">6.666667</span>
    <span class="token number">2</span> <span class="token number">8.333333</span>
    <span class="token number">3</span> <span class="token number">10.000000</span>
    <span class="token number">4</span> <span class="token number">11.666667</span>
    <span class="token number">5</span> <span class="token number">13.333333</span>
    <span class="token number">6</span> <span class="token number">15.000000</span>
    <span class="token number">7</span> <span class="token number">16.666667</span>
    <span class="token number">8</span> <span class="token number">18.333333</span>
    <span class="token number">9</span> <span class="token number">20.000000</span>
    数据类型:float64
</code></pre> 
  <h2>25.7数据帧</h2> 
  <p>Pandas 数据框可以用不同的方式创建。</p> 
  <h3>从列表列表创建数据帧</h3> 
  <pre><code class="prism language-python">数据 <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">[</span> “阿萨本尼”、“芬兰”、“赫尔辛克” <span class="token punctuation">]</span>、
    <span class="token punctuation">[</span> <span class="token string">'大卫'</span>,<span class="token string">'英国'</span>,<span class="token string">'伦敦'</span> <span class="token punctuation">]</span>,
    <span class="token punctuation">[</span> “约翰”、“瑞典”、“斯德哥尔摩” <span class="token punctuation">]</span>
<span class="token punctuation">]</span>
df  <span class="token operator">=</span>  pd。DataFrame <span class="token punctuation">(</span> data <span class="token punctuation">,</span> columns <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token string">'Names'</span> <span class="token punctuation">,</span> <span class="token string">'Country'</span> <span class="token punctuation">,</span> <span class="token string">'City'</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> df <span class="token punctuation">)</span>
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/08ffd0f5c3b5428f9fa92173e70618cb.png" target="_blank"><img src="https://img.it610.com/image/info8/08ffd0f5c3b5428f9fa92173e70618cb.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第10张图片" width="318" height="183" style="border:1px solid black;"></a></p> 
  <h2>25.8使用字典创建 DataFrame</h2> 
  <pre><code class="prism language-python">data  <span class="token operator">=</span> <span class="token punctuation">{
     </span> <span class="token string">'Name'</span> <span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token string">'Asabeneh'</span> <span class="token punctuation">,</span> <span class="token string">'David'</span> <span class="token punctuation">,</span> <span class="token string">'John'</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'国家'</span> <span class="token punctuation">:</span><span class="token punctuation">[</span>
     <span class="token string">'芬兰'</span> <span class="token punctuation">,</span> <span class="token string">'英国'</span> <span class="token punctuation">,</span> <span class="token string">'瑞典'</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'城市'</span> <span class="token punctuation">:</span> <span class="token punctuation">[</span> <span class="token string">'赫尔斯基'</span> <span class="token punctuation">,</span> <span class="token string">'伦敦'</span> <span class="token punctuation">,</span> <span class="token string">'斯德哥尔摩'</span> <span class="token punctuation">]</span><span class="token punctuation">}</span>
 df  <span class="token operator">=</span>  pd <span class="token punctuation">.</span> DataFrame(数据)
打印(df)
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/1642e89f3dfb4902813ec533108c725c.png" target="_blank"><img src="https://img.it610.com/image/info8/1642e89f3dfb4902813ec533108c725c.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第11张图片" width="310" height="179" style="border:1px solid black;"></a></p> 
  <h2>25.9从字典列表创建数据帧</h2> 
  <pre><code class="prism language-python">数据 <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{
     </span> <span class="token string">'姓名'</span> <span class="token punctuation">:</span> <span class="token string">'Asabeneh'</span> <span class="token punctuation">,</span> <span class="token string">'国家'</span> <span class="token punctuation">:</span> <span class="token string">'芬兰'</span> <span class="token punctuation">,</span> <span class="token string">'城市'</span> <span class="token punctuation">:</span> <span class="token string">'赫尔辛基'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{
     </span> <span class="token string">'姓名'</span>:<span class="token string">'大卫'</span>,<span class="token string">'国家'</span>:<span class="token string">'英国'</span>,<span class="token string">'城市'</span>:<span class="token string">'伦敦'</span> <span class="token punctuation">}</span>,
    <span class="token punctuation">{
     </span> <span class="token string">'姓名'</span>:<span class="token string">'约翰'</span>,<span class="token string">'国家'</span>:<span class="token string">'瑞典'</span>,<span class="token string">'城市'</span>:<span class="token string">'斯德哥尔摩'</span> <span class="token punctuation">}</span><span class="token punctuation">]</span>
 df  <span class="token operator">=</span>  pd <span class="token punctuation">.</span> DataFrame(数据)
打印(df)
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/0ca80048917d463ca7389d8ca2710b2f.png" target="_blank"><img src="https://img.it610.com/image/info8/0ca80048917d463ca7389d8ca2710b2f.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第12张图片" width="321" height="189" style="border:1px solid black;"></a></p> 
  <h2>25.10使用 Pandas 读取 CSV 文件</h2> 
  <p>要下载 CSV 文件,本例中需要什么,控制台/命令行就足够了:</p> 
  <pre><code class="prism language-python">curl <span class="token operator">-</span>O https<span class="token punctuation">:</span><span class="token operator">//</span>raw<span class="token punctuation">.</span>githubusercontent<span class="token punctuation">.</span>com<span class="token operator">/</span>Asabeneh<span class="token operator">/</span><span class="token number">30</span><span class="token operator">-</span>Days<span class="token operator">-</span>Of<span class="token operator">-</span>Python<span class="token operator">/</span>master<span class="token operator">/</span>data<span class="token operator">/</span>weight<span class="token operator">-</span>height<span class="token punctuation">.</span>csv
</code></pre> 
  <p>将下载的文件放在您的工作目录中。</p> 
  <pre><code class="prism language-python">将 Pandas 导入为 pd

df  <span class="token operator">=</span>  pd。read_csv <span class="token punctuation">(</span> <span class="token string">'weight-height.csv'</span> <span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> df <span class="token punctuation">)</span>
</code></pre> 
  <h2>25.11数据探索</h2> 
  <p>让我们使用 head() 仅读取前 5 行</p> 
  <pre><code class="prism language-python"><span class="token keyword">print</span> <span class="token punctuation">(</span> df <span class="token punctuation">.</span> head <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># 给五行我们可以通过将参数传递给 head() 方法来增加行数</span>
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/98d513e77f6c4afe9dc991b46db2e120.jpg" target="_blank"><img src="https://img.it610.com/image/info8/98d513e77f6c4afe9dc991b46db2e120.jpg" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第13张图片" width="320" height="265" style="border:1px solid black;"></a><br> 让我们还使用 tail() 方法探索数据帧的最后记录。</p> 
  <pre><code class="prism language-python">打印(DF。尾()) #尾巴给最后的五排,我们可以通过传递参数尾法提高行
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/4c700cd50f20472492421d100efc1a39.jpg" target="_blank"><img src="https://img.it610.com/image/info8/4c700cd50f20472492421d100efc1a39.jpg" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第14张图片" width="362" height="266" style="border:1px solid black;"></a></p> 
  <p>如您所见,csv 文件有三行:性别、身高和体重。如果 DataFrame 有很长的行,就很难知道所有的列。因此,我们应该使用一种方法来知道列。我们不知道行数。让我们使用形状肉类。</p> 
  <pre><code class="prism language-python"><span class="token keyword">print</span> <span class="token punctuation">(</span> df <span class="token punctuation">.</span> shape <span class="token punctuation">)</span> <span class="token comment"># 如你所见 10000 行和三列</span>
<span class="token punctuation">(</span><span class="token number">10000</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
</code></pre> 
  <p>让我们使用列获取所有列。</p> 
  <pre><code class="prism language-python">打印(df。列)
Index<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'Gender'</span><span class="token punctuation">,</span> <span class="token string">'Height'</span><span class="token punctuation">,</span> <span class="token string">'Weight'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span><span class="token string">'object'</span><span class="token punctuation">)</span>
</code></pre> 
  <p>现在,让我们使用列键获取特定列</p> 
  <pre><code class="prism language-python">heights  <span class="token operator">=</span>  df <span class="token punctuation">[</span> <span class="token string">'Height'</span> <span class="token punctuation">]</span> <span class="token comment"># 这是一个系列</span>
打印(高度)
    <span class="token number">0</span> <span class="token number">73.847017</span>
    <span class="token number">1</span> <span class="token number">68.781904</span>
    <span class="token number">2</span> <span class="token number">74.110105</span>
    <span class="token number">3</span> <span class="token number">71.730978</span>
    <span class="token number">4</span> <span class="token number">69.881796</span>
              <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>    
    <span class="token number">9995</span> <span class="token number">66.172652</span>
    <span class="token number">9996</span> <span class="token number">67.067155</span>
    <span class="token number">9997</span> <span class="token number">63.867992</span>
    <span class="token number">9998</span> <span class="token number">69.034243</span>
    <span class="token number">9999</span> <span class="token number">61.944246</span>
    名称:高度,长度:<span class="token number">10000</span>,数据类型:float64
weights  <span class="token operator">=</span>  df <span class="token punctuation">[</span> <span class="token string">'Weight'</span> <span class="token punctuation">]</span> <span class="token comment"># 这是一个系列</span>
打印(重量)
    <span class="token number">0</span> <span class="token number">241.893563</span>
    <span class="token number">1</span> <span class="token number">162.310473</span>
    <span class="token number">2</span> <span class="token number">212.740856</span>
    <span class="token number">3</span> <span class="token number">220.042470</span>
    <span class="token number">4</span> <span class="token number">206.349801</span>
               <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>    
    <span class="token number">9995</span> <span class="token number">136.777454</span>
    <span class="token number">9996</span> <span class="token number">170.867906</span>
    <span class="token number">9997</span> <span class="token number">128.475319</span>
    <span class="token number">9998</span> <span class="token number">163.852461</span>
    <span class="token number">9999</span> <span class="token number">113.649103</span>
    名称:重量,长度:<span class="token number">10000</span>,dtype:float64
打印(<span class="token builtin">len</span>(高度)<span class="token operator">==</span>  <span class="token builtin">len</span>(权重))
<span class="token boolean">True</span>
</code></pre> 
  <p>describe() 方法提供数据集的描述性统计值。</p> 
  <pre><code class="prism language-python"><span class="token keyword">print</span> <span class="token punctuation">(</span> heights <span class="token punctuation">.</span> describe <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># 给出高度数据的统计信息</span>
    数 <span class="token number">10000.000000</span>
    平均 <span class="token number">66.367560</span>
    标准 <span class="token number">3.847528</span>
    分钟 <span class="token number">54.263133</span>
    <span class="token number">25</span><span class="token operator">%</span> <span class="token number">63.505620</span>
    <span class="token number">50</span><span class="token operator">%</span> <span class="token number">66.318070</span>
    <span class="token number">75</span><span class="token operator">%</span> <span class="token number">69.174262</span>
    最大 <span class="token number">78.998742</span>
    名称:高度,数据类型:float64
打印(权重。描述())
    数 <span class="token number">10000.000000</span>
    平均 <span class="token number">161.440357</span>
    标准 <span class="token number">32.108439</span>
    最低 <span class="token number">64.700127</span>
    <span class="token number">25</span><span class="token operator">%</span> <span class="token number">135.818051</span>
    <span class="token number">50</span><span class="token operator">%</span> <span class="token number">161.212928</span>
    <span class="token number">75</span><span class="token operator">%</span> <span class="token number">187.169525</span>
    最大 <span class="token number">269.989699</span>
    名称:重量,数据类型:float64
</code></pre> 
  <pre><code class="prism language-python"><span class="token keyword">print</span> <span class="token punctuation">(</span> df <span class="token punctuation">.</span> describe <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment"># describe 还可以给出来自数据帧的统计信息</span>
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/1273783618524e1a802d91824d22d723.jpg" target="_blank"><img src="https://img.it610.com/image/info8/1273783618524e1a802d91824d22d723.jpg" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第15张图片" width="363" height="379" style="border:1px solid black;"></a></p> 
  <p>与 describe() 类似,info() 方法也提供有关数据集的信息。</p> 
  <h2>25.12修改数据帧</h2> 
  <p>修改DataFrame: * 我们可以创建一个新的DataFrame * 我们可以创建一个新列并将其添加到DataFrame, * 我们可以从DataFrame 中删除现有列, * 我们可以修改DataFrame 中的现有列, * 我们可以更改 DataFrame 中列值的数据类型</p> 
  <h2>25.13创建数据帧</h2> 
  <p>与往常一样,首先我们导入必要的包。现在,让我们导入 pandas 和 numpy,这两个最好的朋友。</p> 
  <pre><code class="prism language-python">将Pandas 导入为 pd
将 numpy 导入为 np
数据 <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{
     </span> “姓名”:“阿萨本尼”,“国家”:“芬兰”,“城市”:“赫尔辛基” <span class="token punctuation">}</span>,
    <span class="token punctuation">{
     </span> “姓名”:“大卫”,“国家”:“英国”,“城市”:“伦敦” <span class="token punctuation">}</span>,
    <span class="token punctuation">{
     </span> “姓名”:“约翰”,“国家”:“瑞典”,“城市”:“斯德哥尔摩” <span class="token punctuation">}</span><span class="token punctuation">]</span>
 df  <span class="token operator">=</span>  pd <span class="token punctuation">.</span> DataFrame(数据)
打印(df)
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/05b3a63f2c704a669413facb9e76fa01.png" target="_blank"><img src="https://img.it610.com/image/info8/05b3a63f2c704a669413facb9e76fa01.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第16张图片" width="315" height="179" style="border:1px solid black;"></a></p> 
  <p>向 DataFrame 添加列就像向字典添加键。</p> 
  <p>首先让我们使用前面的示例来创建一个 DataFrame。创建 DataFrame 后,我们将开始修改列和列值。</p> 
  <h2>25.14添加新列</h2> 
  <p>让我们在 DataFrame 中添加一个权重列</p> 
  <pre><code class="prism language-python">权重 <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">74</span> <span class="token punctuation">,</span> <span class="token number">78</span> <span class="token punctuation">,</span> <span class="token number">69</span> <span class="token punctuation">]</span>
 df <span class="token punctuation">[</span> <span class="token string">'Weight'</span> <span class="token punctuation">]</span> <span class="token operator">=</span> 权重
df
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/6c8a658531184986a0c314192f72d2ea.png" target="_blank"><img src="https://img.it610.com/image/info8/6c8a658531184986a0c314192f72d2ea.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第17张图片" width="384" height="183" style="border:1px solid black;"></a></p> 
  <p>让我们在 DataFrame 中添加一个高度列</p> 
  <pre><code class="prism language-python">高度 <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">173</span> <span class="token punctuation">,</span> <span class="token number">175</span> <span class="token punctuation">,</span> <span class="token number">169</span> <span class="token punctuation">]</span>
 df <span class="token punctuation">[</span> <span class="token string">'高度'</span> <span class="token punctuation">]</span> <span class="token operator">=</span> 高度
打印<span class="token punctuation">(</span> df <span class="token punctuation">)</span>
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/2ea3d6ffbdf64359b0fd09a213be9da9.png" target="_blank"><img src="https://img.it610.com/image/info8/2ea3d6ffbdf64359b0fd09a213be9da9.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第18张图片" width="427" height="177" style="border:1px solid black;"></a></p> 
  <p>正如您在上面的 DataFrame 中看到的,我们确实添加了新的列,重量和高度。让我们通过使用他们的体重和身高计算他们的 BMI 来添加一个额外的列,称为 BMI(身体质量指数)。BMI 是质量除以身高的平方(以米为单位)- 体重/身高 * 身高。</p> 
  <p>如您所见,高度以厘米为单位,因此我们应该将其更改为米。让我们修改高度行。</p> 
  <h2>25.15修改列值</h2> 
  <pre><code class="prism language-python">df <span class="token punctuation">[</span> <span class="token string">'高度'</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  df <span class="token punctuation">[</span> <span class="token string">'高度'</span> <span class="token punctuation">]</span> <span class="token operator">*</span>  <span class="token number">0.01</span> 
df
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/f68103bbf35e4fcca6f64b2c76f723f4.png" target="_blank"><img src="https://img.it610.com/image/info8/f68103bbf35e4fcca6f64b2c76f723f4.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第19张图片" width="432" height="179" style="border:1px solid black;"></a></p> 
  <pre><code class="prism language-python"><span class="token comment"># 使用函数使我们的代码更简洁,但是你可以不用一个来计算 bmi </span>
<span class="token keyword">def</span>  <span class="token function">calculate_bmi</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
     weights  <span class="token operator">=</span>  df <span class="token punctuation">[</span> <span class="token string">'Weight'</span> <span class="token punctuation">]</span>
     heights  <span class="token operator">=</span>  df <span class="token punctuation">[</span> <span class="token string">'Height'</span> <span class="token punctuation">]</span>
     bmi  <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
     <span class="token keyword">for</span>  w <span class="token punctuation">,</span> h  <span class="token keyword">in</span>  <span class="token builtin">zip</span> <span class="token punctuation">(</span> weights <span class="token punctuation">,</span>高度<span class="token punctuation">)</span><span class="token punctuation">:</span>
         b  <span class="token operator">=</span>  w <span class="token operator">/</span> <span class="token punctuation">(</span> h <span class="token operator">*</span> h <span class="token punctuation">)</span>
         bmi。追加<span class="token punctuation">(</span> b <span class="token punctuation">)</span>
    返回 体重指数
    
bmi  <span class="token operator">=</span>  calculate_bmi <span class="token punctuation">(</span><span class="token punctuation">)</span>
df <span class="token punctuation">[</span> <span class="token string">'BMI'</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  bmi 
df
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/2ce68f9f385242a9872fc644154294ef.png" target="_blank"><img src="https://img.it610.com/image/info8/2ce68f9f385242a9872fc644154294ef.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第20张图片" width="527" height="183" style="border:1px solid black;"></a></p> 
  <h2>25.16格式化 DataFrame 列</h2> 
  <p>DataFrame 的 BMI 列值是浮点数,小数点后有许多有效数字。让我们将其更改为一位有效数字。</p> 
  <pre><code class="prism language-python">df <span class="token punctuation">[</span> <span class="token string">'BMI'</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  <span class="token builtin">round</span> <span class="token punctuation">(</span> df <span class="token punctuation">[</span> <span class="token string">'BMI'</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">)</span>
打印<span class="token punctuation">(</span> df <span class="token punctuation">)</span>
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/f6cb4ab0ebcc43dbbcb8ba57d3d0435f.png" target="_blank"><img src="https://img.it610.com/image/info8/f6cb4ab0ebcc43dbbcb8ba57d3d0435f.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第21张图片" width="518" height="186" style="border:1px solid black;"></a></p> 
  <p>DataFrame 中的信息似乎还没有完成,让我们添加出生年份和当前年份列。</p> 
  <pre><code class="prism language-python">birth_year  <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token string">'1769'</span> <span class="token punctuation">,</span> <span class="token string">'1985'</span> <span class="token punctuation">,</span> <span class="token string">'1990'</span> <span class="token punctuation">]</span>
 current_year  <span class="token operator">=</span>  pd。系列<span class="token punctuation">(</span> <span class="token number">2020</span> <span class="token punctuation">,</span> index <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
 df <span class="token punctuation">[</span> <span class="token string">'Birth Year'</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  birth_year 
df <span class="token punctuation">[</span> <span class="token string">'Current Year'</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  current_year 
df
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/0210e1481554450a86dd2835b29211b6.png" target="_blank"><img src="https://img.it610.com/image/info8/0210e1481554450a86dd2835b29211b6.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第22张图片" width="676" height="183" style="border:1px solid black;"></a></p> 
  <h2>25.17检查列值的数据类型</h2> 
  <pre><code class="prism language-python">打印(DF,重量,D型)
    数据类型(<span class="token string">' int64 '</span>)
df <span class="token punctuation">[</span> <span class="token string">'出生年份'</span> <span class="token punctuation">]</span>。dtype  <span class="token comment"># 它给出字符串对象,我们应该将其更改为数字</span>
df <span class="token punctuation">[</span> <span class="token string">'出生年份'</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  df <span class="token punctuation">[</span> <span class="token string">'出生年份'</span> <span class="token punctuation">]</span>。astype <span class="token punctuation">(</span> <span class="token string">'int'</span> <span class="token punctuation">)</span>
 <span class="token keyword">print</span> <span class="token punctuation">(</span> df <span class="token punctuation">[</span> <span class="token string">'Birth Year'</span> <span class="token punctuation">]</span><span class="token punctuation">.</span> dtype <span class="token punctuation">)</span> <span class="token comment"># 现在检查数据类型</span>
    数据类型(<span class="token string">' int32 '</span>)
</code></pre> 
  <p>现在与当年相同:</p> 
  <pre><code class="prism language-python">df <span class="token punctuation">[</span> <span class="token string">'当年'</span> <span class="token punctuation">]</span> <span class="token operator">=</span>  df <span class="token punctuation">[</span> <span class="token string">'当年'</span> <span class="token punctuation">]</span>。astype <span class="token punctuation">(</span> <span class="token string">'int'</span> <span class="token punctuation">)</span>
 df <span class="token punctuation">[</span> <span class="token string">'本年'</span> <span class="token punctuation">]</span>。数据类型
    数据类型(<span class="token string">' int32 '</span>)
</code></pre> 
  <p>现在,出生年份和当前年份的列值为整数。我们可以计算年龄。</p> 
  <pre><code class="prism language-python">年龄 <span class="token operator">=</span>  df <span class="token punctuation">[</span> <span class="token string">'本年'</span> <span class="token punctuation">]</span> <span class="token operator">-</span>  df <span class="token punctuation">[</span> <span class="token string">'出生年份'</span> <span class="token punctuation">]</span>
年龄
<span class="token number">0</span>    <span class="token number">251</span>
<span class="token number">1</span>     <span class="token number">35</span>
<span class="token number">2</span>     <span class="token number">30</span>
dtype<span class="token punctuation">:</span> int32
df <span class="token punctuation">[</span> <span class="token string">'年龄'</span> <span class="token punctuation">]</span> <span class="token operator">=</span> 年龄
打印(df)
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/061b31948bdf4d0e8864011ad4568c39.png" target="_blank"><img src="https://img.it610.com/image/info8/061b31948bdf4d0e8864011ad4568c39.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第23张图片" width="738" height="176" style="border:1px solid black;"></a></p> 
  <p>第一排的人迄今活了251岁。一个人不可能活这么久。要么是打字错误,要么是数据被煮熟了。因此,让我们用列的平均值填充该数据,而不包括异常值。</p> 
  <p>平均值 = (35 + 30)/ 2</p> 
  <pre><code class="prism language-python">mean  <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token number">35</span>  <span class="token operator">+</span>  <span class="token number">30</span> <span class="token punctuation">)</span> <span class="token operator">/</span>  <span class="token number">2</span> 
<span class="token keyword">print</span> <span class="token punctuation">(</span> <span class="token string">'Mean: '</span> <span class="token punctuation">,</span> mean <span class="token punctuation">)</span>	 <span class="token comment">#在输出中添加一些描述很好,所以我们知道什么是什么</span>
   平均值:<span class="token number">32.5</span>
</code></pre> 
  <h2>25.18布尔索引</h2> 
  <pre><code class="prism language-python">打印<span class="token punctuation">(</span> df <span class="token punctuation">[</span> df <span class="token punctuation">[</span> <span class="token string">'年龄'</span> <span class="token punctuation">]</span> <span class="token operator">></span>  <span class="token number">120</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/7e42bd636831415bb23142c377e2841d.png" target="_blank"><img src="https://img.it610.com/image/info8/7e42bd636831415bb23142c377e2841d.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第24张图片" width="723" height="102" style="border:1px solid black;"></a></p> 
  <pre><code class="prism language-python">打印<span class="token punctuation">(</span> df <span class="token punctuation">[</span> df <span class="token punctuation">[</span> <span class="token string">'年龄'</span> <span class="token punctuation">]</span> <span class="token operator"><</span>  <span class="token number">120</span> <span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> 
  <p><a href="https://img.it610.com/image/info8/9306ffb6ee504946a64db9c620acc483.png" target="_blank"><img src="https://img.it610.com/image/info8/9306ffb6ee504946a64db9c620acc483.png" alt="简洁易懂,初学者挑战学习Python编程30天 (四)_第25张图片" width="698" height="147" style="border:1px solid black;"></a></p> 
  <p>初学者挑战学习Python编程30天还有最后一节续集就要结束了,感兴趣了解下面的学习内容,记得关注我。</p> 
 </div> 
</div>
                            </div>
                        </div>
                    </div>
                    <!--PC和WAP自适应版-->
                    <div id="SOHUCS" sid="1445164669760221184"></div>
                    <script type="text/javascript" src="/views/front/js/chanyan.js"></script>
                    <!-- 文章页-底部 动态广告位 -->
                    <div class="youdao-fixed-ad" id="detail_ad_bottom"></div>
                </div>
                <div class="col-md-3">
                    <div class="row" id="ad">
                        <!-- 文章页-右侧1 动态广告位 -->
                        <div id="right-1" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad">
                            <div class="youdao-fixed-ad" id="detail_ad_1"> </div>
                        </div>
                        <!-- 文章页-右侧2 动态广告位 -->
                        <div id="right-2" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad">
                            <div class="youdao-fixed-ad" id="detail_ad_2"></div>
                        </div>
                        <!-- 文章页-右侧3 动态广告位 -->
                        <div id="right-3" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad">
                            <div class="youdao-fixed-ad" id="detail_ad_3"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="container">
        <h4 class="pt20 mb15 mt0 border-top">你可能感兴趣的</h4>
        <div id="paradigm-article-related">
            <div class="recommend-post mb30">
                <ul class="widget-links">
                                <li><a href="/article/125.htm"
                                       title="apache 安装linux windows" target="_blank">apache 安装linux windows</a>
                                    <span class="text-muted">墙头上一根草</span>
<a class="tag" taget="_blank" href="/search/apache/1.htm">apache</a><a class="tag" taget="_blank" href="/search/inux/1.htm">inux</a><a class="tag" taget="_blank" href="/search/windows/1.htm">windows</a>
                                </li>
                                <li><a href="/article/252.htm"
                                       title="fill_parent、wrap_content和match_parent的区别" target="_blank">fill_parent、wrap_content和match_parent的区别</a>
                                    <span class="text-muted">Cb123456</span>
<a class="tag" taget="_blank" href="/search/match_parent/1.htm">match_parent</a><a class="tag" taget="_blank" href="/search/fill_parent/1.htm">fill_parent</a>
                                </li>
                                <li><a href="/article/379.htm"
                                       title="网页自适应设计" target="_blank">网页自适应设计</a>
                                    <span class="text-muted">天子之骄</span>
<a class="tag" taget="_blank" href="/search/html/1.htm">html</a><a class="tag" taget="_blank" href="/search/css/1.htm">css</a><a class="tag" taget="_blank" href="/search/%E5%93%8D%E5%BA%94%E5%BC%8F%E8%AE%BE%E8%AE%A1/1.htm">响应式设计</a><a class="tag" taget="_blank" href="/search/%E9%A1%B5%E9%9D%A2%E8%87%AA%E9%80%82%E5%BA%94/1.htm">页面自适应</a>
                                </li>
                                <li><a href="/article/506.htm"
                                       title="[sql server] 分组取最大最小常用sql" target="_blank">[sql server] 分组取最大最小常用sql</a>
                                    <span class="text-muted">一炮送你回车库</span>
<a class="tag" taget="_blank" href="/search/SQL+Server/1.htm">SQL Server</a>
                                </li>
                                <li><a href="/article/633.htm"
                                       title="ImageIO写图片输出到硬盘" target="_blank">ImageIO写图片输出到硬盘</a>
                                    <span class="text-muted">3213213333332132</span>
<a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/image/1.htm">image</a>
                                </li>
                                <li><a href="/article/760.htm"
                                       title="自己的String动态数组" target="_blank">自己的String动态数组</a>
                                    <span class="text-muted">宝剑锋梅花香</span>
<a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84/1.htm">动态数组</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E7%BB%84/1.htm">数组</a>
                                </li>
                                <li><a href="/article/887.htm"
                                       title="pinyin4j工具类" target="_blank">pinyin4j工具类</a>
                                    <span class="text-muted">darkranger</span>
<a class="tag" taget="_blank" href="/search/.net/1.htm">.net</a>
                                </li>
                                <li><a href="/article/1014.htm"
                                       title="StarUML学习笔记----基本概念" target="_blank">StarUML学习笔记----基本概念</a>
                                    <span class="text-muted">aijuans</span>
<a class="tag" taget="_blank" href="/search/UML%E5%BB%BA%E6%A8%A1/1.htm">UML建模</a>
                                </li>
                </ul>
            </div>
        </div>
    </div>

<div>
    <div class="container">
        <div class="indexes">
            <strong>按字母分类:</strong>
            <a href="/tags/A/1.htm" target="_blank">A</a><a href="/tags/B/1.htm" target="_blank">B</a><a href="/tags/C/1.htm" target="_blank">C</a><a
                href="/tags/D/1.htm" target="_blank">D</a><a href="/tags/E/1.htm" target="_blank">E</a><a href="/tags/F/1.htm" target="_blank">F</a><a
                href="/tags/G/1.htm" target="_blank">G</a><a href="/tags/H/1.htm" target="_blank">H</a><a href="/tags/I/1.htm" target="_blank">I</a><a
                href="/tags/J/1.htm" target="_blank">J</a><a href="/tags/K/1.htm" target="_blank">K</a><a href="/tags/L/1.htm" target="_blank">L</a><a
                href="/tags/M/1.htm" target="_blank">M</a><a href="/tags/N/1.htm" target="_blank">N</a><a href="/tags/O/1.htm" target="_blank">O</a><a
                href="/tags/P/1.htm" target="_blank">P</a><a href="/tags/Q/1.htm" target="_blank">Q</a><a href="/tags/R/1.htm" target="_blank">R</a><a
                href="/tags/S/1.htm" target="_blank">S</a><a href="/tags/T/1.htm" target="_blank">T</a><a href="/tags/U/1.htm" target="_blank">U</a><a
                href="/tags/V/1.htm" target="_blank">V</a><a href="/tags/W/1.htm" target="_blank">W</a><a href="/tags/X/1.htm" target="_blank">X</a><a
                href="/tags/Y/1.htm" target="_blank">Y</a><a href="/tags/Z/1.htm" target="_blank">Z</a><a href="/tags/0/1.htm" target="_blank">其他</a>
        </div>
    </div>
</div>
<footer id="footer" class="mb30 mt30">
    <div class="container">
        <div class="footBglm">
            <a target="_blank" href="/">首页</a> -
            <a target="_blank" href="/custom/about.htm">关于我们</a> -
            <a target="_blank" href="/search/Java/1.htm">站内搜索</a> -
            <a target="_blank" href="/sitemap.xml">Sitemap</a> -
            <a target="_blank" href="/custom/delete.htm">侵权投诉</a>
        </div>
        <div class="copyright">版权所有 IT知识库 CopyRight © 2000-2050 IT知识库 IT610.com , All Rights Reserved.
            <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">京ICP备09083238号</a><br>
        </div>
    </div>
</footer>
<!-- 代码高亮 -->
<script type="text/javascript" src="/static/syntaxhighlighter/scripts/shCore.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/scripts/shLegacy.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/scripts/shAutoloader.js"></script>
<link type="text/css" rel="stylesheet" href="/static/syntaxhighlighter/styles/shCoreDefault.css"/>
<script type="text/javascript" src="/static/syntaxhighlighter/src/my_start_1.js"></script>





</body>

</html>