[Python] Training 2: Create an array and perform operations, create a chess board (Numpy basis)

Topic source:
"Python Data Analysis and Application" Chapter 2 Numpy Numerical Computing Basic Training Part

【Huang Hongmei and Zhang Liangjun are editors-in-chief of China Industry and Information Technology Publishing Group and People's Posts and Telecommunications Publishing House】

The content of the title of this blog comes from: QQ key combination "ctrl+alt+o" is obtained by intercepting book-related files
(if this key combination cannot be used, you can go to the QQ settings to see if the hotkeys conflict)

Practice 1 Create an array and perform operations

1. Training points
(1) Master the array creation and random number generation of NumPy.
(2) Master the basic operation functions for statistical analysis in NumPy.
2. Requirement Description
NumPy arrays are more efficient in numerical operations than lists provided by Python, so it is necessary to flexibly master the creation of arrays and basic operations in NumPy.
3. Implementation ideas and steps
(1) Create an array with a value range of 0 to 1 and an interval of 0.01.
(2) Create 100 random numbers that obey a normal distribution.
(3) Perform four arithmetic operations on the two arrays created.
(4) Perform a simple statistical analysis on the created random array.

1. Reference code

import numpy as np
#1、创建一个数值范围为0~1,间隔为0.01的数组
arr1=np.arange(0,1,0.01)
print("arange函数创建的数值范围为0~1间隔为0.01的数组arr1为:\n",arr1)
#2、创建由100个服从正态分布的随机数构成的数组
arr2=np.random.randn(100)
print('生成的随机数组arr2为:\n',arr2)
#3、对创建的两个数组进行四则运算
print('arr1 + arr2:\n',arr1+arr2)
print('arrl-arr2:\n',arr1-arr2)
print('arrl*arr2:\n',arr1*arr2)
print('arrl/arr2:\n',arr1/arr2)
#print('arrl**arr2:\n',arr1**arr2)
#4、对创建的数组进行简单的统计分析
print('正态分布数数组的累计和为:\n',np.cumsum(arr2))#计算所有元素的累计和
print('正态分布数去重后的值组为:\n',np.unique(arr2))#去除重复值
print('正态分布数组的均值为:',np.mean(arr2))#计算数组均值
print('正态分布数组的标准差为:',np.std(arr2))#计算数组标准差
print('正态分布数组的方差为:',np.var(arr2))#计算数组方差
print('正态分布数组的最小值为:',np.min(arr2))#计算最小值
print('正态分布数组的最大值为:',np.max(arr2))#计算最大值
print('正态分布数组的总和为:',np.sum(arr2))#计算数组总和

2. Results of the experimental part:
insert image description here
insert image description here
insert image description here
training 2 to create a chess board

1. Training points
(1) Master the matrix creation method.
(2) Master the method of array indexing.
2. Description of requirements
Create a chess board and fill in an 8x8 matrix. The chess board is a square, which consists of 64 small squares with 8 horizontal and vertical grids, one dark and one light color staggered. The dark squares are black squares and the light color squares are white squares. 2-3.
insert image description here

1. Don't say anything, it's all in the "code"

import numpy as np
matr1=np.ones((8,8))
for i in range(8):
    for j in range(8):
        if(i+j)%2==0:
            matr1[i,j]=0
print('国际棋盘对应的矩阵为:\n',matr1)

for i in range(0,8):
    for j in range(0,8):
        if matr1[i,j]==0:
            print("□",end=' ')
        else:
            print("■", end=' ')
    print('\n')

2. Output result:
insert image description here
insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324844915&siteId=291194637