当前位置:网站首页>Lambda and filter, index of list and numpy array, as well as various distance metrics, concatenated array and distinction between axis=0 and axis=1
Lambda and filter, index of list and numpy array, as well as various distance metrics, concatenated array and distinction between axis=0 and axis=1
2022-06-12 11:37:00 【king52113141314】
Lambda
lambda Function parameter : Function expression
# Function definition
def add(x,y):
return x+y
# The above function uses lambda rewrite
lambda x,y: x+y
filter
If the first argument is a function :
Set the second parameter ( You can iterate over the data ) Each element of the function is calculated as a parameter of the function , Will return true The value of is filtered out and returned as a list . Be careful : The return value of this function can only be True, Or False.
filter(lambda x: x, [-1, 0, 1]) #[-1, 1] filter(lambda x: not x, [-1, 0, 1]) #[0] def f(x): return True if x == 1 else False filter(lambda x: f(x), [-1, 0, 1]) #[1]
If the first parameter is None:
In the second parameter true Value filteredfilter(None, (True, 1, 0, -1, False)) #(True, 1, -1)
Want to use filter After the results of the , It's better to turn it into list:
list(filter_return)
Python in filter And lambda Combined use of _ Xiao Ge shelwin The blog of -CSDN Blog
List and numpy array The index of
Remove an item from a list in Python (clear, pop, remove, del)
l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l.clear()
print(l)
# []
print(l.pop(3))
# 4
print(l)
# [1, 2, 3, 5, 6, 7, 8, 9]
print(l.pop())#If the argument is omitted, the last item is deleted.
# 9
print(l)
# [1, 2, 3, 5, 6, 7]
del l[-1]
print(l)
# [1, 2, 3, 4, 5, 6, 7, 8]
del l[2:5]
print(l)
# [0, 1, 5, 6, 7, 8, 9]
###Removing items that satisfy the condition
#is equivalent to extracting items that do not satisfy the condition.
print([i for i in l if i % 2 != 0])
# [1, 3, 5, 7, 9]
l = ['Alice', 'Bob', 'Charlie', 'Bob', 'Dave']
print(l)
# ['Alice', 'Bob', 'Charlie', 'Bob', 'Dave']
l.remove('Alice')
print(l)
# ['Bob', 'Charlie', 'Bob', 'Dave']
aaa=np.arange(10)
iii=[0,5,2]
np.delete(aaa, iii, None)
#array([1, 3, 4, 6, 7, 8, 9])Check a nested list or numpy array Is it empty ?
How to check if a nested list or numpy array is essentially empty?
#1, recommend
#Use the any() function.
#This returns True if any list within the list is not empty.
alist = [[],[]]
if not any(alist):
print("Empty list!")
#2
l2 = []
if l2:
print("list is not empty")
elif not l2:
print("list is empty")
#3
if len(l2) == 0:
print("list is empty")
else:
print("list is not empty")
#4 Not recommended
alist = [[],[]]
if not np.any(alist):
print("Empty list!")
#1
arr1 = np.array([])
ran = not np.any(arr1)
if ran:
print('Array is empty')
else:
print('Array is not empty')
#2
arr = np.array([])
flag = np.size(arr)
if flag:
print('Array is not empty')
else:
print('Array is empty')
#3 recommend
a = np.array([])
if a.size == 0:
print("Empty")
#4 Not recommended such as arr = np.array([[0],[]])
arr = np.array([[],[]])
if not np.any(arr):
print("Empty!")
Concatenate arrays and axis=0 and axis=1 The distinction between
import numpy as np
a = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
b = np.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]])
print('Concatenate along axis=0:\n', np.concatenate((a,b), axis=0))
print('Concatenate along axis=0:\n', np.concatenate((a,b)))
print('Vertically stacked array:\n', np.vstack((a,b)))
##pass tuple or list of Numpy arrays is OK
print('Concatenate along axis=1:\n', np.concatenate([a,b], axis=1))
print('Horizontally stacked array:\n', np.hstack((a,b)))
Swap array columns or Rearrange columns of a given array
import numpy as np
array_nums = np.arange(20).reshape(4,5)
print("Original array:",array_nums)
### swapping the column with index of original array
array_nums[:,[0,3]] = array_nums[:,[3,0]]
print("\nAfter swapping column1 with column4:",array_nums)
############# Rearrange columns of a given array by indexes
array1 = np.array([[11, 22, 33, 44, 55],
[66, 77, 88, 99, 100]])
print("Original arrays:",array1)
i = [1,3,0,4,2]
result = array1[:,i]
print("New array:",result)

all distance-metrics
Recommended minkowski distance and the Mahalanobis distance.
Use scipy Function of
Distance computations (scipy.spatial.distance) — SciPy v1.8.1 Manual
scipy/distance.py at v1.8.1 · scipy/scipy · GitHub
GitHub - niranjanbsubramanian/distance-measures
Common in Data Science 9 A distance measure _ Visual algorithm blog -CSDN Blog
https://towardsdatascience.com/9-distance-measures-in-data-science-918109d069fa
4 Distance Measures for Machine Learning
Common distance measurement criteria - You know
Deep understanding of Euclidean distance and Mahalanobis distance - You know
Web of Science — Various distances - Hu Zheng's blog
import numpy as np
from math import sqrt
# Euclidean distance
def euclidean(x, y):
distance = 0
for a, b in zip(x, y):
distance += (sum([(pow((a-b),2))]))
return sqrt(distance)
print("Euclidean Distance:",euclidean([1,3,4,1],[3,2,1,1]))
#Calculate the Euclidean distance using linalg.norm()
# initializing points in
# numpy arrays
point1 = np.array((1, 2, 3))
point2 = np.array((1, 1, 1))
# calculating Euclidean distance
# using linalg.norm()
dist = np.linalg.norm(point1 - point2)
# using sum() and square()
sum_sq = np.sum(np.square(point1 - point2))# finding sum of squares
dist = np.sqrt(sum_sq)# Doing squareroot
# Manhattan distance
def manhattan(x, y):
distance=0
for a,b in zip(x,y):
distance += sum([abs(a-b)])
return distance
print("Manhattan Distance:",manhattan([1,3,4,1],[3,2,1,1]))
#chebyshev distance
def chebyshev(x,y):
distance = []
for a,b in zip(x,y):
distance.append(abs(a-b))
return max(distance)
print("Chebyshev Distance:",chebyshev([1,3,4,1],[3,2,1,1]))
#hamming distance
def hamming(x,y):
distance = 0
for a,b in zip(x,y):
if a != b:
distance += 1
return distance
print("Hamming Distance:",hamming("hello world","hallo warld"))
#cosine_similarity
def cosine_similarity(x,y):
numerator = 0
sum_x = 0
sum_y = 0
for a,b in zip(x,y):
numerator += sum([a * b])
sum_x += sum([a**2])
sum_y += sum([b**2])
denominator = round(sqrt(sum_x) * sqrt(sum_y))
return numerator / denominator
print("Cosine Similarity:", cosine_similarity([1,3,4,1],[3,2,1,1]))
#jaccard
def jaccard_similarity(x,y):
intersection = len(set(x).intersection(set(y)))
union = len(set(x).union(set(y)))
return (intersection / union)
print("Jaccard Similarity:",jaccard_similarity([0,1,2,5,6], [0,2,3,4,5,7,9]))
print("Jaccard Distance:", 1-jaccard_similarity([0,1,2,5,6],[0,2,3,4,5,7,9]))
#minkowski
# calculating minkowski distance between vectors
from math import sqrt
# calculate minkowski distance
def minkowski_distance(a, b, p):
'''
p=1, the distance measure is the Manhattan measure.
p=2, the distance measure is the Euclidean measure.
p = ∞, the distance measure is the Chebyshev measure.
'''
return sum(abs(e1-e2)**p for e1, e2 in zip(a,b))**(1/p)
# define data
row1 = [10, 20, 15, 10, 5]
row2 = [12, 24, 18, 8, 7]
# calculate distance (p=1)
dist = minkowski_distance(row1, row2, 1)
print(dist)
# calculate distance (p=2)
dist = minkowski_distance(row1, row2, 2)
print(dist)
#Mahalanobis
# Importing libraries
import numpy as np
import pandas as pd
import scipy as stats
# calculate Mahalanobis function to calculate
# the Mahalanobis distance
def calculateMahalanobis(y=None, data=None, cov=None):
y_mu = y - np.mean(data)
if not cov:
cov = np.cov(data.values.T)
inv_covmat = np.linalg.inv(cov)
left = np.dot(y_mu, inv_covmat)
mahal = np.dot(left, y_mu.T)
return mahal.diagonal()
# create new column in dataframe that contains
# Mahalanobis distance for each row
df['calculateMahalanobis'] = mahalanobis(x=df, data=df[['Price', 'Distance',
'Emission','Performance',
'Mileage']])GitHub - brando90/Normalized-Euclidean-Distance-and-Similarity-NED-NES-

边栏推荐
- AcWing 1912. 里程表(枚举)
- Drqueueonrails integrated LDAP authentication
- Socket implements TCP communication flow
- Process creation and recycling
- Problems in cross validation code of 10% discount
- SOT23(Small Outline Transistor)
- Doris records service interface calls
- 自然语言处理nlp 数据集下载地址
- One must keep writing, so as not to be submerged by the vast crowd.
- Network topology
猜你喜欢
随机推荐
记录一下使用JPA时遇到的坑
多普勒效应的基本原理
Signal relay rxsf1-rk271018dc110v
AcWing 1995. 见面与问候(模拟)
架构训练模块 7
UI自动化测试中比较少见的异常记录
你需要社交媒体二维码的21个理由
人類想要擁有金錢、權力、美麗、永生、幸福……但海龜只想做一只海龜
AI - face
Arm cross compilation chain download address
自然语言处理nlp 数据集下载地址
Architecture training module 7
Index in MySQL show index from XXX the meaning of each parameter
Drqueueonrails integrated LDAP authentication
InfoQ 极客传媒 15 周年庆征文|position:fixed 虚拟按键触发后无法生效问题分析及解决方案探究
ReentrantLock源码分析
mysql中的索引show index from XXX每个参数的意义
Summary of rosbridge use cases_ Chapter 26 opening multiple rosbridge service listening ports on the same server
Shardingjdbc-5.1.0 monthly horizontal table splitting + read-write separation, automatic table creation and node table refresh
[cf1392d] D. Omkar and bed Wars






![[the 11th national competition of Blue Bridge Cup single chip microcomputer]](/img/da/3c8a9efd5b28f67816f239531a0339.png)


