NLP-Pearson相关系数计算公式及代码

随便记录一下。。。

计算公式

Pearson=ni=1xiyini=1xini=1yinni=1xi2(2i=1xi)2nni=1yi2(ni1yi)2n P e a r s o n = ∑ i = 1 n x i y i − ∑ i = 1 n x i ∑ i = 1 n y i n ∑ i = 1 n x i 2 − ( ∑ i = 1 2 x i ) 2 n ∑ i = 1 n y i 2 − ( ∑ i − 1 n y i ) 2 n

代码

 with tf.name_scope("pearson"):
            mid1 = tf.reduce_mean(self.score * self.input_y) - \
                   tf.reduce_mean(self.score) * tf.reduce_mean(self.input_y)
            mid2 = tf.sqrt(tf.reduce_mean(tf.square(self.score)) - tf.square(tf.reduce_mean(self.score))) * \
                   tf.sqrt(tf.reduce_mean(tf.square(self.input_y)) - tf.square(tf.reduce_mean(self.input_y)))
            self.pearson = mid1 / mid2

你可能感兴趣的:(Python,NLP之路)