stats.ttest_ind返回NoneType对象,但我可以打印它

时间:2017-08-30 06:52:40

标签: python statistics data-analysis

我对Iris数据集Iris data-set进行了t检验。我计算了萼片和花瓣的长度和宽度之间的比例,并创建了一个名为IrisLeafRatio.csv的新数据集。

import scipy.stats as stats

# First, we'll import pandas, a data processing and CSV file I/O library
import pandas as pd
# We'll also import seaborn, a Python graphing library
import warnings # current version of seaborn generates a bunch of warnings that we'll ignore
warnings.filterwarnings("ignore")
import seaborn as sns
import matplotlib.pyplot as plt
import numpy
sns.set(style="dark", color_codes=True)

iris_ratio = pd.read_csv("IrisLeafRatio.csv") # the iris dataset is now a Pandas DataFrame

iris_setosa_sepal_ratio = numpy.asarray(iris_ratio["SepalRatio"][0:50])
iris_versicolor_sepal_ratio = numpy.asarray(iris_ratio["SepalRatio"][50:100])
iris_virginica_sepal_ratio = numpy.asarray(iris_ratio["SepalRatio"][100:150])

setosa_versicolor_sepal = stats.ttest_ind(iris_setosa_sepal_ratio, iris_versicolor_sepal_ratio)
setosa_virginica_sepal = stats.ttest_ind(iris_setosa_sepal_ratio, iris_virginica_sepal_ratio)
versicolor_virginica_sepal = stats.ttest_ind(iris_versicolor_sepal_ratio, iris_virginica_sepal_ratio)

print ("______kelkbladeren________")
print ("Setosa : Versicolor")
print (setosa_versicolor_sepal)
print ("Setosa : Virginica")
print (setosa_virginica_sepal)
print ("Versicolor : Virginica")
print (versicolor_virginica_sepal)

iris_setosa_petal_ratio = numpy.asarray(iris_ratio["PetalRatio"][0:50])
iris_versicolor_petal_ratio = numpy.asarray(iris_ratio["PetalRatio"][50:100])
iris_virginica_petal_ratio = numpy.asarray(iris_ratio["PetalRatio"][100:150])

setosa_versicolor_petal = stats.ttest_ind(iris_setosa_petal_ratio, iris_versicolor_petal_ratio)
setosa_virginica_petal = stats.ttest_ind(iris_setosa_petal_ratio, iris_virginica_petal_ratio)
versicolor_virginica_petal = stats.ttest_ind(iris_versicolor_petal_ratio, iris_virginica_petal_ratio)

print ("\n_____bloembladeren_________")
print ("Setosa : Versicolor")
print (setosa_versicolor_petal) + "Setosa : Versicolor"
print ("Setosa : Virginica")
print (setosa_virginica_petal)
print ("Versicolor : Virginica")
print (versicolor_virginica_petal)

# Returns:  
# statistic : float or array
# The calculated t-statistic.
# pvalue : float or array
# The two-tailed p-value.

问题是当我想将stats.ttest_ind()函数的结果与字符串一起打印时,我收到以下错误:

unsupported operand type(s) for +: 'NoneType' and 'str'

看起来stats.ttest_ind()返回一个NoneType对象但是如果我不尝试连接字符串,为什么我能够打印它? 其次,因为它是一个NoneType,我无法将p值提取到一个单独的变量中。

我制作的完整数据集是:

