利用卡方检验完成鸢尾花数据集的相关分析代码
时间: 2023-10-12 15:21:46 浏览: 72
实验2-鸢尾花数据集分析-checkpoint.ipynb
非常感谢您的提问。以下是利用卡方检验完成鸢尾花数据集的相关分析代码:
```python
from sklearn.datasets import load_iris
from scipy.stats import chi2_contingency
data = load_iris()
X, y = data['data'], data['target']
feature_names = data['feature_names']
target_names = data['target_names']
for i, feature_name in enumerate(feature_names):
print('Feature:', feature_name)
for j, target_name in enumerate(target_names):
obs = X[y == j, i]
bins = np.percentile(obs, [25, 50, 75])
exp = np.array([np.sum(obs <= b) for b in bins] +
[np.sum(obs > b) for b in bins])
_, pvalue, _, _ = chi2_contingency([exp[:3], exp[3:]])
print(' Target:', target_name)
print(' P-value:', pvalue)
```
希望这可以帮助您完成相关分析。如果您还有其他问题,请随时提出。
阅读全文