如何使用NumPy快速创建我们需要的数据?

不论是在平时的零碎学习还是网课的学习中,在代码上现实或者跑通一个算法或者模型不仅能够让自己理解的更深还能加强我们的记忆。所以,专门花点时间学习一下如何创建和组织出我们想要的数据很有必要。

1. NumPy 数组

NumPy 数组需要注意的一点是,数组是相同类型的元素按照一定顺序排列的组合。

2. NumPy 数组的生成方式

注意
① 生成一般数组可以通过 dtype 参数指定 NumPy 数组元素的数据类型。
② 一般都可以传入一个数字或者一个 shape 作为参数,后者生成的是大小为 shape 的数组

2.1 生成一般数组

  • 生成一维数组:传入列表
  • 生成元组的数组:传入元组
  • 生成多维数组:传入嵌套列表

附代码:

import numpy as np

arr1 = np.array([1,2,3,4,5])
print(arr1)

arr2 = np.array((1,2,3,4,5))
print(arr2)

arr3 = np.array([[1,2,3],[2,3,4],[4,5,6]])
print(arr3)  ## 这里注意 numpy 数组的打印规则:最内层轴的元素是从左至右,剩下的轴都是从上至下
[1 2 3 4 5]
[1 2 3 4 5]
[[1 2 3]
 [2 3 4]
 [4 5 6]]

2.2 生成特殊类型数组

2.2.1 zeros、ones、eye

  • 生成指定形状全为 0 的数组(传入单个数字或形状):np.zeros()
  • 生成指定形状全为 1 的数组(传入单个数字或形状):np.ones()
  • 生成一个方形矩阵(传入单个数字):np.eye()

注意:以带括号的元组形式来传递形状

arr4 = np.zeros(3) # 传递单个数字是一维数组
print(arr4)

arr5 = np.ones((3, 3))  # 传递shape 是大小为 shape 的数组
print(arr5)

arr6 = np.eye(3)  # 方阵,至只能传递单个数字
print(arr6)
[0. 0. 0.]
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

2.2.2 等差数列函数:linspace()函数

linspace()函数用于生成线性间距向量。
linspace 类似于冒号运算符“:”,但可以直接控制点数并始终包括端点。
(“linspace”名称中的“lin”指示生成线性间距值而不是同级函数 logspace,后者会生成对数间距值。)

arr70 = np.linspace(1, 10, 6)
print(arr70) 

arr71 = np.linspace(1, 10)  # 默认 num=50
print(arr71)
[ 1.   2.8  4.6  6.4  8.2 10. ]
[ 1.          1.18367347  1.36734694  1.55102041  1.73469388  1.91836735
  2.10204082  2.28571429  2.46938776  2.65306122  2.83673469  3.02040816
  3.20408163  3.3877551   3.57142857  3.75510204  3.93877551  4.12244898
  4.30612245  4.48979592  4.67346939  4.85714286  5.04081633  5.2244898
  5.40816327  5.59183673  5.7755102   5.95918367  6.14285714  6.32653061
  6.51020408  6.69387755  6.87755102  7.06122449  7.24489796  7.42857143
  7.6122449   7.79591837  7.97959184  8.16326531  8.34693878  8.53061224
  8.71428571  8.89795918  9.08163265  9.26530612  9.44897959  9.63265306
  9.81632653 10.        ]

2.3 生成随机数组

2.3.1 arrange() 函数

arrange()

  • 生成指定长度一维的随机数组:np.arange(n)
    (其实就是默认的 start =1, stop =n, step=1)
  • 生成指定长度和步长的随机数组:arange([start,] stop[, step,], dtype=None)

代码

arr7 = np.arange(10) # 其实就是默认的 start =1, stop =n, step=1
print(arr7)

arr8 = np.arange(1, 10, 2)
print(arr8)
[0 1 2 3 4 5 6 7 8 9]
[1 3 5 7 9]

2.3.2 random 模块

使用 NumPy 中的 random 模块

  • 生成 (0, 1) 之间的随机数组: np.random.random() (输入为元组形式的shape)
  • 生成 (0, 1) 之间的随机数组:np.random.rand() (输入为表示shape的数字)
  • 生成满足正态分布的随机数组:np.random.randn() (输入为表示shape的数字 或 输入为元素个数)
  • 生成随机的整数数组:np.random.randint() (输入为元素个数或者定义shape)
  • 生成打乱顺序后的随机数组:np.random.shuffle() (输入随机数组x)
  • 已知数组中选取随机数组:np.radom.choice() (输入随机数字)
rand() 和 randn()

