python数据分析——numpy基本操作

import numpy as np

"""数组结构"""
array = np.array([1, 2, 3, 4, 5])
print(type(array))  # <class 'numpy.ndarray'>
print(array.shape)  # (5,)
print(array.itemsize)  # 4
print(array.size)  # 5
np.size(array)  # 5
np.shape(array)
print(array.ndim)  # 1

tang_list = [1, 2, 3, 4, 5]
tang_array = np.array(tang_list)
print(tang_array[1:3])
tang_array2 = tang_array.copy()
tang_array3 = np.arange(0, 100, 10)
random_array = np.random.rand(10)
mask = random_array > 0.5  # array([ True,  True, False,  True,  True,  True,  True, False,  True,  True], dtype=bool)

"""array数组的数值计算"""
np.sum(tang_array)
np.sum(tang_array, axis=0)
np.sum(tang_array, axis=1)
print(tang_array.argmin())  # 找到索引位置

"""排序"""
tang_array = np.array([[1.5, 1.3, 7.5],
                       [5.6, 7.8, 1.2]])
np.sort(tang_array)
np.sort(tang_array, axis=0)
np.argsort(tang_array)
# 插入
tang_array = np.linspace(0, 10, 10)
values = np.array([2.5, 6.5, 9.5])
np.searchsorted(tang_array, values)  # array([3, 6, 9], dtype=int64)

"""数组形状"""
tang_array = np.arange(10)
tang_array.shape = 2, 5  # array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]])
tang_array = tang_array[:, np.newaxis, np.newaxis]  # (10, 1, 1)
tang_array = tang_array.squeeze()
tang_array.transpose()  # tang_array.T

"""数组的连接"""
a = np.array([[123, 456, 678], [3214, 456, 134]])
b = np.array([[1235, 3124, 432], [43, 13, 134]])
c = np.concatenate((a, b))  # c = np.concatenate((a,b),axis = 0)
d = np.concatenate((a, b), axis=1)
np.vstack((a, b))
np.hstack((a, b))

"""数组的生成"""
np.linspace(0, 10, 10)
np.logspace(0, 1, 5)
# np.r_[0:10:1]
# np.c_[0:10:1]
np.zeros((3, 3))
np.ones((3, 3))
np.zeros_like(tang_array)
np.ones_like(tang_array)
np.identity(5)

"""数组运算"""
x = np.array([5, 5])
y = np.array([2, 2])
np.multiply(x, y)
np.dot(x, y)
np.logical_and(x, y)
np.logical_or(x, y)
np.logical_not(x, y)

"""Numpy的随机模块"""
np.random.rand(3, 2)  # 所有的值都是从0到1
np.random.randint(10, size=(5, 4))  # 返回的是随机的整数,左闭右开
np.random.randint(0, 10, 3)
mu, sigma = 0, 0.1
np.random.normal(mu, sigma, 10)
np.random.seed(100)

"""使用Numpy读写数据"""
# %%writefile tang.txt
# 1 2 3 4 5 6
# 2 3 5 8 7 9
data = np.loadtxt('tang2.txt', delimiter=',', skiprows=1)
# 'tang2.txt':路径最好放到和代码一起
# skiprows : 去掉几行
# delimiter = ',' :分隔符
# usecols = (0,1,4) :指定使用哪几列

# 构建一个shape为(6,7,8)的矩阵,并找到第100个元素的索引值
np.unravel_index(100, (6, 7, 8))

# 对一个5*5的矩阵做归一化操作
tang_array = np.random.random((5, 5))
tang_max = tang_array.max()
tang_min = tang_array.min()
tang_array = (tang_array - tang_min) / (tang_max - tang_min)

# 找到两个数组中相同的值
z1 = np.random.randint(0, 10, 10)
z2 = np.random.randint(0, 10, 10)
print(z1)
print(z2)
print(np.intersect1d(z1, z2))

# 得到一个数的整数部分
z = np.random.uniform(0, 10, 10)
np.floor(z)

# 找到一个数组中最常出现的数字
z = np.random.randint(0, 10, 50)
print(np.bincount(z).argmax())


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值