當前位置: 首頁>>代碼示例>>Python>>正文


Python classification.accuracy_score方法代碼示例

本文整理匯總了Python中sklearn.metrics.classification.accuracy_score方法的典型用法代碼示例。如果您正苦於以下問題:Python classification.accuracy_score方法的具體用法?Python classification.accuracy_score怎麽用?Python classification.accuracy_score使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sklearn.metrics.classification的用法示例。


在下文中一共展示了classification.accuracy_score方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: predict

# 需要導入模塊: from sklearn.metrics import classification [as 別名]
# 或者: from sklearn.metrics.classification import accuracy_score [as 別名]
def predict(self, model, X, y):
        Y_pred = model.predict_proba_dict(X) 
        df = pd.DataFrame(Y_pred).values
        print('Accuracy: ', accuracy_score(y, np.argmax(df, axis=1)))
        return df 
開發者ID:doncat99,項目名稱:StockRecommendSystem,代碼行數:7,代碼來源:Stock_Prediction_Model_DBN.py

示例2: predict

# 需要導入模塊: from sklearn.metrics import classification [as 別名]
# 或者: from sklearn.metrics.classification import accuracy_score [as 別名]
def predict(self, model, X, y, ContinuousColumnName, CategoricalColumnName):
        predictions = np.array(list(model.predict_proba(input_fn=lambda: self.input_fn(X, y, ContinuousColumnName, CategoricalColumnName))))
        
        results = model.evaluate(input_fn=lambda: self.input_fn(X, y, ContinuousColumnName, CategoricalColumnName), steps=1)

        for key in sorted(results):
            print("%s: %s"%(key, results[key]))
        print('Accuracy: ', accuracy_score(y, tf.argmax(predictions, axis=1)))
        return predictions 
開發者ID:doncat99,項目名稱:StockRecommendSystem,代碼行數:11,代碼來源:Stock_Prediction_Recommand_System.py

示例3: predict

# 需要導入模塊: from sklearn.metrics import classification [as 別名]
# 或者: from sklearn.metrics.classification import accuracy_score [as 別名]
def predict(self, model, X, y):
        predictions = model.predict_proba(X)
        if np.isfinite(y).all():
            print('Accuracy: ', accuracy_score(y, np.argmax(predictions, axis=1)))
        return predictions 
開發者ID:doncat99,項目名稱:StockRecommendSystem,代碼行數:7,代碼來源:Stock_Prediction_Model_Random_Forrest.py

示例4: prediction_score

# 需要導入模塊: from sklearn.metrics import classification [as 別名]
# 或者: from sklearn.metrics.classification import accuracy_score [as 別名]
def prediction_score(train_X, train_y, test_X, test_y, metric, model):
    # if the train labels are always the same
    values_train = set(train_y)
    if len(values_train) == 1:
        # predict always that value
        only_value_train = list(values_train)[0]
        test_pred = np.ones_like(test_y) * only_value_train

    # if the train labels have different values
    else:
        # create the model
        if model == "random_forest_classifier":
            m = RandomForestClassifier(n_estimators=10)
        elif model == "logistic_regression":
            m = LogisticRegression()
        else:
            raise Exception("Invalid model name.")

        # fit and predict
        m.fit(train_X, train_y)
        test_pred = m.predict(test_X)

    # calculate the score
    if metric == "f1":
        return f1_score(test_y, test_pred)
    elif metric == "accuracy":
        return accuracy_score(test_y, test_pred)
    else:
        raise Exception("Invalid metric name.") 
開發者ID:rcamino,項目名稱:multi-categorical-gans,代碼行數:31,代碼來源:prediction.py

示例5: train_and_eval

# 需要導入模塊: from sklearn.metrics import classification [as 別名]
# 或者: from sklearn.metrics.classification import accuracy_score [as 別名]
def train_and_eval(ngram_range=(1, 1), max_features=None, max_df=1.0, C=1.0):
    """Train and eval newsgroup classification.

    :param ngram_range: ngram range
    :param max_features: the number of maximum features
    :param max_df: max document frequency ratio
    :param C: Inverse of regularization strength for LogisticRegression
    :return: metrics
    """
    # Loads train and test data.
    train_data = fetch_20newsgroups(subset='train')
    test_data = fetch_20newsgroups(subset='test')

    # Define the pipeline.
    pipeline = Pipeline([
        ('tfidf', TfidfVectorizer()),
        ('clf', LogisticRegression(multi_class='auto'))
    ])

    # Set pipeline parameters.
    params = {
        'tfidf__ngram_range': ngram_range,
        'tfidf__max_features': max_features,
        'tfidf__max_df': max_df,
        'clf__C': C,
    }
    pipeline.set_params(**params)
    print(pipeline.get_params().keys())

    # Train the model.
    pipeline.fit(train_data.data, train_data.target)
    # Predict test data.
    start_time = time()
    predictions = pipeline.predict(test_data.data)
    inference_time = time() - start_time
    avg_inference_time = 1.0 * inference_time / len(test_data.target)
    print("Avg. inference time: {}".format(avg_inference_time))

    # Calculate the metrics.
    accuracy = accuracy_score(test_data.target, predictions)
    recall = recall_score(test_data.target, predictions, average='weighted')
    f1 = f1_score(test_data.target, predictions, average='weighted')
    metrics = {
        'accuracy': accuracy,
        'recall': recall,
        'f1': f1,
    }

    return metrics 
開發者ID:polyaxon,項目名稱:polyaxon-examples,代碼行數:51,代碼來源:model.py

示例6: train_and_eval

# 需要導入模塊: from sklearn.metrics import classification [as 別名]
# 或者: from sklearn.metrics.classification import accuracy_score [as 別名]
def train_and_eval(output, ngram_range=(1, 1), max_features=None, max_df=1.0, C=1.0):
    """Train and eval newsgroup classification.

    :param ngram_range: ngram range
    :param max_features: the number of maximum features
    :param max_df: max document frequency ratio
    :param C: Inverse of regularization strength for LogisticRegression
    :return: metrics
    """
    # Loads train and test data.
    train_data = fetch_20newsgroups(subset='train')
    test_data = fetch_20newsgroups(subset='test')

    # Define the pipeline.
    pipeline = Pipeline([
        ('tfidf', TfidfVectorizer()),
        ('clf', LogisticRegression(multi_class='auto'))
    ])

    # Set pipeline parameters.
    params = {
        'tfidf__ngram_range': ngram_range,
        'tfidf__max_features': max_features,
        'tfidf__max_df': max_df,
        'clf__C': C,
    }
    pipeline.set_params(**params)
    print(pipeline.get_params().keys())

    # Train the model.
    pipeline.fit(train_data.data, train_data.target)
    # Predict test data.
    start_time = time()
    predictions = pipeline.predict(test_data.data)
    inference_time = time() - start_time
    avg_inference_time = 1.0 * inference_time / len(test_data.target)
    print("Avg. inference time: {}".format(avg_inference_time))

    # Calculate the metrics.
    accuracy = accuracy_score(test_data.target, predictions)
    recall = recall_score(test_data.target, predictions, average='weighted')
    f1 = f1_score(test_data.target, predictions, average='weighted')
    metrics = {
        'accuracy': accuracy,
        'recall': recall,
        'f1': f1,
    }

    # Persistent the model.
    joblib.dump(pipeline, output)

    return metrics 
開發者ID:polyaxon,項目名稱:polyaxon-examples,代碼行數:54,代碼來源:model.py


注:本文中的sklearn.metrics.classification.accuracy_score方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。