Seasborn Distplot无法响应 [英] Seasborn Distplot goes unresponsive

查看:97
本文介绍了Seasborn Distplot无法响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pandas seaborn Distplot c $ c>了解数据集的密度。

I am trying to plot a simple Distplot using pandas and seaborn to understand the density of the datasets.

输入

#Car,45
#photo,4
#movie,6
#life,1
#Horse,14
#Pets,20
#run,67
#picture,89

数据集的 10K 行以上,没有标题,而我尝试使用 col [1]

The dataset has above 10K rows, no headers and I am trying to use col[1]

code

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns


df = pd.read_csv('keyword.csv', delimiter=',', header=None, usecols=[1])
#print df
sns.distplot(df)

plt.show()

没有错误,因为我可以打印输入列,但是 distplot 花费了很多时间来计算和冻结我的屏幕。任何加快该过程的建议。

No error as I can print the input column but the distplot is taking ages to compute and freezes my screen. Any suggestion to speed the process.

Edit1:如以下注释中所建议,我尝试从 pandas进行更改。从read_csv np.loadtxt ,现在出现错误。

As Suggested in the Comment Below I try to change from pandas.read_csv to np.loadtxt and now I get an error.

代码:

import numpy as np
from numpy import log as log
import matplotlib.pyplot as plt
import seaborn as sns
import pandas

df = np.loadtxt('keyword.csv', delimiter=',', usecols=(1), unpack=True)
sns.kdeplot(df)
sns.distplot(df)

plt.show()

错误:

Traceback (most recent call last):
  File "0_distplot_csv.py", line 7, in <module>
    df = np.loadtxt('keyword.csv', delimiter=',', usecols=(1), unpack=True)
  File "/usr/local/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 726, in loadtxt
    usecols = list(usecols)
TypeError: 'int' object is not iterable 

编辑2:我确实尝试了评论部分中提到的建议

Edit 2: I did try the mentioned suggestions from the comment section

sns.distplot(df[1])

这与最初提到的相同。屏幕冻结了一段时间。

This does the same as mentioned initially. The screen is frozen for ages.

sns.distplot(df[1].values)

在这种情况下,我看到了一个奇怪的行为。

I see a strange behavior in this case.

输入为

Car,45
photo,4
movie,6
life,1
Horse,14
Pets,20
run,67
picture,89

它确实会绘图,但是当输入低于此值时

It does plot but when the input is below

#Car,45
#photo,4
#movie,6
#life,1
#Horse,14
#Pets,20
#run,67
#picture,89

又是同样冻结整个屏幕,什么也不做。

It is again the same freezing entire screen and would do nothing.

我确实尝试过放置 comments = None 认为可能是将其作为注释来阅读。但是,熊猫中似乎没有使用注释

I did try to put comments=None thinking it might be reading them as comments. But looks like comments isn't used in pandas.

谢谢您

推荐答案

经过几次试用和大量在线访问搜索后,我终于可以找到想要的东西。当我们没有标题时,该代码允许使用列号加载数据。这还会读取带有条注释的行。

After several trials and a lot of online search, I could finally get what I was looking for. The code allows to load data with column number when we do not have headers. This also reads the rows with # comments.

代码:

import numpy as np
import matplotlib.pyplot as plt
from pylab import*
import math
from matplotlib.ticker import LogLocator
from scipy.stats.kde import gaussian_kde
import seaborn as sns

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

d0=data[:,1]

#Plot a simple histogram with binsize determined automatically
sns.kdeplot(np.array(d0), color='b', bw=0.5, marker='o', label='keyword')

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

这篇关于Seasborn Distplot无法响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