【学习笔记9】一些遇到的如何写code的问题

一、计算e(x):

import math
result = math.exp(x)

import numpy as np
result = np.exp(x)

二、matplotlib.pyplot坐标无法显示中文:

plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

三、matplotlib.pyplot横纵坐标无法按顺序显示:

int(money)   #将数据改为实数类型

四、shape函数的性质:

>>> x = np.array([[1, 2, 3], [4, 5, 6]])     # 只适用于numpy数组
>>> x.shape  
(2, 3)                                           # 输出一个元组
>>> x.shape[0] 
2
>>> x.shape[1] 
3

五、np.shape()的使用:

np.shape(x)x.shape,返回元组

六、np.max()和np.maximum()的使用:

np.max()求自身的最大值,而np.maxmum()求两者之间的最大值。

>>>A = np.array([[1,8,3,6,5],[9,2,7,4,5]])
>>>np.max(A)
9
>>>np.max(A, axis=0)
array([9, 8, 7, 6, 5])
>>>np.max(A, axis=1)
array([8, 9])
>>>np.maximum(A, 5)
array([[5, 8, 5, 6, 5],
       [9, 5, 7, 5, 5]])
>>>np.maximum(A,10)
array([[10, 10, 10, 10, 10],
       [10, 10, 10, 10, 10]])

七、np.hstack&np.vstack

np.hstack:接受一个输入,水平方向拼接
np.vstack:接受一个输入,竖直方向拼接,注意拼接元素的维度

>>> import numpy as np
>>> y = np.hstack(1, (2,3,4), 4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 198, in hstack
TypeError: hstack() takes 1 positional argument but 3 were given
>>> y = np.hstack((1, (2,3,4), 4)) 
>>> y
array([1, 2, 3, 4, 4])
>>> y.shape
(5,)
>>> y = np.vstack((1, (2,3,4), 4))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 200, in vstack
  File "D:\Program Files\anaconda\Lib\site-packages\numpy\core\shape_base.py", line 296, in vstack
    return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<__array_function__ internals>", line 200, in concatenate
ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 ha
s size 1 and the array at index 1 has size 3
>>> y = np.vstack((1, 5, 4))       
>>> y
array([[1],
       [5],
       [4]])
>>> y.shape
(3, 1)

八、np.random.normal()

生成指定方差、均值、维度的分布

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值