小编Tre*_*eha的帖子

python:具有多个发行版的distplot

我正在使用seaborn绘制分布图.我想在不同颜色的同一图上绘制多个分布:

以下是我开始分配图的方法:

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
iris = pd.DataFrame(data= np.c_[iris['data'], iris['target']],columns= iris['feature_names'] + ['target'])

sns.distplot(iris[['sepal length (cm)']], hist=False, rug=True);
Run Code Online (Sandbox Code Playgroud)

'target'列包含3个值:0,1,2.

我想看一个萼片长度的分布图,其中target == 0,target == 1,target == 2,总共3个图.

有谁知道我是怎么做到的?

谢谢.

python seaborn

33
推荐指数
5
解决办法
4万
查看次数

R - 在jupyter中更改ggplot绘图大小

在jupyter笔记本中使用R,首先我普遍设置绘图大小.其次,我想绘制一个不同大小的单个地块.

## load ggplot2 library
library("ggplot2")
## set universal plot size:
options(repr.plot.width=6, repr.plot.height=4)

## plot figure. This figure will be 6 X 4
ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width))   +  geom_point() 

## plot another figure. This figure I would like to be 10X8
ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width))   +  geom_point() + HOW DO i CHANGE THE SIZE?
Run Code Online (Sandbox Code Playgroud)

如您所见,我想将第二个图(并且只有第二个图)更改为10X8.我该怎么做呢?

对于一个可能很愚蠢的问题感到抱歉,因为情节调整通常不是Rstudio中的问题.

r ggplot2 jupyter

16
推荐指数
1
解决办法
6787
查看次数

使用seaborn在xy散点图中添加标签

我花了好几个小时试图做我认为是一项简单的任务,即在使用seaborn时将标签添加到XY图上.

这是我的代码

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

df_iris=sns.load_dataset("iris") 

sns.lmplot('sepal_length', # Horizontal axis
           'sepal_width', # Vertical axis
           data=df_iris, # Data source
           fit_reg=False, # Don't fix a regression line
           size = 8,
           aspect =2 ) # size and dimension

plt.title('Example Plot')
# Set x-axis label
plt.xlabel('Sepal Length')
# Set y-axis label
plt.ylabel('Sepal Width')
Run Code Online (Sandbox Code Playgroud)

我想在图中的每个点添加"种类"栏中的文字.

我见过许多使用matplotlib但不使用seaborn的例子.

有任何想法吗?谢谢.

python plot seaborn

13
推荐指数
4
解决办法
2万
查看次数

Windows上的Jupyter中进行python多重处理:AttributeError:无法获取属性“ abc”

我正在尝试运行一个简单的命令,使用多处理程序按名称猜测性别。这段代码可以在以前的机器上运行,所以也许我的设置与此有关。

下面是我的多处理代码:

import sys
import gender_guesser.detector as gender
import multiprocessing
import time

d = gender.Detector()

def guess_gender (name):
    n = name.title() # make first letter upper case and the rest lower case 
    g = d.get_gender(n) # guess gender
    return g

ls = ['john','joe','amamda','derick','peter','ashley','john','joe','amamda','derick','peter','ashley']

t=time.time()

results=[]
def callBack(x):
    results.append(x)

pool = multiprocessing.Pool(processes=multiprocessing.cpu_count()-1, maxtasksperchild=1)

for n in ls:
    print (n)
    pool.apply_async(guess_gender,args=[n],callback=callBack)

pool.close()
pool.join()

results = pd.concat(results)

print(time.time()-t)
Run Code Online (Sandbox Code Playgroud)

它只是运行而不会做任何事情。在我的cmd窗口中,错误消息的结尾处显示以下内容:

AttributeError: Can't get attribute 'guess_gender' on <module '__main__' (built-in)>
Run Code Online (Sandbox Code Playgroud)

在Anaconda上运行python 3.6.1版本:

import sys
print(sys.version) …
Run Code Online (Sandbox Code Playgroud)

python multiprocessing jupyter-notebook

9
推荐指数
3
解决办法
9679
查看次数

如何在python 3.6上的Windows 10上安装cvxopt

如何在python 3.6上的Windows 10上安装cvxopt?

跑步时

conda install cvxopt

Fetching package metadata ...........
Solving package specifications: .

UnsatisfiableError: The following specifications were found to be in conflict:
  - cvxopt -> python 3.5*
  - python 3.6*
Use "conda info <package>" to see the dependencies for each package.
Run Code Online (Sandbox Code Playgroud)

我道歉,我在窗户上...

有任何想法吗?

anaconda cvxopt conda jupyter python-3.6

8
推荐指数
1
解决办法
6405
查看次数

