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


Python FeatureExtractor.extract_full_feature_matrix方法代码示例

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


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

示例1: __init__

# 需要导入模块: from feature_extractor import FeatureExtractor [as 别名]
# 或者: from feature_extractor.FeatureExtractor import extract_full_feature_matrix [as 别名]
class RelationshipPostClassifier:
    """
    Main class for classification and prediction
    """

    def __init__(self, classifier_type="tree"):
        self.data = None
        self.classifier_type = classifier_type
        self.classifier = None
        self.featureExtractor = None
        self.predictions = None

    def read_csv_data(self, csv_file, maxrows=None):
        """
        Read in data from given csv_file into self.data (pandas dataframe)
        maxrows limits number of read rows.
        :param csv_file:
        :param maxrows:
        :return: None
        """
        sys.stderr.write("Reading in data from " + csv_file + "\n")

        if maxrows:
            self.data = pan.read_csv(csv_file, nrows=maxrows, encoding='utf-8')
        else:
            self.data = pan.read_csv(csv_file, encoding='utf-8')

    def train_model(self, model_out_file):
        """
        Extract the features from self.data and train the classifier. Output pickled model to model_out_file
        :param model_out_file:
        :return: None
        """
        if self.data is None:
            raise Exception("Trying to train model without any data.")

        sys.stderr.write("Extracting features from data.\n")

        self.featureExtractor = FeatureExtractor(self.data)
        feature_matrix = self.featureExtractor.extract_full_feature_matrix()

        labels = np.array([0 if lab == "Romantic" else 1 for lab in self.data["is_romantic"]])

        sys.stderr.write("Training classifier.\n")

        self.classifier = LogisticRegression() if self.classifier_type == "logit" else DecisionTreeClassifier()
        self.classifier.fit(feature_matrix, labels)

        sys.stderr.write("Saving classifier.\n")

        with open(model_out_file, "w") as f:
            pickle.dump(self.classifier, f)

    def predict_model(self, model_file=None, output_file=None, output_probability_file=None):
        """
        Predict classes on self.data and output to output_file
        :param model_file: Model file to read model in from. Otherwise looks for self.classifier
        :param output_file: File to save predictions in
        :param output_probability_file: File to save predicted probabilities in
        :return: predicted classes (array)
        """
        if not self.classifier:
            if not model_file:
                raise Exception("No model to predict with.")
            else:
                with open(model_file) as f:
                    self.classifier = pickle.load(f)

        if self.data is None:
            raise Exception("Trying to predict using model with no data loaded.")

        self.featureExtractor = FeatureExtractor(self.data)
        feature_matrix = self.featureExtractor.extract_full_feature_matrix()

        self.predictions = self.classifier.predict(feature_matrix)

        if output_file is not None:
            np.savetxt(output_file, self.predictions, delimiter=",", fmt="%d")

        if output_probability_file is not None:
            pred_probs = self.classifier.predict_proba(feature_matrix)
            np.savetxt(output_probability_file, pred_probs, delimiter=",", fmt="%.3f")

        return self.predictions
开发者ID:linii,项目名称:ling229-final,代码行数:86,代码来源:classify_relationship_posts.py


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