Numpy : Data Processing and Visulisation with Python (Python Exercise 21)

Data Processing and Visulisation with Python

Matrix with range in row

Write a Python function to take a positive integer n as parameter and return a n × n n\times n n×n matrix with values in each row ranging from 0 to n-1.

method 1

def matrixRangeRow(m):
    import numpy as np
    return  np.tile([np.arange(m)],(m,1))

method 2

def matrixRangeRow(n):
    import numpy as np
    return np.array([[i for i in range(n)]]*n)
matrixRangeRow(5)

matrixRangeRow(12)

在这里插入图片描述

Matrix with range in column

Write a Python function to take a positive integer n as parameter and return a n × n n\times n n×n matrix with values in each column ranging from 0 to n-1.

method 1

def matrixRangeColumn(n):
    import numpy as np
    return np.array([[i for i in range(n)]]*n).transpose()

method 2

def matrixRangeColumn(m):
    import numpy as np
    return  np.tile([np.arange(m)],(m,1)).transpose()
matrixRangeColumn(5)

matrixRangeColumn(12)

在这里插入图片描述

Sum of multiples of 3 or 5

Write a Python function to take a 1-d numpay array and return the sum of the elements which are multiples of 3 or 5.

method 1

def sum3_5(a):
    return sum([i for i in a if i % 3 == 0 or i % 5 == 0])

method 2

def sum3_5(n):
    return sum(n[(n%5==0) | (n%3==0)])
import numpy as np
sum3_5(np.array([65, 17, 59,  8, 31,  8, 56, 63, 36, 30, 45, 20, 92,  4, 49,  3, 42, 37, 60,  5, 25, 95, 57, 14, 83, 32,  1, 20, 41, 61]))

sum3_5(np.array([90, 27, 23, 12, 79, 34, 54, 24, 37, 93, 87, 49, 57, 67, 99, 72, 93,
       72, 30, 52, 82, 81, 58, 52, 16, 61, 89, 24,  1, 51, 41, 97, 79, 33,
       95, 17, 50, 81, 22, 77, 51, 66, 37, 86,  4, 13, 60, 30, 33, 94]))

在这里插入图片描述

The m t h m^{th} mth Column

Write a Python function to take a N ∗ M N*M NM matrix a in numpy array and an integer m (0<=m<M), return the m t h m^{th} mth Column.

method 1

def matrixColumn(a,m):
    return a[:,m]

method 2

def matrixColumn(n,m):
    return np.transpose(n[:,m])
import numpy as np
n = np.random.randint(1,100,(5,3))
n

matrixColumn(n, 2)

n = np.random.randint(1,100,(8,12))
n

matrixColumn(n, 10)

在这里插入图片描述

Replace outliers

Write a Python function replaceOutliers(a, ex, value) to replace the outliers (in numpy array a), which values are great than ex, to the value of value.

def replaceOutliers(a, ex, value):
    a[a>ex] = value
    return a
import numpy as np
n = np.random.randint(1,100,(5,4))
n

replaceOutliers(n, 70, 70)

n = np.random.randint(1,100,20)
n

replaceOutliers(n, 70, 70)

在这里插入图片描述

Swap columns

Write a Python function swapColumns(a, m, n) to swap the n t h n^{th} nth column and the m t h m^{th} mth column of the matrix a. (m, n are 0 based)

method 1

def swapColumns(a, m, n):
    b = a.copy()
    a[:,m] = b[:,n]
    a[:,n] = b[:,m]
    return a

method 2

def swapColumns(a, m, n):
    a[:,[m,n]]=a[:,[n,m]]
    return a
import numpy as np
n = np.random.randint(1,100,(5,4))
n

swapColumns(n, 0,2)

n = np.random.randint(1,100,(8,12))
n

swapColumns(n, 2,10)

在这里插入图片描述

Norm

Write a Python function norm(a) to return the norm of numpy array a.

method 1

import numpy as np
def norm(a):
    return np.linalg.norm(a)

method 2

def norm(a):
    return np.sqrt((a*a).sum())
import numpy as np
n = np.array([1,2,3,4,5])
n

norm(n)

n = np.random.randint(1,10,(3,4))
n

norm(n)

n = np.random.randint(1,10,(2,3,2))
n

norm(n)

在这里插入图片描述

Determinant

在这里插入图片描述

def det(a):
    return np.linalg.det(a)
import numpy as np
a = np.array([[1,2],[3,4]])
a

det(a)

n = np.random.randint(1,10,(3,3))
n

det(n)

n = np.random.randint(1,20,(10,10))
n

det(n)

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值