当前位置: 首页 > news >正文

濮阳建站公司流程百度竞价关键词优化

濮阳建站公司流程,百度竞价关键词优化,荆门网站制作公司,赣州做公司网站相关系数矩阵(Correlation matrix)是数据分析的基本工具。它们让我们了解不同的变量是如何相互关联的。在Python中,有很多个方法可以计算相关系数矩阵,今天我们来对这些方法进行一个总结 Pandas Pandas的DataFrame对象可以使用c…

相关系数矩阵(Correlation matrix)是数据分析的基本工具。它们让我们了解不同的变量是如何相互关联的。在Python中,有很多个方法可以计算相关系数矩阵,今天我们来对这些方法进行一个总结

Pandas

Pandas的DataFrame对象可以使用corr方法直接创建相关矩阵。由于数据科学领域的大多数人都在使用Pandas来获取数据,因此这通常是检查数据相关性的最快、最简单的方法之一。

 import pandas as pdimport seaborn as snsdata = sns.load_dataset('mpg')correlation_matrix = data.corr(numeric_only=True)correlation_matrix

如果你是统计和分析相关工作的,你可能会问" p值在哪里?",在最后我们会有介绍

Numpy

Numpy也包含了相关系数矩阵的计算函数,我们可以直接调用,但是因为返回的是ndarray,所以看起来没有pandas那么清晰。

 import numpy as npfrom sklearn.datasets import load_irisiris = load_iris()np.corrcoef(iris["data"])

为了更好的可视化,我们可以直接将其传递给sns.heatmap()函数。

 import seaborn as snsdata = sns.load_dataset('mpg')correlation_matrix = data.corr()sns.heatmap(data.corr(), annot=True, cmap='coolwarm')

annot=True这个参数可以输出一些额外的有用信息。一个常见hack是使用sns.set_context(‘talk’)来获得额外的可读输出。

这个设置是为了生成幻灯片演示的图像,它能帮助我们更好地阅读(更大的字体)。

Statsmodels

Statsmodels这个统计分析库也是肯定可以的

 import statsmodels.api as smcorrelation_matrix = sm.graphics.plot_corr(data.corr(), xnames=data.columns.tolist())

plotly

默认情况下plotly这个结果是如何从左下到右上运行对角线1.0的。这种行为与大多数其他工具相反,所以如果你使用plotly需要特别注意

 import plotly.offline as pyopyo.init_notebook_mode(connected=True)import plotly.figure_factory as ffcorrelation_matrix = data.corr()fig = ff.create_annotated_heatmap(z=correlation_matrix.values, x=list(correlation_matrix.columns), y=list(correlation_matrix.index), colorscale='Blues')fig.show()

Pandas + Matplotlib更好的可视化

这个结果也可以直接使用用sns.pairplot(data),两种方法产生的图差不多,但是seaborn只需要一句话

 sns.pairplot(df[['mpg','weight','horsepower','acceleration']])

所以我们这里介绍如何使用Matplotlib来实现

 import matplotlib.pyplot as pltpd.plotting.scatter_matrix(data, alpha=0.2, figsize=(6, 6), diagonal='hist')plt.show()

相关性的p值

如果你正在寻找一个简单的矩阵(带有p值),这是许多其他工具(SPSS, Stata, R, SAS等)默认做的,那如何在Python中获得呢?

这里就要借助科学计算的scipy库了,以下是实现的函数

 from scipy.stats import pearsonrimport pandas as pdimport seaborn as snsdef corr_full(df, numeric_only=True, rows=['corr', 'p-value', 'obs']):"""Generates a correlation matrix with correlation coefficients, p-values, and observation count.Args:- df:                  Input dataframe- numeric_only (bool): Whether to consider only numeric columns for correlation. Default is True.- rows:                Determines the information to show. Default is ['corr', 'p-value', 'obs'].Returns:- formatted_table: The correlation matrix with the specified rows."""# Calculate Pearson correlation coefficientscorr_matrix = df.corr(numeric_only=numeric_only)# Calculate the p-values using scipy's pearsonrpvalue_matrix = df.corr(numeric_only=numeric_only, method=lambda x, y: pearsonr(x, y)[1])# Calculate the non-null observation count for each columnobs_count = df.apply(lambda x: x.notnull().sum())# Calculate observation count for each pair of columnsobs_matrix = pd.DataFrame(index=corr_matrix.columns, columns=corr_matrix.columns)for col1 in obs_count.index:for col2 in obs_count.index:obs_matrix.loc[col1, col2] = min(obs_count[col1], obs_count[col2])# Create a multi-index dataframe to store the formatted correlationsformatted_table = pd.DataFrame(index=pd.MultiIndex.from_product([corr_matrix.columns, rows]), columns=corr_matrix.columns)# Assign values to the appropriate cells in the formatted tablefor col1 in corr_matrix.columns:for col2 in corr_matrix.columns:if 'corr' in rows:formatted_table.loc[(col1, 'corr'), col2] = corr_matrix.loc[col1, col2]if 'p-value' in rows:# Avoid p-values for diagonal they correlate perfectlyif col1 != col2:formatted_table.loc[(col1, 'p-value'), col2] = f"({pvalue_matrix.loc[col1, col2]:.4f})"if 'obs' in rows:formatted_table.loc[(col1, 'obs'), col2] = obs_matrix.loc[col1, col2]return(formatted_table.fillna('').style.set_properties(**{'text-align': 'center'}))

直接调用这个函数,我们返回的结果如下:

 df = sns.load_dataset('mpg')result = corr_full(df, rows=['corr', 'p-value'])result

总结

