0% found this document useful (0 votes)
1 views16 pages

Task Preprocessing

The document describes the analysis of a social media dataset containing 30,000 entries and 19 columns, including demographics, social media usage, and productivity metrics. It details data cleaning steps such as handling missing values, outlier detection, and encoding categorical variables. Finally, it applies standard scaling to numerical features for further analysis.

Uploaded by

196m1a0424
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views16 pages

Task Preprocessing

The document describes the analysis of a social media dataset containing 30,000 entries and 19 columns, including demographics, social media usage, and productivity metrics. It details data cleaning steps such as handling missing values, outlier detection, and encoding categorical variables. Finally, it applies standard scaling to numerical features for further analysis.

Uploaded by

196m1a0424
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

In [91]: import numpy as np

import pandas as pd
import [Link] as plt
import seaborn as sns

In [92]: df = pd.read_csv(r"D:\Innomatics\data sets\social_media.csv")

In [93]: [Link]()

Out[93]: age gender job_type daily_social_media_time social_platform_preference number_of_notificatio

0 56 Male Unemployed 4.180940 Facebook

1 46 Male Health 3.249603 Twitter

2 32 Male Finance NaN Twitter

3 60 Female Unemployed NaN Facebook

4 25 Male IT NaN Telegram

 
In [94]: [Link]

Out[94]: (30000, 19)

In [95]: [Link].to_list()

Out[95]: ['age',
'gender',
'job_type',
'daily_social_media_time',
'social_platform_preference',
'number_of_notifications',
'work_hours_per_day',
'perceived_productivity_score',
'actual_productivity_score',
'stress_level',
'sleep_hours',
'screen_time_before_sleep',
'breaks_during_work',
'uses_focus_apps',
'has_digital_wellbeing_enabled',
'coffee_consumption_per_day',
'days_feeling_burnout_per_month',
'weekly_offline_hours',
'job_satisfaction_score']

In [96]: [Link]()
<class '[Link]'>
RangeIndex: 30000 entries, 0 to 29999
Data columns (total 19 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 age 30000 non-null int64
1 gender 30000 non-null object
2 job_type 30000 non-null object
3 daily_social_media_time 27235 non-null float64
4 social_platform_preference 30000 non-null object
5 number_of_notifications 30000 non-null int64
6 work_hours_per_day 30000 non-null float64
7 perceived_productivity_score 28386 non-null float64
8 actual_productivity_score 27635 non-null float64
9 stress_level 28096 non-null float64
10 sleep_hours 27402 non-null float64
11 screen_time_before_sleep 27789 non-null float64
12 breaks_during_work 30000 non-null int64
13 uses_focus_apps 30000 non-null bool
14 has_digital_wellbeing_enabled 30000 non-null bool
15 coffee_consumption_per_day 30000 non-null int64
16 days_feeling_burnout_per_month 30000 non-null int64
17 weekly_offline_hours 30000 non-null float64
18 job_satisfaction_score 27270 non-null float64
dtypes: bool(2), float64(9), int64(5), object(3)
memory usage: 3.9+ MB

In [97]: [Link]()

Out[97]: age daily_social_media_time number_of_notifications work_hours_per_day perceived_p

count 30000.000000 27235.000000 30000.000000 30000.000000

mean 41.486867 3.113418 59.958767 6.990792

std 13.835221 2.074813 7.723772 1.997736

min 18.000000 0.000000 30.000000 0.000000

25% 30.000000 1.639566 55.000000 5.643771

50% 41.000000 3.025913 60.000000 6.990641

75% 53.000000 4.368917 65.000000 8.354725

max 65.000000 17.973256 90.000000 12.000000

 
In [98]: [Link]().mean()*100
Out[98]: age 0.000000
gender 0.000000
job_type 0.000000
daily_social_media_time 9.216667
social_platform_preference 0.000000
number_of_notifications 0.000000
work_hours_per_day 0.000000
perceived_productivity_score 5.380000
actual_productivity_score 7.883333
stress_level 6.346667
sleep_hours 8.660000
screen_time_before_sleep 7.370000
breaks_during_work 0.000000
uses_focus_apps 0.000000
has_digital_wellbeing_enabled 0.000000
coffee_consumption_per_day 0.000000
days_feeling_burnout_per_month 0.000000
weekly_offline_hours 0.000000
job_satisfaction_score 9.100000
dtype: float64

In [99]: num_cols = df.select_dtypes(include=["int64","float64"])

In [100… cat_cols = df.select_dtypes(include=["object","bool"])

In [101… num_col_list = num_cols.columns.to_list()

In [102… num_col_list

Out[102… ['age',
'daily_social_media_time',
'number_of_notifications',
'work_hours_per_day',
'perceived_productivity_score',
'actual_productivity_score',
'stress_level',
'sleep_hours',
'screen_time_before_sleep',
'breaks_during_work',
'coffee_consumption_per_day',
'days_feeling_burnout_per_month',
'weekly_offline_hours',
'job_satisfaction_score']

In [103… fig,ax=[Link](len(num_col_list),2,figsize=(14,4*len(num_col_list)))
for i,col in enumerate(num_col_list):
[Link](x=df[col],ax=ax[i][0])
ax[i][0].set_title(f"{col} boxplot")
[Link](df[col],kde=True,ax=ax[i][1])
ax[i][1].set_title(f"{col} histplot")

plt.tight_layout()
[Link]()
In [104… missing_num_cols=[Link][[Link]().mean()*100>5]

In [105… [Link][([Link]().mean()*100<5)&([Link]().mean()*100>0)]

Out[105… Index([], dtype='object')

In [106… fig,ax=[Link](len(missing_num_cols),2,figsize=(14,4*len(missing_num_cols)))
for i,col in enumerate(missing_num_cols):
[Link](x=df[col],ax=ax[i][0])
ax[i][0].set_title(f"{col} boxplot")
[Link](df[col],kde=True,ax=ax[i][1])
ax[i][1].set_title(f"{col} histplot")

plt.tight_layout()
[Link]()
In [107… missing_num_cols.to_list()

Out[107… ['daily_social_media_time',
'perceived_productivity_score',
'actual_productivity_score',
'stress_level',
'sleep_hours',
'screen_time_before_sleep',
'job_satisfaction_score']

In [108… # 'daily_social_media_time': right skewed outliers present so will impute with median
# 'perceived_productivity_score',: mean imputed
# 'actual_productivity_score',mean
# 'stress_level',mean
# 'sleep_hours',mean
# 'screen_time_before_sleep',median
# 'job_satisfaction_score'mean

In [109… df["daily_social_media_time"]=df["daily_social_media_time"].fillna(df["daily_social_media_time
df["perceived_productivity_score"]=df["perceived_productivity_score"].fillna(df["perceived_pro
df["actual_productivity_score"]=df["actual_productivity_score"].fillna(df["actual_productivity
df["stress_level"]=df["stress_level"].fillna(df["stress_level"].mean())
df["sleep_hours"]=df["sleep_hours"].fillna(df["sleep_hours"].mean())
df["screen_time_before_sleep"]=df["screen_time_before_sleep"].fillna(df["screen_time_before_sl
df["job_satisfaction_score"]=df["job_satisfaction_score"].fillna(df["job_satisfaction_score"]

In [110… [Link]().sum()

Out[110… age 0
gender 0
job_type 0
daily_social_media_time 0
social_platform_preference 0
number_of_notifications 0
work_hours_per_day 0
perceived_productivity_score 0
actual_productivity_score 0
stress_level 0
sleep_hours 0
screen_time_before_sleep 0
breaks_during_work 0
uses_focus_apps 0
has_digital_wellbeing_enabled 0
coffee_consumption_per_day 0
days_feeling_burnout_per_month 0
weekly_offline_hours 0
job_satisfaction_score 0
dtype: int64
In [111… #do the same for rest of the columns

In [112… [Link]().sum()

Out[112… np.int64(0)

In [113… for col in num_col_list:


q1 = df[col].quantile(0.25)
q3 = df[col].quantile(0.75)
iqr = q3-q1
lower = q1-(1.5*iqr)
upper = q3+(1.5*iqr)
df[col] = df[col].clip(lower, upper)
print(f"{col}: Lower: {lower}, Upper: {upper}")

age: Lower: -4.5, Upper: 87.5


daily_social_media_time: Lower: -1.8349294337611881, Upper: 7.852210291296753
number_of_notifications: Lower: 40.0, Upper: 80.0
work_hours_per_day: Lower: 1.57734136932575, Upper: 12.421155037645882
perceived_productivity_score: Lower: -1.1055828780214716, Upper: 12.132249300129386
actual_productivity_score: Lower: -0.8210882134314588, Upper: 10.72670926309111
stress_level: Lower: -4.5, Upper: 15.5
sleep_hours: Lower: 2.8988933686607607, Upper: 10.099023022413736
screen_time_before_sleep: Lower: -0.7297757882449388, Upper: 2.735143000411472
breaks_during_work: Lower: -7.0, Upper: 17.0
coffee_consumption_per_day: Lower: -2.0, Upper: 6.0
days_feeling_burnout_per_month: Lower: -16.0, Upper: 48.0
weekly_offline_hours: Lower: -11.596534531854738, Upper: 31.439215468564196
job_satisfaction_score: Lower: -0.7894188309721026, Upper: 10.724995091639753

In [114… fig,ax=[Link](len(num_col_list),1,figsize=(14,4*len(num_col_list)))
for i,col in enumerate(num_col_list):
[Link](x=df[col],ax=ax[i])
ax[i].set_title(f"{col} boxplot")

plt.tight_layout()
[Link]()
In [115… cat_cols

Out[115… gender job_type social_platform_preference uses_focus_apps has_digital_wellbeing_enabled

0 Male Unemployed Facebook False False

1 Male Health Twitter True True

2 Male Finance Twitter True False

3 Female Unemployed Facebook False False

4 Male IT Telegram False True

... ... ... ... ... ...

29995 Female Health Facebook False False

29996 Male Health Instagram False False

29997 Male Education TikTok False False

29998 Female Education Instagram False False

29999 Male Unemployed Twitter False True

30000 rows × 5 columns

 
In [116… for col in cat_cols:
print(col, df[col].nunique())
gender 3
job_type 6
social_platform_preference 5
uses_focus_apps 2
has_digital_wellbeing_enabled 2

In [117… for col in cat_cols:


print(col, df[col].value_counts())

gender gender
Male 14452
Female 14370
Other 1178
Name: count, dtype: int64
job_type job_type
Education 5055
IT 5026
Finance 5017
Student 5012
Unemployed 4958
Health 4932
Name: count, dtype: int64
social_platform_preference social_platform_preference
TikTok 6096
Telegram 6013
Instagram 6006
Twitter 5964
Facebook 5921
Name: count, dtype: int64
uses_focus_apps uses_focus_apps
False 20979
True 9021
Name: count, dtype: int64
has_digital_wellbeing_enabled has_digital_wellbeing_enabled
False 22602
True 7398
Name: count, dtype: int64

In [118… df = pd.get_dummies(df, columns=cat_cols.columns.to_list(), drop_first=True,dtype=int)

In [119… [Link]()

Out[119… age daily_social_media_time number_of_notifications work_hours_per_day perceived_productivity_sc

0 56 4.180940 61 6.753558 8.040

1 46 3.249603 59 9.169296 5.063

2 32 3.025913 57 7.910952 3.861

3 60 3.025913 59 6.355027 2.916

4 25 3.025913 66 6.214096 8.868

5 rows × 27 columns

 
In [120… [Link]

Out[120… (30000, 27)

In [121… num_col_list
Out[121… ['age',
'daily_social_media_time',
'number_of_notifications',
'work_hours_per_day',
'perceived_productivity_score',
'actual_productivity_score',
'stress_level',
'sleep_hours',
'screen_time_before_sleep',
'breaks_during_work',
'coffee_consumption_per_day',
'days_feeling_burnout_per_month',
'weekly_offline_hours',
'job_satisfaction_score']

In [122… from [Link] import StandardScaler

In [124… scaler = StandardScaler()


for col in num_col_list:
df[col] = scaler.fit_transform(df[[col]])

In [125… [Link]()

Out[125… age daily_social_media_time number_of_notifications work_hours_per_day perceived_productiv

0 1.049017 0.615403 0.137734 -0.119901

1 0.326212 0.100934 -0.123787 1.092381

2 -0.685715 -0.022632 -0.385308 0.460910

3 1.338138 -0.022632 -0.123787 -0.319895

4 -1.191679 -0.022632 0.791537 -0.390618

5 rows × 27 columns

 
In [126… for col in num_col_list:
print(f"{col}: mean:{df[col].mean().round(2)} std:{df[col].std().round(2)}")

age: mean:0.0 std:1.0


daily_social_media_time: mean:-0.0 std:1.0
number_of_notifications: mean:0.0 std:1.0
work_hours_per_day: mean:0.0 std:1.0
perceived_productivity_score: mean:0.0 std:1.0
actual_productivity_score: mean:-0.0 std:1.0
stress_level: mean:-0.0 std:1.0
sleep_hours: mean:0.0 std:1.0
screen_time_before_sleep: mean:-0.0 std:1.0
breaks_during_work: mean:-0.0 std:1.0
coffee_consumption_per_day: mean:0.0 std:1.0
days_feeling_burnout_per_month: mean:-0.0 std:1.0
weekly_offline_hours: mean:-0.0 std:1.0
job_satisfaction_score: mean:-0.0 std:1.0

In [130… [Link](figsize=(12,6))
[Link](df[num_col_list])
[Link](rotation=45)
[Link]()
In [131… [Link](figsize=(12,6))
[Link](df)
[Link](rotation=45)
[Link]()

In [132… [Link](figsize=(12,6))
[Link](data=df[num_col_list])
[Link](rotation=45)
[Link]()
In [ ]:

You might also like