python数据科学库

Python的许多吸引力(例如效率,代码可读性和速度)使它成为数据科学爱好者的首选编程语言。 对于想要升级其应用程序功能的数据科学家和机器学习专家而言,Python通常是首选。 (例如,Andrey Bulezyuk使用Python编程语言创建了一个了不起的机器学习应用程序 。)

由于Python的广泛使用,它拥有大量的库,这些库使数据科学家更容易完成复杂的任务,而没有很多编码麻烦。 这是用于数据科学的三大Python库。 如果您想在野外开始自己的职业,请查看他们。

1. NumPy

NumPy (“数值Python”的缩写)是顶级库之一,配备了有用的资源,可帮助数据科学家将Python转变为强大的科学分析和建模工具。 流行的开源库可在BSD许可下获得。 它是用于执行科学计算任务的基础Python库。 NumPy是一个更大的基于Python的开源工具生态系统(称为SciPy)的一部分。

此外,它与其他编程语言(如C / C ++和Fortran)完美集成。 NumPy库的多功能性使其可以轻松,Swift地与广泛的数据库和工具合并。 例如,让我们看看如何使用NumPy(缩写为np )将两个矩阵相乘。

让我们从导入库开始(对于这些示例,我们将使用Jupyter笔记本)。

import numpy as np 
import numpy as np

接下来,让我们使用eye()函数生成具有规定尺寸的单位矩阵。

matrix_one 
     
     = np. 
     
     eye 
     
     ( 
     
     3 
     
     ) 
     
     

matrix_one

这是输出:

array 
     
     ( 
     
     [ 
     
     [ 
     
     1 . 
     
     , 
     
     0 . 
     
     , 
     
     0 . 
     
     ] 
     
     , 
     
     

       
     
     [ 
     
     0 . 
     
     , 
     
     1 . 
     
     , 
     
     0 . 
     
     ] 
     
     , 
     
     

       
     
     [ 
     
     0 . 
     
     , 
     
     0 . 
     
     , 
     
     1 . 
     
     ] 
     
     ] 
     
     )

让我们生成另一个3x3矩阵。

我们将使用arange([起始编号],[终止编号])函数来排列编号。 请注意,函数中的第一个参数是要列出的初始编号,而最后一个编号不包括在生成的结果中。

同样,应用reshape()函数将原始生成的矩阵的尺寸修改为所需的尺寸。 为了使矩阵“可乘”,它们应具有相同的维数。

matrix_two 
     
     = np. 
     
     arange 
     
     ( 
     
     1 
     
     , 
     
     10 
     
     ) . 
     
     reshape 
     
     ( 
     
     3 
     
     , 
     
     3 
     
     ) 
     
     

matrix_two

这是输出:

array 
     
     ( 
     
     [ 
     
     [ 
     
     1 
     
     , 
     
     2 
     
     , 
     
     3 
     
     ] 
     
     , 
     
     

       
     
     [ 
     
     4 
     
     , 
     
     5 
     
     , 
     
     6 
     
     ] 
     
     , 
     
     

       
     
     [ 
     
     7 
     
     , 
     
     8 
     
     , 
     
     9 
     
     ] 
     
     ] 
     
     )

让我们使用dot()函数将两个矩阵相乘。

matrix_multiply 
     
     = np. 
     
     dot 
     
     ( matrix_one 
     
     , matrix_two 
     
     ) 
     
     

matrix_multiply

这是输出:

array 
     
     ( 
     
     [ 
     
     [ 
     
     1 . 
     
     , 
     
     2 . 
     
     , 
     
     3 . 
     
     ] 
     
     , 
     
     

       
     
     [ 
     
     4 . 
     
     , 
     
     5 . 
     
     , 
     
     6 . 
     
     ] 
     
     , 
     
     

       
     
     [ 
     
     7 . 
     
     , 
     
     8 . 
     
     , 
     
     9 . 
     
     ] 
     
     ] 
     
     )

大!

我们成功地将两个矩阵相乘而未使用香草Python。

这是此示例的完整代码:

import numpy 
     
     as np
     
     
#generating a 3 by 3 identity matrix 
     
     

matrix_one 
     
     = np. 
     
     eye 
     
     ( 
     
     3 
     
     ) 
     
     

matrix_one
     
     
#generating another 3 by 3 matrix for multiplication 
     
     