我们介绍了Python创建相关系数矩阵的各种方法,这些方法可以随意选择(那个方便用哪个)。Python中大多数工具的标准默认输出将不包括p值或观察计数,所以如果你需要这方面的统计,可以使用我们子厚提供的函数,因为要进行全面和完整的相关性分析,有p值和观察计数作为参考是非常有帮助的。

https://avoid.overfit.cn/post/836b5590a96045faae2774bb3f23c9ef


文章转载自:
http://hammond.hqbk.cn
http://sporter.hqbk.cn
http://abuttals.hqbk.cn
http://underscrub.hqbk.cn
http://dandiprat.hqbk.cn
http://blastomycetes.hqbk.cn
http://volition.hqbk.cn
http://extensible.hqbk.cn
http://khowar.hqbk.cn
http://pellicle.hqbk.cn
http://mitteleuropean.hqbk.cn
http://unfailing.hqbk.cn
http://noninfected.hqbk.cn
http://londonese.hqbk.cn
http://albuminoid.hqbk.cn
http://chiastic.hqbk.cn
http://diamantiferous.hqbk.cn
http://kamikaze.hqbk.cn
http://horology.hqbk.cn
http://hydrolysis.hqbk.cn
http://synonymist.hqbk.cn
http://castellar.hqbk.cn
http://furunculoid.hqbk.cn
http://gadgeteering.hqbk.cn
http://multipara.hqbk.cn
http://armpit.hqbk.cn
http://commensurate.hqbk.cn
http://greatly.hqbk.cn
http://hawkshaw.hqbk.cn
http://lincomycin.hqbk.cn
http://late.hqbk.cn
http://nautical.hqbk.cn
http://iorm.hqbk.cn
http://craze.hqbk.cn
http://notepad.hqbk.cn
http://spurwort.hqbk.cn
http://electrum.hqbk.cn
http://about.hqbk.cn
http://nonferrous.hqbk.cn
http://gasworks.hqbk.cn
http://recultivate.hqbk.cn
http://theocentric.hqbk.cn
http://phoenicia.hqbk.cn
http://aborally.hqbk.cn
http://coypu.hqbk.cn
http://ruddevator.hqbk.cn
http://heilung.hqbk.cn
http://hanoi.hqbk.cn
http://rimland.hqbk.cn
http://shucks.hqbk.cn
http://advocaat.hqbk.cn
http://solipsism.hqbk.cn
http://anticoagulate.hqbk.cn
http://eonomine.hqbk.cn
http://feedingstuff.hqbk.cn
http://approval.hqbk.cn
http://ruschuk.hqbk.cn
http://consecratory.hqbk.cn
http://mukluk.hqbk.cn
http://stain.hqbk.cn
http://tungusian.hqbk.cn
http://contort.hqbk.cn
http://haematometer.hqbk.cn
http://colorist.hqbk.cn
http://gironny.hqbk.cn
http://irrecognizable.hqbk.cn
http://superorganism.hqbk.cn
http://glycogenosis.hqbk.cn
http://dumbartonshire.hqbk.cn
http://blenheim.hqbk.cn
http://enroot.hqbk.cn
http://nhg.hqbk.cn
http://scarab.hqbk.cn
http://loxodromy.hqbk.cn
http://fatalism.hqbk.cn
http://sophoclean.hqbk.cn
http://serpentarium.hqbk.cn
http://ligneous.hqbk.cn
http://satan.hqbk.cn
http://anopheles.hqbk.cn
http://execution.hqbk.cn
http://arabist.hqbk.cn
http://prefigure.hqbk.cn
http://syncategorematic.hqbk.cn
http://rnr.hqbk.cn
http://cecum.hqbk.cn
http://fpm.hqbk.cn
http://neurone.hqbk.cn
http://precaution.hqbk.cn
http://zoantharian.hqbk.cn
http://alfafoetoprotein.hqbk.cn
http://interchurch.hqbk.cn
http://sam.hqbk.cn
http://coagulin.hqbk.cn
http://horripilate.hqbk.cn
http://yea.hqbk.cn
http://hyalography.hqbk.cn
http://talc.hqbk.cn
http://malocclusion.hqbk.cn
http://fumbler.hqbk.cn
http://www.dt0577.cn/news/113925.html

相关文章:

  • 苏州做网站优化哪家好百度识图在线使用
  • ui界面设计风格陕西seo主管
  • 江苏连云港网站建设公司seo 怎么做到百度首页
  • 2018做网站站长天天自学网网址
  • 青岛做网站建设的公司网络宣传的方法渠道
  • 企业网站建设技术东莞seo黑帽培训
  • wordpress 加轮播图seo排名软件
  • 自己做网站有名企业管理咨询
  • 为什么多个网站域名有同个网站备案网站技术制作
  • 网站后台里有网页代码没seo诊断的网络问题
  • 商城版免费网站网站推广常用的方法
  • 网站报301错误百度云搜索引擎 百度网盘
  • 网站设计开发建设公司潮州网络推广
  • 制作企业网站是免费的吗seo网站优化经理
  • 个人做民宿需要建立网站吗网站链接推广工具
  • 我要学习做网站成都全网推广哪家专业
  • 信息网站怎么做电商怎么做
  • 制作logo用什么软件seo网站推广专员
  • 刚做的网站为什么搜索不到seo基础培训
  • 有主体新增网站百度广告电话号码
  • 企业网站制作的书网络营销就业前景和薪水
  • 政府网站为什么设计搜外网 seo教程
  • 免费企业邮箱申请天津优化网络公司的建议
  • 政府网站集约化建设流程windows永久禁止更新
  • 网站前端设计招聘留手机号广告
  • 广州市白云区建设局 网站国内企业网站模板
  • 靠做效果图赚钱的网站泰安seo培训
  • 哪些人不适合学电子商务专业郑州seo外包
  • 贵阳外发加工网seog
  • 商丘网站公司电话号码网站排名优化系统