立即注册 登录
气象家园 返回首页

主啊的个人空间 https://bbs.06climate.com/?69999 [收藏] [复制] [分享] [RSS]

日志

numpy学习笔记,python数据科学手册

已有 92 次阅读2023-6-13 23:38 |个人分类:python



import pandas as pd
import numpy as np

ndarray = np.array([1,4,2,5],dtype = 'float32')
print(ndarray)

print(ndarray.ndim) #维度
print(ndarray.shape)
print(ndarray.dtype)
# 获取单个元素
print(ndarray[0])

# 运算
print(np.sum(ndarray))
print(np.max(ndarray))
print(np.min(ndarray))
# 二维度的
nd = np.random.random((3,4))
print(nd)
print("*"*60)
print(nd.max(axis=0)) # 每一行
print(nd.min(axis=1)) # 每一列


# 挑选
rng = np.random.RandomState(0)
x = rng.randint(10,size=(3,4))
print("x"*60)
print(x)
print(x[x<6])
# 以下是求小于6的个数和
print(np.count_nonzero(x<6))
print(np.sum(x<6))
print(np.sum(x<6,axis=1))


# 从小到大排序
def selection_sort(x):
    for i in range(len(x)):
        swap = i + np.argmin(x[i:])  # 求最小的索引
        (x[i], x[swap]) = (x[swap], x[i]) # 数值替换
    return x
x= np.array([1,5,9,4,6])
print(selection_sort(x))
print(np.sort(x))
# d对原数组排序并保存
x.sort()
print(x)
print(np.sort(nd,axis=0))
print(np.sort(nd,axis=1))

x = np.zeros(4,dtype=int)
print(x)
# 结构化数组
name = ["alice","bob","caathy","dang"]
age = [25,45,37,19]
weight = [55, 60, 65, 45]
data = np.zeros(4,dtype={"names":("name","age","weight"),
                         "formats":("U10","i4","f8")})
print(data.dtype)
data["name"] = name
data["age"] = age
data["weight"] = weight
print(data)

print(data[0]) # 第一行
print(data["name"]) #所有名字
print(data[-1]["name"]) #最后一行的名字
print(data[data["age"]<30]["name"])




















评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

返回顶部