Id,SepalLengthCm,SepalWidthCm,SepalRatio, PetalLengthCm,PetalWidthCm,PetalRatio,Species
1,5.1,3.5, 1.46, 1.4,0.2, 7.0, Iris-setosa 
2,4.9,3.0, 1.63, 1.4,0.2, 7.0, Iris-setosa 
3,4.7,3.2, 1.47, 1.3,0.2, 6.5, Iris-setosa 
4,4.6,3.1, 1.48, 1.5,0.2, 7.5, Iris-setosa 
5,5.0,3.6, 1.39, 1.4,0.2, 7.0, Iris-setosa 
6,5.4,3.9, 1.38, 1.7,0.4, 4.25, Iris-setosa 
7,4.6,3.4, 1.35, 1.4,0.3, 4.67, Iris-setosa 
8,5.0,3.4, 1.47, 1.5,0.2, 7.5, Iris-setosa 
9,4.4,2.9, 1.52, 1.4,0.2, 7.0, Iris-setosa 
10,4.9,3.1, 1.58, 1.5,0.1, 15.0, Iris-setosa 
11,5.4,3.7, 1.46, 1.5,0.2, 7.5, Iris-setosa 
12,4.8,3.4, 1.41, 1.6,0.2, 8.0, Iris-setosa 
13,4.8,3.0, 1.6, 1.4,0.1, 14.0, Iris-setosa 
14,4.3,3.0, 1.43, 1.1,0.1, 11.0, Iris-setosa 
15,5.8,4.0, 1.45, 1.2,0.2, 6.0, Iris-setosa 
16,5.7,4.4, 1.3, 1.5,0.4, 3.75, Iris-setosa 
17,5.4,3.9, 1.38, 1.3,0.4, 3.25, Iris-setosa 
18,5.1,3.5, 1.46, 1.4,0.3, 4.67, Iris-setosa 
19,5.7,3.8, 1.5, 1.7,0.3, 5.67, Iris-setosa 
20,5.1,3.8, 1.34, 1.5,0.3, 5.0, Iris-setosa 
21,5.4,3.4, 1.59, 1.7,0.2, 8.5, Iris-setosa 
22,5.1,3.7, 1.38, 1.5,0.4, 3.75, Iris-setosa 
23,4.6,3.6, 1.28, 1.0,0.2, 5.0, Iris-setosa 
24,5.1,3.3, 1.55, 1.7,0.5, 3.4, Iris-setosa 
25,4.8,3.4, 1.41, 1.9,0.2, 9.5, Iris-setosa 
26,5.0,3.0, 1.67, 1.6,0.2, 8.0, Iris-setosa 
27,5.0,3.4, 1.47, 1.6,0.4, 4.0, Iris-setosa 
28,5.2,3.5, 1.49, 1.5,0.2, 7.5, Iris-setosa 
29,5.2,3.4, 1.53, 1.4,0.2, 7.0, Iris-setosa 
30,4.7,3.2, 1.47, 1.6,0.2, 8.0, Iris-setosa 
31,4.8,3.1, 1.55, 1.6,0.2, 8.0, Iris-setosa 
32,5.4,3.4, 1.59, 1.5,0.4, 3.75, Iris-setosa 
33,5.2,4.1, 1.27, 1.5,0.1, 15.0, Iris-setosa 
34,5.5,4.2, 1.31, 1.4,0.2, 7.0, Iris-setosa 
35,4.9,3.1, 1.58, 1.5,0.1, 15.0, Iris-setosa 
36,5.0,3.2, 1.56, 1.2,0.2, 6.0, Iris-setosa 
37,5.5,3.5, 1.57, 1.3,0.2, 6.5, Iris-setosa 
38,4.9,3.1, 1.58, 1.5,0.1, 15.0, Iris-setosa 
39,4.4,3.0, 1.47, 1.3,0.2, 6.5, Iris-setosa 
40,5.1,3.4, 1.5, 1.5,0.2, 7.5, Iris-setosa 
41,5.0,3.5, 1.43, 1.3,0.3, 4.33, Iris-setosa 
42,4.5,2.3, 1.96, 1.3,0.3, 4.33, Iris-setosa 
43,4.4,3.2, 1.38, 1.3,0.2, 6.5, Iris-setosa 
44,5.0,3.5, 1.43, 1.6,0.6, 2.67, Iris-setosa 
45,5.1,3.8, 1.34, 1.9,0.4, 4.75, Iris-setosa 
46,4.8,3.0, 1.6, 1.4,0.3, 4.67, Iris-setosa 
47,5.1,3.8, 1.34, 1.6,0.2, 8.0, Iris-setosa 
48,4.6,3.2, 1.44, 1.4,0.2, 7.0, Iris-setosa 
49,5.3,3.7, 1.43, 1.5,0.2, 7.5, Iris-setosa 
50,5.0,3.3, 1.52, 1.4,0.2, 7.0, Iris-setosa 
51,7.0,3.2, 2.19, 4.7,1.4, 3.36, Iris-versicolor 
52,6.4,3.2, 2.0, 4.5,1.5, 3.0, Iris-versicolor 
53,6.9,3.1, 2.23, 4.9,1.5, 3.27, Iris-versicolor 
54,5.5,2.3, 2.39, 4.0,1.3, 3.08, Iris-versicolor 
55,6.5,2.8, 2.32, 4.6,1.5, 3.07, Iris-versicolor 
56,5.7,2.8, 2.04, 4.5,1.3, 3.46, Iris-versicolor 
57,6.3,3.3, 1.91, 4.7,1.6, 2.94, Iris-versicolor 
58,4.9,2.4, 2.04, 3.3,1.0, 3.3, Iris-versicolor 
59,6.6,2.9, 2.28, 4.6,1.3, 3.54, Iris-versicolor 
60,5.2,2.7, 1.93, 3.9,1.4, 2.79, Iris-versicolor 
61,5.0,2.0, 2.5, 3.5,1.0, 3.5, Iris-versicolor 
62,5.9,3.0, 1.97, 4.2,1.5, 2.8, Iris-versicolor 
63,6.0,2.2, 2.73, 4.0,1.0, 4.0, Iris-versicolor 
64,6.1,2.9, 2.1, 4.7,1.4, 3.36, Iris-versicolor 
65,5.6,2.9, 1.93, 3.6,1.3, 2.77, Iris-versicolor 
66,6.7,3.1, 2.16, 4.4,1.4, 3.14, Iris-versicolor 
67,5.6,3.0, 1.87, 4.5,1.5, 3.0, Iris-versicolor 
68,5.8,2.7, 2.15, 4.1,1.0, 4.1, Iris-versicolor 
69,6.2,2.2, 2.82, 4.5,1.5, 3.0, Iris-versicolor 
70,5.6,2.5, 2.24, 3.9,1.1, 3.55, Iris-versicolor 
71,5.9,3.2, 1.84, 4.8,1.8, 2.67, Iris-versicolor 
72,6.1,2.8, 2.18, 4.0,1.3, 3.08, Iris-versicolor 
73,6.3,2.5, 2.52, 4.9,1.5, 3.27, Iris-versicolor 
74,6.1,2.8, 2.18, 4.7,1.2, 3.92, Iris-versicolor 
75,6.4,2.9, 2.21, 4.3,1.3, 3.31, Iris-versicolor 
76,6.6,3.0, 2.2, 4.4,1.4, 3.14, Iris-versicolor 
77,6.8,2.8, 2.43, 4.8,1.4, 3.43, Iris-versicolor 
78,6.7,3.0, 2.23, 5.0,1.7, 2.94, Iris-versicolor 
79,6.0,2.9, 2.07, 4.5,1.5, 3.0, Iris-versicolor 
80,5.7,2.6, 2.19, 3.5,1.0, 3.5, Iris-versicolor 
81,5.5,2.4, 2.29, 3.8,1.1, 3.45, Iris-versicolor 
82,5.5,2.4, 2.29, 3.7,1.0, 3.7, Iris-versicolor 
83,5.8,2.7, 2.15, 3.9,1.2, 3.25, Iris-versicolor 
84,6.0,2.7, 2.22, 5.1,1.6, 3.19, Iris-versicolor 
85,5.4,3.0, 1.8, 4.5,1.5, 3.0, Iris-versicolor 
86,6.0,3.4, 1.76, 4.5,1.6, 2.81, Iris-versicolor 
87,6.7,3.1, 2.16, 4.7,1.5, 3.13, Iris-versicolor 
88,6.3,2.3, 2.74, 4.4,1.3, 3.38, Iris-versicolor 
89,5.6,3.0, 1.87, 4.1,1.3, 3.15, Iris-versicolor 
90,5.5,2.5, 2.2, 4.0,1.3, 3.08, Iris-versicolor 
91,5.5,2.6, 2.12, 4.4,1.2, 3.67, Iris-versicolor 
92,6.1,3.0, 2.03, 4.6,1.4, 3.29, Iris-versicolor 
93,5.8,2.6, 2.23, 4.0,1.2, 3.33, Iris-versicolor 
94,5.0,2.3, 2.17, 3.3,1.0, 3.3, Iris-versicolor 
95,5.6,2.7, 2.07, 4.2,1.3, 3.23, Iris-versicolor 
96,5.7,3.0, 1.9, 4.2,1.2, 3.5, Iris-versicolor 
97,5.7,2.9, 1.97, 4.2,1.3, 3.23, Iris-versicolor 
98,6.2,2.9, 2.14, 4.3,1.3, 3.31, Iris-versicolor 
99,5.1,2.5, 2.04, 3.0,1.1, 2.73, Iris-versicolor 
100,5.7,2.8, 2.04, 4.1,1.3, 3.15, Iris-versicolor 
101,6.3,3.3, 1.91, 6.0,2.5, 2.4, Iris-virginica 
102,5.8,2.7, 2.15, 5.1,1.9, 2.68, Iris-virginica 
103,7.1,3.0, 2.37, 5.9,2.1, 2.81, Iris-virginica 
104,6.3,2.9, 2.17, 5.6,1.8, 3.11, Iris-virginica 
105,6.5,3.0, 2.17, 5.8,2.2, 2.64, Iris-virginica 
106,7.6,3.0, 2.53, 6.6,2.1, 3.14, Iris-virginica 
107,4.9,2.5, 1.96, 4.5,1.7, 2.65, Iris-virginica 
108,7.3,2.9, 2.52, 6.3,1.8, 3.5, Iris-virginica 
109,6.7,2.5, 2.68, 5.8,1.8, 3.22, Iris-virginica 
110,7.2,3.6, 2.0, 6.1,2.5, 2.44, Iris-virginica 
111,6.5,3.2, 2.03, 5.1,2.0, 2.55, Iris-virginica 
112,6.4,2.7, 2.37, 5.3,1.9, 2.79, Iris-virginica 
113,6.8,3.0, 2.27, 5.5,2.1, 2.62, Iris-virginica 
114,5.7,2.5, 2.28, 5.0,2.0, 2.5, Iris-virginica 
115,5.8,2.8, 2.07, 5.1,2.4, 2.12, Iris-virginica 
116,6.4,3.2, 2.0, 5.3,2.3, 2.3, Iris-virginica 
117,6.5,3.0, 2.17, 5.5,1.8, 3.06, Iris-virginica 
118,7.7,3.8, 2.03, 6.7,2.2, 3.05, Iris-virginica 
119,7.7,2.6, 2.96, 6.9,2.3, 3.0, Iris-virginica 
120,6.0,2.2, 2.73, 5.0,1.5, 3.33, Iris-virginica 
121,6.9,3.2, 2.16, 5.7,2.3, 2.48, Iris-virginica 
122,5.6,2.8, 2.0, 4.9,2.0, 2.45, Iris-virginica 
123,7.7,2.8, 2.75, 6.7,2.0, 3.35, Iris-virginica 
124,6.3,2.7, 2.33, 4.9,1.8, 2.72, Iris-virginica 
125,6.7,3.3, 2.03, 5.7,2.1, 2.71, Iris-virginica 
126,7.2,3.2, 2.25, 6.0,1.8, 3.33, Iris-virginica 
127,6.2,2.8, 2.21, 4.8,1.8, 2.67, Iris-virginica 
128,6.1,3.0, 2.03, 4.9,1.8, 2.72, Iris-virginica 
129,6.4,2.8, 2.29, 5.6,2.1, 2.67, Iris-virginica 
130,7.2,3.0, 2.4, 5.8,1.6, 3.62, Iris-virginica 
131,7.4,2.8, 2.64, 6.1,1.9, 3.21, Iris-virginica 
132,7.9,3.8, 2.08, 6.4,2.0, 3.2, Iris-virginica 
133,6.4,2.8, 2.29, 5.6,2.2, 2.55, Iris-virginica 
134,6.3,2.8, 2.25, 5.1,1.5, 3.4, Iris-virginica 
135,6.1,2.6, 2.35, 5.6,1.4, 4.0, Iris-virginica 
136,7.7,3.0, 2.57, 6.1,2.3, 2.65, Iris-virginica 
137,6.3,3.4, 1.85, 5.6,2.4, 2.33, Iris-virginica 
138,6.4,3.1, 2.06, 5.5,1.8, 3.06, Iris-virginica 
139,6.0,3.0, 2.0, 4.8,1.8, 2.67, Iris-virginica 
140,6.9,3.1, 2.23, 5.4,2.1, 2.57, Iris-virginica 
141,6.7,3.1, 2.16, 5.6,2.4, 2.33, Iris-virginica 
142,6.9,3.1, 2.23, 5.1,2.3, 2.22, Iris-virginica 
143,5.8,2.7, 2.15, 5.1,1.9, 2.68, Iris-virginica 
144,6.8,3.2, 2.12, 5.9,2.3, 2.57, Iris-virginica 
145,6.7,3.3, 2.03, 5.7,2.5, 2.28, Iris-virginica 
146,6.7,3.0, 2.23, 5.2,2.3, 2.26, Iris-virginica 
147,6.3,2.5, 2.52, 5.0,1.9, 2.63, Iris-virginica 
148,6.5,3.0, 2.17, 5.2,2.0, 2.6, Iris-virginica 
149,6.2,3.4, 1.82, 5.4,2.3, 2.35, Iris-virginica 
150,5.9,3.0, 1.97, 5.1,1.8, 2.83, Iris-virginica 

