栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

numpy学习(4)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

numpy学习(4)

import numpy as np

t1 = np.arange(12).reshape(3, 4).astype("float")
t1[1, 2:] = np.nan
print(t1)


# 用列均值替换nan
def fill_nan(t1):
    for i in range(t1.shape[1]):  # 遍历每一列
        temp_col = t1[:, i]
        nan_num = np.count_nonzero(temp_col != temp_col)
        if nan_num > 0:
            temp_not_nan_col = temp_col[temp_col == temp_col]
            temp_col[np.isnan(temp_col)] = temp_not_nan_col.mean()
    return t1


print(fill_nan(t1))

t2 = np.arange(12).reshape(3, 4).astype("float")

# 竖直拼接
t3 = np.vstack((t1, t2))
print(t3)
# 水平拼接
t4 = np.hstack((t1, t2))
print(t4)

# 行交换
t1[[1, 2], :] = t1[[2, 1], :]
print(t1)

# 列交换
t1[:, [1, 2]] = t1[:, [2, 1]]
print(t1)

# 创建一个全0数组
t5 = np.ones((2, 3))
print(t5)
t6 = np.zeros((3, 4))
print(t6)
t7 = np.eye(4)
print(t7)

#生成随机数组
t8=np.random.rand(3,4)
print(t8)

#ab结果一致
np.random.seed(3)
a = np.random.rand(3)
print(a)
np.random.seed(3)
b = np.random.rand(3)
print(b)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/317527.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号