简体   繁体   English

双变量CDF / CCDF分布Python

[英]Bivariate CDF/CCDF Distribution Python

I am trying to plot a bivariate ccdf of the dataset that has x and y values both. 我正在尝试绘制同时具有xy值的数据集的双变量ccdf

Univariate I can plot very well, below is the input and the code is for univeriate dataset. 我可以很好地绘制单变量,下面是输入,该代码用于统一数据集。

Input: These are only first 20 rows of the data points. 输入:这些仅是数据点的前20行。 Input has 1000s of rows and of which col[1] and col[3] needs to be plotted as they posses a user and keyword frequency relationship. 输入具有1000行,其中col[1]col[3]具有用户和关键字的频率关系,因此需要对其进行绘制。

tweetcricscore  34 #afgvssco   51
tweetcricscore  23 #afgvszim   46
tweetcricscore  24 #banvsire   12
tweetcricscore  456 #banvsned  46
tweetcricscore  653 #canvsnk   1
tweetcricscore  789 #cricket   178
tweetcricscore  625 #engvswi   46
tweetcricscore  86 #hkvssco    23
tweetcricscore  3 #indvsban    1
tweetcricscore  87 #sausvsvic  8
tweetcricscore  98 #wt20       56

Code: univeriate dataset 代码:统一数据集

import numpy as np
import matplotlib.pyplot as plt
from pylab import*
import math
from matplotlib.ticker import LogLocator

data = np.genfromtxt('keyword.csv', delimiter=',', comments=None)

d0=data[:,1]
X0 = np.sort(d0)
cdf0 = np.arange(len(X0))/float(len(X0))
ccdf0 = 1 - cdf0
plt.plot(X0,ccdf0, color='b', marker='.', label='Keywords')

plt.legend(loc='upper right')
plt.xlabel('Freq (x)')
plt.ylabel('ccdf(x)')
plt.gca().set_xscale("log")
#plt.gca().set_yscale("log")
plt.show()

I am looking for some option for bivariate data points. 我正在寻找一些双变量数据点的选择。 I referred Seaborn Bivariate Distribution But I am not able to put it in proper context with my dataset. 我提到了Seaborn双变量分布,但无法将其与数据集放在适当的上下文中。

Any alternative suggestion within python, matplotlib, seaborn are welcome.. Thanks in advance. 欢迎在python,matplotlib,seaborn中提出任何其他建议。

Bivariate distributions the way you're trying to describe are oftentimes continuous, for instance the size of a house (input, x) and it's price (output, y.) In your case there is no meaningful relationship (I think) in the number of the keyword, as it's probably just an ID assigned to the keyword right? 您尝试描述的双变量分布通常是连续的,例如房屋的大小(输入x)和价格(输出y)。在您的情况下,数字中没有有意义的关系(我认为)的关键字,因为它可能只是分配给关键字的ID对吗?

In your case to me it seems as though you have categories (keywords). 就您而言,好像您有类别(关键字)。 each category appears to have two numbers a tweetcricscore and a keyword number. 每个类别似乎都有两个数字tweetcricscore和一个keyword数字。 \\ \\

Your code here: 您的代码在这里:

cdf0 = np.arange(len(X0))/float(len(X0))

To me suggests that your x range is just their labels and not a meaningful value. 对我来说,您的x范围只是它们的标签,而不是有意义的值。

A better source for categorical plots can be found here . 这里可以找到分类图的更好来源。

To create a bivariate distribution, assuming that's still what you want having read that, you'd do the following using your data as an example using your data from above: 要创建一个双变量分布,并假设您仍然希望阅读该数据,可以使用上面的数据作为示例,执行以下操作:

import numpy as np
import seaborn as sns

col_1 = np.array([34, 23, 24, 456, 653, 789, 625, 86, 3, 87, 98])
col_3 = np.array([51, 46, 12, 46, 1, 178, 46, 23, 1, 8, 56])

sns.jointplot(x=col_3, y=col_1)

Which produces the very nonsensical figure here: 在这里产生非常荒谬的数字:

在此处输入图片说明

You'll have to add the x and y labels manually; 您必须手动添加x和y标签; this is because you're passing numpy array s instead of pandas Dataframes which can be thought of like dictionaries where each key in the dictionary is the title of a column, and the value the numpy array. 这是因为您传递的是numpy array而不是pandas Dataframes ,可以将其视为dictionaries ,其中dictionaries中的每个键都是列的标题,而值是numpy数组。

Using random numbers to show how it might look with a more random, continuous, related dataset. 使用随机数显示在随机性,连续性和相关性更高的数据集中的外观。

This is the example taken from the docs. 这是从文档中获取的示例。

import numpy as np
import seaborn as sns
import pandas as pd

mean, cov = [0, 1], [(1, .5), (.5, 1)]
data = np.random.multivariate_normal(mean, cov, 200)
df = pd.DataFrame(data, columns=["x", "y"])
sns.jointplot(x="x", y="y", data=df);

Which gives this: 这给出了:

在此处输入图片说明

The bar graphs on top of the chart can be thought of as uni variate charts (what you probably have produced) because they just describe the distribution of one or the other variable (x, or y, col_3, or col_1) 图表顶部的条形图可以被视为单变量图表(您可能已生成),因为它们仅描述一个或另一个变量(x或y,col_3或col_1)的分布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM