-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
67 lines (55 loc) · 1.99 KB
/
analysis.py
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
# Import necessary libraries
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Load dataset
df = pd.read_csv('Data/train.csv')
# Data Cleaning
df = df.drop(columns=['PassengerId', 'Name', 'Ticket', 'Cabin'])
df['Age'] = df['Age'].fillna(df['Age'].median())
df['Embarked'] = df['Embarked'].fillna(df['Embarked'].mode()[0])
# Encoding
df['Sex'] = df['Sex'].map({'male': 0, 'female': 1})
df['Embarked'] = df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2})
# Statistical Analysis
summary_stats = df.describe()
correlation_matrix = df.corr()
# Display Summary Statistics and Correlation Matrix
print("Summary Statistics:\n", summary_stats)
print("\nCorrelation Matrix:\n", correlation_matrix)
# Visualization---------------------------------------------
# 1. Histogram of Ages
plt.figure(figsize=(8, 5))
sns.histplot(df['Age'], bins=30, kde=True, color='skyblue')
plt.title('Age Distribution of Titanic Passengers')
plt.xlabel('Age')
plt.ylabel('Frequency')
# plt.show()
plt.savefig("Histogram.png", dpi=300, bbox_inches='tight')
plt.close()
# 2. Bar Chart of Survival Rates of passangers
plt.figure(figsize=(8, 5))
sns.barplot(x='Pclass', y='Survived', data=df, ci=None, palette="muted")
plt.title('Survival Rate by Passenger Class')
plt.xlabel('Passenger Class')
plt.ylabel('Survival Rate')
# plt.show()
plt.savefig("Bar_chart.png", dpi=300, bbox_inches='tight')
plt.close()
# 3. Scatter Plot of Fare VS Age
plt.figure(figsize=(8, 5))
sns.scatterplot(x='Age', y='Fare', hue='Survived', data=df, palette='coolwarm', alpha=0.6)
plt.title('Fare vs Age with Survival Indicator')
plt.xlabel('Age')
plt.ylabel('Fare')
# plt.show()
plt.savefig("Scatter_plot.png", dpi=300, bbox_inches='tight')
plt.close()
# 4. Heatmap of Correlation_Matrix
plt.figure(figsize=(10, 6))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt='.2f')
plt.title('Correlation Matrix of Titanic Dataset')
# plt.show()
plt.savefig("correlation_matrix_heatmap.png", dpi=300, bbox_inches='tight')
plt.close()