找到 34423 篇文章,关于编程

如何在 R 数据框列中将数字转换为文字?

Nizamuddin Siddiqui
更新于 2021-03-16 10:21:07

3K+ 阅读量

为了在 R 数据框列中将数字转换为文字,我们可以使用 english 包中的 english 函数。例如,如果我们有一个名为 df 的数据框,其中包含一个数字列 x,那么我们可以使用命令 as.character(english(df$x)) 将数字转换为文字。示例考虑以下数据框 - 在线演示x

如何在 R 中查找矩阵的行积?

Nizamuddin Siddiqui
更新于 2021-03-16 10:16:29

1K+ 阅读量

为了在 R 中查找矩阵的行积,我们可以使用 apply 函数以及 prod 函数。例如,如果我们有一个名为 M 的矩阵,那么要查找矩阵的行积,我们可以使用命令 apply(M,1,prod)。我们需要记住,输出将是一个向量而不是矩阵。查看以下示例以了解如何执行矩阵的行积。示例考虑以下矩阵 - 在线演示M1

如何使用 Numpy 查找给定矩阵中所有元素的总和?

Prasad Naik
更新于 2021-03-16 10:34:44

2K+ 阅读量

在这个程序中,我们将使用 numpy 库中的 sum() 函数添加 numpy 矩阵的所有项。我们将首先创建一个随机的 numpy 矩阵,然后我们将获得所有元素的总和。算法步骤 1:导入 numpy。步骤 2:使用 random() 函数创建一个随机的 m×n 矩阵。步骤 3:使用 sum() 函数获得矩阵中所有元素的总和。示例代码import numpy as np matrix = np.random.rand(3, 3) print("The numpy matrix is: ", matrix) print("The sum of the matrix is: ", np.sum(matrix))输出The numpy matrix is: [[0.66411969 0.43672579 0.48448593] [0.76110384 ... 阅读更多

Matplotlib 子图中的行和列标题

Rishikesh Kumar Rishi
更新于 2021-03-16 10:18:23

436 阅读量

使用 subplot 方法,我们可以配置行数和列数。nrows*nclos 将创建绘制图表的位置数量。步骤行数 = 2,列数 = 1,所以总位置为:2*1 = 2。向当前图形添加一个子图,nrow = 2,ncols = 1,index = 1。向当前图形添加一个子图,nrow = 2,ncols = 1,index = 2。使用 plt.show(),我们可以显示图形。示例from matplotlib import pyplot as plt row_count = 2 col_count = 1 index1 = 1 # 子图数量为:row*col,index 是位置 ... 阅读更多

如何在 Matplotlib 中更改后端?

Rishikesh Kumar Rishi
更新于 2021-03-16 10:18:47

2K+ 阅读量

我们可以使用 atplotlib.rcParams['backend'] 变量覆盖后端值。步骤使用 get_backend() 方法,返回当前后端名称,即默认名称。现在覆盖后端名称。使用 get_backend() 方法,返回当前后端名称,即更新后的名称。示例import matplotlib print("Before, Backend used by matplotlib is: ", matplotlib.get_backend()) matplotlib.rcParams['backend'] = 'TkAgg' print("After, Backend used by matplotlib is: ", matplotlib.get_backend())输出Before, Backend used by matplotlib is: GTK3Agg After, Backend used by matplotlib is: TkAgg 输入条形图数量:5

如何为 Pandas/Matplotlib 条形图设置自定义颜色?

Rishikesh Kumar Rishi
更新于 2021-03-16 10:19:16

4K+ 阅读量

为了创建自定义颜色,我们可以创建一个十六进制字符串。从中,我们可以创建不同的颜色表示集,并将它们传递到 scatter 方法以获得所需的输出。使用 set_color 方法,我们可以设置条形图的颜色。步骤获取用户输入的条形图数量。使用 plt.bar() 方法添加条形图。通过选择随机字符从十六进制字母创建颜色。使用 set_color() 方法为每个条形图设置颜色。要显示图形,我们可以使用 plt.show() 方法。示例from matplotlib import pyplot as plt import random bar_count = int(input("Enter number of bars: ")) bars = plt.bar([i for ... 阅读更多

使用 Numpy 查找给定矩阵的行数和列数

Prasad Naik
更新于 2021-03-16 10:20:19

3K+ 阅读量

我们将首先创建一个 numpy 矩阵,然后找出该矩阵的行数和列数算法步骤 1:创建一个随机数的 numpy 矩阵。步骤 2:使用 numpy.shape 函数查找矩阵的行和列。步骤 3:打印行数和列数。示例代码import numpy as np matrix = np.random.rand(2,3) print(matrix) print("Total number of rows and columns in the given matrix are: ", matrix.shape)输出[[0.23226052 0.89690884 0.19813164] [0.85170808 0.97725669 0.72454096]] Total number of rows and columns in the given matrix are: (2, 3)

如何使用 Numpy 创建单位矩阵?

Prasad Naik
更新于 2021-03-16 10:20:45

4K+ 阅读量

在这个程序中,我们将打印一个大小为 nxn 的单位矩阵,其中 n 将作为用户输入获取。我们将使用 numpy 库中的 identity() 函数,它将维度和元素的数据类型作为参数算法步骤 1:导入 numpy。步骤 2:从用户处获取维度作为输入。步骤 3:使用 numpy.identity() 函数打印单位矩阵。示例代码import numpy as np dimension = int(input("Enter the dimension of identitiy matrix: ")) identity_matrix = np.identity(dimension, dtype="int") print(identity_matrix)输出Enter the dimension of identitiy matrix: 5 [[1 0 0 0 0] [0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [0 0 0 0 1]]

如何在给定范围内创建 numpy 数组?

Prasad Naik
更新于 2021-03-16 10:21:08

1K+ 阅读量

我们必须在用户提供的范围内创建一个 numpy 数组。我们将使用 numpy 库中的 arange() 函数来获得我们的输出。算法步骤 1:导入 numpy。步骤 2:从用户处获取 start_value、end_value 和 Step。步骤 3:使用 numpy 中的 arange() 函数打印数组。示例代码import numpy as np start_val = int(input("Enter starting value: ")) end_val = int(input("Enter ending value: ")) Step_val = int(input("Enter Step value: ")) print(np.arange(start_val, end_val, Step_val))输出Enter starting value: 5 Enter ending value: 50 Enter Step value: 5 [ 5 10 15 20 25 30 35 40 45]

如何在 Matplotlib 中生成随机颜色?

Rishikesh Kumar Rishi
更新于 2021-03-16 10:21:32

3K+ 阅读量

为了创建自定义颜色,我们可以创建一个十六进制字符串。从中,我们可以创建不同的颜色表示集,并将它们传递到 scatter 方法以获得所需的输出。步骤从用户处获取颜色数量的输入,即 number_of_colors = 20。使用十六进制字母获取颜色。通过从步骤 2 数据中选择随机字符创建颜色。为步骤 1 输入数据绘制散点图,使用步骤 3 颜色。要显示图形,请使用 plt.show() 方法。示例import matplotlib.pyplot as plt import random number_of_colors = int(input("Please enter number of colors: ")) hexadecimal_alphabets ... 阅读更多

广告

© . All rights reserved.