带有Python的AI –入门

带有Python的AI –入门 (AI with Python – Getting Started)

In this chapter, we will learn how to get started with Python. We will also understand how Python helps for Artificial Intelligence.

在本章中,我们将学习如何开始使用Python。 我们还将了解Python如何帮助人工智能。

为什么要使用Python进行AI (Why Python for AI)

Artificial intelligence is considered to be the trending technology of the future. Already there are a number of applications made on it. Due to this, many companies and researchers are taking interest in it. But the main question that arises here is that in which programming language can these AI applications be developed? There are various programming languages like Lisp, Prolog, C++, Java and Python, which can be used for developing applications of AI. Among them, Python programming language gains a huge popularity and the reasons are as follows −

人工智能被认为是未来的趋势技术。 已经有很多应用程序在上面。 因此,许多公司和研究人员对此感兴趣。 但是这里出现的主要问题是,可以使用哪种编程语言开发这些AI应用程序? 有多种编程语言,例如Lisp,Prolog,C ++,Java和Python,可用于开发AI应用程序。 其中,Python编程语言非常流行,其原因如下-

简单的语法和更少的编码 (Simple syntax & less coding)

Python involves very less coding and simple syntax among other programming languages which can be used for developing AI applications. Due to this feature, the testing can be easier and we can focus more on programming.

在其他可用于开发AI应用程序的编程语言中,Python涉及的编码和语法简单得多。 由于此功能,测试可以更轻松,我们可以将更多精力放在编程上。

内置的AI项目库 (Inbuilt libraries for AI projects)

A major advantage for using Python for AI is that it comes with inbuilt libraries. Python has libraries for almost all kinds of AI projects. For example, NumPy, SciPy, matplotlib, nltk, SimpleAI are some the important inbuilt libraries of Python.

将Python用于AI的一个主要优点是它带有内置库。 Python拥有适用于几乎所有AI项目的库。 例如, NumPy,SciPy,matplotlib,nltk,SimpleAI是一些重要的Python内置库。

  • Open source − Python is an open source programming language. This makes it widely popular in the community.

    开源 -Python是一种开源编程语言。 这使其在社区中广受欢迎。

  • Can be used for broad range of programming − Python can be used for a broad range of programming tasks like small shell script to enterprise web applications. This is another reason Python is suitable for AI projects.

    可以用于广泛的编程 -Python可以用于广泛的编程任务,例如从小型Shell脚本到企业Web应用程序。 这是Python适用于AI项目的另一个原因。

Python的功能 (Features of Python)

Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages. Python's features include the following −

Python是一种高级,解释性,交互式和面向对象的脚本语言。 Python的设计具有很高的可读性。 与其他语言使用标点符号一样,它经常使用英语关键字,并且其语法结构比其他语言更少。 Python的功能包括以下内容-

  • Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly.

    易于学习 -Python具有少量关键字,简单的结构和清晰定义的语法。 这使学生可以快速掌握语言。

  • Easy-to-read − Python code is more clearly defined and visible to the eyes.

    易于阅读 -Python代码更清晰地定义并且对眼睛可见。

  • Easy-to-maintain − Python's source code is fairly easy-to-maintain.

    易于维护 -Python的源代码非常易于维护。

  • A broad standard library − Python's bulk of the library is very portable and cross-platform compatible on UNIX, Windows, and Macintosh.

    广泛的标准库 -Python的大部分库在UNIX,Windows和Macintosh上具有很高的可移植性和跨平台兼容性。

  • Interactive Mode − Python has support for an interactive mode which allows interactive testing and debugging of snippets of code.

    交互模式 -Python支持交互模式,该模式允许交互测试和调试代码片段。

  • Portable − Python can run on a wide variety of hardware platforms and has the same interface on all platforms.

    可移植 -Python可在多种硬件平台上运行,并且在所有平台上具有相同的接口。

  • Extendable − We can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient.

    可扩展 -我们可以向Python解释器添加低级模块。 这些模块使程序员能够添加或自定义其工具,从而提高效率。

  • Databases − Python provides interfaces to all major commercial databases.

    数据库 -Python提供了到所有主要商业数据库的接口。

  • GUI Programming − Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of Unix.

    GUI编程 -Python支持可创建并移植到许多系统调用,库和Windows系统(例如Windows MFC,Macintosh和Unix的X Window系统)的GUI应用程序。

  • Scalable − Python provides a better structure and support for large programs than shell scripting.

    可扩展-Python比Shell脚本为大型程序提供更好的结构和支持。

Python的重要功能 (Important features of Python)

Let us now consider the following important features of Python −

