Numpy矩阵的常用操作

news/2024/7/4 19:46:25
  1. 开方

    import numpy as np
    B=np.arange(3)
    print(B)
    print(np.exp(B))
    print(np.sqrt(B))
    

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

  2. floor向下取整

    a=np.floor(10*np.random.random((3,4)))
    print(a)
    print('----')
    # 把矩阵变成向量
    print(a.ravel())
    print('----')
    a.shape=(6,2)
    print(a)
    print('----')
    print(a.T)
    

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

  3. 水平拼接:

    a=np.floor(10*np.random.random((2,2)))
    b=np.floor(10*np.random.random((2,2)))
    print(a)
    print('---')
    print(b)
    print('---')
    print(np.hstack((a,b)))
    

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

  4. 垂直拼接:

    a=np.floor(10*np.random.random((2,2)))
    b=np.floor(10*np.random.random((2,2)))
    print(a)
    print('---')
    print(b)
    print('---')
    print(np.vstack((a,b)))
    

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

  5. 分割:

    a=np.floor(10*np.random.random((2,12)))
    print(a)
    print('---')
    print(np.hsplit(a,3))
    print('---')
    print(np.hsplit(a,(3,4)))
    a=np.floor(10*np.random.random((12,2)))
    print('---')
    print(a)
    np.vsplit(a,3)
    

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


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

相关文章

不同复制操作对比

复制 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…

VS 2010之多显示器支持 / Multi-Monitor Support (VS 2010 and .NET 4 Series)

【原文地址】Multi-Monitor Support (VS 2010 and .NET 4 Series) 【原文发表日期】 Monday, August 31, 2009 10:37 PM 这是我针对即将发布的VS 2010 和 .NET 4所撰写的 贴子系列的第四篇。 今天的贴子讨论其中一个IDE改进,我知道很多人都在迫切期望VS 2010的--…

pandas自定义函数

sort_values和reset_index new_titanic_survival titanic_survival.sort_values("Age",ascendingFalse) print (new_titanic_survival[0:10]) titanic_reindexed new_titanic_survival.reset_index(dropTrue) print(titanic_reindexed.iloc[0:10])运行结果&#xf…

Series结构

读取csv文件: import pandas as pd fandango pd.read_csv(fandango_score_comparison.csv) series_film fandango[FILM] print(series_film[0:5]) series_rt fandango[RottenTomatoes] print (series_rt[0:5])运行结果: 制作Series # Import the Se…

折线图的绘制

to_datetime import pandas as pd unrate pd.read_csv(unrate.csv) unrate[DATE] pd.to_datetime(unrate[DATE]) print(unrate.head(12))运行结果: 绘图 from pandas.plotting import register_matplotlib_converters #%matplotlib inline #Using the different…