自编卡方程序 sklearn里面是拟合优度检验

我这个是列联表独立性检验(针对文本)

import pandas as pd
#X = np.array([[1,0,1,0],[0,0,1,0],[0,1,1,0],[1,1,1,1],[1,0,1,0],[0,0,1,0]])
import numpy as np
import warnings
from numpy import mat
from scipy import special, stats
from scipy.sparse import issparse
from sklearn.preprocessing import LabelBinarizer
from sklearn.utils.extmath import safe_sparse_dot, row_norms
from sklearn.base import BaseEstimator
from sklearn.preprocessing import LabelBinarizer
from sklearn.utils import (as_float_array, check_array, check_X_y, safe_sqr,
                     safe_mask)
from sklearn.utils.extmath import safe_sparse_dot, row_norms
from sklearn.utils.validation import check_is_fitted



def my_chisquare(f_obs, f_exp):
    
    f_obs = np.asarray(f_obs, dtype=np.float64)

    k = len(f_obs)
    # Reuse f_obs for chi-squared statistics
    chisq = f_obs
    chisq -= f_exp
    chisq **= 2
    with np.errstate(invalid="ignore"):
        chisq /= f_exp
    chisq = chisq.sum(axis=0)
    return chisq


def my_chi2(X, y):
    
    X = check_array(X, accept_sparse='csr')
    if np.any((X.data if issparse(X) else X) < 0):
        raise ValueError("Input X must be non-negative.")


    Y = LabelBinarizer().fit_transform(y)
    if Y.shape[1] == 1:
        Y = np.append(1 - Y, Y, axis=1)


    
    observed1 = safe_sparse_dot(Y.T, X)          # n_classes * n_features
    
    feature_count = X.sum(axis=0).reshape(1, -1)
    class_prob = Y.mean(axis=0).reshape(1, -1)
    expected1 = np.dot(class_prob.T, feature_count)
    feature_count2 = (X.shape[0]-feature_count).reshape(1,-1)   # X.shape[0]代表len(X)
    expected2 = np.dot(class_prob.T, feature_count2)
    expected = np.concatenate([expected1,expected2],axis=0)
    
    y_num0 = (Y.sum(axis = 0).reshape(1,-1))[0,0]       # 两个类别的总数
    y_num1 = (Y.sum(axis = 0).reshape(1,-1))[0,1] 
    C = y_num0-observed1[0].reshape(1,-1)
    D = y_num1-observed1[1].reshape(1,-1)
    observed2 = np.concatenate([observed1,C,D],axis=0)
    
    
    return my_chisquare(observed2, expected)

my_chi2([[1,0,1,0],[0,0,1,0],[0,1,1,0],[1,1,1,1],[1,0,1,0],[0,0,1,0]],['yes','yes','no','yes','no','no'])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值