首頁 資訊 python大數據分析

python大數據分析

來源:泰然健康網 時間:2024年11月24日 04:02

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

!pip install xgboost

import pandas as pd

import numpy as np

import seaborn as sns

import matplotlib.pyplot as plt

from sklearn.preprocessing import LabelEncoder

import warnings

import xgboost

from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestRegressor,RandomForestClassifier

from sklearn.metrics import confusion_matrix, classification_report, accuracy_score

warnings.filterwarnings('ignore')

plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']

df = pd.read_csv('/home/mw/input/data1581/Sleep_health_and_lifestyle_dataset.csv',encoding='gbk')

df.head()

df.info()

df.duplicated().sum()df_new = df.copy()cat_cols = []for each in df_new.columns.tolist():

    if df_new[each].dtype == 'object' and each != '血壓':

        cat_cols.append(each)

        print(df_new[each].value_counts().to_frame())

    le = LabelEncoder()

    le.fit(df_new[col])

    df_new[col] = le.transform(df_new[col])df_new.head()xueya = df_new['血壓'].str.split('/',expand=True)

xueya.columns = ['高壓','低壓']

xueya = xueya.astype(int)df_new = pd.concat([df_new,xueya],axis=1)df_new.info()plt.figure(figsize=(12,8))

sns.countplot(x='性別',hue='職業(yè)',data=df,palette='Set3')

plt.title('男女及從事職業(yè)情況',fontsize=20)

plt.show()plt.figure(figsize=(12,8))

sns.countplot(x='性別',hue='睡眠障礙',data=df,palette='Set3')

plt.title('男女睡眠障礙情況',fontsize=20)

plt.show()plt.figure(figsize=(12,8))

sns.countplot(x='性別',hue='BMI',data=df,palette='Set3')

plt.title('男女BMI情況',fontsize=20)

plt.show()plt.figure(figsize=(12,8))

plt.hist(df['年齡'],density=True,bins=15,color=plt.cm.RdBu(0.6),edgecolor=plt.cm.RdBu(0.7))

df['年齡'].plot(kind = 'kde')

plt.title('年齡分布',fontsize=20)

plt.show()df['年齡'].min(),df['年齡'].max()plt.figure(figsize=(12,8))

plt.hist(df['睡眠時長'],density=True,bins=15,color=plt.cm.RdBu(0.6),edgecolor=plt.cm.RdBu(0.7))

df['睡眠時長'].plot(kind = 'kde')

plt.title('睡眠時間分布',fontsize=20)

plt.show()plt.figure(figsize=(12,8))

plt.hist(df['睡眠質量'],density=True,bins=6,color=plt.cm.RdBu(0.6),edgecolor=plt.cm.RdBu(0.7))

df['睡眠質量'].plot(kind = 'kde')

plt.title('睡眠質量分布',fontsize=20)

plt.show()plt.figure(figsize=(12,8))

plt.hist(df['心率'],density=True,bins=15,color=plt.cm.RdBu(0.6),edgecolor=plt.cm.RdBu(0.7))

df['心率'].plot(kind = 'kde')

plt.title('心率分布',fontsize=20)

plt.show()plt.figure(figsize=(12,8))

plt.hist(df_new['高壓'],density=True,bins=15,color=plt.cm.RdBu(0.6),edgecolor=plt.cm.RdBu(0.7),label='高壓')

df_new['高壓'].plot(kind = 'kde',label='高壓')

plt.hist(df_new['低壓'],density=True,bins=15,color=plt.cm.RdBu(0.3),edgecolor=plt.cm.RdBu(0.2),label='低壓')

df_new['低壓'].plot(kind = 'kde',label='低壓')

plt.title('血壓分布',fontsize=20)

plt.legend()

plt.show()df_new['高壓'].max(),df_new['高壓'].min(),df_new['低壓'].min(),df_new['低壓'].max()plt.figure(figsize=(12,8))

plt.hist(df['身體活動水平'],density=True,bins=15,color=plt.cm.RdBu(0.6),edgecolor=plt.cm.RdBu(0.7))

df['身體活動水平'].plot(kind = 'kde')

plt.title('身體活動水平分布',fontsize=20)

plt.show()df['身體活動水平'].min(),df['身體活動水平'].max()plt.figure(figsize=(12,8))

plt.hist(df['壓力水平'],density=True,bins=15,color=plt.cm.RdBu(0.6),edgecolor=plt.cm.RdBu(0.7))

df['壓力水平'].plot(kind = 'kde')

plt.title('壓力水平分布',fontsize=20)

plt.show()plt.figure(figsize=(12,8))

plt.hist(df['每日步數'],density=True,bins=15,color=plt.cm.RdBu(0.6),edgecolor=plt.cm.RdBu(0.7))

df['每日步數'].plot(kind = 'kde')

plt.title('每日步數分布',fontsize=20)

plt.show()sns.pairplot(df_new[df_new.columns.tolist()[1:]])plt.figure(figsize=(12,12))

plt.imshow(df_new.iloc[:,1:].corr(),cmap='Blues')

