我有一个熊猫数据帧,我希望把它分成3组.我知道使用train_test_split从sklearn.cross_validation,一个可以在两个集(训练集和测试)分割数据.但是,我找不到任何关于将数据拆分为三组的解决方案.最好,我想拥有原始数据的索引.
我知道解决方法是使用train_test_split两次并以某种方式调整索引.但有没有更标准/内置的方法将数据分成3组而不是2组?
Elasticsearch不会开始使用./bin/elasticsearch.它引发了以下异常:
- ElasticsearchIllegalStateException[Failed to obtain node lock, is the following location writable?: [/home/user1/elasticsearch-1.4.4/data/elasticsearch]
Run Code Online (Sandbox Code Playgroud)
我检查了同一位置的权限,该位置拥有777权限,并由user1拥有.
ls -al /home/user1/elasticsearch-1.4.4/data/elasticsearch
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)drwxrwxrwx 3 user1 wheel 4096 Mar 8 13:24 . drwxrwxrwx 3 user1 wheel 4096 Mar 8 13:00 .. drwxrwxrwx 52 user1 wheel 4096 Mar 8 13:51 nodes
问题是什么?
尝试在没有root访问权限的情况下在linux上运行elasticsearch 1.4.4.
使用train_test_split()时如何获取数据的原始索引?
我所拥有的是以下内容
from sklearn.cross_validation import train_test_split
import numpy as np
data = np.reshape(np.randn(20),(10,2)) # 10 training examples
labels = np.random.randint(2, size=10) # 10 labels
x1, x2, y1, y2 = train_test_split(data, labels, size=0.2)
Run Code Online (Sandbox Code Playgroud)
但这并没有给出原始数据的索引.一种解决方法是将索引添加到数据(例如data = [(i, d) for i, d in enumerate(data)]),然后将其传递到内部train_test_split,然后再次展开.有没有更清洁的解决方案?
如何使用spacy从文本中提取名词短语?
我不是指词性标签.在文档中,我找不到任何关于名词短语或常规解析树的内容.
我正在使用nltk PunkSentenceTokenizer将文本标记为一组句子.但是,标记化器似乎不会将新段落或新行视为新句子.
>>> from nltk.tokenize.punkt import PunktSentenceTokenizer
>>> tokenizer = PunktSentenceTokenizer()
>>> tokenizer.tokenize('Sentence 1 \n Sentence 2. Sentence 3.')
['Sentence 1 \n Sentence 2.', 'Sentence 3.']
>>> tokenizer.span_tokenize('Sentence 1 \n Sentence 2. Sentence 3.')
[(0, 24), (25, 36)]
Run Code Online (Sandbox Code Playgroud)
我希望将新行视为句子的边界.无论如何要做到这一点(我也需要保存偏移量)?
我使用以下命令初始化配置文件:
ipython profile create myserver
Run Code Online (Sandbox Code Playgroud)
添加了这些行~/.ipython/profile_myserver/ipython_notebook_config.py:
c = get_config()
c.NotebookApp.ip = '*'
c.NotebookApp.port = 8889
Run Code Online (Sandbox Code Playgroud)
尝试启动笔记本:
ipython notebook --profile=myserver --debug
Run Code Online (Sandbox Code Playgroud)
它根本不读取配置文件.这是日志输出:
[W 16:26:56.607 NotebookApp] Unrecognized alias: '--profile=myserver', it will probably have no effect.
[D 16:26:56.609 NotebookApp] Config changed:
[D 16:26:56.609 NotebookApp] {'profile': u'myserver', 'NotebookApp': {'log_level': 10}}
...
[I 16:26:56.665 NotebookApp] 0 active kernels
[I 16:26:56.665 NotebookApp] The IPython Notebook is running at: http://localhost:8888/
Run Code Online (Sandbox Code Playgroud)
因为我已经明确指定了端口8889并且它仍然在8888上运行,所以它显然忽略了配置文件.我错过了什么?
是否有内置的方法可以分别获得每个班级的准确度分数?我知道在sklearn中我们可以通过使用获得整体准确性metric.accuracy_score.有没有办法获得个别班级的准确度分数?类似的东西metrics.classification_report.
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
Run Code Online (Sandbox Code Playgroud)
classification_report 不给出准确度分数:
print(classification_report(y_true, y_pred, target_names=target_names, digits=4))
Out[9]: precision recall f1-score support
class 0 0.5000 1.0000 0.6667 1
class 1 0.0000 0.0000 0.0000 1
class 2 1.0000 0.6667 0.8000 3
avg / total 0.7000 0.6000 0.6133 5
Run Code Online (Sandbox Code Playgroud)
准确度分数仅给出整体准确度:
accuracy_score(y_true, y_pred)
Out[10]: 0.59999999999999998
Run Code Online (Sandbox Code Playgroud) 我有一个列表字典,列表很长.如何以只显示列表中的几个元素的方式打印它?显然,我可以为此编写一个自定义函数,但有没有可以实现此功能的内置方法或库?例如,在打印大型数据框时,pandas可以很快地打印出来.
这个例子更能说明我的意思:
obj = {'key_1': ['EG8XYD9FVN',
'S2WARDCVAO',
'J00YCU55DP',
'R07BUIF2F7',
'VGPS1JD0UM',
'WL3TWSDP8E',
'LD8QY7DMJ3',
'J36U3Z9KOQ',
'KU2FUGYB2U',
'JF3RQ315BY'],
'key_2': ['162LO154PM',
'3ROAV881V2',
'I4T79LP18J',
'WBD36EM6QL',
'DEIODVQU46',
'KWSJA5WDKQ',
'WX9SVRFO0G',
'6UN63WU64G',
'3Z89U7XM60',
'167CYON6YN']}
Run Code Online (Sandbox Code Playgroud)
期望的输出:这样的事情:
{'key_1':
['EG8XYD9FVN', 'S2WARDCVAO', '...'],
'key_2':
['162LO154PM', '3ROAV881V2', '...']
}
Run Code Online (Sandbox Code Playgroud) 在弹性搜索中是否有更好的方法(除了发出匹配所有查询并手动平均所有返回文档的长度)以获得特定索引的平均文档长度?
如何将数据框中的所有数值乘以常量而不必明确指定列名?例:
In [13]: df = pd.DataFrame({'col1': ['A','B','C'], 'col2':[1,2,3], 'col3': [30, 10,20]})
In [14]: df
Out[14]:
col1 col2 col3
0 A 1 30
1 B 2 10
2 C 3 20
Run Code Online (Sandbox Code Playgroud)
我试过df.multiply但它也会影响字符串值,也可以连接几次.
In [15]: df.multiply(3)
Out[15]:
col1 col2 col3
0 AAA 3 90
1 BBB 6 30
2 CCC 9 60
Run Code Online (Sandbox Code Playgroud)
有没有办法保持字符串值完整而只将数值乘以常量?
python ×7
scikit-learn ×3
dataframe ×2
pandas ×2
dictionary ×1
ipython ×1
jupyter ×1
list ×1
nlp ×1
nltk ×1
numpy ×1
pretty-print ×1
scipy ×1
spacy ×1
tokenize ×1