数据科学库--numpy中对数组,文件的操作,索引和切片,对缺失值的处理,数据拼接,等其他方法

这篇博客详细介绍了numpy库在数据科学中的应用,包括数组的基本操作如转置,文件读取,索引和切片,以及如何修改数组值。还探讨了如何处理缺失值(NaN),使用where()和clip()函数,以及统计方法如求和、均值、中值等。此外,文章展示了填充NaN值、数组拼接和一些实用的numpy方法,如获取最大值和最小值位置,创建特殊数组,以及生成随机数。
摘要由CSDN通过智能技术生成

numpy中的对数组的一些基本操作

下面是numpy在对数组处理上的一些基本应用:

import numpy as np
import random

t1 = np.array([1, 2, 3])
print(t1)
print(type(t1))

t2 = np.arange(4, 10, 3)
print(t2)
print(type(t2))
print(t2.dtype)

# numpy中的数据类型
t3 = np.array(range(1, 4), dtype="i1")
print(t3)
print(t3.dtype)

# numpy中的bool类型
t4 = np.array([1, 1, 0, 1, 0, 0], dtype=bool)
print(t4)
print(t4.dtype)

# numpy中调整数据类型

t5 = t4.astype("int8")
print(t5)
print(t5.dtype)

# numpy 中的小树
t6 = np.array([random.random() for i in range(10)])
print(t6)
print(t6.dtype)

# 设置精度
t7 = np.round(t6, 2)
print(t7)

# shape 和 reshape 方法

t8 = np.array([[[1, 2, 3], [4, 5, 6]], [[10, 11, 12], [13, 14, 15]]])
print(t8.shape)

t9 = np.arange(12)  # reshape 方法有返回值,并不会改变原数组
print(t9.reshape((3, 4)))
print(t9)
t9 = t9.reshape((3, 4))
# t9变回一元数组的两种方式的列数
print(t9.reshape(12, ))
print(t9.flatten())

# numpy中数组的运算,数据的运算具有广播原则
t10 = np.array(range(5))
print(t10 / 5)
print(t10 + 2)

numpy数组转置

import  numpy as np


t1 = np.arange(24).reshape((4,6))
#numpy数组转置的三种方式
print("原数组为:\n",t1)
print("第一种方式为:\n",t1.transpose())
print("-"*100)
print("第二种方式为:\n",t1.T)
print("-"*100)
print("第三种方式为:\n",t1.swapaxes(1,0))

输出:

原数组为:
 [[ 0  1  2  3  4  5]
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值