当前位置: 首页>>代码示例>>Python>>正文


Python OneVsOneClassifier.to_csv方法代码示例

本文整理汇总了Python中sklearn.multiclass.OneVsOneClassifier.to_csv方法的典型用法代码示例。如果您正苦于以下问题:Python OneVsOneClassifier.to_csv方法的具体用法?Python OneVsOneClassifier.to_csv怎么用?Python OneVsOneClassifier.to_csv使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.multiclass.OneVsOneClassifier的用法示例。


在下文中一共展示了OneVsOneClassifier.to_csv方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: CountVectorizer

# 需要导入模块: from sklearn.multiclass import OneVsOneClassifier [as 别名]
# 或者: from sklearn.multiclass.OneVsOneClassifier import to_csv [as 别名]
from sklearn import cross_validation
from sklearn.ensemble import RandomForestClassifier
import numpy as np

##With Bag of words and One-vs-all and One-vs-One

train=pd.read_csv("train.csv")
test=pd.read_csv("test.csv")
vectorizer = CountVectorizer(analyzer = "word",max_features=5000)
train_data_features = vectorizer.fit_transform(train["Summary"])
test_data_features=vectorizer.transform(test["Summary"])

resultWithOneVsRest = OneVsRestClassifier(LinearSVC(random_state=0)).fit(train_data_features, train["Topic"]).predict(test_data_features)
resultWithOneVsOne= OneVsOneClassifier(LinearSVC(random_state=0)).fit(train_data_features, train["Topic"]).predict(test_data_features)

outputWithOneVsRest = pd.DataFrame( data={"record_id":test["RecordID"], "topic_id":resultWithOneVsRest} )
outputWithOneVsRest.to_csv( "Task1_outputWithOneVsRest.tsv", index=False, quoting=3 , sep="\t")

resultWithOneVsOne = pd.DataFrame( data={"record_id":test["RecordID"], "topic_id":resultWithOneVsOne} )
resultWithOneVsOne.to_csv( "Task1_resultWithOneVsOne.tsv", index=False, quoting=3, sep="\t")

##with TF-idf with One-vs-all and One-vs-One
tf_idf=TfidfVectorizer(max_features=5000)
train_data_features = tf_idf.fit_transform(train["Summary"])
test_data_features=tf_idf.transform(test["Summary"])
resultWithOneVsRestModel = OneVsRestClassifier(LinearSVC(random_state=0)).fit(train_data_features, train["Topic"])
print np.mean(cross_validation.cross_val_score(resultWithOneVsRestModel,train_data_features,train["Topic"],cv=20))
resultWithOneVsRest_tfidf = resultWithOneVsRestModel.predict(test_data_features)

op=pd.DataFrame(data={"record_id":test["RecordID"],"topic_id":resultWithOneVsRest_tfidf})
op.to_csv("Task-1_With_TfIdf.tsv",index=False,quoting=3,sep="\t");
开发者ID:nive20,项目名称:BingHackathon,代码行数:33,代码来源:Task-1.py


注:本文中的sklearn.multiclass.OneVsOneClassifier.to_csv方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。