没有连接任何东西,我的输出是:

______kelkbladeren________
Setosa : Versicolor
Ttest_indResult(statistic=-18.800548677777353, pvalue=2.8240078085535015e-34)
Setosa : Virginica
Ttest_indResult(statistic=-19.473337102574838, pvalue=1.8505071407928178e-35)
Versicolor : Virginica
Ttest_indResult(statistic=-1.4692886838299219, pvalue=0.14495744237234628)

_____bloembladeren_________
Setosa : Versicolor
Ttest_indResult(statistic=8.6379129492322715, pvalue=1.0844901752453786e-13)
Setosa : Virginica
Ttest_indResult(statistic=9.6474890569153793, pvalue=7.0491944878035697e-16)
Versicolor : Virginica
Ttest_indResult(statistic=6.3788055904508321, pvalue=5.910030892093215e-09)

行中出现错误:

print (setosa_versicolor_petal) + "Setosa : Versicolor"

2 个答案:

答案 0 :(得分:0)

当您尝试打印两个不同数据类型的对象时,会发生此错误。 要修复它,您可以使用format,例如:

print("{}  Setosa : Versicolor").format(setosa_versicolor_petal)

请参阅How to get Python to gracefully format None and non-existing fields,了解如何显示字符串代替无。

否则您可以使用!r

print("{!r}  Setosa : Versicolor").format(setosa_versicolor_petal)

这将打印无“无”。

不确定scipy.stats.ttest_ind的输出,但您可以尝试打印值

print("Statistic: {} P-value: {} Setosa : Versicolor").format(setosa_versicolor_petal.statistic, setosa_versicolor_petal.pvalue)

答案 1 :(得分:0)

我找到了解决方案

当我以这种方式设置变量时:

setosa_versicolor_sepal = ('%6.3f %6.25f' % stats.ttest_ind(iris_setosa_sepal_ratio, iris_versicolor_sepal_ratio))

Z值和P值作为字符串分配给变量,可以在我的代码中使用。