GVKun编程网logo

Python numpy 模块-less_equal() 实例源码

3

如果您对Pythonnumpy模块-less_equal()实例源码感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Pythonnumpy模块-less_equal()实例源码的各种细节,此外还

如果您对Python numpy 模块-less_equal() 实例源码感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Python numpy 模块-less_equal() 实例源码的各种细节,此外还有关于Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable、numpy.random.random & numpy.ndarray.astype & numpy.arange、numpy.ravel()/numpy.flatten()/numpy.squeeze()、Numpy:数组创建 numpy.arrray() , numpy.arange()、np.linspace ()、数组基本属性的实用技巧。

本文目录一览:

Python numpy 模块-less_equal() 实例源码

Python numpy 模块-less_equal() 实例源码

Python numpy 模块,less_equal() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.less_equal()

项目:radar    作者:amoose136    | 项目源码 | 文件源码
  1. def equal(x1, x2):
  2. """
  3. Return (x1 == x2) element-wise.
  4.  
  5. Unlike `numpy.equal`,this comparison is performed by first
  6. stripping whitespace characters from the end of the string. This
  7. behavior is provided for backward-compatibility with numarray.
  8.  
  9. Parameters
  10. ----------
  11. x1,x2 : array_like of str or unicode
  12. Input arrays of the same shape.
  13.  
  14. Returns
  15. -------
  16. out : ndarray or bool
  17. Output array of bools,or a single bool if x1 and x2 are scalars.
  18.  
  19. See Also
  20. --------
  21. not_equal,greater_equal,less_equal,greater,less
  22. """
  23. return compare_chararrays(x1, x2, ''=='', True)
项目:radar    作者:amoose136    | 项目源码 | 文件源码
  1. def not_equal(x1, x2):
  2. """
  3. Return (x1 != x2) element-wise.
  4.  
  5. Unlike `numpy.not_equal`,or a single bool if x1 and x2 are scalars.
  6.  
  7. See Also
  8. --------
  9. equal, ''!='', True)
项目:radar    作者:amoose136    | 项目源码 | 文件源码
  1. def greater_equal(x1, x2):
  2. """
  3. Return (x1 >= x2) element-wise.
  4.  
  5. Unlike `numpy.greater_equal`,this comparison is performed by
  6. first stripping whitespace characters from the end of the string.
  7. This behavior is provided for backward-compatibility with
  8. numarray.
  9.  
  10. Parameters
  11. ----------
  12. x1,not_equal, ''>='', True)
项目:radar    作者:amoose136    | 项目源码 | 文件源码
  1. def less_equal(x1, x2):
  2. """
  3. Return (x1 <= x2) element-wise.
  4.  
  5. Unlike `numpy.less_equal`, ''<='', True)
项目:radar    作者:amoose136    | 项目源码 | 文件源码
  1. def greater(x1, x2):
  2. """
  3. Return (x1 > x2) element-wise.
  4.  
  5. Unlike `numpy.greater`, ''>'', True)
项目:radar    作者:amoose136    | 项目源码 | 文件源码
  1. def test_NotImplemented_not_returned(self):
  2. # See gh-5964 and gh-2091. Some of these functions are not operator
  3. # related and were fixed for other reasons in the past.
  4. binary_funcs = [
  5. np.power, np.add, np.subtract, np.multiply, np.divide,
  6. np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
  7. np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
  8. np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
  9. np.logical_and, np.logical_or, np.logical_xor, np.maximum,
  10. np.minimum, np.mod
  11. ]
  12.  
  13. # These functions still return NotImplemented. Will be fixed in
  14. # future.
  15. # bad = [np.greater,np.greater_equal,np.less,np.less_equal,np.not_equal]
  16.  
  17. a = np.array(''1'')
  18. b = 1
  19. for f in binary_funcs:
  20. assert_raises(TypeError, f, a, b)