matrix_two 
     
     = np. 
     
     arange 
     
     ( 
     
     1 
     
     , 
     
     10 
     
     ) . 
     
     reshape 
     
     ( 
     
     3 
     
     , 
     
     3 
     
     ) 
     
     

matrix_two
     
     
#multiplying the two arrays 
     
     

matrix_multiply 
     
     = np. 
     
     dot 
     
     ( matrix_one 
     
     , matrix_two 
     
     ) 
     
     

matrix_multiply

2.熊猫

Pandas是另一个很棒的库,可以增强您的Python数据科学技能。 就像NumPy一样,它属于SciPy开源软件家族,可根据BSD免费软件许可使用。

Pandas提供多种功能强大的工具来处理数据结构和执行广泛的数据分析。 该库可以很好地处理不完整,无结构和无序的现实世界数据,并带有用于整形,汇总,分析和可视化数据集的工具。

该库中有三种类型的数据结构:

  • 系列:一维均匀阵列
  • DataFrame:二维,具有异构类型的列
  • 面板:三维尺寸可变数组

例如,让我们看看如何将Panda Python库(缩写为pd )用于执行一些描述性统计计算。

让我们从导入库开始。

import pandas as pd 
import pandas as pd

让我们创建一个系列字典。

d 
     
     = 
     
     { 
     
     'Name' :pd. 
     
     Series 
     
     ( 
     
     [ 
     
     'Alfrick' 
     
     , 
     
     'Michael' 
     
     , 
     
     'Wendy' 
     
     , 
     
     'Paul' 
     
     , 
     
     'Dusan' 
     
     , 
     
     'George' 
     
     , 
     
     'Andreas' 
     
     , 
     
     

   
     
     'Irene' 
     
     , 
     
     'Sagar' 
     
     , 
     
     'Simon' 
     
     , 
     
     'James' 
     
     , 
     
     'Rose' 
     
     ] 
     
     ) 
     
     , 
     
     

   
     
     'Years of Experience' :pd. 
     
     Series 
     
     ( 
     
     [ 
     
     5 
     
     , 
     
     9 
     
     , 
     
     1 
     
     , 
     
     4 
     
     , 
     
     3 
     
     , 
     
     4 
     
     , 
     
     7 
     
     , 
     
     9 
     
     , 
     
     6 
     
     , 
     
     8 
     
     , 
     
     3 
     
     , 
     
     1 
     
     ] 
     
     ) 
     
     , 
     
     

   
     
     'Programming Language' :pd. 
     
     Series 
     
     ( 
     
     [ 
     
     'Python' 
     
     , 
     
     'JavaScript' 
     
     , 
     
     'PHP' 
     
     , 
     
     'C++' 
     
     , 
     
     'Java' 
     
     , 
     
     'Scala' 
     
     , 
     
     'React' 
     
     , 
     
     'Ruby' 
     
     , 
     
     'Angular' 
     
     , 
     
     'PHP' 
     
     , 
     
     'Python' 
     
     , 
     
     'JavaScript' 
     
     ] 
     
     ) 
     
     

    
     
     }

让我们创建一个DataFrame。

df = pd. DataFrame ( d ) 
df = pd. DataFrame ( d )

这是一个漂亮的输出表:

Name Programming Language  Years of Experience
     
     
0   Alfrick               Python                    
     
     5 
     
     
1   Michael           JavaScript                    
     
     9 
     
     
2     Wendy                  PHP                    
     
     1 
     
     
3      Paul                  C++                    
     
     4 
     
     
4     Dusan                 Java                    
     
     3 
     
     
5    George                Scala                    
     
     4 
     
     
6   Andreas                React                    
     
     7 
     
     
7     Irene                 Ruby                    
     
     9 
     
     
8     Sagar              Angular                    
     
     6 
     
     
9     Simon                  PHP                    
     
     8 
     
     
10    James               Python                    
     
     3 
     
     
11     Rose           JavaScript                    
     
     1

这是此示例的完整代码:

import pandas 
     
     as pd
     
     
#creating a dictionary of series 
     
     

