Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2020-02-29 12:27:47 +08:00
commit 0d7f8738ca
5 changed files with 1020 additions and 743 deletions

View File

@ -1,116 +1,99 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-11942-1.html)
[#]: subject: (How to get MongoDB Server on Fedora)
[#]: via: (https://fedoramagazine.org/how-to-get-mongodb-server-on-fedora/)
[#]: author: (Honza Horak https://fedoramagazine.org/author/hhorak/)
如何在 Fedora 上获取 MongoDB 服务器
如何在 Fedora 上安装 MongoDB 服务器
======
![][1]
Mongo来自 “humongous”)是一个高性能,开源,无模式的面向文档的数据库,它是最受欢迎的 [NoSQL][2] 数据库之一。它使用 JSON 作为文档格式,并且可以在多个服务器节点之间进行扩展和复制。
Mongo来自 “humongous” —— 巨大的)是一个高性能、开源、无模式的、面向文档的数据库,它是最受欢迎的 [NoSQL][2] 数据库之一。它使用 JSON 作为文档格式,并且可以在多个服务器节点之间进行扩展和复制。
### 有关许可证更改的故事
上游 MongoD 决定更改服务器代码的许可证已经一年多了。先前的许可证是 GNU Affero General Public License v3AGPLv3。但是上游写了一个新许可证,为了使运行 MongoDB 即服务的公司回馈社区。新许可证称为 Server Side Public LicenseSSPLv1关于这个及其原理的更多说明请参见[MongoDB SSPL FAQ][3]。
MongoDB 上游决定更改服务器代码的许可证已经一年多了。先前的许可证是 GNU Affero General Public License v3AGPLv3。但是上游公司写了一个新许可证,旨在使运行 MongoDB 即服务的公司可以回馈社区。新许可证称为 Server Side Public LicenseSSPLv1关于这个举措及其解释的更多说明请参见 [MongoDB SSPL FAQ][3]。
Fedora 一直只包含自由软件。当 SSPL 发布后Fedora [确定][4]它并不是自由软件许可。许可证更改日期2018 年 10 月)之前发布的所有 MongoDB 版本都可保留在 Fedora 中,但之后再也不更新软件包会带来安全问题。因此,从 Fedora 30 开始Fedora 社区决定完全[移除 MongoDB 服务器][5]。
Fedora 一直只包含自由软件。当 SSPL 发布后Fedora [确定][4]它并不是自由软件许可。许可证更改日期2018 年 10 月)之前发布的所有 MongoDB 版本都可保留在 Fedora 中,但之后再也不更新软件包会带来安全问题。因此,从 Fedora 30 开始Fedora 社区决定完全[移除 MongoDB 服务器][5]。
### 开发人员还有哪些选择?
是的,还有替代方案,例如 PostgreSQL 在最新版本中也支持 JSON它可以在无法再使用 MongoDB 的情况下使用它。使用 JSONB 类型,索引在 PostgreSQL 中可以很好地工作,其性能可与 MongoDB 媲美,甚至不会受到 ACID 的影响。
开发人员可能选择 MongoDB 的技术原因并未随许可证而改变因此许多人仍想使用它。重要的是要意识到SSPL 许可证仅更改仅针对 MongoDB 服务器。MongoDB 上游还开发了其他项目,例如 MongoDB 工具C 和 C++ 客户端库以及用于各种动态语言的连接器,这些项目在客户端(要通过网络与服务器通信的应用中)使用。由于这些包的许可证是自由的(主要是 Apache 许可证),因此它们保留在 Fedora 仓库中,因此用户可以将其用于应用开发。
开发人员可能选择 MongoDB 的技术原因并未随许可证而改变因此许多人仍想使用它。重要的是要意识到SSPL 许可证仅更改仅针对 MongoDB 服务器。MongoDB 上游还开发了其他项目,例如 MongoDB 工具、C 和 C++ 客户端库以及用于各种动态语言的连接器,这些项目在客户端使用(通过网络与服务器通信的应用中)。由于这些包的许可证人保持自由(主要是 Apache 许可证),因此它们保留在 Fedora 仓库中,因此用户可以将其用于应用开发。
唯一的变化实际是服务器包本身,它已从 Fedora 仓库中完全删除。让我们看看 Fedora 用户可以如何获取非自由的包。
唯一的变化实际是服务器软件包本身,它已从 Fedora 仓库中完全删除。让我们看看 Fedora 用户可以如何获取非自由的包。
### 如何从上游安装 MongoDB 服务器
当 Fedora 用户想要安装 MongoDB 服务器时,他们需要直接向上游获取 MongoDB。但是上游不为 Fedora 提供 RPM 包。相反MongoDB 服务器可以获取源码 tarball用户需要自己进行编译这需要一些开发知识或者 Fedora 用户可以使用一些兼容的包。在兼容的选项中,最好的选择是 RHEL-8 RPM。以下步骤描述了如何安装它们以及如何启动守护进程。
#### 1\. 使用上游 RPM 创建仓库RHEL-8 构建)
```
#### 1、使用上游 RPM 创建仓库RHEL-8 构建)
```
$ sudo cat > /etc/yum.repos.d/mongodb.repo <<EOF
$ sudo cat > /etc/yum.repos.d/mongodb.repo >>EOF
[mongodb-upstream]
name=MongoDB Upstream Repository
baseurl=<https://repo.mongodb.org/yum/redhat/8Server/mongodb-org/4.2/x86\_64/>
baseurl=https://repo.mongodb.org/yum/redhat/8Server/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=<https://www.mongodb.org/static/pgp/server-4.2.asc>
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
EOF
```
```
#### 2\. 安装元软件包,来拉取服务器和工具包
```
#### 2、安装元软件包来拉取服务器和工具包
```
$ sudo dnf install mongodb-org
&amp;lt;snipped&gt;
......
Installed:
  mongodb-org-4.2.3-1.el8.x86_64           mongodb-org-mongos-4.2.3-1.el8.x86_64  
  mongodb-org-server-4.2.3-1.el8.x86_64    mongodb-org-shell-4.2.3-1.el8.x86_64
  mongodb-org-tools-4.2.3-1.el8.x86_64          
mongodb-org-4.2.3-1.el8.x86_64 mongodb-org-mongos-4.2.3-1.el8.x86_64
mongodb-org-server-4.2.3-1.el8.x86_64 mongodb-org-shell-4.2.3-1.el8.x86_64
mongodb-org-tools-4.2.3-1.el8.x86_64
Complete!
```
```
#### 3\. 启动 MongoDB 守护进程
```
#### 3、启动 MongoDB 守护进程
```
$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-02-08 12:33:45 EST; 2s ago
     Docs: <https://docs.mongodb.org/manual>
  Process: 15768 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 15769 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 15770 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 15771 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 15773 (mongod)
   Memory: 70.4M
      CPU: 611ms
   CGroup: /system.slice/mongod.service
           └─15773 /usr/bin/mongod -f /etc/mongod.conf