现在让我们考虑一下Python的以下重要功能-

  • It supports functional and structured programming methods as well as OOP.

    它支持功能性和结构化编程方法以及OOP。

  • It can be used as a scripting language or can be compiled to byte-code for building large applications.

    它可以用作脚本语言,也可以编译为字节码以构建大型应用程序。

  • It provides very high-level dynamic data types and supports dynamic type checking.

    它提供了非常高级的动态数据类型,并支持动态类型检查。

  • It supports automatic garbage collection.

    它支持自动垃圾收集。

  • It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.

    它可以轻松地与C,C ++,COM,ActiveX,CORBA和Java集成。

安装Python (Installing Python)

Python distribution is available for a large number of platforms. You need to download only the binary code applicable for your platform and install Python.

Python发行版可用于许多平台。 您只需要下载适用于您的平台的二进制代码并安装Python。

If the binary code for your platform is not available, you need a C compiler to compile the source code manually. Compiling the source code offers more flexibility in terms of choice of features that you require in your installation.

如果平台的二进制代码不可用,则需要C编译器来手动编译源代码。 在安装所需的功能选择方面,编译源代码可提供更大的灵活性。

Here is a quick overview of installing Python on various platforms −

这是在各种平台上安装Python的快速概述-

Unix和Linux安装 (Unix and Linux Installation)

Follow these steps to install Python on Unix/Linux machine.

请按照以下步骤在Unix / Linux计算机上安装Python。

  • Open a Web browser and go to https://www.python.org/downloads

    打开网络浏览器并转到 https://www.python.org/downloads

  • Follow the link to download zipped source code available for Unix/Linux.

    单击链接下载适用于Unix / Linux的压缩源代码。

  • Download and extract files.

    下载并解压缩文件。

  • Editing the Modules/Setup file if you want to customize some options.

    如果要自定义某些选项,请编辑“ 模块/设置”文件。

  • run ./configure script

    运行./configure脚本

  • make

    使

  • make install

    进行安装

This installs Python at the standard location /usr/local/bin and its libraries at /usr/local/lib/pythonXX where XX is the version of Python.

这会将Python安装在标准位置/ usr / local / bin中,并将其库安装在/ usr / local / lib / pythonXX中 ,其中XX是Python的版本。

Windows安装 (Windows Installation)

Follow these steps to install Python on Windows machine.

请按照以下步骤在Windows计算机上安装Python。

  • Open a Web browser and go to https://www.python.org/downloads

    打开网络浏览器并转到 https://www.python.org/downloads

  • Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you need to install.

    单击Windows安装程序python-XYZ .msi文件的链接,其中XYZ是您需要安装的版本。

  • To use this installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Save the installer file to your local machine and then run it to find out if your machine supports MSI.

    要使用此安装程序python-XYZ .msi,Windows系统必须支持Microsoft Installer 2.0。 将安装程序文件保存到本地计算机,然后运行它以查找您的计算机是否支持MSI。

  • Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just accept the default settings and wait until the install is finished.

    运行下载的文件。 这将弹出Python安装向导,该向导非常易于使用。 只需接受默认设置,然后等待安装完成即可。

Macintosh安装 (Macintosh Installation)

If you are on Mac OS X, it is recommended that you use Homebrew to install Python 3. It is a great package installer for Mac OS X and it is really easy to use. If you don't have Homebrew, you can install it using the following command −

如果您使用的是Mac OS X,建议您使用Homebrew来安装Python3。它是Mac OS X的出色软件包安装程序,非常易于使用。 如果没有Homebrew,可以使用以下命令进行安装-


$ ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install)"

We can update the package manager with the command below −

我们可以使用以下命令更新软件包管理器-


$ brew update

Now run the following command to install Python3 on your system −

现在运行以下命令在系统上安装Python3-


$ brew install python3

设置路径 (Setting up PATH)

Programs and other executable files can be in many directories, so operating systems provide a search path that lists the directories that the OS searches for executables.

程序和其他可执行文件可以在许多目录中,因此操作系统提供了搜索路径,该路径列出了OS搜索可执行文件的目录。

The path is stored in an environment variable, which is a named string maintained by the operating system. This variable contains information available to the command shell and other programs.

路径存储在环境变量中,该变量是操作系统维护的命名字符串。 此变量包含命令外壳和其他程序可用的信息。

The path variable is named as PATH in Unix or Path in Windows (Unix is case-sensitive; Windows is not).

path变量在Unix中被命名为PATH,在Windows中被命名为Path(Unix区分大小写; Windows不区分大小写)。

In Mac OS, the installer handles the path details. To invoke the Python interpreter from any particular directory, you must add the Python directory to your path.

在Mac OS中,安装程序将处理路径详细信息。 要从任何特定目录调用Python解释器,必须将Python目录添加到路径中。

在Unix / Linux上设置路径 (Setting Path at Unix/Linux)

To add the Python directory to the path for a particular session in Unix −

