我有一个矩阵,命名X_test,从产生的sklearn.feature_extraction.text.CountVectorizer.当我执行以下功能时:
import numpy as np
np.set_printoptions(threshold='nan')
print(X_test.shape)
print(X_test.size)
print(X_test.ndim)
print(np.array(X_test))
Run Code Online (Sandbox Code Playgroud)
我明白了:
(10211, 218904)
477881
2
(0, 934) 14
(0, 6773) 1
(0, 11035) 1
(0, 22362) 1
(0, 23619) 1
(0, 24812) 1
(0, 25224) 1
: :
(0, 64428) 1
(0, 66506) 1
Run Code Online (Sandbox Code Playgroud)
我不知道为什么尺寸不是矩阵尺寸(形状)的乘积.
我正在使用scikit-learn.我想聚集一个6GB的文档数据集并找到文档集群.
我只有4Gb内存.有没有办法让k-means在scikit-learn中处理大型数据集?
谢谢,如果您有任何疑问,请与我们联系.
我正在研究这里提供的数据集的机器学习算法.
共有26列数据.大部分都是毫无意义的.我怎样才能有效,快速地确定哪些特征是有趣的 - 哪些特征告诉我这样或那样的特定URL是短暂的还是常绿的(这是数据集中的因变量)?是否有智能的,程序化的scikit学习如何做到这一点,或者它只是一个图形的每个功能对依赖功能('标签',第26列)的图形,并看到有什么影响?
肯定有比这更好的方法!
有人可以帮忙吗?:)
编辑:我找到的分类器的一些代码 - 如何在这里打印出给每个功能的权重?
import numpy as np
import matplotlib.pyplot as plt
from sklearn import metrics,preprocessing,cross_validation
from sklearn.feature_extraction.text import TfidfVectorizer
import sklearn.linear_model as lm
import pandas as p
loadData = lambda f: np.genfromtxt(open(f,'r'), delimiter=' ')
print "loading data.."
traindata = list(np.array(p.read_table('train.tsv'))[:,2])
testdata = list(np.array(p.read_table('test.tsv'))[:,2])
y = np.array(p.read_table('train.tsv'))[:,-1]
tfv = TfidfVectorizer(min_df=3, max_features=None, strip_accents='unicode',
analyzer='word',token_pattern=r'\w{1,}',ngram_range=(1, 2), use_idf=1,smooth_idf=1,sublinear_tf=1)
rd = lm.LogisticRegression(penalty='l2', dual=True, tol=0.0001,
C=1, fit_intercept=True, intercept_scaling=1.0,
class_weight=None, random_state=None)
X_all = traindata + testdata
lentrain = len(traindata) …Run Code Online (Sandbox Code Playgroud) python artificial-intelligence machine-learning feature-detection scikit-learn
我致力于分类一些评论(段落)由多个句子组成.我通过libSVM在Weka中使用词袋功能对它们进行了分类.但是,我有另一个想法,我不知道如何实现:
我认为在评论中为每个句子创建基于语法和浅层语义的特征是值得尝试的.但是,由于段落的句子大小不同,我找不到任何顺序编码这些功能的方法.我想将这些特征保持在一个顺序中的原因是句子特征的顺序可以为分类提供更好的线索.例如,如果我有两个实例P1(有3个句子)和P2(2个句子),我会有一个这样的空格(假设每个句子有一个二进制特征作为a或b):
P1 - > abb/classX P2 - > ba/classY
所以,我的问题是我是否可以在特征空间中实现不同特征尺寸的分类?如果是的话,我可以在Weka,scikit-learn或Mallet中使用任何类型的分类器吗?我将不胜感激任何回应.
谢谢
我有2个文件doc1.txt和doc2.txt.这两份文件的内容如下:
#doc1.txt
very good, very bad, you are great
#doc2.txt
very bad, good restaurent, nice place to visit
Run Code Online (Sandbox Code Playgroud)
我想让我的语料库分开,,以便我的最终DocumentTermMatrix成为:
terms
docs very good very bad you are great good restaurent nice place to visit
doc1 tf-idf tf-idf tf-idf 0 0
doc2 0 tf-idf 0 tf-idf tf-idf
Run Code Online (Sandbox Code Playgroud)
我知道,如何计算DocumentTermMatrix的各个单词(使用http://scikit-learn.org/stable/modules/feature_extraction.html),但不知道如何计算DocumentTermMatrix的stringsPython编写的.
聚类数据集,然后将数据转换为使用sklearn.cluster.KMeans从质心的距离后,才可能扭转改造,给出的质心,找回原来的特点是什么?
python machine-learning k-means dimensionality-reduction scikit-learn
我正在使用RBF内核训练1200个标签2和1200个标签1示例的SVM.我以为我的准确率达到了77%,而且我正在使用它sklearn.metrics.accuracy_score.但是,当我手动推出自己的精确分数时,如下:
def naive_accuracy(true, pred):
number_correct = 0
i = 0
for y in true:
if pred[i] == y:
number_correct += 1.0
return number_correct / len(true)
Run Code Online (Sandbox Code Playgroud)
它得到了50%.我相信基于错误的准确度分数和分类报告,我浪费了数周的工作.任何人都能向我提供解释为什么会发生这种情况的原因吗?关于如何发生这种情况,我非常非常困惑.我不明白我做错了什么.当我在一些虚拟数据上测试metrics.accuracy_score函数时pred = [1, 1, 2, 2]; test = [1, 2, 1, 2],它给了我50%的预期效果.我认为,根据我的具体数据,accuracy_score可能会犯错误.
我有27个特征向量和1200个向量的类1和1200向量的类2.我的代码如下:
X = scale(np.asarray(X))
y = np.asarray(y)
X_train, X_test, y_train, y_test = train_test_split(X, y)
######## SVM ########
clf = svm.SVC()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
# 77%
print "SVM Accuracy:", accuracy_score(y_test, y_pred) # debugging
# 50%
print "*True* SVM …Run Code Online (Sandbox Code Playgroud) 我对python和机器学习都很陌生.我正在努力研究Twitter数据的情感分析,所以在研究时我直接使用sklearn而不需要在nltk中进行任何预处理.
#reading data from csv having 1 column with text and other with sentiment as pos and neg
for index, row in val.iterrows():
statement = row['tweets'].strip() #get the tweet from csv
tweets.append((statement, row['emo'])) #append the tweet and emotion(pos,neg)
Run Code Online (Sandbox Code Playgroud)
然后我用了这个classfier
classifier = Pipeline([
('vectorizer', CountVectorizer()),
('tfidf', TfidfTransformer()),
('classifier', OneVsRestClassifier(LinearSVC())
)])
#Dividing data into training and Testing
np.random.shuffle(tweets)
for key, value in tweets:
keys.append(key)
values.append(value)
size = len(keys) * 1 / 2
X_train = np.array(keys[0:size])
y_train = np.array(values[0:size])
X_test = np.array(keys[size + …Run Code Online (Sandbox Code Playgroud) 我正在使用sklearn用tf-idf Vectorizer对象进行一些NLP矢量化.可以使用关键字"token_pattern"构造此对象.
我想避免使用hashtags(#foobar),数字(以及以数字开头的字符串,即10mg),以"RT"(转发)开头的任何行,或"删除的推文"行.
另外,我想忽略unicode.
我想保留URL(不是'http://'),并将它们标记为可能存在于其中的任何单词([A-Za-z] +).
我对Regex有一些经验,但到目前为止还不需要更复杂的模式.
以下是我对所有内容的刺激......这显然不是最好的调查方式,但它总结了我目前对正则表达式规则的看法.
注意:这里的skearn doc 显示了使用字符串上的unicode标志的默认"token_pattern",我不明白为什么......也许是单独的问题.
pat2 = r"(?im)([A-Z]+)(?<!^@)([A-Z]+)(?<!^#)([A-Z]+)(?<!^(RT))([A-Z]+)(?<!^Deleted)(?<=^(http://))([A-Z]+)"
Run Code Online (Sandbox Code Playgroud)
我的分解:
(?im) #Are flags for 'multi-line' and 'case insensitive'
([A-Z]+)(?<!^@) #A negative look back, match [A-Z]+ only if not preceded by 'starts with @'.
(?<=^(http://))([A-Z]+) #A positive look forward, match [A-Z]+ only if 'starts with "http://"' is present.
Run Code Online (Sandbox Code Playgroud)
我觉得这不是一个优雅的解决方案,即使它被调整为工作......
TIA
更新:原始数据示例:
如果知道有用,我正在使用pandas数据帧来加载数据.我是熊猫的新手,可能会错过一些基于熊猫的解决方案.
从这些原始数据中,我只想要从文本和URL中获取的单词.这个例子很糟糕......请进一步评论,以帮助我更好地定义...... thx!
生的:
http://foxsportswisconsin.ning.com/profiles/blogs/simvastatin-20-mg-pas-cher-sur-internet-acheter-du-simvastatin-20
Run Code Online (Sandbox Code Playgroud)
符号化:
[foxsportswisconsin, ning, com, profiles, blogs, simvastatin, mg, pas, cher, sur, internet, acheter, du, simvastatin]
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用朴素贝叶斯文本分类器进行文本分类.我的数据采用以下格式,根据问题和摘录,我必须决定问题的主题.培训数据有超过20K的记录.我知道SVM会是一个更好的选择,但我想使用sklearn库与Naive Bayes一起使用.
{[{"topic":"electronics","question":"What is the effective differencial effective of this circuit","excerpt":"I'm trying to work out, in general terms, the effective capacitance of this circuit (see diagram: http://i.stack.imgur.com/BS85b.png). \n\nWhat is the effective capacitance of this circuit and will the ...\r\n "},
{"topic":"electronics","question":"Outlet Installation--more wires than my new outlet can use [on hold]","excerpt":"I am replacing a wall outlet with a Cooper Wiring USB outlet (TR7745). The new outlet has 3 wires coming out of it--a black, a white, …Run Code Online (Sandbox Code Playgroud) python machine-learning scikit-learn text-classification naivebayes
scikit-learn ×10
python ×7
k-means ×1
naivebayes ×1
nlp ×1
numpy ×1
python-2.7 ×1
regex ×1
tf-idf ×1
tokenize ×1
weka ×1