rand(d0, d1, …, dn)

randn(d0, d1, …, dn)

arr90 = np.random.rand(5)
print(arr90)   # 这里 n=5 是返回的元素的个数(因为已经默认元素取值为 0-1 之间)
arr91 = np.random.rand(4, 2)  # 这里的参数不需要加()
print(arr91)

arr101 = np.random.randn(5)
print(arr101)  # 这里 n=5 是返回的元素的个数
arr102 = np.random.randn(2, 3)    # 这里的参数不需要加()
print(arr102) 
[0.06005572 0.39201155 0.65739602 0.82993779 0.6834849 ]
[[0.22448704 0.46166822]
 [0.1313628  0.81239307]
 [0.82737089 0.08500505]
 [0.48441804 0.86254244]]
[ 0.31277617  2.11994118 -1.67146697 -0.9564838  -1.20713141]
[[ 0.81955507  1.5992909  -0.72949892]
 [-0.40706872 -1.80163208  0.37124959]]

randint()

randint(low, high=None, size=None, dtype=‘l’)

arr11 = np.random.randint(5)
print(arr11)  # 这里 n=5 相当于上限(并不是返回的元素个数),只返回了一个数字
arr12 = np.random.randint(1,5, (2, 3))  # 这里的shape参数需要加(),或者选择 size=(,)
print(arr12)
arr13 = np.random.randint(5, size=(2, 3))
print(arr13)
4
[[2 4 4]
 [2 1 3]]
[[3 2 3]
 [3 2 1]]

注意:
① 对于 rand() 方法和 randn() 方法
如果输入的参数是一个数字n,则返回n 个随机数;
如果输入的是两个或者多个数字,则返回这多个数字形状大小的数组;

② 对于 randint() 方法
如果输入的参数是一个数字n,则返回不超过 n 的 1个随机数;如果输入的单个数字 n 和 shape,则 n 为元素上限(不取),shape
为生成的数组大小。

2.4 改变数组的形状

使用 reshape() 函数

arr14 = np.arange(8).reshape(4,2)
print(arr14)
 
arr15 = np.random.rand(8).reshape(4,2)
print(arr15)

arr16 = np.random.randn(8).reshape(4,2)
print(arr16)

arr17 = np.random.randint(8,size=(2,4)).reshape(4,2)
print(arr17)
[[0 1]
 [2 3]
 [4 5]
 [6 7]]
[[0.74805737 0.4715235 ]
 [0.61835228 0.36544255]
 [0.19811176 0.37036763]
 [0.21455191 0.09242501]]
[[-0.4904222   0.40334254]
 [ 1.06279067  0.8108894 ]
 [-0.62594376  0.79288496]
 [ 1.12483527  0.66219263]]
[[7 4]
 [5 3]
 [1 2]
 [1 6]]

其实从上面也可以看出来,使用reshape()的方法非常直接,比在传入 shape 到函数的参数中更加方便!

注意一点:randint()这里必须先用 size 参数生成多维数组,再使用reshape().

[ : , np.newaxis ]

机器学习中常见的一种方式是,使用[ : , np.newaxis ]将一维数组转化为我们想要的特征矩阵形式。

注: np.newaxis 为 numpy.ndarray 增加了一个轴

import numpy as np

rng = np.random.RandomState(42) 
x = 10* rng.rand(5)
print(x)
print(x.shape)
[3.74540119 9.50714306 7.31993942 5.98658484 1.5601864 ]
(5,)

一种方法是使用之前讲过的 reshape()方法:

X = x.reshape(-1, 1)
print(X)
print(X.shape)
X = x.reshape(-1, 1)
print(X)
print(X.shape)
[[3.74540119]
 [9.50714306]
 [7.31993942]
 [5.98658484]
 [1.5601864 ]]
(5, 1)

然后就是这个常见的方法:

X = x[:, np.newaxis]
print(X)
print(X.shape)
[[3.74540119]
 [9.50714306]
 [7.31993942]
 [5.98658484]
 [1.5601864 ]]
(5, 1)

3. 应用

生成布满指定矩形区域的散点(二维随机点)

  • 导入模块
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns; sns.set()

from sklearn.datasets import make_blobs
  • 二维随机点(固定区域)
rng = np.random.RandomState(0)
X_new = [-1, -1] + [5, 7] * rng.rand(2000, 2)

plt.scatter(X_new[:, 0], X_new[:, 1], s=10) 

X_new 的 shape : (2000, 2)

随机点布满的范围: X_new 对应:左下角的坐标 + 增加的值

  • 可视化结果
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Robin_Pi/article/details/103860398