d 
     
     = 
     
     { 
     
     'Name' :pd. 
     
     Series 
     
     ( 
     
     [ 
     
     'Alfrick' 
     
     , 
     
     'Michael' 
     
     , 
     
     'Wendy' 
     
     , 
     
     'Paul' 
     
     , 
     
     'Dusan' 
     
     , 
     
     'George' 
     
     , 
     
     'Andreas' 
     
     , 
     
     

   
     
     'Irene' 
     
     , 
     
     'Sagar' 
     
     , 
     
     'Simon' 
     
     , 
     
     'James' 
     
     , 
     
     'Rose' 
     
     ] 
     
     ) 
     
     , 
     
     

   
     
     'Years of Experience' :pd. 
     
     Series 
     
     ( 
     
     [ 
     
     5 
     
     , 
     
     9 
     
     , 
     
     1 
     
     , 
     
     4 
     
     , 
     
     3 
     
     , 
     
     4 
     
     , 
     
     7 
     
     , 
     
     9 
     
     , 
     
     6 
     
     , 
     
     8 
     
     , 
     
     3 
     
     , 
     
     1 
     
     ] 
     
     ) 
     
     , 
     
     

   
     
     'Programming Language' :pd. 
     
     Series 
     
     ( 
     
     [ 
     
     'Python' 
     
     , 
     
     'JavaScript' 
     
     , 
     
     'PHP' 
     
     , 
     
     'C++' 
     
     , 
     
     'Java' 
     
     , 
     
     'Scala' 
     
     , 
     
     'React' 
     
     , 
     
     'Ruby' 
     
     , 
     
     'Angular' 
     
     , 
     
     'PHP' 
     
     , 
     
     'Python' 
     
     , 
     
     'JavaScript' 
     
     ] 
     
     ) 
     
     

    
     
     } 
     
     

#Create a DataFrame 
     
     

df 
     
     = pd. 
     
     DataFrame 
     
     ( d 
     
     ) 
     
     
print 
     
     ( df 
     
     )

3. Matplotlib

Matplotlib也是SciPy核心软件包的一部分,并根据BSD许可证提供。 它是一个流行的Python科学库,用于生成简单而强大的可视化文件。 您可以将Python框架用于数据科学,以生成创意图形,图表,直方图以及其他形状和图形,而不必担心编写许多代码行。 例如,让我们看看如何使用Matplotlib库创建简单的条形图。

让我们从导入库开始。

from matplotlib import pyplot as plt 
from matplotlib import pyplot as plt

让我们为x轴和y轴生成值。

x 
     
     = 
     
     [ 
     
     2 
     
     , 
     
     4 
     
     , 
     
     6 
     
     , 
     
     8 
     
     , 
     
     10 
     
     ] 
     
     

y 
     
     = 
     
     [ 
     
     10 
     
     , 
     
     11 
     
     , 
     
     6 
     
     , 
     
     7 
     
     , 
     
     4 
     
     ]

我们将其称为绘制条形图的函数。

plt. bar ( x , y ) 
plt. bar ( x , y )

让我们来展示情节。

plt. show ( ) 
plt. show ( )

这是条形图:

python大数计算库 python 大数库_数据分析

这是此示例的完整代码:

#importing Matplotlib Python library  
     
     
from matplotlib 
     
     import pyplot 
     
     as plt
     
     
#same as import matplotlib.pyplot as plt 
     
     

 
     
     
#generating values for x-axis  
     
     

x 
     
     = 
     
     [ 
     
     2 
     
     , 
     
     4 
     
     , 
     
     6 
     
     , 
     
     8 
     
     , 
     
     10 
     
     ] 
     
     

 
     
     
#generating vaues for y-axis  
     
     

y 
     
     = 
     
     [ 
     
     10 
     
     , 
     
     11 
     
     , 
     
     6 
     
     , 
     
     7 
     
     , 
     
     4 
     
     ] 
     
     

 
     
     
#calling function for plotting the bar chart 
     
     

plt. 
     
     bar 
     
     ( x 
     
     , y 
     
     ) 
     
     

 
     
     
#showing the plot 
     
     

plt. 
     
     show 
     
     ( 
     
     )

结语

Python编程语言在数据处理和准备方面一直做得很好,但是在复杂的科学数据分析和建模方面却做得不好。 数据科学的顶级Python框架有助于填补这一空白,使您能够进行复杂的数学计算并创建能够理解数据的复杂模型。

您还知道其他哪些Python数据挖掘库? 您对他们有什么经验? 请在下面分享你的评论。

翻译自: https://opensource.com/article/18/9/top-3-python-libraries-data-science

python数据科学库