小编Tas*_*sos的帖子

python情节和powerlaw适合

我有以下列表:

[6, 4, 0, 0, 0, 0, 0, 1, 3, 1, 0, 3, 3, 0, 0, 0, 0, 1, 1, 0, 0, 0, 3, 2, 3, 3, 2, 5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 0, 0, 0, 2, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 3, 1, 1, 0, …
Run Code Online (Sandbox Code Playgroud)

python matplotlib power-law

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

用于python的tfidf算法

我有这个代码用于计算与tf-idf的文本相似性.

from sklearn.feature_extraction.text import TfidfVectorizer

documents = [doc1,doc2]
tfidf = TfidfVectorizer().fit_transform(documents)
pairwise_similarity = tfidf * tfidf.T
print pairwise_similarity.A
Run Code Online (Sandbox Code Playgroud)

问题是这个代码作为输入普通字符串,我想通过删除停用词,词干和tokkenize来准备文档.所以输入将是一个列表.如果我documents = [doc1,doc2]用tokkenized文件调用该错误是:

    Traceback (most recent call last):
  File "C:\Users\tasos\Desktop\my thesis\beta\similarity.py", line 18, in <module>
    tfidf = TfidfVectorizer().fit_transform(documents)
  File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\feature_extraction\text.py", line 1219, in fit_transform
    X = super(TfidfVectorizer, self).fit_transform(raw_documents)
  File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\feature_extraction\text.py", line 780, in fit_transform
    vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary)
  File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\feature_extraction\text.py", line 715, in _count_vocab
    for feature in analyze(doc):
  File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\feature_extraction\text.py", line 229, in <lambda>
    tokenize(preprocess(self.decode(doc))), stop_words)
  File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\feature_extraction\text.py", line …
Run Code Online (Sandbox Code Playgroud)

python tf-idf scikit-learn

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

美丽的汤和UnicodeDecodeError

我正在尝试抓取页面,但我有一个UnicodeDecodeError.这是我的代码:

def soup_def(link):
    req = urllib2.Request(link, headers={'User-Agent' : "Magic Browser"}) 
    usock = urllib2.urlopen(req)
    encoding = usock.headers.getparam('charset')
    page = usock.read().decode(encoding)
    usock.close()
    soup = BeautifulSoup(page)
    return soup

soup = soup_def("http://www.geekbuying.com/item/Ainol-Novo-10-Hero-II-Quad-Core--Tablet-PC-10-1-inch-IPS-1280-800-1GB-RAM-16GB-ROM-Android-4-1--HDMI-313618.html")
Run Code Online (Sandbox Code Playgroud)

而错误:

UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 284: invalid start byte
Run Code Online (Sandbox Code Playgroud)

我检查了几个用户有相同的错误,但我无法找到任何解决方案.

python encoding beautifulsoup

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

使用潜在语义分析和sklearn

我正在尝试编写一个脚本,我将计算几个文档的相似性.我想通过使用LSA来做到这一点.我找到了以下代码并稍微改了一下.我输入3个文档,然后作为输出3x3矩阵,它们之间具有相似性.我想进行相同的相似度计算,但只能使用sklearn库.那可能吗?

from numpy import zeros
from scipy.linalg import svd
from math import log
from numpy import asarray, sum
from nltk.corpus import stopwords
from sklearn.metrics.pairwise import cosine_similarity

titles = [doc1,doc2,doc3]
ignorechars = ''',:'!'''

class LSA(object):
    def __init__(self, stopwords, ignorechars):
        self.stopwords = stopwords.words('english')
        self.ignorechars = ignorechars
        self.wdict = {}
        self.dcount = 0        
    def parse(self, doc):
        words = doc.split();
        for w in words:
            w = w.lower()
            if w in self.stopwords:
                continue
            elif w in self.wdict:
                self.wdict[w].append(self.dcount)
            else:
                self.wdict[w] = [self.dcount]
        self.dcount += 1 …
Run Code Online (Sandbox Code Playgroud)

python scikit-learn

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

在Excel中使用If计算标准偏差

我有一个包含两列的数据集.一个标签和一个数字.使用sumifcountif,我设法计算组中每个标签的平均值.现在,我试图用标准偏差做同样的事情,但我有一个问题:

=STDEV.P(IF($A$2:$A$625129=F4,$B$2:$B$625129))

这是我使用的公式,其中A列是带有标签的列,B带有数字,F4是标签组之一.结果是0.我为每个标签组做同样的事情,所有标签组都是0.任何想法公式中有什么问题?

编辑:在评论之后,我尝试将公式应用为数组,它几乎起作用.现在,唯一的问题是单元格F4是静态的,而我希望它对于数组公式中的每个单元格都是动态的(F5,F6,F7等).

excel excel-2013

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

潜在语义分析(LSA)教程

