栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1002133.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号