```
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2020-02-08 12:33:45 EST; 2s ago
Docs: https://docs.mongodb.org/manual
Process: 15768 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 15769 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 15770 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 15771 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 15773 (mongod)
Memory: 70.4M
CPU: 611ms
CGroup: /system.slice/mongod.service
```
#### 4\. 通过 mongo shell 连接服务器来验证是否运行
```
#### 4、通过 mongo shell 连接服务器来验证是否运行
```
$ mongo
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&amp;amp;gssapiServiceName=mongodb
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&amp;gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("20b6e61f-c7cc-4e9b-a25e-5e306d60482f") }
MongoDB server version: 4.2.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    <http://docs.mongodb.org/>
\---
&gt; _
http://docs.mongodb.org/
---
```
```
就是这样了。如你所见RHEL-8 包完美兼容,只要 Fedora 包还与 RHEL-8 兼容,它就应该会一直兼容。 请注意,在使用时必须遵守 SSPLv1 许可证。
就是这样了。如你所见RHEL-8 包完美兼容,只要 Fedora 包还与 RHEL-8 兼容,它就应该会一直兼容。请注意,在使用时必须遵守 SSPLv1 许可证。
--------------------------------------------------------------------------------
@ -119,7 +102,7 @@ via: https://fedoramagazine.org/how-to-get-mongodb-server-on-fedora/
作者:[Honza Horak][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -0,0 +1,481 @@
[#]: collector: (lujun9972)
[#]: translator: (heguangzhi)
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-11943-1.html)
[#]: subject: (Using Python and GNU Octave to plot data)
[#]: via: (https://opensource.com/article/20/2/python-gnu-octave-data-science)
[#]: author: (Cristiano L. Fontana https://opensource.com/users/cristianofontana)
使用 Python 和 GNU Octave 绘制数据
======
> 了解如何使用 Python 和 GNU Octave 完成一项常见的数据科学任务。
![](https://img.linux.net.cn/data/attachment/album/202002/29/114750tr7qykssk90yrvyz.jpg)
数据科学是跨越编程语言的知识领域。有些语言以解决这一领域的问题而闻名,而另一些则鲜为人知。这篇文章将帮助你熟悉用一些流行的语言完成数据科学的工作。
### 选择 Python 和 GNU Octave 做数据科学工作
我经常尝试学习一种新的编程语言。为什么?这既有对旧方式的厌倦,也有对新方式的好奇。当我开始学习编程时,我唯一知道的语言是 C 语言。那些年的编程生涯既艰难又危险,因为我必须手动分配内存、管理指针、并记得释放内存。
后来一个朋友建议我试试 Python现在我的编程生活变得轻松多了。虽然程序运行变得慢多了但我不必通过编写分析软件来受苦了。然而我很快就意识到每种语言都有比其它语言更适合自己的应用场景。后来我学习了一些其它语言每种语言都给我带来了一些新的启发。发现新的编程风格让我可以将一些解决方案移植到其他语言中这样一切都变得有趣多了。
为了对一种新的编程语言(及其文档)有所了解,我总是从编写一些执行我熟悉的任务的示例程序开始。为此,我将解释如何用 Python 和 GNU Octave 编写一个程序来完成一个你可以归类为数据科学的特殊任务。如果你已经熟悉其中一种语言,从它开始,然后通过其他语言寻找相似之处和不同之处。这篇文章并不是对编程语言的详尽比较,只是一个小小的展示。
所有的程序都应该在[命令行][2]上运行,而不是用[图形用户界面][3]GUI。完整的例子可以在 [polyglot_fit 存储库][4]中找到。
### 编程任务
你将在本系列中编写的程序:
* 从 [CSV 文件][5]中读取数据
* 用直线插入数据(例如 `f(x)=m ⋅ x + q`
* 将结果生成图像文件
这是许多数据科学家遇到的常见情况。示例数据是 [Anscombe 的四重奏][6]的第一组,如下表所示。这是一组人工构建的数据,当用直线拟合时会给出相同的结果,但是它们的曲线非常不同。数据文件是一个文本文件,以制表符作为列分隔符,开头几行作为标题。此任务将仅使用第一组(即前两列)。
### Python 方式
[Python][7] 是一种通用编程语言,是当今最流行的语言之一(依据 [TIOBE 指数][8]、[RedMonk 编程语言排名][9]、[编程语言流行指数][10]、[GitHub Octoverse 状态][11]和其他来源的调查结果)。它是一种[解释型语言][12];因此,源代码由执行该指令的程序读取和评估。它有一个全面的[标准库][13]并且总体上非常好用(我对这最后一句话没有证据;这只是我的拙见)。
#### 安装
要使用 Python 开发,你需要解释器和一些库。最低要求是:
* [NumPy][14] 用于简化数组和矩阵的操作
* [SciPy][15] 用于数据科学
* [Matplotlib][16] 用于绘图
在 [Fedora][17] 安装它们是很容易的:
```
sudo dnf install python3 python3-numpy python3-scipy python3-matplotlib
```
#### 代码注释
在 Python中[注释][18]是通过在行首添加一个 `#` 来实现的,该行的其余部分将被解释器丢弃:
```
# 这是被解释器忽略的注释。
```
[fitting_python.py][19] 示例使用注释在源代码中插入许可证信息,第一行是[特殊注释][20],它允许该脚本在命令行上执行:
```
#!/usr/bin/env python3
```
这一行通知命令行解释器,该脚本需要由程序 `python3` 执行。
#### 需要的库
在 Python 中,库和模块可以作为一个对象导入(如示例中的第一行),其中包含库的所有函数和成员。可以通过使用 `as` 方式用自定义标签重命名它们:
```
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
```
你也可以决定只导入一个子模块(如第二行和第三行)。语法有两个(基本上)等效的方式:`import module.submodule` 和 `from module import submodule`
#### 定义变量
Python 的变量是在第一次赋值时被声明的:
```
input_file_name = "anscombe.csv"
delimiter = "\t"
skip_header = 3
column_x = 0
column_y = 1
```
变量类型由分配给变量的值推断。没有具有常量值的变量,除非它们在模块中声明并且只能被读取。习惯上,不应被修改的变量应该用大写字母命名。
#### 打印输出
通过命令行运行程序意味着输出只能打印在终端上。Python 有 [print()][21] 函数,默认情况下,该函数打印其参数,并在输出的末尾添加一个换行符:
```
print("#### Anscombe's first set with Python ####")
```
在 Python 中,可以将 `print()` 函数与[字符串类][23]的[格式化能力][22]相结合。字符串具有`format` 方法,可用于向字符串本身添加一些格式化文本。例如,可以添加格式化的浮点数,例如:
```
print("Slope: {:f}".format(slope))
```
#### 读取数据
使用 NumPy 和函数 [genfromtxt()][24] 读取 CSV 文件非常容易,该函数生成 [NumPy 数组][25]
```
data = np.genfromtxt(input_file_name, delimiter = delimiter, skip_header = skip_header)
```
在 Python 中,一个函数可以有数量可变的参数,你可以通过指定所需的参数来传递一个参数的子集。数组是非常强大的矩阵状对象,可以很容易地分割成更小的数组:
```
x = data[:, column_x]
y = data[:, column_y]
```
冒号选择整个范围,也可以用来选择子范围。例如,要选择数组的前两行,可以使用:
```
first_two_rows = data[0:1, :]
```
#### 拟合数据
SciPy 提供了方便的数据拟合功能,例如 [linregress()][26] 功能。该函数提供了一些与拟合相关的重要值,如斜率、截距和两个数据集的相关系数:
```
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
print("Slope: {:f}".format(slope))
print("Intercept: {:f}".format(intercept))
print("Correlation coefficient: {:f}".format(r_value))
```
因为 `linregress()` 提供了几条信息,所以结果可以同时保存到几个变量中。
#### 绘图
Matplotlib 库仅仅绘制数据点,因此,你应该定义要绘制的点的坐标。已经定义了 `x``y` 数组,所以你可以直接绘制它们,但是你还需要代表直线的数据点。
```
fit_x = np.linspace(x.min() - 1, x.max() + 1, 100)
```
[linspace()][27] 函数可以方便地在两个值之间生成一组等距值。利用强大的 NumPy 数组可以轻松计算纵坐标,该数组可以像普通数值变量一样在公式中使用:
```
fit_y = slope * fit_x + intercept
```
该公式在数组中逐元素应用;因此,结果在初始数组中具有相同数量的条目。
要绘图,首先,定义一个包含所有图形的[图形对象][28]
```
fig_width = 7 #inch
fig_height = fig_width / 16 * 9 #inch
fig_dpi = 100
fig = plt.figure(figsize = (fig_width, fig_height), dpi = fig_dpi)
```
一个图形可以画几个图;在 Matplotlib 中,这些图被称为[轴][29]。本示例定义一个单轴对象来绘制数据点:
```
ax = fig.add_subplot(111)
ax.plot(fit_x, fit_y, label = "Fit", linestyle = '-')
ax.plot(x, y, label = "Data", marker = '.', linestyle = '')
ax.legend()
ax.set_xlim(min(x) - 1, max(x) + 1)
ax.set_ylim(min(y) - 1, max(y) + 1)
ax.set_xlabel('x')
ax.set_ylabel('y')
```
将该图保存到 [PNG 图形文件][30]中,有:
```
fig.savefig('fit_python.png')
```
如果要显示(而不是保存)该绘图,请调用:
```
plt.show()
```
此示例引用了绘图部分中使用的所有对象:它定义了对象 `fig` 和对象 `ax`。这在技术上是不必要的,因为 `plt` 对象可以直接用于绘制数据集。《[Matplotlib 教程][31]》展示了这样一个接口:
```
plt.plot(fit_x, fit_y)
```
坦率地说,我不喜欢这种方法,因为它隐藏了各种对象之间发生的重要交互。不幸的是,有时[官方的例子][32]有点令人困惑,因为他们倾向于使用不同的方法。在这个简单的例子中,引用图形对象是不必要的,但是在更复杂的例子中(例如在图形用户界面中嵌入图形时),引用图形对象就变得很重要了。
#### 结果
命令行输入:
```
#### Anscombe's first set with Python ####
Slope: 0.500091
Intercept: 3.000091
Correlation coefficient: 0.816421
```
这是 Matplotlib 产生的图像:
![Plot and fit of the dataset obtained with Python][33]
### GNU Octave 方式
[GNU Octave][34] 语言主要用于数值计算。它提供了一个简单的操作向量和矩阵的语法,并且有一些强大的绘图工具。这是一种像 Python 一样的解释语言。由于 Octave 的语法[几乎兼容][35] [MATLAB][36],它经常被描述为一个替代 MATLAB 的免费方案。Octave 没有被列为最流行的编程语言,而 MATLAB 则是,所以 Octave 在某种意义上是相当流行的。MATLAB 早于 NumPy我觉得它是受到了前者的启发。当你看这个例子时你会看到相似之处。
#### 安装
[fitting_octave.m][37] 的例子只需要基本的 Octave 包,在 Fedora 中安装相当简单:
```
sudo dnf install octave
```
#### 代码注释
在 Octave 中,你可以用百分比符号(``%`)为代码添加注释,如果不需要与 MATLAB 兼容,你也可以使用 `#`。使用 `#` 的选项允许你编写像 Python 示例一样的特殊注释行,以便直接在命令行上执行脚本。
#### 必要的库
本例中使用的所有内容都包含在基本包中,因此你不需要加载任何新的库。如果你需要一个库,[语法][38]是 `pkg load module`。该命令将模块的功能添加到可用功能列表中。在这方面Python 具有更大的灵活性。
#### 定义变量
变量的定义与 Python 的语法基本相同:
```
input_file_name = "anscombe.csv";
delimiter = "\t";
skip_header = 3;
column_x = 1;
column_y = 2;
```
请注意,行尾有一个分号;这不是必需的,但是它会抑制该行结果的输出。如果没有分号,解释器将打印表达式的结果:
```
octave:1> input_file_name = "anscombe.csv"
input_file_name = anscombe.csv
octave:2> sqrt(2)
ans = 1.4142
```
#### 打印输出结果
强大的函数 [printf()][39] 是用来在终端上打印的。与 Python 不同,`printf()` 函数不会自动在打印字符串的末尾添加换行,因此你必须添加它。第一个参数是一个字符串,可以包含要传递给函数的其他参数的格式信息,例如:
```
printf("Slope: %f\n", slope);
```
在 Python 中,格式是内置在字符串本身中的,但是在 Octave 中,它是特定于 `printf()` 函数。
#### 读取数据
[dlmread()][40] 函数可以读取类似 CSV 文件的文本内容:
```
data = dlmread(input_file_name, delimiter, skip_header, 0);
```
结果是一个[矩阵][41]对象,这是 Octave 中的基本数据类型之一。矩阵可以用类似于 Python 的语法进行切片:
```
x = data(:, column_x);
y = data(:, column_y);
```
根本的区别是索引从 1 开始,而不是从 0 开始。因此,在该示例中,`x` 列是第一列。
#### 拟合数据
要用直线拟合数据,可以使用 [polyfit()][42] 函数。它用一个多项式拟合输入数据,所以你只需要使用一阶多项式:
```
p = polyfit(x, y, 1);
slope = p(1);
intercept = p(2);
```
结果是具有多项式系数的矩阵;因此,它选择前两个索引。要确定相关系数,请使用 [corr()][43] 函数:
```
r_value = corr(x, y);
```
最后,使用 `printf()` 函数打印结果:
```
printf("Slope: %f\n", slope);
printf("Intercept: %f\n", intercept);
printf("Correlation coefficient: %f\n", r_value);
```
#### 绘图
与 Matplotlib 示例一样,首先需要创建一个表示拟合直线的数据集:
```
fit_x = linspace(min(x) - 1, max(x) + 1, 100);
fit_y = slope * fit_x + intercept;
```
与 NumPy 的相似性也很明显,因为它使用了 [linspace()][44] 函数,其行为就像 Python 的等效版本一样。
同样,与 Matplotlib 一样,首先创建一个[图][45]对象,然后创建一个[轴][46]对象来保存这些图:
```
fig_width = 7; %inch
fig_height = fig_width / 16 * 9; %inch
fig_dpi = 100;
fig = figure("units", "inches",
             "position", [1, 1, fig_width, fig_height]);
ax = axes("parent", fig);
set(ax, "fontsize", 14);
set(ax, "linewidth", 2);
```
要设置轴对象的属性,请使用 [set()][47] 函数。然而,该接口相当混乱,因为该函数需要一个逗号分隔的属性和值对列表。这些对只是代表属性名的一个字符串和代表该属性值的第二个对象的连续。还有其他设置各种属性的函数:
```
xlim(ax, [min(x) - 1, max(x) + 1]);
ylim(ax, [min(y) - 1, max(y) + 1]);
xlabel(ax, 'x');
ylabel(ax, 'y');
```
绘图是用 [plot()][48] 功能实现的。默认行为是每次调用都会重置坐标轴,因此需要使用函数 [hold()][49]。
```
hold(ax, "on");
plot(ax, fit_x, fit_y,
     "marker", "none",
     "linestyle", "-",
     "linewidth", 2);
plot(ax, x, y,
     "marker", ".",
     "markersize", 20,
     "linestyle", "none");
hold(ax, "off");
```
此外,还可以在 `plot()` 函数中添加属性和值对。[legend][50] 必须单独创建,标签应手动声明:
```
lg = legend(ax, "Fit", "Data");
set(lg, "location", "northwest");
```
最后,将输出保存到 PNG 图像:
```
image_size = sprintf("-S%f,%f", fig_width * fig_dpi, fig_height * fig_dpi);
image_resolution = sprintf("-r%f,%f", fig_dpi);
print(fig, 'fit_octave.png',
      '-dpng',
      image_size,
      image_resolution);
```
令人困惑的是,在这种情况下,选项被作为一个字符串传递,带有属性名和值。因为在 Octave 字符串中没有 Python 的格式化工具,所以必须使用 [sprintf()][51] 函数。它的行为就像 `printf()` 函数,但是它的结果不是打印出来的,而是作为字符串返回的。
在这个例子中,就像在 Python 中一样,图形对象很明显被引用以保持它们之间的交互。如果说 Python 在这方面的文档有点混乱,那么 [Octave 的文档][52]就更糟糕了。我发现的大多数例子都不关心引用对象;相反,它们依赖于绘图命令作用于当前活动图形。全局[根图形对象][53]跟踪现有的图形和轴。
#### 结果
命令行上的结果输出是:
```
#### Anscombe's first set with Octave ####
Slope: 0.500091
Intercept: 3.000091
Correlation coefficient: 0.816421
```
它显示了用 Octave 生成的结果图像。
![Plot and fit of the dataset obtained with Octave][54]
###接下来
Python 和 GNU Octave 都可以绘制出相同的信息,尽管它们的实现方式不同。如果你想探索其他语言来完成类似的任务,我强烈建议你看看 [Rosetta Code][55]。这是一个了不起的资源,可以看到如何用多种语言解决同样的问题。
你喜欢用什么语言绘制数据?在评论中分享你的想法。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/2/python-gnu-octave-data-science
作者:[Cristiano L. Fontana][a]
选题:[lujun9972][b]
译者:[heguangzhi](https://github.com/heguangzhi)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/cristianofontana
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/analytics-graphs-charts.png?itok=sersoqbV (Analytics: Charts and Graphs)
[2]: https://en.wikipedia.org/wiki/Command-line_interface
[3]: https://en.wikipedia.org/wiki/Graphical_user_interface
[4]: https://gitlab.com/cristiano.fontana/polyglot_fit
[5]: https://en.wikipedia.org/wiki/Comma-separated_values
[6]: https://en.wikipedia.org/wiki/Anscombe%27s_quartet
[7]: https://www.python.org/
[8]: https://www.tiobe.com/tiobe-index/
[9]: https://redmonk.com/sogrady/2019/07/18/language-rankings-6-19/
[10]: http://pypl.github.io/PYPL.html
[11]: https://octoverse.github.com/
[12]: https://en.wikipedia.org/wiki/Interpreted_language
[13]: https://docs.python.org/3/library/
[14]: https://numpy.org/
[15]: https://www.scipy.org/
[16]: https://matplotlib.org/
[17]: https://getfedora.org/
[18]: https://en.wikipedia.org/wiki/Comment_(computer_programming)
[19]: https://gitlab.com/cristiano.fontana/polyglot_fit/-/blob/master/fitting_python.py
[20]: https://en.wikipedia.org/wiki/Shebang_(Unix)
[21]: https://docs.python.org/3/library/functions.html#print
[22]: https://docs.python.org/3/library/string.html#string-formatting
[23]: https://docs.python.org/3/library/string.html
[24]: https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html
[25]: https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html
[26]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html
[27]: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
[28]: https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure
[29]: https://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes
[30]: https://en.wikipedia.org/wiki/Portable_Network_Graphics
[31]: https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py
[32]: https://matplotlib.org/gallery/index.html
[33]: https://opensource.com/sites/default/files/uploads/fit_python.png (Plot and fit of the dataset obtained with Python)
[34]: https://www.gnu.org/software/octave/
[35]: https://wiki.octave.org/FAQ#Differences_between_Octave_and_Matlab
[36]: https://en.wikipedia.org/wiki/MATLAB
[37]: https://gitlab.com/cristiano.fontana/polyglot_fit/-/blob/master/fitting_octave.m
[38]: https://octave.org/doc/v5.1.0/Using-Packages.html#Using-Packages
[39]: https://octave.org/doc/v5.1.0/Formatted-Output.html#XREFprintf
[40]: https://octave.org/doc/v5.1.0/Simple-File-I_002fO.html#XREFdlmread
[41]: https://octave.org/doc/v5.1.0/Matrices.html
[42]: https://octave.org/doc/v5.1.0/Polynomial-Interpolation.html
[43]: https://octave.org/doc/v5.1.0/Correlation-and-Regression-Analysis.html#XREFcorr
[44]: https://octave.sourceforge.io/octave/function/linspace.html
[45]: https://octave.org/doc/v5.1.0/Multiple-Plot-Windows.html
[46]: https://octave.org/doc/v5.1.0/Graphics-Objects.html#XREFaxes
[47]: https://octave.org/doc/v5.1.0/Graphics-Objects.html#XREFset
[48]: https://octave.org/doc/v5.1.0/Two_002dDimensional-Plots.html#XREFplot
[49]: https://octave.org/doc/v5.1.0/Manipulation-of-Plot-Windows.html#XREFhold
[50]: https://octave.org/doc/v5.1.0/Plot-Annotations.html#XREFlegend
[51]: https://octave.org/doc/v5.1.0/Formatted-Output.html#XREFsprintf
[52]: https://octave.org/doc/v5.1.0/Two_002dDimensional-Plots.html#Two_002dDimensional-Plots
[53]: https://octave.org/doc/v5.1.0/Graphics-Objects.html#XREFgroot
[54]: https://opensource.com/sites/default/files/uploads/fit_octave.png (Plot and fit of the dataset obtained with Octave)
[55]: http://www.rosettacode.org/

View File

@ -0,0 +1,91 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How Melissa Di Donato Is Going To Reinvent SUSE)
[#]: via: (https://www.linux.com/articles/how-melissa-di-donato-is-going-to-reinvent-suse/)
[#]: author: (Swapnil Bhartiya https://www.linux.com/author/swapnil/)
How Melissa Di Donato Is Going To Reinvent SUSE
======
[![][1]][2]
[![][1]][2]
SUSE is one of the oldest open source companies and the first to market Linux for the enterprise. Even though it has undergone several acquisitions and a merger, it remains a strong player in the business. It has maintained its integrity and core values around open source. It continues to rely on its tried-and-tested Linux business and European markets, and generally shies away from making big moves taking big risks.
Until now.
[SUSE appointed Melissa Di Donato as its first female CEO][3]. She is making some serious changes to the company, from building a diverse and inclusive culture to betting on emerging technologies and taking risks.
Soon after taking the helm last year, Di Donato spent the first few months traveling around the globe to meet SUSE teams and customers and get a better sense of the perception of the market about the company.
Just like [Red Hat CEO Jim Whitehurst,][4] Di Donato didnt come to the company from an open source background. She had spent the last 25 years of her career as a SUSE customer, so she did have an outsiders perspective of the company.
“I am not interested in what SUSE was when I joined. I am more interested in what we want to become,” she said.
**Innovating for customers**
After her 100-day global tour, Di Donato had a much clearer picture of the company. She found that more than 80% of SUSE customers were still traditionalists, i.e., companies such as Walgreens and Daimler who have been around for a long time.
Over the years, these customers brought technologies into their environments to simplify things, but they ended up creating more complexities. Its a tall order to weave through the legacy technical debt they incurred and embrace emerging technologies such as Cloud Foundry, Kubernetes and so on.
These customers want to modernize their legacy environments and workloads, but they cant do that with the complex environments they have built. They cant iterate faster; they cant respond to new opportunities and new competitors faster.
They want to leverage cloud-native technologies like Kubernetes and containers, but it is overwhelming to evaluate technologies that are emerging at such a rapid pace. Which ones are just shiny new things and which ones do they really need them to accelerate their business goals?
“We have to help our customers simplify their infrastructure and environment so that they can start modernizing it and start leveraging new technologies,” Di Donato said.
While SUSE will continue to focus on core Linux OS, it will also invest in the next generation of Linux. It has been working on technologies like Kubic and MicroOS that change the way Linux is installed, managed, and operated.
She explains, “We are going to reinvent the way operating systems are used. We are going to make sure that we provide solutions that help our customers optimize their environment, automate components to help the applications run in a much more efficient and modern way. Thats what SUSE is going to be — an innovator. Were not there quite yet, but thats our focus.”.
**Evolving the company **
Historically, SUSE has been a fairly conservative company compared to other companies like Red Hat, which has been embracing emerging technologies at a much faster rate than any other open source software vendor.
“We have not been in a place where weve been considered the risk taker. Were the steady, stable provider of the most comprehensive unbreakable solutions in the market,” Di Donato admitted. “But we need to take that strong foundation and begin to become a bit of a risk taker, and begin to become very innovative.”
She is also gunning for explosive growth. “Were going to double in size by 2023. We have to go from just under half-a-billion in revenue to a billion.”
To achieve that, SUSE will be looking at both organic and inorganic growth, including acquisition of companies, talent and technologies. “We are going to be the default choice for innovation. We are going to be the default choice for highly innovative technologies that really change the landscape,” Di Donato said.
**Refining the brand**
Aside from making significant changes within the company, Di Donato is working on refining the SUSE brand. She hired seasoned Ivo Totev to lead Product and Marketing and showcase the companys differentiation.
“Were trying to get into the psychology of reinventing the brand,” Di Donato said. Her goal is to allocate 30-40% of SUSEs total revenue outside of the core Linux OS towards emerging markets and develop the technologies that theyve already built.
SUSE _is_ home to many innovative technologies that are being used by other open source communities, even its competitors. It just didnt market them the way Red Hat would market its technologies and projects. Even though SUSE started before Red Hat, the latter has much more visibility around the globe.
“Its a matter of getting the word out. We build things, but we dont talk about it or do anything about it. We actually have to put a package around it and start selling it so people can see who we are and what value we bring to them.”
In Di Donatos eyes, though, good marketing isnt everything. She argued that customers are going to demand flexibility and they are going to demand innovation that is not tied to the stack of a company. “Red Hat has a very locked-in stack that doesnt allow them to be agnostic at all.”
Its quite true that unlike Red Hat, SUSE is known as an “open open-source company”, one that believes in working with partners to create an ecosystem around open source, instead of creating a tightly integrated stack that locks everyone out.
She believes that eventually, customers would want the freedom and flexibility of picking and choosing the components they want in their stack.
**Conclusion**
Expect some big moves from SUSE in the near future. Less than a year into the company, new CEO Di Donato has developed a very clear vision. “Were going to build this company based on an innovative and agile mindset. Were not going to give up the stability and the quality of our core. What we are going to do is surround the core with really innovative thought-leading technologies that are going to set us apart from our competition… You are going to feel and experience a very different sense of excitement because were going to be talking much, much louder than weve ever talked about it before.”
--------------------------------------------------------------------------------
via: https://www.linux.com/articles/how-melissa-di-donato-is-going-to-reinvent-suse/
作者:[Swapnil Bhartiya][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linux.com/author/swapnil/
[b]: https://github.com/lujun9972
[1]: https://www.linux.com/wp-content/uploads/2020/02/Melissa-Di-Donato-1068x763.jpg (Melissa Di Donato)
[2]: https://www.linux.com/wp-content/uploads/2020/02/Melissa-Di-Donato-scaled.jpg
[3]: https://www.tfir.io/suse-gets-its-first-female-ceo-melissa-di-donato/
[4]: https://www.cio.com/article/3090140/jim-whitehurst-if-its-important-to-the-linux-community-its-important-to-red-hat.html

View File

@ -0,0 +1,411 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Fedoras gaggle of desktops)
[#]: via: (https://fedoramagazine.org/fedoras-gaggle-of-desktops/)
[#]: author: (Troy Dawson https://fedoramagazine.org/author/tdawson/)
Fedoras gaggle of desktops
======
![][1]
There are 38 different desktops or window managers in Fedora 31. You could try a different one every day for a month, and still have some left over. Some have very few features. Some have so many features they are called a desktop environment. This article cant go into detail on each, but its interesting to see the whole list in one place.
### Criteria for desktops
To be on this list, the desktop must show up on the desktop managers selection list. If the desktop has more than one entry in the desktop manager list, they are counted just as that one desktop. An example is “GNOME”, “GNOME Classic” and “GNOME (Wayland).” These all show up on the desktop manager list, but they are still just GNOME.
### List of desktops
```
```
#### [**9wm**][2]
```
Emulation of the Plan 9 window manager 8 1/2
dnf install 9wm
```
#### [**awesome**][3]
```
Highly configurable, framework window manager for X. Fast, light and extensible
https://fedoramagazine.org/5-cool-tiling-window-managers/
dnf install awesome
```
#### [**blackbox**][4]
```
Very small and fast Window Manager
Fedora uses the maintained fork on github
dnf install blackbox
```
#### [**bspwm**][5]
```
A tiling window manager based on binary space partitioning
https://github.com/windelicato/dotfiles/wiki/bspwm-for-dummies
dnf install bspwm
```
#### **[byobu][6]**
```
Light-weight, configurable window manager built upon GNU screen
dnf install byobu
```
#### **[Cinnamon][7]**
```
Cinnamon provides a desktop with a traditional layout, advanced features, easy to use, powerful and flexible.
https://projects.linuxmint.com/cinnamon/
https://opensource.com/article/19/12/cinnamon-linux-desktop
dnf group install "Cinnamon Desktop"
```
#### **[cwm][8]**
```
Calm Window Manager by OpenBSD project
https://steemit.com/technology/@jamesdeagle/the-calm-window-manager-cwm-a-quick-start-guide
dnf install cwm
```
#### **[Deepin][9]**
```
Deepin desktop is the desktop environment released with deepin (the linux distribution). It aims at being elegant and easy to use.
dnf group install "Deepin Desktop"
(optional) dnf group install "Deepin Desktop Office" "Media packages for Deepin Desktop"
```
#### **[dwm][10]**
```
Dynamic window manager for X
https://fedoramagazine.org/lets-try-dwm-dynamic-window-manger/
https://fedoramagazine.org/5-cool-tiling-window-managers/
dnf install dwm
(optional) dnf install dwm-user
```
#### **[enlightenment][11]**
```
Enlightenment window manager
https://opensource.com/article/19/12/linux-enlightenment-desktop
dnf install enlightenment
```
#### **[e16][11]**
```
The Enlightenment window manager, DR16
dnf install e16
(optional) dnf install e16-epplets e16-keyedit e16-themes
```
#### **[fluxbox][12]**
```
Window Manager based on Blackbox
dnf install fluxbox
(optional) dnf install fluxbox-pulseaudio fluxbox-vim-syntax
```
#### **[fvwm][13]**
```
Highly configurable multiple virtual desktop window manager
http://www.fvwm.org/
https://opensource.com/article/19/12/fvwm-linux-desktop
dnf install fvwm
```
#### **[GNOME][14]**
```
GNOME is a highly intuitive and user friendly desktop environment.
* both X11 and wayland
https://opensource.com/article/19/12/gnome-linux-desktop
https://fedoramagazine.org/3-simple-and-useful-gnome-shell-extensions/
dnf group install "GNOME"
(optional but large) dnf group install "Fedora Workstation"
```
#### **[herbstluftwm][15]**
```
A manual tiling window manager
https://opensource.com/article/19/12/herbstluftwm-linux-desktop
dnf install herbstluftwm
(optional) dnf install herbstluftwm-zsh herbstluftwm-fish
```
#### **[i3][16]**
```
Improved tiling window manager
https://fedoramagazine.org/getting-started-i3-window-manager/
https://fedoramagazine.org/using-i3-with-multiple-monitors/
dnf install i3
(optional) dnf install i3-doc i3-ipc
```
#### **[icewm][17]**
```
Window manager designed for speed, usability, and consistency
https://fedoramagazine.org/icewm-a-really-cool-desktop/
dnf install icewm
(optional) dnf install icewm-minimal-session
```
#### **[jwm][18]**
```
Joe's Window Manager
https://opensource.com/article/19/12/joes-window-manager-linux-desktop
dnf install jwm
```
#### **[KDE Plasma Desktop][19]**
```
The KDE Plasma Workspaces, a highly-configurable graphical user interface which includes a panel, desktop, system icons and desktop widgets, and many powerful KDE applications.
* both X11 and wayland
https://opensource.com/article/19/12/linux-kde-plasma
https://fedoramagazine.org/installing-kde-plasma-5/
dnf group install "KDE Plasma Workspaces"
(optional) dnf group install "KDE Applications" "KDE Educational applications" "KDE Multimedia support" "KDE Office" "KDE Telepathy"
(optional for wayland) dnf install kwin-wayland plasma-workspace-wayland
```
#### **[lumina][20]**
```
A lightweight, portable desktop environment
https://opensource.com/article/19/12/linux-lumina-desktop
dnf install lumina-desktop
(optional) dnf install lumina-*
```
#### **[LXDE][21]**
```
LXDE is a lightweight X11 desktop environment designed for computers with low hardware specifications like netbooks, mobile devices or older computers.
https://opensource.com/article/19/12/lxqt-lxde-linux-desktop
dnf group install "LXDE Desktop"
(optional) dnf group install "LXDE Office" "Multimedia support for LXDE"
```
#### **[LXQt][22]**
```
LXQt is a lightweight X11 desktop environment designed for computers with low hardware specifications like netbooks, mobile devices or older computers.
https://opensource.com/article/19/12/lxqt-lxde-linux-desktop
dnf group install "LXQt Desktop"
(optional) dnf group install "LXQt Office" "Multimedia support for LXQt"
```
#### **[MATE][23]**
```
MATE Desktop is based on GNOME 2 and provides a powerful graphical user interface for users who seek a simple easy to use traditional desktop interface.
https://opensource.com/article/19/12/mate-linux-desktop
https://fedoramagazine.org/installing-another-desktop/
dnf group install "MATE Desktop"
(optional) dnf group install "MATE Applications"
```
#### **[musca][24]**
```
A simple dynamic window manager fox X
dnf install musca
```
#### **[openbox][25]**
```
A highly configurable and standards-compliant X11 window manager
https://opensource.com/article/19/12/openbox-linux-desktop
https://fedoramagazine.org/openbox-fedora/
dnf install openbox
(optional) dnf install openbox-kde openbox-theme-mistral-thin-dark
```
#### **[Pantheon][26]**
```
The Pantheon desktop environment is the DE that powers elementaryOS.
https://github.com/elementary
https://opensource.com/article/19/12/pantheon-linux-desktop
dnf group install "Pantheon Desktop"
(optional) dnf install elementary-capnet-assist elementary-greeter elementary-shortcut-overlay
```
#### **[pekwm][27]**
```
A small and flexible window manager
https://opensource.com/article/19/12/pekwm-linux-desktop
dnf install pekwm
```
#### **[qtile][28]**
```
A pure-Python tiling window manager
https://fedoramagazine.org/5-cool-tiling-window-managers/
dnf install qtile
```
#### **[ratpoison][29]**
```
Minimalistic window manager
https://opensource.com/article/19/12/ratpoison-linux-desktop
dnf install ratpoison
```
#### **[sawfish][30]**
```
An extensible window manager for the X Window System
dnf install sawfish
(optional) dnf install sawfish-pager
```
#### **[spectrwm][31]**
```
Minimalist tiling window manager written in C
dnf install spectrwm
```
#### **[Sugar][32]**
```
A software playground for learning about learning.
* Possibly the most unique desktop of this list.
dnf group install "Sugar Desktop Environment"
(optional) dnf group install "Additional Sugar Activities"
```
#### **[sway][33]**
```
i3-compatible window manager for Wayland
* Wayland only
https://fedoramagazine.org/setting-up-the-sway-window-manager-on-fedora/
https://fedoramagazine.org/5-cool-tiling-window-managers/
dnf install sway
```
#### **[twm][34]**
```
X.Org X11 twm window manager
https://en.wikipedia.org/wiki/Twm
https://opensource.com/article/19/12/twm-linux-desktop
dnf install xorg-x11-twm
```
#### **[WindowMaker][35]**
```
A fast, feature rich Window Manager
https://opensource.com/article/19/12/linux-window-maker-desktop
dnf install WindowMaker
(optional) dnf install WindowMaker-extra
```
#### **[wmx][36]**
```
A really simple window manager for X
dnf install wmx
```
#### **[XFCE][37]**
```
A lightweight desktop environment that works well on low end machines.
https://opensource.com/article/19/12/xfce-linux-desktop
dnf group install "Xfce Desktop"
(optional) dnf group install "Applications for the Xfce Desktop" "Extra plugins for the Xfce panel" "Multimedia support for Xfce" "Xfce Office"
```
#### **[xmonad][38]**
```
A tiling window manager
dnf install xmonad
(optional) dnf install xmonad-mate
```
* * *
_Photo by [Annie Spratt][39] on [Unsplash][40]._
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/fedoras-gaggle-of-desktops/
作者:[Troy Dawson][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://fedoramagazine.org/author/tdawson/
[b]: https://github.com/lujun9972
[1]: https://fedoramagazine.org/wp-content/uploads/2020/01/gaggle-desktops-816x345.jpg
[2]: https://github.com/9wm/9wm
[3]: https://awesomewm.org/
[4]: https://github.com/bbidulock/blackboxwm
[5]: https://github.com/baskerville/bspwm
[6]: https://byobu.org/
[7]: https://github.com/linuxmint/cinnamon
[8]: https://github.com/leahneukirchen/cwm
[9]: https://www.deepin.org/en/dde/
[10]: http://dwm.suckless.org/
[11]: https://www.enlightenment.org/
[12]: http://fluxbox.org/
[13]: https://github.com/fvwmorg/fvwm
[14]: https://www.gnome.org/
[15]: http://herbstluftwm.org/
[16]: https://i3wm.org/
[17]: https://ice-wm.org/
[18]: http://joewing.net/projects/jwm/
[19]: https://kde.org/
[20]: https://lumina-desktop.org/
[21]: https://lxde.org/
[22]: https://lxqt.org/
[23]: https://mate-desktop.org/
[24]: https://github.com/enticeing/musca
[25]: http://openbox.org
[26]: https://elementary.io/
[27]: http://www.pekwm.org/
[28]: http://qtile.org
[29]: http://www.nongnu.org/ratpoison/
[30]: http://sawfish.wikia.com/
[31]: https://github.com/conformal/spectrwm
[32]: https://sugarlabs.org/
[33]: https://github.com/swaywm/sway
[34]: https://www.x.org/releases/X11R7.6/doc/man/man1/twm.1.xhtml
[35]: http://www.windowmaker.org
[36]: http://www.all-day-breakfast.com/wmx/
[37]: https://www.xfce.org/
[38]: https://hackage.haskell.org/package/xmonad
[39]: https://unsplash.com/@anniespratt?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
[40]: https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText

View File

@ -1,689 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (heguangzhi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Using Python and GNU Octave to plot data)
[#]: via: (https://opensource.com/article/20/2/python-gnu-octave-data-science)
[#]: author: (Cristiano L. Fontana https://opensource.com/users/cristianofontana)
使用 Python 和 GNU Octave 绘制数据
======
了解如何使用 Python 和 GNU Octave 完成一项常见的数据科学任务。
[分析:图表和图形][1]
数据科学是跨越编程语言的知识领域。有些人以解决这一领域的问题而闻名,而另一些人则鲜为人知。这篇文章将帮助你熟悉用一些流行语言做数据科学。
### 为数据科学选择 Python 和 GNU Octave
我经常尝试学习一种新的编程语言。为什么?这主要是对旧方式的厌倦和对新方式的好奇的结合。当我开始编程时,我唯一知道的语言是 C 语言。那些年的编程生涯既艰难又危险,因为我不得不手动分配内存,管理指针,并记得释放内存。
然后一个朋友建议我试试 Python现在编程生活变得简单多了。虽然程序运行变得慢多了但我不必通过编写分析软件来受苦了。然而我很快就意识到每种语言都有比其他语言更适合自己应用场景。后来我学习了其他一些语言每种语言都给我带来了一些新的启发。发现新的编程风格让我可以将一些解决方案移植到其他语言中这样一切都变得有趣多了。
为了对一种新的编程语言(及其文档)有所了解,我总是从编写一些执行我熟悉的任务的示例程序开始。为此,我将解释如何用 Python 和 GNU Octave 编写一个程序来完成一个你可以归类为数据科学的特殊任务。如果你已经熟悉其中一种语言,从中开始,浏览其他语言,寻找相似之处和不同之处。这并不是对编程语言的详尽比较,只是一个小小的展示。
所有的程序都应该在[命令行][2]上运行,而不是用[图形用户界面][3](GUI)。完整的例子可以在[多语种知识库][4]中找到。
### 编程任务
你将在本系列中编写的程序:
* 从[CSV文件][5]中读取数据
* 用直线插入数据(例如 _f(x)=m ⋅ x + q_)
* 将结果生成图像文件
这是许多数据科学家遇到的常见情况。示例数据是第一组[Anscombe's quartet][6],如下表所示。这是一组人工构建的数据,当用直线拟合时会给出相同的结果,但是它们的曲线非常不同。数据文件是一个文本文件,以制表符作为列分隔,以几行作为标题。此任务将仅使用第一组(例如:前两列)。
I
II
III
IV
x
y
x
y
x
y
x
y
10.0
8.04
10.0
9.14
10.0
7.46
8.0
6.58
8.0
6.95
8.0
8.14
8.0
6.77
8.0
5.76
13.0
7.58
13.0
8.74
13.0
12.74
8.0
7.71
9.0
8.81
9.0
8.77
9.0
7.11
8.0
8.84
11.0
8.33
11.0
9.26
11.0
7.81
8.0
8.47
14.0
9.96
14.0
8.10
14.0
8.84
8.0
7.04
6.0
7.24
6.0
6.13
6.0
6.08
8.0
5.25
4.0
4.26
4.0
3.10
4.0
5.39
19.0
12.50
12.0
10.84
12.0
9.13
12.0
8.15
8.0
5.56
7.0
4.82
7.0
7.26
7.0
6.42
8.0
7.91
5.0
5.68
5.0
4.74
5.0
5.73
8.0
6.89
### Python 方式
[Python][7]是一种通用编程语言,是当今最流行的语言之一(从[TIOBE index][8]、[RedMonk编程语言排名][9]、[编程语言流行指数][10]、[State of the Octoverse of GitHub][11]和其他来源的调查结果可以看出)。这是一种[解释的语言][12];因此,源代码由执行指令的程序读取和评估。它有一个全面的[标准库][13]并且总体上非常好用(我没有参考这最后一句话;这只是我的拙见)。
#### 安装
要使用 Python 开发,你需要解释器和一些库。最低要求是:
* [NumPy][14]用于合适的数组和矩阵操作
* [SciPy][15]进行数据科学
* [Matplotlib][16]绘图
在 [Fedora][17] 安装它们是很容易的:
```
`sudo dnf install python3 python3-numpy python3-scipy python3-matplotlib`
```
#### 注释代码
在 Python中[注释][18]是通过在行首添加一个 **#** 来实现的,该行的其余部分将被解释器丢弃:
```
`# This is a comment ignored by the interpreter.`
```
[fitting_python.py][19]示例使用注释在源代码中插入许可信息,第一行是[特殊注释][20],它允许在命令行上执行脚本:
```
`#! /usr/bin/env python3`
```
这一行通知命令行解释器,脚本需要由程序**python3**执行。
#### Required libraries
在 Python 中,库和模块可以作为一个对象导入(如示例中的第一行),其中包含库的所有函数和成员。通过使用 **as** 规范可以用于定义标签并重命名它们:
```
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
```
你也可以决定只导入一个子模块(如第二行和第三行)。语法有两个(或多或少)等效选项: **import module.submodule****from module import submodule**
#### 定义变量
Python 的变量是在第一次赋值时被声明的:
```
input_file_name = "anscombe.csv"
delimiter = "\t"
skip_header = 3
column_x = 0
column_y = 1
```
变量类型由分配给变量的值推断。没有常量值的变量,除非它们在模块中声明并且只能被读取。习惯上,不被修改的变量应该用大写字母命名。
#### 打印输出
通过命令行运行程序意味着输出只能打印在终端上。Python 有[**print()**][21]函数,默认情况下,该函数打印其参数,并在输出的末尾添加一个换行符:
```
`print("#### Anscombe's first set with Python ####")`
```
在 Python 中,可以将**print()**函数与[字符串类][23]的[格式化能力][22]相结合。字符串具有**format**方法,可用于向字符串本身添加一些格式化文本。例如,可以添加格式化的浮点数,例如:
```
`print("Slope: {:f}".format(slope))`
```
#### 读取数据
使用 NumPy 和 函数[**genfromtxt()**][24]读取CSV文件非常容易该函数生成[NumPy数组][25]:
```
`data = np.genfromtxt(input_file_name, delimiter = delimiter, skip_header = skip_header)`
```
在 Python中一个函数可以有可变数量的参数您可以通过指定所需的参数来让它传递一个子集。数组是非常强大的矩阵状对象可以很容易地分割成更小的数组:
```
x = data[:, column_x]
y = data[:, column_y]
```
冒号选择整个范围,也可以用来选择子范围。例如,要选择数组的前两行,可以使用:
```
`first_two_rows = data[0:1, :]`
```
#### 拟合数据
SciPy提供了方便的数据拟合功能例如[**linregress()**][26]功能。该函数提供了一些与拟合相关的重要值,如斜率、截距和两个数据集的相关系数:
```
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
print("Slope: {:f}".format(slope))
print("Intercept: {:f}".format(intercept))
print("Correlation coefficient: {:f}".format(r_value))
```
因为**linregress()**提供了几条信息,所以结果可以同时保存到几个变量中。
#### 绘图
Matplotlib 库仅仅绘制数据点,因此,你应该定义要绘制的点的坐标。已经定义了**x** 和 **y** 数组,所以你可以直接绘制它们,但是你还需要代表直线的数据点。
```
`fit_x = np.linspace(x.min() - 1, x.max() + 1, 100)`
```
[**linspace()**][27]函数可以方便地在两个值之间生成一组等距值。利用强大的 NumPy 数组可以轻松计算纵坐标,该数组可以像普通数值变量一样在公式中使用:
```
`fit_y = slope * fit_x + intercept`
```
公式在数组中逐元素应用;因此,结果在初始数组中具有相同数量的条目。
要绘图,首先,定义一个包含所有图形的[图形对象][28]:
```
fig_width = 7 #inch
fig_height = fig_width / 16 * 9 #inch
fig_dpi = 100
fig = plt.figure(figsize = (fig_width, fig_height), dpi = fig_dpi)
```
一个图形可以画几个图;在 Matplotlib 中,这些图块被称为[轴][29]。本示例定义一个单轴对象来绘制数据点:
```
ax = fig.add_subplot(111)
ax.plot(fit_x, fit_y, label = "Fit", linestyle = '-')
ax.plot(x, y, label = "Data", marker = '.', linestyle = '')
ax.legend()
ax.set_xlim(min(x) - 1, max(x) + 1)
ax.set_ylim(min(y) - 1, max(y) + 1)
ax.set_xlabel('x')
ax.set_ylabel('y')
```
将该图保存到[PNG image file][30]中,有:
```
`fig.savefig('fit_python.png')`
```
如果要显示(而不是保存)绘图,请调用:
```
`plt.show()`
```
此示例引用了绘图部分中使用的所有对象:它定义了对象 **fig** 和对象 **ax**。这种技术细节是不必要的,因为 **plt** 对象可以直接用于绘制数据集。《[Matplotlib 教程][31]展示了这样一个界面:
```
`plt.plot(fit_x, fit_y)`
```
坦率地说,我不喜欢这种方法,因为它隐藏了各种对象之间发生的重要的的交互。不幸的是,有时[官方的例子][32]有点令人困惑,因为他们倾向于使用不同的方法。在这个简单的例子中,引用图形对象是不必要的,但是在更复杂的例子中(例如在图形用户界面中嵌入图形时),引用图形对象就变得很重要了。
#### 结果
命令行输入:
```
#### Anscombe's first set with Python ####
Slope: 0.500091
Intercept: 3.000091
Correlation coefficient: 0.816421
```
这是 Matplotlib 产生的图像:
![Plot and fit of the dataset obtained with Python][33]
### GNU Octave 方式
[GNU Octave][34]语言主要用于数值计算。它提供了一个简单的操作向量和矩阵的语法,并且有一些强大的绘图工具。这是一种像 Python 一样的解释语言。由于 Octave的语法是[最兼容][35] [MATLAB][36],它经常被描述为一个免费的替代 MATLAB 的方案。Octave 没有被列为最流行的编程语言,但是 MATLAB 是,所以 Octave 在某种意义上是相当流行的。MATLAB 早于 NumPy我觉得它是受到了前者的启发。当你看这个例子时你会看到相似之处。
#### 安装
[fitting_octave.m][37]的例子只需要基本的 Octave 包,在 Fedora 中安装相当简单:
```
`sudo dnf install octave`
```
#### 注释代码
在Octave中你可以用百分比符号(**%**)为代码添加注释,如果不需要与 MATLAB 兼容,你也可以使用 **#**。使用 **#** 的选项允许你从 Python 示例中编写相同的特殊注释行,以便直接在命令行上执行脚本。
#### 必要的库
本例中使用的所有内容都包含在基本包中,因此你不需要加载任何新的库。如果你需要一个库,[语法][38]是 **pkg load module**。该命令将模块的功能添加到可用功能列表中。在这方面Python 具有更大的灵活性。
#### 定义变量
变量的定义与 Python 的语法基本相同:
```
input_file_name = "anscombe.csv";
delimiter = "\t";
skip_header = 3;
column_x = 1;
column_y = 2;
```
请注意,行尾有一个分号;这不是必需的,但是它会抑制行结果的输出。如果没有分号,解释器将打印表达式的结果:
```
octave:1&gt; input_file_name = "anscombe.csv"
input_file_name = anscombe.csv
octave:2&gt; sqrt(2)
ans =  1.4142
```
#### 打印输出结果
强大的功能[**printf()**][39]是用来在终端上打印的。与 Python 不同,**printf()** 函数不会自动在打印字符串的末尾添加换行,因此你必须添加它。第一个参数是一个字符串,可以包含要传递给函数的其他参数的格式信息,例如:
```
`printf("Slope: %f\n", slope);`
```
在 Python 中,格式是内置在字符串本身中的,但是在 Octave 中,它是特定于 **printf()** 函数。
#### 读取数据
[**dlmread()**][40]函数可以读取类似CSV文件的文本内容:
```
`data = dlmread(input_file_name, delimiter, skip_header, 0);`
```
结果是一个[矩阵][41]对象,这是 Octave 中的基本数据类型之一。矩阵可以用类似于 Python 的语法进行切片:
```
x = data(:, column_x);
y = data(:, column_y);
```
根本的区别是索引从1开始而不是从0开始。因此在该示例中__x__列是第一列。
#### 拟合数据
要用直线拟合数据,可以使用[**polyfit()**][42]函数。它用一个多项式拟合输入数据,所以你只需要使用一阶多项式:
```
p = polyfit(x, y, 1);
slope = p(1);
intercept = p(2);
```
结果是具有多项式系数的矩阵;因此,它选择前两个索引。要确定相关系数,请使用[**corr()**][43]函数:
```
`r_value = corr(x, y);`
```
最后,使用 **printf()** 函数打印结果:
```
printf("Slope: %f\n", slope);
printf("Intercept: %f\n", intercept);
printf("Correlation coefficient: %f\n", r_value);
```
#### 绘图
与 Matplotlib 示例一样,首先需要创建一个表示拟合直线的数据集:
```
fit_x = linspace(min(x) - 1, max(x) + 1, 100);
fit_y = slope * fit_x + intercept;
```
与 NumPy 的相似性也很明显,因为它使用了[**linspace()**][44]函数,其行为就像 Python 的等效版本一样。
同样,与 Matplotlib 一样,首先创建一个[图][45]对象,然后创建一个[轴][46]对象来保存这些图:
```
fig_width = 7; %inch
fig_height = fig_width / 16 * 9; %inch
fig_dpi = 100;
fig = figure("units", "inches",
             "position", [1, 1, fig_width, fig_height]);
ax = axes("parent", fig);
set(ax, "fontsize", 14);
set(ax, "linewidth", 2);
```
要设置轴对象的属性,请使用[**set()**][47]函数。然而,该接口相当混乱,因为该函数需要一个逗号分隔的属性和值对列表。这些对只是代表属性名的一个字符串和代表该属性值的第二个对象的连续。还有其他设置各种属性的功能:
```
xlim(ax, [min(x) - 1, max(x) + 1]);
ylim(ax, [min(y) - 1, max(y) + 1]);
xlabel(ax, 'x');
ylabel(ax, 'y');
```
标图是用[**plot()**][48]功能实现的。默认行为是每次调用都会重置坐标轴,因此需要使用函数[**hold()**][49]。
```
hold(ax, "on");
plot(ax, fit_x, fit_y,
     "marker", "none",
     "linestyle", "-",
     "linewidth", 2);
plot(ax, x, y,
     "marker", ".",
     "markersize", 20,
     "linestyle", "none");
hold(ax, "off");
```
此外,还可以在 **plot()** 函数中添加属性和值对。[legend][50]必须单独创建,标签应手动声明:
```
lg = legend(ax, "Fit", "Data");
set(lg, "location", "northwest");
```
最后将输出保存到PNG图像:
```
image_size = sprintf("-S%f,%f", fig_width * fig_dpi, fig_height * fig_dpi);
image_resolution = sprintf("-r%f,%f", fig_dpi);
print(fig, 'fit_octave.png',
      '-dpng',
      image_size,
      image_resolution);
```
令人困惑的是,在这种情况下,选项被作为一个字符串传递,带有属性名和值。因为在 Octave 字符串中没有 Python 的格式化工具,所以必须使用[**sprintf()**][51]函数。它的行为就像**printf()**函数,但是它的结果不是打印出来的,而是作为字符串返回的。
在这个例子中,就像在 Python 中一样,图形对象很明显被引用以保持它们之间的交互。如果说 Python 在这方面的文档有点混乱,那么[Octave 的文档][52]就更糟糕了。我发现的大多数例子都不关心引用对象;相反,它们依赖于绘图命令作用于当前活动图形。全局[根图形对象][53]跟踪现有的图形和轴。
#### 结果
命令行上的结果输出是:
```
#### Anscombe's first set with Octave ####
Slope: 0.500091
Intercept: 3.000091
Correlation coefficient: 0.816421
```
它显示了用 Octave 生成的结果图像。
![Plot and fit of the dataset obtained with Octave][54]
### 下一个
Python 和 GNU Octave 都可以绘制出相同的信息,尽管它们的实现方式不同。如果你想探索其他语言来完成类似的任务,我强烈建议你看看[Rosetta 代码][55]。这是一个了不起的资源,可以看到如何用多种语言解决同样的问题。
你喜欢用什么语言绘制数据?在评论中分享你的想法。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/2/python-gnu-octave-data-science
作者:[Cristiano L. Fontana][a]
选题:[lujun9972][b]
译者:[heguangzhi](https://github.com/heguangzhi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/cristianofontana
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/analytics-graphs-charts.png?itok=sersoqbV (Analytics: Charts and Graphs)
[2]: https://en.wikipedia.org/wiki/Command-line_interface
[3]: https://en.wikipedia.org/wiki/Graphical_user_interface
[4]: https://gitlab.com/cristiano.fontana/polyglot_fit
[5]: https://en.wikipedia.org/wiki/Comma-separated_values
[6]: https://en.wikipedia.org/wiki/Anscombe%27s_quartet
[7]: https://www.python.org/
[8]: https://www.tiobe.com/tiobe-index/
[9]: https://redmonk.com/sogrady/2019/07/18/language-rankings-6-19/
[10]: http://pypl.github.io/PYPL.html
[11]: https://octoverse.github.com/
[12]: https://en.wikipedia.org/wiki/Interpreted_language
[13]: https://docs.python.org/3/library/
[14]: https://numpy.org/
[15]: https://www.scipy.org/
[16]: https://matplotlib.org/
[17]: https://getfedora.org/
[18]: https://en.wikipedia.org/wiki/Comment_(computer_programming)
[19]: https://gitlab.com/cristiano.fontana/polyglot_fit/-/blob/master/fitting_python.py
[20]: https://en.wikipedia.org/wiki/Shebang_(Unix)
[21]: https://docs.python.org/3/library/functions.html#print
[22]: https://docs.python.org/3/library/string.html#string-formatting
[23]: https://docs.python.org/3/library/string.html
[24]: https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html
[25]: https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html
[26]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html
[27]: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
[28]: https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure
[29]: https://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes
[30]: https://en.wikipedia.org/wiki/Portable_Network_Graphics
[31]: https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py
[32]: https://matplotlib.org/gallery/index.html
[33]: https://opensource.com/sites/default/files/uploads/fit_python.png (Plot and fit of the dataset obtained with Python)
[34]: https://www.gnu.org/software/octave/
[35]: https://wiki.octave.org/FAQ#Differences_between_Octave_and_Matlab
[36]: https://en.wikipedia.org/wiki/MATLAB
[37]: https://gitlab.com/cristiano.fontana/polyglot_fit/-/blob/master/fitting_octave.m
[38]: https://octave.org/doc/v5.1.0/Using-Packages.html#Using-Packages
[39]: https://octave.org/doc/v5.1.0/Formatted-Output.html#XREFprintf
[40]: https://octave.org/doc/v5.1.0/Simple-File-I_002fO.html#XREFdlmread
[41]: https://octave.org/doc/v5.1.0/Matrices.html
[42]: https://octave.org/doc/v5.1.0/Polynomial-Interpolation.html
[43]: https://octave.org/doc/v5.1.0/Correlation-and-Regression-Analysis.html#XREFcorr
[44]: https://octave.sourceforge.io/octave/function/linspace.html
[45]: https://octave.org/doc/v5.1.0/Multiple-Plot-Windows.html
[46]: https://octave.org/doc/v5.1.0/Graphics-Objects.html#XREFaxes
[47]: https://octave.org/doc/v5.1.0/Graphics-Objects.html#XREFset
[48]: https://octave.org/doc/v5.1.0/Two_002dDimensional-Plots.html#XREFplot
[49]: https://octave.org/doc/v5.1.0/Manipulation-of-Plot-Windows.html#XREFhold
[50]: https://octave.org/doc/v5.1.0/Plot-Annotations.html#XREFlegend
[51]: https://octave.org/doc/v5.1.0/Formatted-Output.html#XREFsprintf
[52]: https://octave.org/doc/v5.1.0/Two_002dDimensional-Plots.html#Two_002dDimensional-Plots
[53]: https://octave.org/doc/v5.1.0/Graphics-Objects.html#XREFgroot
[54]: https://opensource.com/sites/default/files/uploads/fit_octave.png (Plot and fit of the dataset obtained with Octave)
[55]: http://www.rosettacode.org/