首页 > 其他分享 >numpy学习

numpy学习

时间:2023-02-27 16:45:30浏览次数:37  
标签:arange 学习 print shape test np array numpy

import numpy as np

# a = np.array([1, 2, 3])
# print(a[0])
# b = np.array([[1, 2, 3], [4, 5, 6]])
# print(b[0])
# print(b.shape)

# 创造numpy的方式
# np.array(), np.zeros(), np.ones(),
# np.empty(), np.arange(), np.linspace(), dtype
print(np.zeros(4))
print(np.ones(4, dtype=np.int16))
print(np.empty(3))  # 随机赋值
print(np.arange(4))
print(np.arange(4, 12, 4))
print(np.linspace(0, 10, num=6))  # 生成指定范围内的均匀等差数列

# 增添、移除和排序元素
# np.sort(), np.concatenate()
# aixs用来指定轴,第0轴为纵向,第1轴为横向,None为平展排序
print(np.sort(np.array([[2, 4, 3], [3, 7, 1]]), axis=0))
dtype = [('name', 'S10'), ('height', float), ('age', int)]
values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38), ('Galahad', 1.7, 38)]
people = np.array(values, dtype=dtype)       # create a structured array
print(np.sort(people, order=['name', 'age']))
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
print(np.concatenate((x, y), axis=0))

# 获取array的shape和size
# ndarray.ndim, ndarray.size, ndarray.shape
# 分别是维数、总元素、shape(各个维数的元素个数)

# reshape array
# arr.reshape() ?arr和ndarray的区别
print(np.arange(6))
# order C按行输出 F按列输出 A按内存顺序输出
print(np.arange(6).reshape((2, 3), order='A'))
print(np.reshape(np.arange(6), (2, 3), order='F'))

# 把1D array转化为2D array
# np.newaxis, np.expand_dims
print(np.arange(1, 7)[np.newaxis, :, np.newaxis].shape)
print(np.expand_dims(np.arange(1, 7), axis=1).shape)

# 索引和切片
# 直接索引[index]
# 切片索引1[start:end](不包括end)
# 切片索引2[start:]
# 若start为正数,从前往后索引;负数,从后往前
# 条件索引
test = np.array([[1, 2, 3, 4], [4, 6, 7, 8], [9, 10, 11, 12]])
print(test[test < 5])
five_up = (test > 5)
print(test[five_up])
print(five_up)
divisible_by_2 = test[test % 2 == 0]
print(divisible_by_2)
# 返回满足条件的数字的索引,返回值数据类型为tuple
print(np.nonzero(test < 5))
print(test[np.nonzero(test < 5)])  

# 怎么从已存在的数据创建array

 

标签:arange,学习,print,shape,test,np,array,numpy
From: https://www.cnblogs.com/zqyn/p/17160305.html

相关文章

  • 学习笔记
    绑定事件bindtap="函数名",函数传参xxdata-xx="1122"js里函数(e){获取参数varx=e.currentTarget.dataset.xx;或者e.target.dataset.xx}跳转页面wx.navigateTo(......
  • python+playwright 学习-15.录制视频功能
    前言使用Playwright,您可以为测试录制视频。录制视频在测试结束时浏览器上下文关闭时保存。如果您手动创建浏览器上下文,请确保browser_context.close(),会在调用close......
  • 各类梯度下降算法的numpy实现
    layout:posttitle:深度学习subtitle:梯度下降算法实现description:梯度下降算法实现date:2022-10-25categories:deeplearningtags:codepy......
  • Ubuntu-深度学习环境搭建(yolov3)
    layout:posttitle:深度学习环境搭建subtitle:深度学习环境搭建date:2021-04-25author:Yinheader-img:img/post-bg-cook.jpgcatalog:tru......
  • 学习swoole之前,你需要知道的几件事
    学习swoole需要的前置知识学习一项新的技术,最重要的就why、what、how。这门技术是什么,为什么用它,要怎么用它。这篇文档的作用就是为了解释what与why。php-fpm与swoole的......
  • Jmeter学习:插件
    第三方插件官方下载网址:https://jmeter-plugins.org/install/Install/第三方插件官方文档网址:https://jmeter-plugins.org/wiki/Start/插件安装过程如下:1、下载plugin......
  • 推荐 7 个学习 TypeScript 的宝库,2021 学 TS 看这篇就够了!
    前言猫哥是一个常年混迹在GitHub上的猫星人,所以发现了不少好的前端开源项目、常用技巧,在此分享给大家。公众号:前端GitHub,专注于挖掘GitHub上优秀的前端开源项目,并以......
  • 借助chatgpt学习对比学习中的疑惑
    使用pytorch得到对比学习的正负样本,要求是NLP方向,正样本通过dropout(0.1)得到,负样本使用batch内其他样本。同时,帮我用pytorch实现他们的对比学习损失,用simcse的损失以下......
  • python+playwright 学习-14.导航page.goto(url) 详解
    前言Playwright可以导航到URL并处理由页面交互引起的导航。本篇涵盖了等待页面导航和加载完成的常见场景。导航生命周期导航从更改页面URL或通过与页面交互(例如,单......
  • python Numpy数组2.27
    #成员类型转换arr.astype(np.float_)#转换数组对象成员的类型为float,形状不变。#形状转换arr.resize(shape)#返回值是一个None,不能引用内部的属性arr.reshape(shape)#......