当前位置:网站首页>Numpy - Construction of array
Numpy - Construction of array
2022-07-25 09:28:00 【Bubble Yi】
1.Numpy summary
Standard installation Python Use the list (list) Save a set of values , Can be used as an array , However, since the elements of the list can be any object , So what's in the list is a pointer to the object . So in order to save a simple [1,2,3], Need to have 3 Three pointers and three integer objects . For numerical operation, this structure is obviously a waste of memory and CPU computing time .NumPy The birth of the new century has made up for these shortcomings ,NumPy Two basic objects are provided :ndarray(N-dimensional array object) and ufunc(universal function object).ndarray( We call it array ) It's a multidimensional array that stores a single data type , and ufunc Is a function that can process arrays , That is, the built-in function that can perform vectorization .
2. Array array Construction
(1)tuple and list Turn into numpy Of array data type
import numpy as np
# By list list Generating arrays array
my_list=[2,3,5,7,8]
print(my_list)
my_np_array=np.array(my_list)
print(my_np_array)
# By tuples tuple Generating arrays array
my_tuple=(9,6,3,12)
my_np_array1=np.array(my_tuple)
# list 、 The multiple of tuple is different from that of array
print(my_list*3)
print(my_tuple*3)
print(np.array(my_tuple)*3)result :
[2, 3, 5, 7, 8] [2 3 5 7 8] [2, 3, 5, 7, 8, 2, 3, 5, 7, 8, 2, 3, 5, 7, 8] (9, 6, 3, 12, 9, 6, 3, 12, 9, 6, 3, 12) [27 18 9 36]
(2)arange()、linspace() Generating arrays
import numpy as np
#1. function arange()
np.arange(9) # This function generates an array array, Excluding mantissa
np.arange(2,9) # Left closed right away
print(np.arange(2,9,2) )# [2 4 6 8]
#2. function linspace()
print(np.linspace(2,10,10)) # Within the specified time , Return fixed interval data (3) Element is 1 Matrix ones And elements are all 0 Matrix zeros Automatic generation of
np.zeros(6) # One dimensional zero array
np.zeros((2,3)) # Two dimensional zero array
np.ones(5) # One dimensional array , The elements are 1
np.ones((5,3)) # Two dimensional array , The elements are 1
np.ones(5,dtype='int32') # Specifies the data type of the array element , The default is floating point .(4) Random array generation
. Generate random numbers
import numpy as np
np.random.seed(10) # random.seed(), Omitting parameters means taking the current system time as the seed
np.random.random() ## Produce a [0,1) Floating point random number between
print(np.random.random()) # The same seed value , The resulting random numbers are the same . Generate a random array with normal distribution
#1. Generate standard normal distribution random array
import numpy as np
np.random.normal(size=(5, 2)) # produce 5x2 Each element of comes from the sample matrix of standard normal distribution ,
#2. The mean value of the generation is 85, The standard deviation is 4 Of 100 A normally distributed random number
sampleNo = 100; # Number of samples
mu = 85 # mean value
sigma = 4 # Standard deviation
s1 = np.random.normal(mu, sigma, size=sampleNo) # The return is a one-dimensional array
s2 = np.random.normal(mu, sigma, size=(5,2)) # The return is a two-dimensional array 3 Example
Construct a 3 That's ok 4 Two dimensional array of columns , The elements in the array come from the average value 2, The standard deviation is 3 Is a normal distribution .
import numpy as np
np.random.seed(20)
mu=2
sigma=3
my_array=np.random.normal(mu, sigma, size=(3,4))
print(my_array)result :
[[ 4.65167934 2.58759507 3.07260955 -5.02978572] [-1.25449776 3.67908887 4.81840805 -0.93544313] [ 3.50929052 3.21924341 2.97038303 0.51976735]]
边栏推荐
猜你喜欢
随机推荐
『每日一问』简单聊聊JMM/说说对JMM的了解
sqli-labs安装 环境:ubuntu18 php7
C language and SQL Server database technology
C#语言和SQL Server数据库技术
Notes on in-depth analysis of C language 2
Deep understanding of static keyword
~1 ccf 2022-06-2 寻宝!大冒险!
¥1-1 SWUST oj 941: 有序顺序表的合并操作的实现
Go基础3
@1-1 CCF 2021-04-1 灰度直方图
梦想启航(第一篇博客)
【代码源】每日一题 三段式
Nacos启动报错Unable to start web server
卷积神经网络的兴趣简单介绍
神经网络方法——美国波士顿房价(回归问题)
【线程知识点】-- 自旋锁
~2 ccf 2022-03-1 未初始化警告
Go基础4
数据库操作语言(DML)
初始Flask以及简单地上手应用







![[De1CTF 2019]SSRF Me](/img/12/44c37cc713b49172a10579c9628c94.png)