plt.xticks(range(len(df_new.iloc[:,1:].corr().columns.tolist())),df_new.iloc[:,1:].corr().columns.tolist(),rotation=45)

plt.yticks(range(len(df_new.iloc[:,1:].corr().columns.tolist())),df_new.iloc[:,1:].corr().columns.tolist(),rotation=45)

plt.colorbar()

plt.show()target = ['睡眠時長','睡眠質量','睡眠障礙']

df_new.drop(columns=['ID'],inplace=True)df_new.drop(columns=['血壓'],inplace=True)for i in range(len(target[:2])):

    y = df_new[target[i]]

    X = df_new.iloc[:,~df_new.columns.isin(target)]

    model = RandomForestRegressor()

    model.fit(X,y)

    print('在'+ target[i] + '作為因變量時,各因素重要性為:')

    plt.figure(figsize=(8,8))

    plt.subplot(2,1,i+1)

    plt.imshow(model.feature_importances_.reshape(-1,1))

    plt.yticks(range(len(X.columns.tolist())),X.columns.tolist())

    plt.xticks(range(1))

    plt.xlabel(target[i])

    plt.colorbar()

    plt.show()y = df_new[target[2]]

X = df_new.iloc[:,~df_new.columns.isin(target)]

model1 = RandomForestClassifier()

model1.fit(X,y)

plt.imshow(model1.feature_importances_.reshape(-1,1))

plt.yticks(range(len(X.columns.tolist())),X.columns.tolist())

plt.xticks(range(1))

plt.xlabel(target[2])

plt.colorbar()

plt.show()plt.figure(figsize=(4,8))

sns.boxplot(x='性別',y='身體活動水平',palette='Set3',data=df_new)

plt.title('不同性別身體活動水平的箱型圖分析',fontsize=15)

plt.show()plt.figure(figsize=(4,8))

sns.boxplot(x='性別',y='壓力水平',palette='Set3',data=df_new)

plt.title('不同性別壓力水平的箱型圖分析',fontsize=15)

plt.show()lt.figure(figsize=(4,8))

sns.boxplot(x='性別',y='心率',palette='Set3',data=df_new)

plt.title('不同性別心率的箱型圖分析',fontsize=15)

plt.show()plt.figure(figsize=(12,8))

sns.boxplot(x='性別',y='高壓',palette='Set3',data=df_new)

sns.boxplot(x='性別',y='低壓',palette='Set3',data=df_new)

plt.title('不同性別血壓的箱型圖分析',fontsize=15)

plt.show()X = df_new.drop(columns=['睡眠障礙'])

y = df_new[['睡眠障礙']]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, train_size=0.7, random_state=42)model2 = xgboost.XGBClassifier()

model2.fit(X_train, y_train)y_pred = model2.predict(X_test)df_new['睡眠障礙'].unique()cm = confusion_matrix(y_test, y_pred)label_mapping = {0:'失眠',1:'無',2:'睡眠呼吸暫停'}

for i, true_label in enumerate(label_mapping):

    row = ''

    for j, pred_label in enumerate(label_mapping.values()):

        row += f'{cm[i, j]} ({pred_label})t'

    print(f'{row} | {true_label}')

print(classification_report(y_test, y_pred,target_names=['失眠','無', '睡眠呼吸暫停'])) 

fig, ax = plt.subplots()

im = ax.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)

ax.figure.colorbar(im, ax=ax)

ax.set(xticks=np.arange(cm.shape[1]),

       yticks=np.arange(cm.shape[0]),

       xticklabels=label_names, yticklabels=label_names,

       title='Confusion matrix',

       ylabel='True label',

       xlabel='Predicted label')

thresh = cm.max() / 2.

for i in range(cm.shape[0]):

    for j in range(cm.shape[1]):

        ax.text(j, i, format(cm[i, j], 'd'),

                ha="center", va="center",

                color="white" if cm[i, j] > thresh else "black")

fig.tight_layout()

plt.show()

相關知識

最新國內減肥行業(yè)大數據分析.pptx
材料一:國家有關部門根據學生體質健康數據,進行統計分析,全面了解學生健康狀況及變化趨勢,制定了《國家學生體質健康標準》,其中高一男生的正常體重指數為16.5~23.2。 材料二:體重指數BMI是國際,上常用來衡量人體肥胖程度的重要標志,
IT知識講解:Python語言中=和==有什么區(qū)別
2016年十大保健品品牌排行榜數據分析
國內外健康醫(yī)療大數據建設及應用發(fā)展現狀分析
簡答題:身體質量指數(BMI)是衡量身體健康與否的標準之一??茖W家經過大量的統計、分析,推導出計算公式為:BMI=w/(h×h),其中w表示體重(單位為千克),
薄荷健康社媒營銷分析報告|數據報告PDF下載
大數據為健康醫(yī)療賦效能
中國健康瘦身行業(yè)數據分析: 48.8%消費者體重超重原因是生活作息習慣
健康瘦身行業(yè)數據分析:2021年中國45.6%網民認為自己超重

網址: python大數據分析 http://m.u1s5d6.cn/newsview46260.html

推薦資訊