Numpy温习函数方法

简介: Numpy温习函数方法

一、Numpy Pandas


1.1 简介


方便数组 矩阵运算


1.2 优势


运算速度快:numpy 和 pandas 都是采用 C 语言编写, pandas 又是基于 numpy, 是 numpy 的升级版本。

消耗资源少:采用的是矩阵运算,会比 python 自带的字典或者列表快好多。

代码更简洁:大量的数学函数【数组、矩阵】

性能更高效:数组 自己的存储格式,比原生的List好很好。


二、安装Numpy


2.1 anaconda


1.自带了Numpy,继承了非常多的库,首选安装环境。

2.pip install numpy

import numpy as np


三、Numpy vs Python


3.1 数组加法


1.数组 A B为1~N的数字的平方和立方

1 2 4 9 …

1 3 27 64 …

20201127154823533.png


20201127154839622.png


20201127155456465.png

四、Numpy-Array


1.Numpy的核心数据结构,就叫做array就是数组, array对象可以是一维数组, 也可以是多维数组;

2.Python的List也可以实现相同的功能, 但是aray比List的优点在于性能好、 包含数组元数据信息大量的便捷函数;

3.Numpy成为事实上的Scipy. Pandas、 Scikit-Learn. Tensorflow. PaddlePaddle等框架的“通用底层语言”

4.Numpy的array和Python的List的一 个区别,它元素必须都是同一种数据类型,比如都是数字int类型,这也是Numpy高性能的一个原因;


array本身的属性

●shape: 返回一个元组,表示array的维度

●ndim: -个数字,表示array的维度的数目

●size: -个数字,表示array中所有数据元素的数目

●dtype: array中元素的数据类型


4.1 Array创建操作


1.从Python的列表L ist和嵌套列表创建array

2.使用预定函数arange. ones/ones_ like. zeros/zeros_ ike. empty/empty like. fllulll like. eye等函数创建

3.生成随机数的np.random模块构建


import numpy as np
# 1.创建数组
x = np.array([1,2,3,4,5,6,7,8])
y = np.array([[123,456,789],[123,456,789]])
print(x)      # [1 2 3 4 5 6 7 8]
print(y)      # [[123 456 789] [123 456 789]]
# 2.array数组的属性
print(x.shape)  # (8,)
print(y.shape)  # (2, 3)
print(y.size)   # 6
print(x.dtype)  # int32
# 3.创建便捷函数
z1 = np.arange(10)      # [0 1 2 3 4 5 6 7 8 9]
z2 = np.arange(2,10,2)  # [2 4 6 8]
print(np.random.rand())       # 0.8003246658237291
print(np.random.rand(3))      # [0.22203232 0.07369858 0.02427968]
print(np.random.rand(3,2))    # [[0.66349363 0.86112248] [0.11068548 0.89208492] [0.34897576 0.57025273]]


4.2 数组索引查询

image.png

image.png


1.给已有的数据添加多行,比如增添一些样本数据进去;

2.给已有的数据添加多列,比如增添一些特征进去;

以下操作均可以实现数组合并:

●np.concatenatelarray_ list, axis=0/1) : 沿着指定axis进行数组的合并

●np.vstack或者np.row_ stackarray_ list) :垂直vertically.按行row wise进行数据合并

●np.hstack或者np.column_ stackarray_ Jist) :水平horizontally.按列column wise进行数据合并


Numpy与Pandas数据的相互转换

Pandas是在Numpy基础上建立的非常流行的数据分析类库;

提供了强大针对异构、表格类型数据的处理与分析能力。

本节绍Numpy和Pandas的转换方法:


Numpy数组怎样输入给Pandas的Series、DataFrame ;

Pandas的Series、 DataFrame怎样转换成Numpy的数组

import numpy as np

