Scipy/Numpy/scikits - calculating precision/recall scores based on two arrays

2024/10/18 14:45:51
  • I fit a Logistic Regression Model and train the model based on training dataset using the following
import scikits as sklearn
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(C=0.1, penalty='l1')
model = lr.fit(training[:,0:-1], training[:,-1)
  • I have a cross validation dataset which contains a labels associated in input matrix and can be accessed as

cv[:,-1]

  • I run my cross validation dataset against the trained model which returns me the list of 0s and 1s based on prediction

cv_predict = model.predict(cv[:,0:-1])

Question

I want to calculate the precision and recall scores based on acutal labels and predicted labels. Is there a standard method to do it using numpy/scipy/scikits?

Thank you

Answer

Yes there are, see the documentation: http://scikit-learn.org/stable/modules/classes.html#classification-metrics

You should also have a look at the sklearn.metrics.classification_report utility:

>>> from sklearn.metrics import classification_report
>>> from sklearn.linear_model import SGDClassifier
>>> from sklearn.datasets import load_digits>>> digits = load_digits()
>>> n_samples, n_features = digits.data.shape
>>> n_split = n_samples / 2>>> clf = SGDClassifier().fit(digits.data[:n_split], digits.target[:n_split])>>> predictions = clf.predict(digits.data[n_split:])
>>> expected = digits.target[n_split:]>>> print classification_report(expected, predictions)precision    recall  f1-score   support0       0.90      0.98      0.93        881       0.81      0.69      0.75        912       0.94      0.98      0.96        863       0.94      0.85      0.89        914       0.90      0.93      0.91        925       0.92      0.92      0.92        916       0.92      0.97      0.94        917       1.00      0.85      0.92        898       0.71      0.89      0.79        889       0.89      0.83      0.86        92avg / total       0.89      0.89      0.89       899
https://en.xdnf.cn/q/73011.html

Related Q&A

Cant create test client during unit test of Flask app

I am trying to find out how to run a test on a function which grabs a variable value from session[user_id]. This is the specific test method:def test_myProfile_page(self):with app.test_client() as c:w…

My python installation is broken/corrupted. How do I fix it?

I followed these instructions on my RedHat Linux version 7 server (which originally just had Python 2.6.x installed):beginning of instructions install build toolssudo yum install make automake gcc gcc-…

Function that returns a tuple gives TypeError: NoneType object is not iterable

What does this error mean? Im trying to make a function that returns a tuple. Im sure im doing all wrong. Any help is appreciated.from random import randint A = randint(1,3) B = randint(1,3) def make_…

Error when plotting DataFrame containing NaN with Pandas 0.12.0 and Matplotlib 1.3.1 on Python 3.3.2

First of all, this question is not the same as this one.The problem Im having is that when I try to plot a DataFrame which contains a numpy NaN in one cell, I get an error:C:\>\Python33x86\python.ex…

Java method which can provide the same output as Python method for HMAC-SHA256 in Hex

I am now trying to encode the string using HMAC-SHA256 using Java. The encoded string required to match another set of encoded string generated by Python using hmac.new(mySecret, myPolicy, hashlib.sha2…

How to get response from scrapy.Request without callback?

I want to send a request and wait for a response from the server in order to perform action-dependent actions. I write the followingresp = yield scrapy.Request(*kwargs)and got None in resp. In document…

install error thinks pythonpath is empty

I am trying to install the scikits.nufft package here I download the zip file, unpack and cd to the directory. It contains a setup.py file so I run python setup.py installbut it gives me the following …

Conditionally installing importlib on python2.6

I have a python library that has a dependency on importlib. importlib is in the standard library in Python 2.7, but is a third-party package for older pythons. I typically keep my dependencies in a pip…

Python/pandas: Find matching values from two dataframes and return third value

I have two different dataframes (df1, df2) with completely different shapes: df1: (64, 6); df2: (564, 9). df1 contains a column (df1.objectdesc) which has values (strings) that can also be found in a c…

random.choice broken with dicts

The random.choice input is supposed to be a sequence. This causes odd behavior with a dict, which is not a sequence type but can be subscripted like one: >>> d = {0: spam, 1: eggs, 3: potato} …