我正在尝试使用此链接中的LSA教程(编辑:2017年7月.删除死链接)

这是教程的代码:

titles = [doc1,doc2]
stopwords = ['and','edition','for','in','little','of','the','to']
ignorechars = ''',:'!'''

class LSA(object):
    def __init__(self, stopwords, ignorechars):
        self.stopwords = open('stop words.txt', 'r').read()
        self.ignorechars = ignorechars
        self.wdict = {}
        self.dcount = 0        
    def parse(self, doc):
        words = doc.split();
        for w in words:
            w = w.lower()
            if w in self.stopwords:
                continue
            elif w in self.wdict:
                self.wdict[w].append(self.dcount)
            else:
                self.wdict[w] = [self.dcount]
        self.dcount += 1      
    def build(self):
        self.keys = [k for k in self.wdict.keys() if len(self.wdict[k]) > 1]
        self.keys.sort()
        self.A = zeros([len(self.keys), …
Run Code Online (Sandbox Code Playgroud)

python lsa

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

在美丽的汤上缺少部分结果

我试图<p>在以下HTML代码中检索几个标签.这里只是其中的一部分

<td class="eelantext">
    <a class="fBlackLink"></a>
    <center></center>
    <span> … </span><br></br>
    <table width="402" vspace="5" cellspacing="0" cellpadding="3" 
        border="0" bgcolor="#ffffff" align="Left">
    <tbody> … </tbody></table>
      <!--edstart-->
    <p> … </p>
    <p> … </p>
    <p> … </p>
    <p> … </p>
    <p> … </p>
</td>
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到这个网页

我的Python代码如下

soup = BeautifulSoup(page)
div = soup.find('td', attrs={'class': 'eelantext'})
print div
text = div.find_all('p') 
Run Code Online (Sandbox Code Playgroud)

但是text变量是空的,如果我打印div变量,除了<p>标签之外,我有完全相同的html .

python beautifulsoup

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

为动态类名称设置 CSS 规则

我有一个 WordPress 主题,可以创建具有动态类名称的元素。我不想弄乱 PHP 代码,所以我只想对 CSS 进行更改。

每个元素都有这样的代码:

<dd class="variation-testing">Testing</dd>
Run Code Online (Sandbox Code Playgroud)

我尝试使用这个 CSS 规则,它应该有效,但它似乎根本没有应用该元素:

dd.[class^=variation-] {
  width: 48%;
  float: left;
}
Run Code Online (Sandbox Code Playgroud)

如何仅使用类的第一部分添加 CSS?

更新

答案很简单,可以在评论中找到。正确的 CSS 是没有点的。

dd[class^=variation-] {
  width: 48%;
  float: left;
}
Run Code Online (Sandbox Code Playgroud)

css

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

以编程方式向 WooCommerce 购物车添加免税费用

我尝试根据我在 Woocommerce 购物车上所做的一些计算来增加费用,但我想将其从增值税中排除。这是我的代码:

function woo_add_cart_fee( $cart ) {
    global $woocommerce; $bookable_total = 0; 

    foreach(WC()->cart->get_cart() as $cart_item_key => $values) { 
        $_product = $values['data'];

        //doing my stuff to calculate $fee variable

    WC()->cart->add_fee( 'Fees: ', $fee, false, '' );
    //WC()->cart->add_fee( 'Fees: ', $fee, true, '' );
    //WC()->cart->add_fee( 'Fees: ', $fee, false, 'zero rate' );
    //WC()->cart->add_fee( 'Fees: ', $fee, true, 'zero rate' );
}

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
Run Code Online (Sandbox Code Playgroud)

我已经尝试了所有评论版本,每个版本也都包含增值税。

知道我如何实现它吗?

php wordpress cart woocommerce fee

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

从 Pandas 专栏中删除 Twitter 提及

我有一个包含 Twitter 推文的数据集。其中一些还有用户提及,例如@thisisauser. 我尝试在执行其他清洁过程的同时删除该文本。

def clean_text(row, options):

    if options['lowercase']:
        row = row.lower()

    if options['decode_html']:
        txt = BeautifulSoup(row, 'lxml')
        row = txt.get_text()

    if options['remove_url']:
        row = row.replace('http\S+|www.\S+', '')

    if options['remove_mentions']:
        row = row.replace('@[A-Za-z0-9]+', '')

    return row

clean_config = {
    'remove_url': True,
    'remove_mentions': True,
    'decode_utf8': True,
    'lowercase': True
    }

df['tweet'] = df['tweet'].apply(clean_text, args=(clean_config,))
Run Code Online (Sandbox Code Playgroud)

但是,当我运行上面的代码时,所有 Twitter 提到的内容仍然在文本中。我使用 Regex 在线工具验证了我的 Regex 工作正常,所以问题应该出在 Pandas 的代码上。

python pandas

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