线性回归并将结果存储在数据框中

我正在对数据框中的一些变量进行线性回归.我希望能够通过分类变量对线性回归进行子集化,对每个分类变量运行线性回归,然后将t-stats存储在数据框中.如果可能的话,我想在没有循环的情况下这样做.

这是我正在尝试做的一个示例:

  a<-  c("a","a","a","a","a",
         "b","b","b","b","b",
         "c","c","c","c","c")     
  b<-  c(0.1,0.2,0.3,0.2,0.3,
         0.1,0.2,0.3,0.2,0.3,
         0.1,0.2,0.3,0.2,0.3)
  c<-  c(0.2,0.1,0.3,0.2,0.4,
         0.2,0.5,0.2,0.1,0.2,
         0.4,0.2,0.4,0.6,0.8)
      cbind(a,b,c)
Run Code Online (Sandbox Code Playgroud)

我可以从运行以下线性回归开始,非常容易地拉出t统计量:

  summary(lm(b~c))$coefficients[2,3]
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够在列a为a,b或c时运行回归.我想将t-stats存储在一个如下所示的表中:

variable t-stat
a        0.9
b        2.4
c        1.1
Run Code Online (Sandbox Code Playgroud)

希望有道理.如果您有任何建议,请告诉我!

r linear-regression lm

7
推荐指数
3
解决办法
2万
查看次数

SQL 中使用 CASE 语句时不等于

在postgresql中,我有一个case声明,我需要添加一个“不等于”子句。

v1等于 v2 时,我希望它说 1,当v1 不等于 v2时,我想说 2。

create table test (
v1      varchar(20),
v2      varchar(20)
);

insert into test values ('Albert','Al'),('Ben','Ben')

select case v1
when v2 then 1
    else 3
end 
from test
Run Code Online (Sandbox Code Playgroud)

我尝试使用!=or <>,但这似乎不起作用。

有谁知道如何在caseSQL 语句中使用不等于吗?

sql postgresql case

6
推荐指数
1
解决办法
3万
查看次数

plotly:大量数据点

我正在尝试使用 plotly 绘制具有大量数据点(2mm-3mm)的东西。

当我跑

py.iplot(fig, filename='test plot')
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Woah there! Look at all those points! Due to browser limitations, the Plotly SVG drawing functions have a hard time graphing more than 500k data points for line charts, or 40k points for other types of charts. Here are some suggestions:
(1) Use the `plotly.graph_objs.Scattergl` trace object to generate a WebGl graph.
(2) Trying using the image API to return an image instead of a graph URL
(3) Use matplotlib
(4) See …
Run Code Online (Sandbox Code Playgroud)

python plotly

6
推荐指数
2
解决办法
4572
查看次数

python中的ggplot:绘图大小和颜色

各位,

我正在尝试在 python 中使用 ggplot。

from ggplot import *
ggplot(diamonds, aes(x='price', fill='cut')) + geom_density(alpha=0.25) + facet_wrap("clarity")
Run Code Online (Sandbox Code Playgroud)

我正在尝试做的几件事:

1)我希望颜色既填充又线条,但正如你所看到的颜色都是灰色的

2)我正在尝试调整绘图的大小。在 RI 中,会在情节之前运行:

options(repr.plot.width=12, repr.plot.height=4)
Run Code Online (Sandbox Code Playgroud)

但是,这在这里不起作用。

有谁知道我如何在分布中着色并改变绘图大小?

谢谢你。附上电流输出。

在此处输入图片说明

python ggplot2 python-ggplot

4
推荐指数
2
解决办法
6531
查看次数

Mutate - NA 处理

我已经使用了排列和变异组合来根据分组进行添加。例如,我使用了以下内容:

master_df <-group_by(master_df,asof_dt)
mutate(master_df,tot_flag=ls_flag)
Run Code Online (Sandbox Code Playgroud)

这将我的数据框 master_df 按 asof_dt 分组,然后创建 tot_flag 并按日期添加 ls_flag。

但是,我的 ls_flag 列包含 NA。

我想做以下事情:1) 找出如何添加 ls_flag,忽略任何 NA 2) 找出如何添加每天 NA 的总数。

这是完整的示例:

asof_dt<-c("2014-10-01","2014-10-01","2014-10-01","2014-10-02","2014-10-02","2014-10-02")
ls_flag<-c(1,1,NA,NA,1,1)
master_df<-data.frame(asof_dt,ls_flag)
master_df <-group_by(master_df,asof_dt)
mutate(master_df,tot_flag=sum(ls_flag))
Run Code Online (Sandbox Code Playgroud)

非常感谢!

r mutated na dplyr

3
推荐指数
1
解决办法
5052
查看次数