Numpy task01

ndarray基础

1. 大数据容器

2. 执行数学运算

3. 容器内所有的元素必须是相同类型的

4. 构成: 一个shape(一个表示各维度大小的原件)和 dtype(一个用于说明数组数据类型的对象)

5. 创建ndarry

#创建数组--使用array函数
data1 = [6, 7.5, 8, 0, 1]
arr1 = np.array(data1)
arr1
array([6., 7.5., 8., 0., 1.])

#嵌套序列,一组等长列表组成的列表,将会被转换为一个多维数组
data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr2 = np.array(data2)
arr2
array = ([[1., 2., 3., 4.], [5., 6., 7., 8.]])

6. 对比ndim、shape、dtype、astype的用法

–ndim 返回的是数组的维度,返回的只有一个数,该数即表示数组的维度。
–shape:表示各位维度大小的元组。返回的是一个元组。
–dtype:一个用于说明数组数据类型的对象。返回的是该数组的数据类型。由于图中的数据都为整形,所以返回的都是int32。如果数组中有数据带有小数点,那么就会返回float64。
–astype:转换数组的数据类型。

#数据
data1 = [1, 2, 3, 4, 5, 6]
data2 = [[1, 2, 3], [4, 5, 6]]
data3 = [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]
arr1 = np.array(data1)
arr2 = np.array(data2)
arr3 = np.array(data3)

#ndim
print(arr1.ndim, arr2.ndim, arr3.ndim)
1 2 3
#在numpy中可以理解为方向,使用0,1,2...数字表示,对于一个一维数组,只有一个0轴,对于2维数组(shape(2,2)),有0轴和1轴,对于三维数组(shape(2,2, 3)),有0,1,2轴

#shape
print(arr1.shape, arr2.shape, arr3.shape)
623) (223)
arr1 一维数组 1个数组 6个元素
arr2 二维数组 2个数组 3个元素 
arr3 二维数组 2个数组,2个元素,3个元素

#dtype
print(arr1.dtype, arr2.dype, arr3.dype)
int32 int32 int32

#astype
arr1
array([1., 2., 3.])

arr1.astype('int32')
array([1, 2, 3])

#astype:转换数组的数据类型。
#int32 --> float64        完全ok
#float64 --> int32        会将小数部分截断
#string_ --> float64        如果字符串数组表示的全是数字,也可以用astype转化为数值类型

例如
str1 = np.array(['1.25', '2.51', '3.11'], dtype=np.string)
str1.astype('float')
array([1.25, 2.51, 3.11])

7. zeros, ones, empty

-zeros 创建长度或者形状为0的数组

import numpy as np
 
x = np.zeros(5)
print(x)  # [0. 0. 0. 0. 0.]
x = np.zeros([2, 3])
print(x)
# [[0. 0. 0.]
#  [0. 0. 0.]]
 
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.zeros_like(x)
print(y)
# [[0 0 0]
#  [0 0 0]]

-ones 创建长度或者形状为1的数组


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于安装numpy3.6.8,你可以按照以下步骤进行操作: 1. 首先,你需要在官网下载numpy的安装包。你可以在https://pypi.Python.org/pypi/numpy找到numpy的下载地址。请确保选择与你的Python版本相对应的numpy版本。\[1\] 2. 下载完成后,你可以在命令行中使用pip命令来安装numpy。首先,打开命令行终端。在Windows系统中,你可以在开始菜单中搜索cmd来打开终端。\[3\] 3. 在终端中,输入以下命令来升级pip工具: ``` python -m pip install -U pip ``` 4. 然后,使用pip命令来安装numpy。你可以使用以下命令: ``` pip install numpy==3.6.8 ``` 请注意,这里的"3.6.8"是你想要安装的numpy版本号。确保你输入的版本号与你下载的numpy安装包相对应。\[1\] 这样,你就可以成功安装numpy3.6.8了。希望对你有帮助! #### 引用[.reference_title] - *1* *3* [python3.6安装numpy-Python3.6_安装numpy](https://blog.csdn.net/weixin_37988176/article/details/109420238)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [CentOS下完全离线安装python3.6.8、pip3和numpy](https://blog.csdn.net/qq_32599479/article/details/98951430)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值