Numpy矩阵基础

news/2024/7/5 21:26:05
  1. 与操作

    vector=numpy.array([5,10,15,20])
    equal_to_ten_and_five=(vector==10)&(vector==5)
    print(equal_to_ten_and_five)
    

    运行结果:
    在这里插入图片描述

  2. 或操作

    equal_to_ten_or_five=(vector==10)|(vector==5)
    print(equal_to_ten_or_five)
    

    运行结果:
    在这里插入图片描述

  3. 类型转换

    vector=numpy.array(["1","2","3"])
    print(vector.dtype)
    print(vector)
    vector=vector=vector.astype(float)
    print(vector.dtype)
    print(vector)
    

    运行结果:
    在这里插入图片描述

  4. 最值

    vector=numpy.array([5,10,15,20])
    vector.min()
    

    运行结果:
    在这里插入图片描述

  5. 求和

    matrix=numpy.array([[5,10,15],
                       [0,25,30],
                       [30,35,40]])
    matrix.sum(axis=1)
    

    运行结果:
    在这里插入图片描述

    matrix.sum(axis=0)
    

    运行结果:在这里插入图片描述


http://www.niftyadmin.cn/n/4714820.html

相关文章

编译和测试环境——VMware:简介和教程

因为开发过程是非常依赖开发环境的,比如我们使用的是Windows,如果要开发Unix的软件,往往要到Unix环境才行。至少要在该环境下测试一下,看看有没有问题。但是,通常我们只有一台或几台电脑,不可能每个环境都有…

Visual Studio 2010并行编程及调试诊断功能

Visual Studio2010 Beta 1发布后,开发人员从其新特性,新功能中得到了不少的帮助。这里将介绍Visual Studio 2010并行编程方面的改进,较Visual Studio 2008有了极大的提高。 每当出现新的编程模型时,开发人员便需要一个用来学习、…

微软Windows操作系统发展史

Windows发展史古人云:名满天下,谤亦随之。微软Windows操作系统获得巨大成功的同时,批判声总不绝于耳。下面,笔者将和大家一道回顾Windows的发展历程。MS-DOS 1.01981年8月,IBM公司推出了运行微软16位操作系统MS-DOS 1.…

Numpy矩阵的常用操作

开方 import numpy as np Bnp.arange(3) print(B) print(np.exp(B)) print(np.sqrt(B))运行结果: floor向下取整 anp.floor(10*np.random.random((3,4))) print(a) print(----) # 把矩阵变成向量 print(a.ravel()) print(----) a.shape(6,2) print(a) print(----)…

不同复制操作对比

复制 anp.arange(12) ba print(b is a) b.shape3,4 print(a.shape) print(id(a)) print(id(b))运行结果: 浅复制 ca.view() print(c is a) c.shape2,6 print(a.shape) c[0,4]1234 print(a) print(id(a)) print(id(c))运行结果: 深复制 da.copy() pri…

Pandas数据读取+索引计算

读csv文件 import pandas food_infopandas.read_csv(food_info.csv) print(type(food_info)) print(food_info.dtypes) print(help(pandas.read_csv))运行结果: 显示前5行 food_info.head()运行结果: food_info.head(3)运行结果: 显示后…

pandas数据预处理实例

排序,默认从小到大排 #By default, pandas will sort the data by the column we specify in ascending order and return a new DataFrame # Sorts the DataFrame in-place, rather than returning a new DataFrame. #print food_info["Sodium_(mg)"] fo…

pandas常用预处理方法

求均值,表格中含有空值: #The result of this is that mean_age would be nan. This is because any calculations we do with a null value also result in a null value mean_age sum(titanic_survival["Age"]) / len(titanic_survival[&qu…