Python数据分析——NumPy、Matplotlib、Pandas

   题目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()

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值