要将Python目录添加到Unix中特定会话的路径-

  • In the csh shell

    在csh shell中

    Type setenv PATH "$PATH:/usr/local/bin/python" and press Enter.

    输入setenv PATH“ $ PATH:/ usr / local / bin / python”并按Enter

  • In the bash shell (Linux)

    在bash shell(Linux)中

    Type export ATH = "$PATH:/usr/local/bin/python" and press Enter.

    输入export ATH =“ $ PATH:/ usr / local / bin / python”并按Enter

  • In the sh or ksh shell

    在sh或ksh shell中

    Type PATH = "$PATH:/usr/local/bin/python" and press Enter.

    输入PATH =“ $ PATH:/ usr / local / bin / python”并按Enter键

Note − /usr/local/bin/python is the path of the Python directory.

注意 -/ usr / local / bin / python是Python目录的路径。

在Windows上设置路径 (Setting Path at Windows)

To add the Python directory to the path for a particular session in Windows −

要将Python目录添加到Windows中特定会话的路径-

  • At the command prompt − type path %path%;C:\Python and press Enter.

    在命令提示符下 -键入path%path%; C:\ Python并按Enter

Note − C:\Python is the path of the Python directory.

注意 -C:\ Python是Python目录的路径。

运行Python (Running Python)

Let us now see the different ways to run Python. The ways are described below −

现在让我们看看运行Python的不同方法。 方式描述如下-

互动翻译 (Interactive Interpreter)

We can start Python from Unix, DOS, or any other system that provides you a command-line interpreter or shell window.

我们可以从Unix,DOS或任何其他提供命令行解释器或Shell窗口的系统中启动Python。

  • Enter python at the command line.

    在命令行输入python

  • Start coding right away in the interactive interpreter.

    立即在交互式解释器中开始编码。


$python # Unix/Linux

or

要么


python% # Unix/Linux

or

要么


C:> python # Windows/DOS

Here is the list of all the available command line options −

这是所有可用命令行选项的列表-

S.No.Option & Description
1

-d

It provides debug output.

2

-o

It generates optimized bytecode (resulting in .pyo files).

3

-S

Do not run import site to look for Python paths on startup.

4

-v

Verbose output (detailed trace on import statements).

5

-x

Disables class-based built-in exceptions (just use strings); obsolete starting with version 1.6.

6

-c cmd

Runs Python script sent in as cmd string.

7

File

Run Python script from given file.

序号 选项和说明
1个

-d

它提供调试输出。

2

-o

它生成优化的字节码(产生.pyo文件)。

3

-S

不要在启动时运行导入站点来查找Python路径。

4

-v

详细输出(对import语句的详细跟踪)。

5

-X

禁用基于类的内置异常(仅使用字符串); 从1.6版开始已过时。

6

-c cmd

运行以cmd字符串形式发送的Python脚本。

7

文件

从给定文件运行Python脚本。

命令行脚本 (Script from the Command-line)

A Python script can be executed at the command line by invoking the interpreter on your application, as in the following −

通过在应用程序上调用解释器,可以在命令行执行Python脚本,如下所示-


$python script.py # Unix/Linux

or,

要么,


python% script.py # Unix/Linux

or,

要么,


C:> python script.py # Windows/DOS

Note − Be sure the file permission mode allows execution.

注意 -确保文件许可模式允许执行。

集成开发环境 (Integrated Development Environment)

You can run Python from a Graphical User Interface (GUI) environment as well, if you have a GUI application on your system that supports Python.

如果您的系统上有支持Python的GUI应用程序,则也可以从图形用户界面(GUI)环境中运行Python。

  • Unix − IDLE is the very first Unix IDE for Python.

    Unix -IDLE是第一个用于Python的Unix IDE。

  • Windows − PythonWin is the first Windows interface for Python and is an IDE with a GUI.

    Windows -PythonWin是Python的第一个Windows界面,并且是带有GUI的IDE。

  • Macintosh − The Macintosh version of Python along with the IDLE IDE is available from the main website, downloadable as either MacBinary or BinHex'd files.

    Macintosh-可以从主网站上获得Macintosh版本的Python和IDLE IDE,可以将其下载为MacBinary或BinHex文件。

If you are not able to set up the environment properly, then you can take help from your system admin. Make sure the Python environment is properly set up and working perfectly fine.

如果您无法正确设置环境,则可以从系统管理员那里获取帮助。 确保正确设置了Python环境,并且工作正常。

We can also use another Python platform called Anaconda. It includes hundreds of popular data science packages and the conda package and virtual environment manager for Windows, Linux and MacOS. You can download it as per your operating system from the link https://www.anaconda.com/download/.

我们还可以使用另一个名为Anaconda的Python平台。 它包括数百个流行的数据科学软件包以及用于Windows,Linux和MacOS的conda软件包和虚拟环境管理器。 您可以根据操作系统从https://www.anaconda.com/download/下载

For this tutorial we are using Python 3.6.3 version on MS Windows.

对于本教程,我们在MS Windows上使用Python 3.6.3版本。

翻译自: https://www.tutorialspoint.com/artificial_intelligence_with_python/artificial_intelligence_with_python_getting_started.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值