Numpy values and lists of similarities and differences

Values ​​and lists of similarities and differences 1.numpy

 

arr ndarray is defined, you need to know about the array index, array indices are zero-based.

>>> list[0]
1
>>> arr[0]
1

To the array assignment, list assigned to the character, you can, and arr assigned to a string, you will be prompted element is not valid, that is to say, the definition of np array, dtype type on fixed

>>> list[-1]='mystring'
>>> arr[-1]='mystring'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'mystring'

>>> arr.dtype
dtype('int64') arr的dtype为int64

Modify the array arr value inside, we must also meet this condition, if not automatically be converted to an integer

>>> arr[-1]=1000.1234
>>> arr[-1]
1000

I want to define float, directly in the defined time

>>> arr2=np.array([1.0,2.0,3.0,4.0,5.0])
>>> arr2.dtype
dtype('float64')

 

Often used in an array of random numbers random.randn

>>> arr3=np.random.randn(5)
>>> arr3
array([ 0.26470504,  0.38225925, -1.11953881,  0.07058837, -1.10583038])

 

2. multidimensional arrays

>>> list2d=[[1,2],[3,4]]
>>> type(list2d)
<class 'list'>
>>> arr2d=np.array([[1,2],[3,4]])
>>> type(arr2d)
<class 'numpy.ndarray'>
>>> list2d
[[1, 2], [3, 4]]
>>> arr2d   #定义了2*2
array([[1, 2],
       [3, 4]])


All numerical values ​​0

>>> arr0=np.zeros((2,3))
>>> arr0
array([[0., 0., 0.],
       [0., 0., 0.]])
 

5 is defined as a mean value, the standard deviation of the normally distributed random value 2 * 3 4 array

>>> np.random.normal(5,3,(2,4))
array([[4.24157662, 9.9695751 , 7.72405208, 4.16709509],
       [9.78426533, 0.4325178 , 3.65920354, 8.35030401]])

 

>>> arr4=np.arange(8)   #1*8数组
>>> arr4
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> arr24=np.arange(8).reshape(2,4) #2*4 数组
>>> arr24
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
>>> arr24.dtype
dtype('int64')

 

Property multidimensional arrays

import numpy as np
arr=np.arange(8).reshape(2,4)
print(arr)
print("Dtype ",arr.dtype)
print("元素大小 ",arr.size)
print("dim ",arr.ndim)
print("shape ",arr.shape)
print("Memory used ",arr.nbytes)
print("最大值,最小值 ",arr.max(),arr.min())
print("总值,prod ",arr.sum(),arr.prod())
print("mean,std ",arr.mean(),arr.std())

 


 

 

Published 301 original articles · won praise 16 · views 30000 +

Guess you like

Origin blog.csdn.net/keny88888/article/details/105338577