我正在自学如何使用scikit-learn,我决定开始第二项任务但是用我自己的语料库.我手工获得了一些二重奏,让我们说:
training_data = [[('this', 'is'), ('is', 'a'),('a', 'text'), 'POS'],
[('and', 'one'), ('one', 'more'), 'NEG']
[('and', 'other'), ('one', 'more'), 'NEU']]
Run Code Online (Sandbox Code Playgroud)
我想以一种格式对它们进行矢量化,这种格式可以很好地填写scikit-learn(svc,多项式朴素贝叶斯等)提供的一些分类算法.这是我试过的:
from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer(analyzer='word')
X = count_vect.transform(((' '.join(x) for x in sample)
for sample in training_data))
print X.toarray()
Run Code Online (Sandbox Code Playgroud)
这个问题是我不知道如何处理标签(即'POS', 'NEG', 'NEU'),我是否需要"矢量化"标签,以便传递training_data给分类算法,或者我可以让它像'POS'或任何其他那种字符串?另一个问题是我得到了这个:
raise ValueError("Vocabulary wasn't fitted or is empty!")
ValueError: Vocabulary wasn't fitted or is empty!
Run Code Online (Sandbox Code Playgroud)
那么,我怎样才能将bigrams像矢量化一样training_data.我也在阅读有关dictvectorizer和Sklearn-pandas的文章,你们认为使用它们对于这个任务来说可能是更好的方法吗?
我想将一个pandas数据帧显示为带有flask的boostrap-html表,因此我尝试了以下方法:
数据(.csv表):
Name Birth Month Origin Age Gender
Carly Jan Uk 10 F
Rachel Sep UK 20 F
Nicky Sep MEX 30 F
Wendy Oct UK 40 F
Judith Nov MEX 39 F
Run Code Online (Sandbox Code Playgroud)
python代码(python_script.py):
from flask import *
import pandas as pd
app = Flask(__name__)
@app.route("/tables")
def show_tables():
data = pd.read_csv('table.csv')
data.set_index(['Name'], inplace=True)
data.index.name=None
females = data.loc[data.Gender=='f']
return render_template('view.html',tables=[females.to_html(classes='female')],
titles = ['na', 'Female surfers'])
if __name__ == "__main__":
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
该templates目录(view.html):
<!doctype html>
<title>Simple tables</title> …Run Code Online (Sandbox Code Playgroud) 让我们假设我有以下列表:
l = ['the quick fox', 'the', 'the quick']
Run Code Online (Sandbox Code Playgroud)
我想将列表中的每个元素转换为URL,如下所示:
['<a href="http://url.com/the">the</a>', '<a href="http://url.com/quick">quick</a>','<a href="http://url.com/fox">fox</a>', '<a href="http://url.com/the">the</a>','<a href="http://url.com/the">the</a>', '<a href="http://url.com/quick">quick</a>']
Run Code Online (Sandbox Code Playgroud)
到目前为止,我尝试了以下内容:
list_words = ['<a href="http://url.com/{}">{}</a>'.format(a, a) for a in x[0].split(' ')]
Run Code Online (Sandbox Code Playgroud)
问题是上面的列表理解只是为列表的第一个元素工作:
['<a href="http://url.com/the">the</a>',
'<a href="http://url.com/quick">quick</a>',
'<a href="http://url.com/fox">fox</a>']
Run Code Online (Sandbox Code Playgroud)
我也试过了,map但它不起作用:
[map('<a href="http://url.com/{}">{}</a>'.format(a,a),x) for a in x[0].split(', ')]
Run Code Online (Sandbox Code Playgroud)
有关如何从句子列表的标记创建此类链接的任何想法?
我有一些大事,让我们说:[('word','word'),('word','word'),...,('word','word')].我如何使用scikit HashingVectorizer创建一个特征向量,随后将呈现给某些分类算法,例如SVC或Naive Bayes或任何类型的分类算法?
我有一个不平衡的文本数据集,它的外观如下:
label | texts(documents)
----------
5 |1190
4 |839
3 |239
1 |204
2 |127
Run Code Online (Sandbox Code Playgroud)
我试图使用fit(X, y[, sample_weight])参数,但我在文档中没有理解这是如何预期的.我尝试了以下方法:
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import balance_weights
classifier=RandomForestClassifier(n_estimators=10,criterion='entropy')
classifier.fit(X_train, y_train,sample_weight = balance_weights(y))
prediction = classifier.predict(X_test)
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个例外:
/usr/local/lib/python2.7/site-packages/sklearn/utils/__init__.py:93: DeprecationWarning: Function balance_weights is deprecated; balance_weights is an internal function and will be removed in 0.16
warnings.warn(msg, category=DeprecationWarning)
Traceback (most recent call last):
File "/Users/user/RF_classification.py", line 34, in <module>
classifier.fit(X_train, y_train,sample_weight = balance_weights(y))
File "/usr/local/lib/python2.7/site-packages/sklearn/ensemble/forest.py", line 279, in fit
for …Run Code Online (Sandbox Code Playgroud) 从我准备好的文档中读到:
FunctionTransformer 将其 X(和可选的 y)参数转发给用户定义的函数或函数对象,并返回该函数的结果。这对于无状态转换非常有用,例如获取频率对数、进行自定义缩放等。
但是,我不明白这个功能有什么用处。有人能解释一下这个功能的目的吗?
我有文件.txt包含这样的单词列表:
5.91686268506 exclusively, catering, provides, arms, georgia, formal, purchase, choose
5.91560417296 hugh, senlis
5.91527936181 italians
5.91470429433 soil, cultivation, fertile
5.91468087491 increases, moderation
....
5.91440227412 farmers, descendants
Run Code Online (Sandbox Code Playgroud)
我想将这些数据转换为pandas表,我希望将其显示到html/bootstrap模板中,如下所示(*):
COL_A COL_B
5.91686268506 exclusively, catering, provides, arms, georgia, formal, purchase, choose
5.91560417296 hugh, senlis
5.91527936181 italians
5.91470429433 soil, cultivation, fertile
5.91468087491 increases, moderation
....
5.91440227412 farmers, descendants
Run Code Online (Sandbox Code Playgroud)
所以我用熊猫尝试了以下内容:
import pandas as pd
df = pd.read_csv('file.csv',
sep = ' ', names=['Col_A', 'Col_B'])
df.head(20)
Run Code Online (Sandbox Code Playgroud)
但是,我的表没有上述所需的结构:
COL_A COL_B
6.281426 engaged, chance, makes, meeting, nations, things, believe, …Run Code Online (Sandbox Code Playgroud) lis在进行了大量的重新格式化之后,我有以下元组:
[[(('A', 'X'), ('43,23', 'Y'), ('wepowe', 'd'))]]
Run Code Online (Sandbox Code Playgroud)
我怎样才能重新格式化为:
'A', '43,23', 'wepowe'
Run Code Online (Sandbox Code Playgroud)
我试过了:
[' '.join(map(str,lis[0][0])) for x in lis]
Run Code Online (Sandbox Code Playgroud)
和
[' '.join(map(str,lis[0][:1])) for x in lis]
Run Code Online (Sandbox Code Playgroud)
和
' '.join(map(str, lis))
Run Code Online (Sandbox Code Playgroud)
但是,我没有得到所需的格式.哪个是重新格式化元组和列表的简单方法,如上所述?
我安装了 rasa-demo 代码示例。为了打开 rasa API,我做了:
\n\nuser@User:~/rasa-demo \xe2\x80\xb9master*\xe2\x80\xba$ rasa run\nNo chat connector configured, falling back to the REST input channel. To connect your bot to another channel, read the docs here: https://rasa.com/docs/rasa/user-guide/messaging-and-voice-channels\n2020-06-19 13:20:02 INFO root - Starting Rasa server on http://localhost:5005\n2020-06-19 13:20:08 INFO absl - Using /var/folders/h5/9rj1zn8x4s59bk_mg_ktzv740000gn/T/tfhub_modules to cache modules.\n2020-06-19 13:20:08 INFO absl - Downloading TF-Hub Module \'http://models.poly-ai.com/convert/v1/model.tar.gz\'.\n2020-06-19 13:20:26 INFO absl - Downloading http://models.poly-ai.com/convert/v1/model.tar.gz: 22.35MB\n2020-06-19 13:20:43 INFO absl - Downloading http://models.poly-ai.com/convert/v1/model.tar.gz: 42.35MB\n2020-06-19 13:21:02 INFO absl - Downloading http://models.poly-ai.com/convert/v1/model.tar.gz: 82.35MB\n2020-06-19 13:21:21 …Run Code Online (Sandbox Code Playgroud)