题目1:
如何计算两个数组之间的欧式距离?
【知识点:数学函数、线性代数】
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([4, 5, 6, 7, 8])
#法一 欧式距离公式
dist1 = np.sqrt(np.sum((x - y) ** 2))
print(dist1)
#法二 调用库里函数
dist2 = np.linalg.norm(x - y)
print(dist2)
运行结果:
题目2:
pandas初级训练
1.查看pandas版本信息
print(pd.__version__)
运行结果:
2.从列表创建 Series
arr = [0, 1, 2, 3, 4]
df = pd.Series(arr)
print(df)
3.从dict创建Series
arry = {'AA':1,'BB':2,'CC':3,'DD':4,'EE':5}
df= pd.Series(arry)
print(df)
运行结果:
4.修改Series索引
df.index = ['A','B','C','D','E']
print(df)
运行结果:
5.纵向拼接
ListSeries = [1,2,3,4,5,5.6]
df2 = pd.Series(ListSeries)
print(df2)
arry = {'AA':1,'BB':2,'CC':3,'DD':4,'EE':5}
df= pd.Series(arry)
print(df)
df3 = df.append(df2)
print(df3)
6.Series按指定索引删除元素
df4= df3.drop('EE')
print(df4)
7.Series修改指定索引的元素
df3['AA'] = 100
print(df3)
8.Series的切片操作
print(df3[:3]) #前3个
print(df3[1:4]) #中间3个
print(df3[:-3]) #后三个
题目3:
Histogram and density estimation
Generate a vector z of 10000 observations from your favorite exotic distribution. Then make a plot that shows a histogram of z (with 25 bins), along with an estimate for the density, using a Gaussian kernel density estimator (see scipy.stats). See Figure 2 for an example plot.
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
z = np.random.normal(100, 50, 10000)
kernel = stats.gaussian_kde(z)
ind = np.linspace(-100,300,1000)
plt.hist(z, 25,rwidth=0.8,density=True)
plt.plot(ind, kernel.evaluate(ind), label='kde')
plt.show()