import numpy as np
# 1.创建数组
x = np.array([1,2,3,4,5,6,7,8])
y = np.array([[123,456,789],[123,456,789]])
print(x)      # [1 2 3 4 5 6 7 8]
print(y)      # [[123 456 789] [123 456 789]]
# 2.array数组的属性
print(x.shape)  # (8,)
print(y.shape)  # (2, 3)
print(y.size)   # 6
print(x.dtype)  # int32
# 3.创建便捷函数
z1 = np.arange(10)      # [0 1 2 3 4 5 6 7 8 9]
z2 = np.arange(2,10,2)  # [2 4 6 8]
print(np.random.rand())       # 0.8003246658237291
print(np.random.rand(3))      # [0.22203232 0.07369858 0.02427968]
print(np.random.rand(3,2))    # [[0.66349363 0.86112248] [0.11068548 0.89208492] [0.34897576 0.57025273]]
# 4.改变reshape
print(np.arange(10).reshape(2,5).shape)  # (2, 5) 以为数组 转换成 2行5列的数组
# 5.索引查询
a = np.arange(10)
b = np.arange(20).reshape(4,5)
print(a[2],a[5],a[-1])          # 2 5 9
print(a[2:4])                   # [2 3]
print(a[2:-1])                  # [2 3 4 5 6 7 8]
print(a[2:])                    # [2 3 4 5 6 7 8 9]
print(a[:6])                    # [0 1 2 3 4 5]
print(b[0,0])                   # 0
print(b[-1,2])                  # 17
print(b[:-1])                   # [[ 0  1  2  3  4] [ 5  6  7  8  9] [10 11 12 13 14]]
print(b[:2,2:4])                # [[2 3] [7 8]]
# 布尔索引
print(a>5)                      # [False False False False False False  True  True  True  True]
print(a[a>5])                   # [6 7 8 9]
# 6.random随机函数
np.random.seed(666)
print(np.random.rand(5))
print(np.random.rand(3, 4))
print(np.random.rand(2, 3, 4))
print(np.random.randn(5))                    # [-1.20990266 -0.04618272 -0.44118244  0.46953431  0.44325817]
print(np.random.randint(1,10))               # 9
print(np.random.randint(10,30,size=(5,)))    # [25 13 29 15 15]
print(np.random.randint(10,30,size=(2,3,4)))
print(np.random.random(5))         # [0.2026436  0.63279787 0.18935861 0.1308497  0.75765845]
print(np.random.random(size=(3,4)))
c = np.arange(10)
print(np.random.shuffle(c))
d = np.random.normal(1,10,10)  # 均值为1 方差为10 的10个高斯分布数字
# 7.统计学函数
arr = np.arange(12).reshape(3,4)
print(np.sum(arr))        # 66
print(np.prod(arr))       # 0
print(np.cumsum(arr))     # [ 0  1  3  6 10 15 21 28 36 45 55 66]
print(np.cumprod(arr))    # [0 0 0 0 0 0 0 0 0 0 0 0]
print(np.min(arr))        # 0
print(np.mean(arr))       # 5.5
print(np.std(arr))        # 3.452052529534663
print(np.var(arr))        # 11.916666666666666
# 8.axis=0 代表行 axis=1 代表列
print(arr.sum(axis=0))    # [12 15 18 21]
print(arr.sum(axis=1))    # [6 22 38]
# 9.机器学习标准化   A = (A - mean(A,axis=0))/std(A,axis=0)
mean = np.mean(arr,axis=0)  # 计算每列的均值
std = np.std(arr,axis=0)    # 计算每列的方差
# 10.数据合并操作
# 不同来源数据的合并,行【数据】列【特征】
e = np.arange(6).reshape(2,3)
f = np.random.randint(10,20,size=(4,3))
print(np.concatenate([e, f]))
print(np.vstack([e, f]))
print(np.row_stack([e, f]))
# 列合并
g = np.arange(12).reshape(3,4)
h = np.random.randint(10,20,size=(3,2))
np.concatenate([e, f],axis=1)
np.hstack([e, f])
np.column_stack([e, f])
# 11.计算逆矩阵求解线性方程组
A = np.array([
    [1,1,1],
    [0,2,5],
    [2,5,-1]
])
B = np.linalg.inv(A) # B为A的逆矩阵
print(np.matmul(A, B))
# 12.Numpy 与 Pandas 数据转换
# 13.Numpy输入到sklearn


目录
相关文章
|
1月前
|
数据采集 机器学习/深度学习 存储
【机器学习】数据清洗——基于Numpy库的方法删除重复点
【机器学习】数据清洗——基于Numpy库的方法删除重复点
78 1
|
4月前
|
Python
NumPy生成数组的方法
NumPy生成数组的方法
|
5月前
|
数据挖掘 索引 Python
【Python】数据分析:numpy的常用方法
【Python】数据分析:numpy的常用方法
43 0
|
7月前
|
机器学习/深度学习 数据处理 C语言
numpy通用函数:快速的逐元素数组函数
numpy通用函数:快速的逐元素数组函数
numpy通用函数:快速的逐元素数组函数
|
11天前
|
搜索推荐 数据挖掘 数据处理
NumPy数组统计与排序方法全览
【4月更文挑战第17天】本文介绍了NumPy在Python中的数组统计和排序功能。主要包括计算平均值、标准差和方差的`np.mean()`, `np.std()`, `np.var()`方法,以及求最大值、最小值、百分位数的功能。在排序方面,讲解了基本排序的`np.sort()`,获取排序索引的`np.argsort()`,逆序排序和随机排序的方法。这些工具对于数据分析和科学计算十分实用,能有效提升数据处理效率。
|
3月前
|
存储 测试技术 数据库
NumPy 秘籍中文第二版:六、特殊数组和通用函数
NumPy 秘籍中文第二版:六、特殊数组和通用函数
37 0
|
4月前
|
数据处理 Python
NumPy 中级教程——通用函数(ufuncs)
NumPy 中级教程——通用函数(ufuncs)
93 0
|
4月前
|
Python
Python21day学习---numpy生成数组的若干方法----day19
Python21day学习---numpy生成数组的若干方法----day19
34 0
|
4月前
|
Python
关于Python的Numpy库reshape()函数的用法
1.介绍 更改数组的形状,不改变原数组 2.语法 a = np.reshape(mat, newshape, order = ‘C’) a : newshape形状的新数组 mat : 原数组
53 0
|
8月前
|
Python
Python map() 函数 和 numpy mean()函数
Python map() 函数 和 numpy mean()函数
91 0

相关实验场景

更多