我正在研究我的学士论文,并正在使用python来分析我的数据。不幸的是,我不是编程专家,也不认识任何正在使用python的人。
我有一个代码,用逗号分隔CSV文件中的列。我希望代码用|。
分隔列我试图将第58行中的逗号替换为|但这不起作用,惊喜。因为我在编程领域是一个菜鸟,谷歌搜索根本没有任何意义。任何帮助都会受到很大的赞赏!
from sklearn.feature_extraction.text import CountVectorizer
from sklearn import linear_model
import csv
import cPickle
from sklearn.metrics import accuracy_score
def main():
train_file = "train.csv"
test_file = "test.csv"
# Read documents
train_docs, Y = read_docs(train_file)
# Define which features to extract (character bigrams in this case)
extract = CountVectorizer(lowercase=False, ngram_range=(2,2),
analyzer="char")
extract.fit(train_docs) # create vocabulary from training data
# Extract features from train data
X = extract.transform(train_docs)
# Initialize model
model = linear_model.LogisticRegression()
# Train model
model.fit(X, Y)
# Write model to file so it can be reused
cPickle.dump((extract,model),open("model.pickle","w"))
# Print coefficients to see which features are important
for i,f in enumerate(extract.get_feature_names()):
print f, model.coef_[0][i]
# Testing
# Read test data
test_docs, Y_test = read_docs(test_file)
# Extract features from test data
X_test = extract.transform(test_docs)
# Apply model to test data
Y_predict = model.predict(X_test)
# Evaluation
print accuracy_score(Y_test, Y_predict)
def read_docs(filename):
'''
Return X,Y where X is the list of documents and Y the list of their
labels.
'''
X = []
Y = []
with open(filename) as f:
r = csv.reader(f)
for row in r:
text,label = row
X.append(text)
Y.append(int(label))
return X,Y
main()
此刻我得到了这个:
csv.register_dialect('pipes', delimiter='|')
with open(filename) as f:
r = csv.reader(f, dialect ='pipes')
for row in r:
text,label = row
X.append(text)
Y.append(int(label))
return X,Y
但我现在不断收到错误:
Traceback (most recent call last):
File "D:/python/logreggwen.py", line 67, in <module>
main()
File "D:/python/logreggwen.py", line 11, in main
train_docs, Y = read_docs(train_file)
File "D:/python/logreggwen.py", line 61, in read_docs
text,label = row
ValueError: need more than 1 value to unpack
答案 0 :(得分:1)
您需要告诉CSV阅读器您的数据文件使用了哪些分隔符:
csv.reader(f, delimiter='|')
但实际上,您需要阅读相应的文档: