小编DBE*_*BE7的帖子

python - 如何将numpy数组附加到pandas数据帧

我已经训练了一个Logistic回归分类器来预测评论是正面还是负面.现在,我想将函数返回的预测概率附加predict_proba到包含评论的Pandas数据框中.我尝试过这样的事情:

test_data['prediction'] = sentiment_model.predict_proba(test_matrix)
Run Code Online (Sandbox Code Playgroud)

显然,这不起作用,因为predict_proba返回2D-numpy数组.那么,最有效的方法是什么?我test_matrix用SciKit-Learn的CountVectorizer 创建:

vectorizer = CountVectorizer(token_pattern=r'\b\w+\b')
train_matrix = vectorizer.fit_transform(train_data['review_clean'].values.astype('U'))
test_matrix = vectorizer.transform(test_data['review_clean'].values.astype('U'))
Run Code Online (Sandbox Code Playgroud)

示例数据如下所示:

| Review                                     | Prediction         |                      
| ------------------------------------------ | ------------------ |
| "Toy was great! Our six-year old loved it!"|   0.986            |
Run Code Online (Sandbox Code Playgroud)

python numpy machine-learning pandas scikit-learn

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

PostgreSQL:如何连接行

所以,我有一个非常规的问题。我希望能够将具有相同 ID 的行连接成一大行。为了说明我的问题,让我举一个例子。这是查询:

SELECT b.id AS "ID",
       m.content AS "Conversation"
FROM bookings b 
INNER JOIN conversations c on b.id = c.conversable_id AND c.conversable_type = 'Booking'
INNER JOIN messages m on m.conversation_id = c.id
WHERE b.state IS NOT NULL
GROUP BY 1,2
LIMIT 1000;
Run Code Online (Sandbox Code Playgroud)

这是输出:

ID     **Conversation
1223    "blah, blah, blah, blah"
1223    " ... blaaaah, blah.."
1223    "more blaaah"
1223    "last blah"
5000    "new id, with more blah"
5000    "and the blah continues"
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以将对话行连接成一个聚合行,同时保留 ID?

像这样:

ID     Conversation
1223    "blah, …
Run Code Online (Sandbox Code Playgroud)

sql postgresql

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

PostgreSQL-错误:列不存在SQL状态:42703

我正在尝试进行队列分析,并根据承租人的第一个租赁年份(=承租人第一次租赁的年份)比较平均租金。基本上,我在问一个问题:我们是否保留第一年是2013年的房客而不是第一年是2015年的房客?

这是我的代码:

SELECT renter_id, 
       Min(Date_part('year', created_at)) AS first_rental_year, 
       ( Count(trip_finish) )             AS number_of_trips 
FROM   bookings 
WHERE  state IN ( 'approved', 'aboard', 'ashore', 'concluded', 'disputed' ) 
  AND  first_rental_year = 2013 
GROUP  BY 1 
ORDER  BY 1; 
Run Code Online (Sandbox Code Playgroud)

我收到的错误消息是:

ERROR:  column "first_rental_year" does not exist
LINE 6: ... 'aboard', 'ashore', 'concluded', 'disputed') AND first_rent...
                                                             ^

********** Error **********

ERROR: column "first_rental_year" does not exist
SQL state: 42703
Character: 208
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢。

sql postgresql

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

PostgreSQL:如何在使用 case 语句时避免被零除

我试图将两个 case 语句分开。如何避免偶尔被零除?我似乎无法弄清楚NULLIF方法。

 SELECT
           ROUND(100 * SUM(CASE WHEN be.event IN ('a') THEN 1 ELSE 0 END) / SUM(CASE WHEN be.event IN ('b', 'a') THEN 1 ELSE 0 END), 2)  AS "% Instant Book Today"
    FROM booking_events be
    WHERE
          be.created_at >= current_date AND
          be.created_at < current_date + interval '1 day';
Run Code Online (Sandbox Code Playgroud)

解决这个问题的最有效方法是什么?

sql postgresql

2
推荐指数
1
解决办法
6800
查看次数

如何将函数应用于向量中的每个连续n个元素

我正在尝试将季度收益转换为年度收益。给定一个季度回报向量,该怎么做?

我是R编程的新手,所以我真的一无所获。

给定一个a包含季度收益的向量:

a <- c(0.11, 0.02, 0.01, 0.1, 0.08, 0.04, 0.02, 0.03) # Two years worth of returns
Run Code Online (Sandbox Code Playgroud)

我想应用一些函数,该函数使用公式输出带有年收益的长度为2的向量:

Year 1: ((1 + 0.11) * (1 + 0.02) * (1 + 0.01) * (1 + 0.1))-1  = 0,2578742
Year 2: ((1 + 0.08) * (1 + 0.04) * (1 + 0.02) * (1 + 0.03))-1 = 0,180033
Run Code Online (Sandbox Code Playgroud)

最终向量:

yearly_vec <- c(0.2578742,  0.180033)

Run Code Online (Sandbox Code Playgroud)

r

2
推荐指数
1
解决办法
69
查看次数

为什么Django会引发"KeyError"?

我目前正在尝试为我的Django Web应用程序实现分页.但是,当我尝试使用库中的模板标签时,Django会引发一个关键错误.再具体一点:

KeyError
KeyError: 'request'
Run Code Online (Sandbox Code Playgroud)

这是我的settings.py:

from django.core.urlresolvers import reverse_lazy
from os.path import dirname, join, exists
import os

# Build paths inside the project like this: join(BASE_DIR, "directory")
BASE_DIR = dirname(dirname(dirname(__file__)))
STATICFILES_DIRS = [join(BASE_DIR, 'static')]
MEDIA_ROOT = join(BASE_DIR, 'media')
MEDIA_URL = "/media/"

# Use Django templates using the new Django 1.8 TEMPLATES settings
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            join(BASE_DIR, 'templates'),
            # insert more TEMPLATE_DIRS here
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Insert your …
Run Code Online (Sandbox Code Playgroud)

python django pagination twitter-bootstrap

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