ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python for Data Science - Delving into non-parametric methods using pandas and scipy

2021-01-16 11:35:05  阅读:241  来源: 互联网

标签:spearmanr non methods Python cars value chi2 import cyl


Chapter 5 - Basic Math and Statistics

Segment 6 - Delving into non-parametric methods using pandas and scipy

import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sb
from pylab import rcParams

import scipy
from scipy.stats import spearmanr
%matplotlib inline
rcParams['figure.figsize'] = 14, 7
plt.style.use('seaborn-whitegrid')

The Spearman Rank Correlation

address = '~/Data/mtcars.csv'

cars = pd.read_csv(address)
cars.columns = ['car_names','mpg','cyl','disp', 'hp', 'drat', 'wt', 'qsec', 'vs', 'am', 'gear', 'carb']
cars.head()
car_names mpg cyl disp hp drat wt qsec vs am gear carb
0 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
1 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
2 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
3 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
4 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
sb.pairplot(cars)
<seaborn.axisgrid.PairGrid at 0x7f1891238e80>

output_6_1--

X = cars[['cyl','vs','am','gear']]
sb.pairplot(X)
<seaborn.axisgrid.PairGrid at 0x7f188b9b8ba8>

output_7_1--

cyl = cars['cyl']
vs = cars['vs']
am = cars['am']
gear = cars['gear']

spearmanr_coefficient, p_value = spearmanr(cyl,vs)

print('Spearman Rank Correlation Coefficient %0.3f' % (spearmanr_coefficient))
Spearman Rank Correlation Coefficient -0.814
spearmanr_coefficient, p_value = spearmanr(cyl,am)

print('Spearman Rank Correlation Coefficient %0.3f' % (spearmanr_coefficient))
Spearman Rank Correlation Coefficient -0.522
spearmanr_coefficient, p_value = spearmanr(cyl,gear)

print('Spearman Rank Correlation Coefficient %0.3f' % (spearmanr_coefficient))
Spearman Rank Correlation Coefficient -0.564

Chi-square test for independence

table = pd.crosstab(cyl, am)

from scipy.stats import chi2_contingency
chi2, p, dof, expected = chi2_contingency(table.values)
print('Chi-square statistic %0.3f p_value %0.3f' % (chi2,p))
Chi-square statistic 8.741 p_value 0.013
table = pd.crosstab(cyl, vs)

from scipy.stats import chi2_contingency
chi2, p, dof, expected = chi2_contingency(table.values)
print('Chi-square statistic %0.3f p_value %0.3f' % (chi2,p))
Chi-square statistic 21.340 p_value 0.000
table = pd.crosstab(cyl, gear)

from scipy.stats import chi2_contingency
chi2, p, dof, expected = chi2_contingency(table.values)
print('Chi-square statistic %0.3f p_value %0.3f' % (chi2,p))
Chi-square statistic 18.036 p_value 0.001

标签:spearmanr,non,methods,Python,cars,value,chi2,import,cyl
来源: https://www.cnblogs.com/keepmoving1113/p/14285316.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有