项目:radar    作者:amoose136    | 项目源码 | 文件源码
  1. def test_identity_equality_mismatch(self):
  2. a = np.array([np.nan], dtype=object)
  3.  
  4. with warnings.catch_warnings():
  5. warnings.filterwarnings(''always'', '''', FutureWarning)
  6. assert_warns(FutureWarning, np.equal, a)
  7. assert_warns(FutureWarning, np.not_equal, a)
  8.  
  9. with warnings.catch_warnings():
  10. warnings.filterwarnings(''error'', FutureWarning)
  11. assert_raises(FutureWarning, a)
  12. assert_raises(FutureWarning, a)
  13. # And the other do not warn:
  14. with np.errstate(invalid=''ignore''):
  15. np.less(a, a)
  16. np.greater(a, a)
  17. np.less_equal(a, a)
  18. np.greater_equal(a, a)
项目:radar    作者:amoose136    | 项目源码 | 文件源码
  1. def approx(a, b, fill_value=True, rtol=1e-5, atol=1e-8):
  2. """
  3. Returns true if all components of a and b are equal to given tolerances.
  4.  
  5. If fill_value is True,masked values considered equal. Otherwise,
  6. masked values are considered unequal. The relative error rtol should
  7. be positive and << 1.0 The absolute error atol comes into play for
  8. those elements of b that are very small or zero; it says how small a
  9. must be also.
  10.  
  11. """
  12. m = mask_or(getmask(a), getmask(b))
  13. d1 = filled(a)
  14. d2 = filled(b)
  15. if d1.dtype.char == "O" or d2.dtype.char == "O":
  16. return np.equal(d1, d2).ravel()
  17. x = filled(masked_array(d1, copy=False, mask=m), fill_value).astype(float_)
  18. y = filled(masked_array(d2, 1).astype(float_)
  19. d = np.less_equal(umath.absolute(x - y), atol + rtol * umath.absolute(y))
  20. return d.ravel()
项目:blmath    作者:bodylabs    | 项目源码 | 文件源码
  1. def points_in_front(self, points, inverted=False, ret_indices=False):
  2. ''''''
  3. Given an array of points,return the points which lie either on the
  4. plane or in the half-space in front of it (i.e. in the direction of
  5. the plane normal).
  6.  
  7. points: An array of points.
  8. inverted: When `True`,invert the logic. Return the points that lie
  9. behind the plane instead.
  10. ret_indices: When `True`,return the indices instead of the points
  11. themselves.
  12.  
  13. ''''''
  14. sign = self.sign(points)
  15.  
  16. if inverted:
  17. mask = np.less_equal(sign, 0)
  18. else:
  19. mask = np.greater_equal(sign, 0)
  20.  
  21. indices = np.flatnonzero(mask)
  22.  
  23. return indices if ret_indices else points[indices]
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
  1. def equal(x1, True)
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
  1. def not_equal(x1, True)
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
  1. def greater_equal(x1, True)
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
  1. def less_equal(x1, True)
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
  1. def greater(x1, True)
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
  1. def test_NotImplemented_not_returned(self):
  2. # See gh-5964 and gh-2091. Some of these functions are not operator
  3. # related and were fixed for other reasons in the past.
  4. binary_funcs = [
  5. np.power, b)
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
  1. def test_identity_equality_mismatch(self):
  2. a = np.array([np.nan], a)
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
  1. def approx(a, atol + rtol * umath.absolute(y))
  2. return d.ravel()
项目:skggm    作者:skggm    | 项目源码 | 文件源码
  1. def has_approx_support(m, m_hat, prob=0.01):
  2. """Returns 1 if model selection error is less than or equal to prob rate,
  3. 0 else.
  4.  
  5. NOTE: why does np.nonzero/np.flatnonzero create so much problems?
  6. """
  7. m_nz = np.flatnonzero(np.triu(m, 1))
  8. m_hat_nz = np.flatnonzero(np.triu(m_hat, 1))
  9.  
  10. upper_diagonal_mask = np.flatnonzero(np.triu(np.ones(m.shape), 1))
  11. not_m_nz = np.setdiff1d(upper_diagonal_mask, m_nz)
  12.  
  13. intersection = np.in1d(m_hat_nz, m_nz) # true positives
  14. not_intersection = np.in1d(m_hat_nz, not_m_nz) # false positives
  15.  
  16. true_positive_rate = 0.0
  17. if len(m_nz):
  18. true_positive_rate = 1. * np.sum(intersection) / len(m_nz)
  19. true_negative_rate = 1. - true_positive_rate
  20.  
  21. false_positive_rate = 0.0
  22. if len(not_m_nz):
  23. false_positive_rate = 1. * np.sum(not_intersection) / len(not_m_nz)
  24.  
  25. return int(np.less_equal(true_negative_rate + false_positive_rate, prob))
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
  1. def equal(x1, True)
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
  1. def not_equal(x1, True)
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
  1. def greater_equal(x1, True)
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
  1. def less_equal(x1, True)
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
  1. def greater(x1, True)
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
  1. def test_NotImplemented_not_returned(self):
  2. # See gh-5964 and gh-2091. Some of these functions are not operator
  3. # related and were fixed for other reasons in the past.
  4. binary_funcs = [
  5. np.power, b)
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
  1. def test_identity_equality_mismatch(self):
  2. a = np.array([np.nan], a)
项目:aws-lambda-numpy    作者:vitolimandibhrata    | 项目源码 | 文件源码
  1. def equal(x1, True)
项目:aws-lambda-numpy    作者:vitolimandibhrata    | 项目源码 | 文件源码
  1. def not_equal(x1, True)
项目:aws-lambda-numpy    作者:vitolimandibhrata    | 项目源码 | 文件源码
  1. def greater_equal(x1, True)
项目:aws-lambda-numpy    作者:vitolimandibhrata    | 项目源码 | 文件源码
  1. def less_equal(x1, True)
项目:aws-lambda-numpy    作者:vitolimandibhrata    | 项目源码 | 文件源码
  1. def greater(x1, True)
项目:aws-lambda-numpy    作者:vitolimandibhrata    | 项目源码 | 文件源码
  1. def test_NotImplemented_not_returned(self):
  2. # See gh-5964 and gh-2091. Some of these functions are not operator
  3. # related and were fixed for other reasons in the past.
  4. binary_funcs = [
  5. np.power, b)
项目:aws-lambda-numpy    作者:vitolimandibhrata    | 项目源码 | 文件源码
  1. def test_identity_equality_mismatch(self):
  2. a = np.array([np.nan], a)
项目:lambda-numba    作者:rlhotovy    | 项目源码 | 文件源码
  1. def equal(x1, True)
项目:lambda-numba    作者:rlhotovy    | 项目源码 | 文件源码
  1. def not_equal(x1, True)
项目:lambda-numba    作者:rlhotovy    | 项目源码 | 文件源码
  1. def greater_equal(x1, True)
项目:lambda-numba    作者:rlhotovy    | 项目源码 | 文件源码
  1. def less_equal(x1, True)
项目:lambda-numba    作者:rlhotovy    | 项目源码 | 文件源码
  1. def greater(x1, True)
项目:lambda-numba    作者:rlhotovy    | 项目源码 | 文件源码
  1. def test_NotImplemented_not_returned(self):
  2. # See gh-5964 and gh-2091. Some of these functions are not operator
  3. # related and were fixed for other reasons in the past.
  4. binary_funcs = [
  5. np.power, b)
项目:lambda-numba    作者:rlhotovy    | 项目源码 | 文件源码
  1. def test_identity_equality_mismatch(self):
  2. a = np.array([np.nan], a)
项目:lambda-numba    作者:rlhotovy    | 项目源码 | 文件源码
  1. def approx(a, atol + rtol * umath.absolute(y))
  2. return d.ravel()
项目:deliver    作者:orchestor    | 项目源码 | 文件源码
  1. def equal(x1, True)
项目:deliver    作者:orchestor    | 项目源码 | 文件源码
  1. def not_equal(x1, True)
项目:deliver    作者:orchestor    | 项目源码 | 文件源码
  1. def greater_equal(x1, True)
项目:deliver    作者:orchestor    | 项目源码 | 文件源码
  1. def less_equal(x1, True)
项目:deliver    作者:orchestor    | 项目源码 | 文件源码
  1. def greater(x1, True)
项目:deliver    作者:orchestor    | 项目源码 | 文件源码
  1. def test_NotImplemented_not_returned(self):
  2. # See gh-5964 and gh-2091. Some of these functions are not operator
  3. # related and were fixed for other reasons in the past.
  4. binary_funcs = [
  5. np.power, b)
项目:deliver    作者:orchestor    | 项目源码 | 文件源码
  1. def test_identity_equality_mismatch(self):
  2. a = np.array([np.nan], a)
项目:deliver    作者:orchestor    | 项目源码 | 文件源码
  1. def approx(a, atol + rtol * umath.absolute(y))
  2. return d.ravel()
项目:BigbrotherBot-For-UrT43    作者:ptitbigorneau    | 项目源码 | 文件源码
  1. def atmax(a,upperlimit,dimension=None,inclusive=1):
  2. """
  3. Returns the maximum value of a,along dimension,including only values greater
  4. than (or equal to,if inclusive=1) upperlimit. If the limit is set to None,
  5. a limit larger than the max value in the array is used.
  6.  
  7. Usage: atmax(a,upperlimit,dimension=None,inclusive=1)
  8. """
  9. if inclusive: upperfcn = N.less
  10. else: upperfcn = N.less_equal
  11. if dimension == None:
  12. a = N.ravel(a)
  13. dimension = 0
  14. if upperlimit == None:
  15. upperlimit = N.maximum.reduce(N.ravel(a))+1
  16. smallest = N.minimum.reduce(N.ravel(a))
  17. ta = N.where(upperfcn(a,upperlimit),a,smallest)
  18. return N.maximum.reduce(ta,dimension)
项目:unmixing    作者:arthur-e    | 项目源码 | 文件源码
  1. def density_slice(rast, rel=np.less_equal, threshold=1000, nodata=-9999):
  2. ''''''
  3. Returns a density slice from a given raster. Arguments:
  4. rast A gdal.Dataset or a NumPy array
  5. rel A NumPy logic function; defaults to np.less_equal
  6. threshold An integer number
  7. ''''''
  8. # Can accept either a gdal.Dataset or numpy.array instance
  9. if not isinstance(rast, np.ndarray):
  10. rastr = rast.ReadAsArray()
  11.  
  12. else:
  13. rastr = rast.copy()
  14.  
  15. if (len(rastr.shape) > 2 and min(rastr.shape) > 1):
  16. raise ValueError(''Expected a single-band raster array'')
  17.  
  18. return np.logical_and(
  19. rel(rastr, np.ones(rast.shape) * threshold),
  20. np.not_equal(rastr, np.ones(rast.shape) * nodata)).astype(np.int0)

Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable

Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable

如何解决Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: ''numpy.ndarray'' object is not callable?

晚安, 尝试打印以下内容时,我在 jupyter 中遇到了 numpy 问题,并且得到了一个 错误: 需要注意的是python版本是3.8.8。 我先用 spyder 测试它,它运行正确,它给了我预期的结果

使用 Spyder:

import numpy as np
    for i in range (5):
        n = np.random.rand ()
    print (n)
Results
0.6604903457995978
0.8236300859753154
0.16067650689842816
0.6967868357083673
0.4231597934445466

现在有了 jupyter

import numpy as np
    for i in range (5):
        n = np.random.rand ()
    print (n)
-------------------------------------------------- ------
TypeError Traceback (most recent call last)
<ipython-input-78-0c6a801b3ea9> in <module>
       2 for i in range (5):
       3 n = np.random.rand ()
---->  4 print (n)

       TypeError: ''numpy.ndarray'' object is not callable

感谢您对我如何在 Jupyter 中解决此问题的帮助。

非常感谢您抽出宝贵时间。

阿特,约翰”

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

numpy.random.random & numpy.ndarray.astype & numpy.arange

numpy.random.random & numpy.ndarray.astype & numpy.arange

今天看到这样一句代码:

xb = np.random.random((nb, d)).astype(''float32'') #创建一个二维随机数矩阵(nb行d列)
xb[:, 0] += np.arange(nb) / 1000. #将矩阵第一列的每个数加上一个值

要理解这两句代码需要理解三个函数

1、生成随机数

numpy.random.random(size=None) 

size为None时,返回float。

size不为None时,返回numpy.ndarray。例如numpy.random.random((1,2)),返回1行2列的numpy数组

 

2、对numpy数组中每一个元素进行类型转换

numpy.ndarray.astype(dtype)

返回numpy.ndarray。例如 numpy.array([1, 2, 2.5]).astype(int),返回numpy数组 [1, 2, 2]

 

3、获取等差数列

numpy.arange([start,]stop,[step,]dtype=None)

功能类似python中自带的range()和numpy中的numpy.linspace

返回numpy数组。例如numpy.arange(3),返回numpy数组[0, 1, 2]

numpy.ravel()/numpy.flatten()/numpy.squeeze()

numpy.ravel()/numpy.flatten()/numpy.squeeze()

numpy.ravel(a, order=''C'')

  Return a flattened array

numpy.chararray.flatten(order=''C'')

  Return a copy of the array collapsed into one dimension

numpy.squeeze(a, axis=None)

  Remove single-dimensional entries from the shape of an array.

 

相同点: 将多维数组 降为 一维数组

不同点:

  ravel() 返回的是视图(view),意味着改变元素的值会影响原始数组元素的值;

  flatten() 返回的是拷贝,意味着改变元素的值不会影响原始数组;

  squeeze()返回的是视图(view),仅仅是将shape中dimension为1的维度去掉;

 

ravel()示例:

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 def log_type(name,arr):
 5     print("数组{}的大小:{}".format(name,arr.size))
 6     print("数组{}的维度:{}".format(name,arr.shape))
 7     print("数组{}的维度:{}".format(name,arr.ndim))
 8     print("数组{}元素的数据类型:{}".format(name,arr.dtype))
 9     #print("数组:{}".format(arr.data))
10     
11 a = np.floor(10*np.random.random((3,4)))
12 print(a)
13 log_type(''a'',a)
14 
15 a1 = a.ravel()
16 print("a1:{}".format(a1))
17 log_type(''a1'',a1)
18 a1[2] = 100
19 
20 print(a)
21 log_type(''a'',a)

 

flatten()示例

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 def log_type(name,arr):
 5     print("数组{}的大小:{}".format(name,arr.size))
 6     print("数组{}的维度:{}".format(name,arr.shape))
 7     print("数组{}的维度:{}".format(name,arr.ndim))
 8     print("数组{}元素的数据类型:{}".format(name,arr.dtype))
 9     #print("数组:{}".format(arr.data))
10     
11 a = np.floor(10*np.random.random((3,4)))
12 print(a)
13 log_type(''a'',a)
14 
15 a1 = a.flatten()
16 print("修改前a1:{}".format(a1))
17 log_type(''a1'',a1)
18 a1[2] = 100
19 print("修改后a1:{}".format(a1))
20 
21 print("a:{}".format(a))
22 log_type(''a'',a)

 

squeeze()示例:

1. 没有single-dimensional entries的情况

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 def log_type(name,arr):
 5     print("数组{}的大小:{}".format(name,arr.size))
 6     print("数组{}的维度:{}".format(name,arr.shape))
 7     print("数组{}的维度:{}".format(name,arr.ndim))
 8     print("数组{}元素的数据类型:{}".format(name,arr.dtype))
 9     #print("数组:{}".format(arr.data))
10     
11 a = np.floor(10*np.random.random((3,4)))
12 print(a)
13 log_type(''a'',a)
14 
15 a1 = a.squeeze()
16 print("修改前a1:{}".format(a1))
17 log_type(''a1'',a1)
18 a1[2] = 100
19 print("修改后a1:{}".format(a1))
20 
21 print("a:{}".format(a))
22 log_type(''a'',a)

从结果中可以看到,当没有single-dimensional entries时,squeeze()返回额数组对象是一个view,而不是copy。

 

2. 有single-dimentional entries 的情况

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 def log_type(name,arr):
 5     print("数组{}的大小:{}".format(name,arr.size))
 6     print("数组{}的维度:{}".format(name,arr.shape))
 7     print("数组{}的维度:{}".format(name,arr.ndim))
 8     print("数组{}元素的数据类型:{}".format(name,arr.dtype))
 9     #print("数组:{}".format(arr.data))
10 
11 a = np.floor(10*np.random.random((1,3,4)))
12 print(a)
13 log_type(''a'',a)
14 
15 a1 = a.squeeze()
16 print("修改前a1:{}".format(a1))
17 log_type(''a1'',a1)
18 a1[2] = 100
19 print("修改后a1:{}".format(a1))
20 
21 print("a:{}".format(a))
22 log_type(''a'',a)

 

Numpy:数组创建 numpy.arrray() , numpy.arange()、np.linspace ()、数组基本属性

Numpy:数组创建 numpy.arrray() , numpy.arange()、np.linspace ()、数组基本属性

一、Numpy数组创建

 part 1:np.linspace(起始值,终止值,元素总个数

 

import numpy as np
''''''
numpy中的ndarray数组
''''''

ary = np.array([1, 2, 3, 4, 5])
print(ary)
ary = ary * 10
print(ary)

''''''
ndarray对象的创建
''''''
# 创建二维数组
# np.array([[],[],...])
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(a)

# np.arange(起始值, 结束值, 步长(默认1))
b = np.arange(1, 10, 1)
print(b)

print("-------------np.zeros(数组元素个数, dtype=''数组元素类型'')-----")
# 创建一维数组:
c = np.zeros(10)
print(c, ''; c.dtype:'', c.dtype)

# 创建二维数组:
print(np.zeros ((3,4)))

print("----------np.ones(数组元素个数, dtype=''数组元素类型'')--------")
# 创建一维数组:
d = np.ones(10, dtype=''int64'')
print(d, ''; d.dtype:'', d.dtype)

# 创建三维数组:
print(np.ones( (2,3,4), dtype=np.int32 ))
# 打印维度
print(np.ones( (2,3,4), dtype=np.int32 ).ndim)  # 返回:3(维)

 

结果图:

 

part 2 :np.linspace ( 起始值,终止值,元素总个数)

 

import numpy as np
a = np.arange( 10, 30, 5 )

b = np.arange( 0, 2, 0.3 )

c = np.arange(12).reshape(4,3)

d = np.random.random((2,3))  # 取-1到1之间的随机数,要求设置为诶2行3列的结构

print(a)
print(b)
print(c)
print(d)

print("-----------------")
from numpy import pi
print(np.linspace( 0, 2*pi, 100 ))

print("-------------np.linspace(起始值,终止值,元素总个数)------------------")
print(np.sin(np.linspace( 0, 2*pi, 100 )))

 

结果图:

 

 

 

 

二、Numpy的ndarray对象属性:

数组的结构:array.shape

数组的维度:array.ndim

元素的类型:array.dtype

数组元素的个数:array.size

数组的索引(下标):array[0]

 

''''''
数组的基本属性
''''''
import numpy as np

print("--------------------案例1:------------------------------")
a = np.arange(15).reshape(3, 5)
print(a)
print(a.shape)     # 打印数组结构
print(len(a))      # 打印有多少行
print(a.ndim)     # 打印维度
print(a.dtype)    # 打印a数组内的元素的数据类型
# print(a.dtype.name)
print(a.size)    # 打印数组的总元素个数


print("-------------------案例2:---------------------------")
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)

# 测试数组的基本属性
print(''a.shape:'', a.shape)
print(''a.size:'', a.size)
print(''len(a):'', len(a))
# a.shape = (6, )  # 此格式可将原数组结构变成1行6列的数据结构
# print(a, ''a.shape:'', a.shape)

# 数组元素的索引
ary = np.arange(1, 28)
ary.shape = (3, 3, 3)   # 创建三维数组
print("ary.shape:",ary.shape,"\n",ary )

print("-----------------")
print(''ary[0]:'', ary[0])
print(''ary[0][0]:'', ary[0][0])
print(''ary[0][0][0]:'', ary[0][0][0])
print(''ary[0,0,0]:'', ary[0, 0, 0])

print("-----------------")


# 遍历三维数组:遍历出数组里的每个元素
for i in range(ary.shape[0]):
    for j in range(ary.shape[1]):
        for k in range(ary.shape[2]):
            print(ary[i, j, k], end='' '')
            

 

结果图:

 

今天关于Python numpy 模块-less_equal() 实例源码的讲解已经结束,谢谢您的阅读,如果想了解更多关于Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable、numpy.random.random & numpy.ndarray.astype & numpy.arange、numpy.ravel()/numpy.flatten()/numpy.squeeze()、Numpy:数组创建 numpy.arrray() , numpy.arange()、np.linspace ()、数组基本属性的相关知识,请在本站搜索。

本文标签: