【人工智障学习指北-003】70道 Numpy 面试题-答案

基础 — Numpy 入门70题

👇题目来源
70 道 NumPy 面试题

👇答案参考
numpy70道基础训练题(解释说明)

👇解题过程 + 出坑记录
【人工智障学习指北-002】70 道 NumPy 面试题—解题出坑记


以下为自己的做题答案,仅供参考+留待以后查阅用。

#001 将 NumPy 导入为 np,并查看版本
import numpy as np
from numpy.lib import append
print(np.__version__)

#002 如何创建 1 维数组?
#np.arange(int) 生成以0~(n-1)的一维int数组
print(np.arange(10))

#003 创建所有 True 的 3×3 NumPy 数组。
print(np.ones((1,3,3),dtype=bool))

#004 从 arr 中提取所有奇数。
arr = np.array([0,1,2,3,4,5,6,7,8,9])
seq = []
for item in arr:
    if item%2 != 0:
        seq.append(item)
arr2 = np.array(seq)
print(arr2)

#005 将 arr 中的所有奇数替换成 -1。
arr = np.array([0,1,2,3,4,5,6,7,8,9])
for i in range(len(arr)):
    if arr[i]%2 != 0:
        arr[i] = -1
print(arr)

#oo6 将 arr 中所有奇数替换成 -1,且不改变 arr。
arr = np.array([0,1,2,3,4,5,6,7,8,9])
seq = np.ones_like(arr)
for i in range(len(arr)):
    if arr[i]%2 != 0:
        arr[i] = -1    
print(arr)

#007 将 1 维数组转换成 2 维数组(两行)
a = np.arange(9)
n = len(a)
#way1:
if n%2==0:
    b = a.reshape(2,n//2)
else:
    temp = a[:((n//2)+1):]
    temp = np.append(temp, a[((n//2)+1)::])
    temp = np.append(temp, 0)
    b = temp.reshape(2,(n//2)+1)
    print(b)
#way2:
if n%2==0:
    c = np.reshape(a, (2,-1))
    print(c)
else:
    c = np.append(a, 0)
    c = np.reshape(c, (2,-1))
    print(c)

#008 垂直堆叠数组 a 和 b
a = np.arange(10).reshape(2,-1)
b = np.repeat(1, 10).reshape(2,-1)
c = a
for i in range(len(b)):
    c = np.append(c, [b[i]], axis=0)
print(c)

#009 水平堆叠数组 a 和 b
a = np.arange(10).reshape(2,-1)
b = np.repeat(1, 10).reshape(2,-1)
c = [[]]
print(len(c[0]))
for i in range(len(b)):
    temp = np.append(a[i],b[i])
    #print("type(temp) is :", type(np.array([temp])))
    #print(np.array([temp]))
    if len(c[0])==0:
        c = np.array([temp])
    else:
        c = np.append(c, np.array([temp]), axis=0)
print(c)

#010 在不使用硬编码的前提下, 在 NumPy 中生成自定义序列
#仅使用 NumPy 函数和以下输入数组 a = np.array([1,2,3])
#期望输出:
#> array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
a = np.array([1,2,3])
b = np.repeat(a,3)
b = np.append(b, [1,2,3]*3)
print(b)

#011 获取数组 a 和 b 中的共同项
a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
c = set()
for item in a:
    for j in b:
        if item == j:
            c.add(item)
print(np.array(list(c)))

#012 从数组 a 中移除出现在数组 b 中的所有项
a = np.array([1,2,3,4,5])
b = np.array([5,6,7,8,9])
c = []
for item in a:
    flag = 0
    for j in b:
        if item == j:
            flag = 1
    if flag != 1:
        c.append(item)
print(np.array(c))

#013 获取数组 a 和 b 中匹配元素的位置
a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
c = []
for i in range(len(a)):
    if a[i] == b[i]:
        c.append(i)
print(np.array(c))

#014 从数组 a 中提取 5 和 10 之间的所有项
a = np.arange(15)
c = []
for item in a:
    if item >= 5 and item <= 10:
        c.append(item)
print(np.array(c))

#015 转换函数 maxx,使其从只能对比标量而变为对比两个数组
def maxx(a,b):
    return a if a >= b else b

def pair_max(a, b):
    c = []
    for i in range(maxx(len(a), len(b))):
        if i < len(a) and i < len(b):
            c.append(maxx(a[i], b[i]))
        else:
            c.append(a[i] if i<len(a) else b[i])
    return np.array(c)
            
a = np.array([5, 7, 9, 8, 6, 4, 5])
b = np.array([6, 3, 4, 8, 9, 7, 1])
print(pair_max(a, b))

#016 在数组 arr 中交换列 1 和列 2
#way1: temp=a[0],两者指向同一个对象
a = np.arange(9).reshape(3,3)
print(a)
b = np.transpose(a)
temp = np.copy(b[0])
b[0] = b[1]
b[1] = temp
c = np.transpose(b)
print(c)

#way2: 用高维数组的切片 a[[y0,y1,y2],[x1,x4,x5,x6]]
a = np.arange(9).reshape(3,3)
a[:,[0,1]] = a[:,[1,0]]
print(a)

#017 在 2d NumPy 数组中交换行 1 和行 2
a = np.arange(9).reshape(3,3)
a[[0,1],:] = a[[1,0],:]
print(a)

#018 反转 2D 数组 arr 中的所有行
# Input
a = np.arange(9).reshape(3,3)
print(a[::-1,:])

#019 反转 2D 数组 arr 中的所有列
# Input
a = np.arange(9).reshape(3,3)
print(a[:,::-1])

#020 创建一个形态为 5×3 的 2 维数组,包含 5 和 10 之间的随机十进制小数
#way1: np.random.random((a,b))  生成a行b列的随机数组,0~1之间
a = np.random.random((5,3))
print(a*5+5)

#way2: uniform正态分布
a = np.random.uniform(5, 10, (5,3))
print(a)

#021 输出或显示 NumPy 数组 rand_arr 中小数点后三位的数字
rand_arr = np.random.random((5,3))
print(np.around(rand_arr, 4))

#022 通过禁用科学计数法(如 1e10)打印 NumPy 数组 rand_arr
# Create the random array
np.random.seed(100)
rand_arr = np.random.random([3,3])/1e3
np.set_printoptions(suppress=True, threshold=1)
print(rand_arr)

#023 将 Python NumPy 数组 a 输出的项的数目限制在最多 6 个元素
a = np.arange(15)
np.set_printoptions(threshold=6)
print(a)

#024 在不截断数组的前提下打印出完整的 NumPy 数组 a
import sys
a = np.arange(15)
np.set_printoptions(threshold=sys.maxsize)
print(a)

#025 导入 iris 数据集,保持文本不变
from sklearn.datasets import load_iris
#initialize
iris = load_iris()
target_map_dict={0:'Itis-setosa',1:'Iris-versicolor',2:'Iris-virginica'}
#定义转换字典
def target_map(x, map_dict):
    return map_dict[x] if x in map_dict else x
target_map_vector = np.vectorize(target_map)    #对iris中每一个['target']进行一次函数操作
#生成转换后数据
iris_target_maped = target_map_vector(iris['target'],target_map_dict)
iris_data_map = np.c_[iris['data'], iris_target_maped]
np.savetxt('iris.data', iris_data_map, fmt='%s',delimiter=',')

#026 从前一个问题导入的 1 维 iris 中提取文本列 species
#way1: type = ndarray
from sklearn.datasets import load_iris
iris = load_iris()
print(iris['target'])
#way2: type = 1d-array + tuple
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_1d = np.genfromtxt(url, delimiter=',' , dtype=None)
a = np.array([x[4] for x in iris_1d])
print(a)

#027 忽略 species 文本字段,将 1 维 iris 转换成 2 维数组 iris_2d
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_1d = np.genfromtxt(url, delimiter=',' , dtype=None)
a = np.array([x[0] for x in iris_1d])
b = np.array([x[1] for x in iris_1d])
c = np.array([x[2] for x in iris_1d])
d = np.array([x[3] for x in iris_1d])
con = np.array([a,b,c,d])
con = np.transpose(con)
print(con)

#028 找出 iris sepallength(第一列)的平均值、中位数和标准差
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_1d = np.genfromtxt(url, delimiter=',' , dtype=None)
a = np.array([x[0] for x in iris_1d])
#print(a)
mean = np.mean(a, axis=0)
median = np.median(a, axis=0)
std = np.std(a, axis=0)
print(mean,'\n',median,'\n',std)

#029 创建 iris sepallength 的归一化格式,使其值在 0 到 1 之间
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_1d = np.genfromtxt(url, delimiter=',' , dtype=None)
a = np.array([x[0] for x in iris_1d])
length = max(a)-min(a)
a = a-np.repeat(min(a), a.shape)
a = a / length
print(a)

#030 计算 sepallength 的 softmax 分数       ##np.vectorize()用法
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
sepallength = np.genfromtxt(url, delimiter=',' , dtype='float', usecols=[0])
def expo(x):
    return np.exp(x)
step1 = np.vectorize(expo)
a = step1(sepallength)
print(a/sum(a))

#031 找出 iris sepallength(第一列)的第 5 个和第 95 个百分数
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
sepallength = np.genfromtxt(url, delimiter=',' , dtype='float' , usecols=[0])
print(np.percentile(sepallength, [5,95]))

#032 在 iris_2d 数据集中的 20 个随机位置插入 np.nan 值
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype= object )
iris_2d = iris_2d.reshape(1,-1)
index = np.random.randint(0,len(iris_2d[0]),size=[1,20])
index = np.squeeze(index, axis=0)
print(index)
output = np.insert(iris_2d, index, np.nan)
output = np.expand_dims(output, 0)
print(output) 

#033 在 iris_2d 的 sepallength(第一列)中找出缺失值的数目和位置
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',' , dtype='float')
iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan
x0 = iris_2d[:,0:1]
print(len(x0))
count = 0
index_record = []
for i in range(len(x0)):
    #print(type(x0[i]))
    if np.isnan(x0[i]):
        count += 1
        index_record.append(i)
print("count = ",count)
print("index = ", index_record)

#034 过滤 iris_2d 中满足 petallength(第三列)> 1.5 和 sepallength(第一列)< 5.0 的行
#way1: 用 i in range(len(iris_2d)) 索引遍历的方法
#np.append(a, value) 发生未知错误

#way2: & 按位与运算符——全真为真,并且可以计算array数据类型
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
condition = (iris_2d[:,2] > 1.5) & (iris_2d[:,0] < 5.0)
ret = iris_2d[condition]
print(ret)

#035 选择 iris_2d 中不包含 nan 值的行。
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
#condition = not ((np.isnan(iris_2d[:,0])) | ((np.isnan(iris_2d[:,1])) | ((np.isnan(iris_2d[:,2])) | ((np.isnan(iris_2d[:,3])) ))
condition = np.isnan(iris_2d[:,0]) | np.isnan(iris_2d[:,1]) | np.isnan(iris_2d[:,2]) | np.isnan(iris_2d[:,3])
condition = ~ condition
#print(condition)
ret = iris_2d[condition]
print(ret)

#036 找出 iris_2d 中 SepalLength(第一列)和 PetalLength(第三列)之间的关联性
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
print(np.corrcoef(iris_2d[:,0],iris_2d[:,2]))

#037 确定 iris_2d 是否有缺失值
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
print(np.any(np.isnan(iris_2d)))

#038 在 NumPy 数组中将所有 nan 替换成 0
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan
print(np.where(np.isnan(iris_2d), 0, iris_2d))

#039 在 iris 的 species 列中找出唯一值及其数量
url ='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter= ',', dtype= object)
names = ('sepallength','sepalwidth', 'petallength' , 'petalwidth' , 'species')
species = iris[:,4]
values, counts = np.unique(species, return_counts=True)
out = np.array([])
for i in range(len(counts)):
    if counts[i] == 1:
        np.append(out, values[i])
print(out)

#040 将 iris_2d 的 petallength(第三列)转换以构建一个文本数组,按如下规则进行转换:
#Less than 3 –> ‘small’
#3-5 –> medium
#>=5 –> large
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
names = ('sepallength','sepalwidth', 'petallength' , 'petalwidth' , 'species')
#way1:where()嵌套l[]函数
petallength = iris_2d[:, 2]
step1 = np.where(petallength<3, 'small', ['large' if item>=5 else 'medium' for item in petallength])
print(step1)

#way2:利用digitize() + .astype()
length = np.digitize(iris_2d[:,2].astype('float'), [0,3,5,10])
label = {1:'small', 2:'medium', 3:'large',4:np.nan}
length_petal = [label[i] for i in length]
print(length_petal)


#041 为 iris_2d 中的 volume 列创建一个新的列,volume 指 (pi x petallength x sepal_length^2)/3
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')
names = ('sepallength','sepalwidth', 'petallength' , 'petalwidth' , 'species')
petallength =  iris_2d[:,2].astype(float)
sepal_length = iris_2d[:,0].astype(float)
volume = (np.pi *petallength*sepal_length**2)/3
#way1: 转置矩阵
volume = np.expand_dims(volume, axis=0)
volume = np.around(volume, 2)
new = np.append(np.transpose(iris_2d),volume,axis=0)
new = np.transpose(new)
print(new)

#way2:newaixs + hstack(tuple)
volume0 = volume[:,np.newaxis]
print(volume0.shape)
print(np.hstack((iris_2d, volume0)))

#042 随机采样 iris 数据集中的 species 列,使得 setose 的数量是 versicolor 和 virginica 数量的两倍
url ='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter= ',', dtype= object)
species = iris[:,4]
print(np.unique(species,return_counts='True'))
index = np.r_[np.linspace(0.0,0.500,num=50),np.linspace(0.501,0.750,num=50),np.linspace(0.751,1,num=50)]
#print(index)
index = np.searchsorted(index, np.random.random(150))
out = species[index]
print(np.unique(out,return_counts=True))

#043 在 species setosa 的 petallength 列中找到第二最大值
url ='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter= ',', dtype= object)
setosa = iris[0:50,2]
sorted, index = np.unique(setosa, return_index=True)
print('The second largest is : ',sorted[-2])

#044 基于 sepallength 列将 iris 数据集排序
url ='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter= ',', dtype= object)
names = ('sepallength','sepalwidth', 'petallength' , 'petalwidth' , 'species')
print(type(iris))
print(iris.shape)
print(iris[np.argsort(iris, axis=0)[:,0]])

#045 在 iris 数据集中找到 petallength(第三列)中最频繁出现的值
url ='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter= ',', dtype= object)
names = ('sepallength','sepalwidth', 'petallength' , 'petalwidth' , 'species')
values, counts = np.unique(iris[:,2], return_counts=True)
print(values[np.argmax(counts)])

#046 在 iris 数据集的 petalwidth(第四列)中找到第一个值大于 1.0 的数的位置
url ='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter= ',', dtype= object)
print(np.argmax(iris[:,3].astype('float') > 1.0))  #True > False

#047 对于数组 a,将所有大于 30 的值替换为 30,将所有小于 10 的值替换为 10
np.random.seed(100)
a = np.random.uniform(1,50, 20)
#way1: where()嵌套l[]函数
step1 = np.where(a<10, 10, [30 if item>30 else item for item in a])
print(step1)

#way2: 用clip(a, low, high)函数
print(np.clip(a,10,30))

#048 在给定数组 a 中找到 top-5 最大值的位置
np.random.seed(100)
a = np.random.uniform(1,50, 20)
print(np.argsort(a)[-1:-6:-1])

#049 逐行计算唯一值的数量
'''
输入:
arr
> array([[ 9, 9, 4, 8, 8, 1, 5, 3, 6, 3],
> [ 3, 3, 2, 1, 9, 5, 1, 10, 7, 3],
> [ 5, 2, 6, 4, 5, 5, 4, 8, 2, 2],
> [ 8, 8, 1, 3, 10, 10, 4, 3, 6, 9],
> [ 2, 1, 8, 7, 3, 1, 9, 3, 6, 2],
> [ 9, 2, 6, 5, 3, 9, 4, 6, 1, 10]])
期望输出:
> [[1, 0, 2, 1, 1, 1, 0, 2, 2, 0],
> [2, 1, 3, 0, 1, 0, 1, 0, 1, 1],
> [0, 3, 0, 2, 3, 1, 0, 1, 0, 0],
> [1, 0, 2, 1, 0, 1, 0, 2, 1, 2],
> [2, 2, 2, 0, 0, 1, 1, 1, 1, 0],
> [1, 1, 1, 1, 1, 2, 0, 0, 2, 1]]
'''
np.random.seed(100)
arr = np.random.randint(1,11,size=(6, 10))
x = [np.unique(row, return_counts=True) for row in arr]
out = [[int(b[a==i]) if i in a else 0 for i in np.unique(arr)] for a, b in x]
print(np.array(out))

#050 将 array_of_arrays 转换为平面线性 1 维数组
arr1 = np.arange(3)
arr2 = np.arange(3,7)
arr3 = np.arange(7,10)
array_of_arrays = np.array([arr1, arr2, arr3])
#way1: 用list转换
out = []
[[out.append(item) for item in row] for row in array_of_arrays]
print(np.array(out))
#way2: 
arr = np.array([a for i in array_of_arrays for a in i])
print(arr)

#051 计算 one-hot 编码
'''期望输出
#> array([[ 0., 1., 0.],
#> [ 0., 0., 1.],
#> [ 0., 1., 0.],
#> [ 0., 1., 0.],
#> [ 0., 1., 0.],
#> [ 1., 0., 0.]])
'''
np.random.seed(101)
arr = np.random.randint(1,4, size=6)
feature = np.unique(arr)
out = np.array([[1 if item==i else 0 for i in feature] for item in arr])
print(out)

#052 创建由类别变量分组的行数。使用以下来自 iris species 的样本作为输入
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
species = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)
species_small = np.sort(np.random.choice(species, size=20))
out = [i for a in np.unique(species_small) for i, b in enumerate(species_small[species_small==a])]
print(out)

#053 基于给定的类别变量创建分组 id。使用以下来自 iris species 的样本作为输入
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
species = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)
species_small = np.sort(np.random.choice(species, size=20))
x = [np.argwhere(np.unique(species_small)==s)[0][0] for a in np.unique(species_small) for s in species_small[species_small==a]]
print(x)

#054 为给定的数值数组 a 创建排序
np.random.seed(10)
a = np.random.randint(20, size=10)
print(a)
#way1: 正常思路
b = np.argsort(a)
print(a[b])
#way2: ????
#######################没看懂在干嘛##################
c = a.argsort().argsort()
print(c)

#055 给出一个数值数组 a,创建一个形态相同的排序数组
np.random.seed(10)
a = np.random.randint(20, size=[2,5])
#way1: 基本想法
shape = a.shape
print(shape)
a = np.sort(a.reshape(1,-1))
a = a.reshape(shape)
print(a)
#way2: ?????没看懂为何排序排两遍
#######################没看懂在干嘛##################
print(a.ravel().argsort().argsort().reshape(a.shape))

#056 在给定数组中找到每一行的最大值
np.random.seed(100)
a = np.random.randint(1,10, [5,3])
print(a)
#way1: 列表推导式
print([np.amax(item) for item in a])
#way2: amax()-numpy的数组友好性
print([np.amax(a,axis=0)])

#057 给定一个 2 维 NumPy 数组,计算每一行的 min-by-max
np.random.seed(100)
a = np.random.randint(1,10, [5,3])
print(a)
print(np.amax(a,axis=1)*np.amin(a,axis=1))

#058 在给定的 NumPy 数组中找到重复条目(从第二次出现开始),
#并将其标记为 True。第一次出现的条目需要标记为 False
#> Array: [0 0 3 0 2 4 2 2 2 2]
#期望输出:
#> [False True False True False False True True True True]
#逆向思维:先全True, 再剔除出首次出现的
np.random.seed(100)
a = np.random.randint(0, 5, 10)
print('Array:', a)
out = np.full(a.shape, True)
out[np.unique(a, return_index=True)[1]] = False
print(out)

#059 在 2 维 NumPy 数组的类别列中找到数值的平均值
#期望输出:
#> [[b Iris-setosa , 3.418],
#> [b Iris-versicolor , 2.770],
#> [b Iris-virginica , 2.974]]
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter=',', dtype='object')
names = ('sepallength','sepalwidth', 'petallength' , 'petalwidth' , 'species')
sepal_length, species = iris[:,1], iris[:,4]
x = np.array([np.argwhere(np.unique(species)==s)[0][0] for a in np.unique(species) for s in species[species==a]])
out0 = [np.mean([float(item[0].decode()) for item in sepal_length[np.argwhere(x==i)]]) for i in range(3)]
out1 = np.around(np.squeeze(np.array(out0).reshape(-1,1), axis=-1),3)
out = np.c_['-1',np.unique(species),out1]
print(np.array(out))

#060 从以下 URL 中导入图像,并将其转换成 NumPy 数组
#URL = 'https://upload.wikimedia.org/wi'
URL = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fa_auto%2Cc_cut%2Cx_42%2Cy_53%2Cw_416%2Ch_416%2Fimages%2F20200426%2F9a9b7d6dde8b4882b49c6a45c00d1a9e.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1630029150&t=3cfb2ca524dd86745f691421d2a27fd5'
from io import BytesIO
from PIL import Image
import PIL, requests
response = requests.get(URL)
image = Image.open(BytesIO(response.content))
image = image.resize([1500,1500])
arr = np.asarray(image)
image1 = PIL.Image.fromarray(np.uint8(arr))
Image.Image.show(image1)

#061 从 1 维 NumPy 数组中删除所有的 nan 值
a = np.array([1,2,3,np.nan,5,6,7,np.nan])
#way1: 
out = [not np.isnan(item) for item in a]
print(a[out])
#way2:
print(a[~np.isnan(a)])

#062 计算两个数组 a 和 b 之间的欧几里得距离
a = np.array([1,2,3,4,5])
b = np.array([4,5,6,7,8])
out = np.sum([(abs(a[i]-b[i]))**2 for i in range(len(a))])
print(np.sqrt(out))

#063 在 1 维数组 a 中找到所有的 peak,peak 指一个数字比两侧的数字都大
##############做法清奇##########################
a = np.array([1, 3, 7, 1, 2, 6, 0, 1])
b = np.diff(np.sign(np.diff(a)))
peak = np.where(b == -2)[0] + 1
print(peak)

#064 从 2 维数组 a_2d 中减去 1 维数组 b_1d,
# 即从 a_2d 的每一行分别减去 b_1d 的每一项
#期望输出:
#> [[2 2 2]
#> [2 2 2]
#> [2 2 2]]
a_2d = np.array([[3,3,3],[4,4,4],[5,5,5]])
b_1d = np.array([1,2,3])
#way1:
c = np.repeat(b_1d, a_2d.shape[0]).reshape(a_2d.shape)
print(a_2d-c)
#way2: (3,3) - (3,1) 后者自动repeat扩充为(3,3)
print(b_1d[:,None])
d = a_2d - b_1d[:,None]
print(d)

#065 找到数组 x 中数字 1 的第 5 个重复索引
x = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2])
#way1: argwhere(condition)返回2d数组(array(索引))
print(np.argwhere(x==1)[4][0])
#way2: where(condition)返回2元元组(array(索引),dtype=?)
n = 5
print(np.where(x==1)[0][n-1])

#066 将 NumPy 的 datetime64 对象(object)转换为 datetime 的 datetime 对象
# Input: a numpy datetime64 object
import datetime
dt64 = np.datetime64( '018-02-25 22:10:10')
print(dt64)
print(dt64.astype(datetime.datetime))

#067 给定 1 维数组,计算 window size 为 3 的移动平均数
################不是很懂“移动平均值”#################
np.random.seed(100)
Z = np.random.randint(10, size=10)
def moving_average(a, n=3):
    r = np.cumsum(a, dtype=float)
    r[n:] = r[n:] - r[:-n]
    return r[n-1:]/n
print(Z)
print(moving_average(Z, n=3).round(2))

#068从 5 开始,创建一个 length 为 10 的 NumPy 数组,相邻数字的差是 3
start, step, n = 5, 3, 10
stop = start + n*step
a = np.arange(start,stop,step)
print(a)

#069 给定一个非连续日期序列的数组,通过填充缺失的日期,使其变成连续的日期序列
##################不是很懂:为何要np.hstack??################
dates = np.arange(np.datetime64('2018-02-01'), np.datetime64('2018-02-25'), 2)
print(dates)
filled = np.array([np.arange(date, (date+d)) for date, d in zip(dates, np.diff(dates))]).reshape(-1)
output = np.hstack([filled,dates[-1]])
print(output)

#070 给定 1 维数组 arr,使用 strides 生成一个 2 维矩阵,
#其中 window length 等于 4,strides 等于 2
#例如 [[0,1,2,3], [2,3,4,5], [4,5,6,7]..]
#way1: 面向过程
arr = np.arange(15)
print(arr)
stride, width, size = 2, 4, len(arr)
out = [arr[i:i+width] for i in range(0, size, stride)]
out = np.array(out)
out = np.array([a for i in out for a in i])
print(out.reshape(-1,width))
#way2: OOP
def gen_strides(arr, stride_len = 5, window_len = 5):
    n_strides = ((arr.size - window_len) // stride_len) + 1
    return np.array([arr[i:(i + window_len)]  for i in np.arange(0, n_strides * stride_len, stride_len)])
print(gen_strides(arr, stride_len=2, window_len=4))
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值