Skip to main content

ScoreCardModel

Desc

a simple tool for score card model

keywords:math,finance

Feature

  • Serializable

  • mutil classifier model support

  • ks-curve support

Change

  • scorecard now can set a threshold value to return a bool result

Example

>>> from sklearn import datasets
>>> import pandas as pd
>>> from ScoreCardModel.binning.discretization import Discretization
>>> from ScoreCardModel.weight_of_evidence import WeightOfEvidence
>>> from ScoreCardModel.models.logistic_regression_model import LogisticRegressionModel
>>> from ScoreCardModel.score_card import ScoreCardModel
>>>
>>> class MyLR(LogisticRegressionModel):
>>>     def predict(self, x):
>>>          x = self.pre_trade(x)
>>>          return self._predict_proba(x)
>>>
>>>     def pre_trade(self, x):
>>>         import numpy as np
>>>         result = []
>>>         for i,v in x.items():
>>>             t = self.ds[i].transform([v])[0]
>>>             r = self.woes[i].transform([t])[0]
>>>             result.append(r)
>>>         return np.array(result)
>>>
>>>     def _pre_trade_batch_row(self,row,Y,bins):
>>>         d = Discretization(bins)
>>>         d_row = d.transform(row)
>>>         woe = WeightOfEvidence()
>>>         woe.fit(d_row,Y)
>>>         return d,woe,woe.transform(d_row)
>>>
>>>     def pre_trade_batch(self, X,Y):
>>>         self.ds = {}
>>>         self.woes = {}
>>>         self.table = {}
>>>         self.ds["sepal length (cm)"],self.woes["sepal length (cm)"],self.table["sepal length (cm)"]= self._pre_trade_batch_row(
>>>             X["sepal length (cm)"],Y,[0,2,5,8])
>>>         self.ds['sepal width (cm)'],self.woes['sepal width (cm)'],self.table['sepal width (cm)'] = self._pre_trade_batch_row(
>>>             X['sepal width (cm)'],Y,[0,2,2.5,3,3.5,5])
>>>         self.ds['petal length (cm)'],self.woes['petal length (cm)'],self.table['petal length (cm)'] = self._pre_trade_batch_row(
>>>             X['petal length (cm)'],Y,[0,1,2,3,4,5,7])
>>>         self.ds['petal width (cm)'],self.woes['petal width (cm)'],self.table['petal width (cm)'] = self._pre_trade_batch_row(
>>>             X['petal width (cm)'],Y,[0,1,2,3])
>>>         return pd.DataFrame(self.table)
>>>
>>> iris = datasets.load_iris()
>>> y = iris.target
>>> z = (y==0)
>>> l = pd.DataFrame(iris.data,columns=iris.feature_names)
>>> lr = MyLR()
>>> lr.train(l,z)
>>> lr.predict(l.loc[0].to_dict())
array([[ 0.46315882,  0.53684118]])
>>> sc = ScoreCardModel(lr)
>>> sc.predict(sc.pre_trade(l.loc[0].to_dict()))
104.3
>>> scs = []
>>> for i in range(len(l)):
>>>    score = sc.predict(sc.pre_trade(l.loc[i].to_dict()))
>>>    scs.append(score)
>>> print(ScoreCardWithKSModel.Threshold_to_score(scs, 0.5))
1.0
>>> print(ScoreCardWithKSModel.Score_to_threshold(scs, score=70))
1.0
.
.
.
>>> print(ScoreCardWithKSModel.Score_to_threshold(scs, y=z, score=100))
0.3467
>>> print(ScoreCardWithKSModel.Get_ks(scs, y=z, threshold=0.4).ks)
0.9
>>> # ScoreCardWithKSModel.Drawks(scs, y=z)
>>> scsc = [l.loc[i].to_dict() for i in range(len(l))]
>>> scks = ScoreCardWithKSModel.From_scorecard(sc)
>>> print(scks.threshold_to_score(scsc, 0.5))
1.0
>>> print(scks.score_to_threshold(scsc, score=70))
1.0
.
.
.
>>> print(scks.score_to_threshold(scsc, y=z, score=100))
0.3467
>>> print(scks.get_ks(scsc, y=z, threshold=0.4).ks)
0.9
>>> scks.drawks(scsc, y=z)

Install

  • python -m pip install ScoreCardModel

Documentation

Documentation on Readthedocs.

Metadata

Release files for ScoreCardModel 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ScoreCardModel 1.1.3
File Size Uploaded Provenance Actions
ScoreCardModel-1.1.3.tar.gz 15.1 kB Without provenance Details for ScoreCardModel-1.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ScoreCardModel 1.1.3
File Interpreter ABI Platform Actions
ScoreCardModel-1.1.3-py3-none-any.whl Python 3 none any Details for ScoreCardModel-1.1.3-py3-none-any.whl

Total release size: 40.7 kB

Release files / ScoreCardModel-1.1.3.tar.gz

Download URL ScoreCardModel-1.1.3.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
1a1247b272db1caa344b7e245a266f31282fc3d2aa4958f4121770c599f18421
BLAKE2b-256 checksum
How to use checksums
3e4eb7d0f4a8849b5d6641dcb7cf29a7b2b81455d6837586d4dfdc9a06147b33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / ScoreCardModel-1.1.3-py3-none-any.whl

Download URL ScoreCardModel-1.1.3-py3-none-any.whl
Size 25.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1f254b50b399a29d61a9fbd4dff84e8862b0252a8aa3063b250ffeca23b4363d
BLAKE2b-256 checksum
How to use checksums
611ca97828daa41692f22135448ae5633346c3cde178d100bc1424465034f44e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for ScoreCardModel 1.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page