numpy函数------周东海

这篇博客介绍了如何使用numpy库进行数据类型的定义和数组创建,包括int32和float32数据类型,以及如何创建包含结构化数据的数组。此外,还展示了数组的变形、迭代、维度对换、广播、连接和分割等操作,提供了丰富的示例来说明这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


import numpy as np
a = np.dtype(np.int32)
print(a)

# 数据类型对象
b = np.dtype(np.float32)
# b = np.array([1, 2, 3])
# b = np.dtype('i4')
# 创建数组,指定数据类型
c = np.array([1, 2, 3], dtype=b)
print(c)
# 定义数据类型
dt = np.dtype([('age', np.int8)])
# 用dt数据类型创建数组
e = np.array([(10,), (20,), (30,)], dtype=dt)
print(e)
# 定义数据类型
st = np.dtype([('name','S20'),('age','i4'),('marks','f4')])
#创建数组
student = np.array([('zhang',18,60),('li',22,80)])
print(student)
a = np.dtype(np.int32)
print(a)

# 数据类型对象
b = np.dtype(np.float32)
# b = np.array([1, 2, 3])
# b = np.dtype('i4')
# 创建数组,指定数据类型
c = np.array([1, 2, 3], dtype=b)
print(c)
# 定义数据类型
dt = np.dtype([('age', np.int8)])
# 用dt数据类型创建数组
e = np.array([(10,), (20,), (30,)], dtype=dt)
print(e)
# 定义数据类型
st = np.dtype([('name','S20'),('age','i4'),('marks','f4')])
#创建数组
student = np.array([('zhang',18,60),('li',22,80)])
print(student)

# 变形
a = np.arange(8)
print(a)
b = a.reshape(4,2)
print(b)

# 迭代
a = np.arange(9).reshape(3,3)
# 输出行
for i in a:
    print(i)
# 输出单个数据
for i in a.flat:
    print(i)
# 输出单个数据
for i in np.nditer(a):
    print(i)

# 拷贝一份数据
a = np.arange(8).reshape(2,4)
print(a.flatten().reshape(4,2))
print(a)

print(a.ravel().reshape(4,2))
print(a)

# 对换数组的维度
a = np.arange(12).reshape(3,4)
print(np.transpose(a))

# 广播到新形状
a = np.arange(4).reshape(1,4)
print(np.broadcast_to(a,(4,4)))

# 连接数组
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
print(np.concatenate((a,b)))

# 分割数组
a = np.arange(9)
b = np.split(a,3)
print(b)

# 添加数据
a = np.array([[1,2,3],[4,5,6]])
b = np.append(a,[7,8,9])
print(b.reshape(3,3))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[25], line 12 10 ax.coastlines() 11 ax.set_extent([120, 130, 25, 35]) # 东海经纬度范围 ---> 12 plt.contourf(lon, lat, marine_heatwaves[year_idx], transform=proj, cmap='Reds') 13 plt.colorbar(label='海洋热浪发生区域') 14 plt.title(f'东海海洋热浪分布({time_dates[jan1_indices[year_idx]].year}年1月1日)') File D:\anaconda\app\Lib\site-packages\matplotlib\pyplot.py:3154, in contourf(data, *args, **kwargs) 3152 @_copy_docstring_and_deprecators(Axes.contourf) 3153 def contourf(*args, data=None, **kwargs) -> QuadContourSet: -> 3154 __ret = gca().contourf( 3155 *args, **({"data": data} if data is not None else {}), **kwargs 3156 ) 3157 if __ret._A is not None: # type: ignore[attr-defined] 3158 sci(__ret) File ~\AppData\Roaming\Python\Python312\site-packages\cartopy\mpl\geoaxes.py:306, in _add_transform.<locals>.wrapper(self, *args, **kwargs) 301 raise ValueError(f'Invalid transform: Spherical {func.__name__} ' 302 'is not supported - consider using ' 303 'PlateCarree/RotatedPole.') 305 kwargs['transform'] = transform --> 306 return func(self, *args, **kwargs) File ~\AppData\Roaming\Python\Python312\site-packages\cartopy\mpl\geoaxes.py:350, in _add_transform_first.<locals>.wrapper(self, *args, **kwargs) 348 # Use the new points as the input arguments 349 args = (x, y, z) + args[3:] --> 350 return func(self, *args, **kwargs) File ~\AppData\Roaming\Python\Python312\site-packages\cartopy\mpl\geoaxes.py:1646, in GeoAxes.contourf(self, *args, **kwargs) 1625 @_add_transform 1626 @_add_transform_first 1627 def contourf(self, *args, **kwargs): 1628 """ 1629 Add the "transform" keyword to :func:`~matplotlib.pyplot.contourf`. 1630 (...) 1644 The default is False, to co
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值