Skip to content

Commit 1f54839

Browse files
committed
Added Social Network Analysis folder in Data Analysis Folder
1 parent dee6dcf commit 1f54839

10 files changed

+879
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
```markdown
2+
# Social Network Analysis of Star Wars Episode IV
3+
4+
This project utilizes Social Network Analysis (SNA) to explore and visualize the interactions between characters in *Star Wars Episode IV*. The analysis is performed using the `networkx` library for network creation and analysis, and `matplotlib` for visualization.
5+
6+
## Table of Contents
7+
- [Introduction](#introduction)
8+
- [Requirements](#requirements)
9+
- [Data](#data)
10+
- [Code Overview](#code-overview)
11+
- [Visualizations](#visualizations)
12+
- [Network Statistics](#network-statistics)
13+
- [Traditional Methods vs. Our Model](#traditional-methods-vs-our-model)
14+
- [How to Run the Code](#how-to-run-the-code)
15+
- [License](#license)
16+
17+
## Introduction
18+
Social Network Analysis (SNA) focuses on the relationships between entities rather than the entities themselves. In this project, we analyze the co-occurrences of characters in *Star Wars Episode IV* using graph theory to uncover patterns in their interactions.
19+
20+
## Requirements
21+
To run this project, you will need:
22+
- Python 3.x
23+
- `networkx`
24+
- `matplotlib`
25+
- `pandas` (optional, if you need to manipulate the data)
26+
27+
You can install the required libraries using pip:
28+
```bash
29+
pip install networkx matplotlib pandas
30+
```
31+
32+
## Data
33+
The network data is provided in a CSV file named `star-wars-network-edges.csv`, which contains edges representing interactions between characters. The file format is as follows:
34+
```
35+
node1,node2,weight
36+
```
37+
- `node1`: Character 1
38+
- `node2`: Character 2
39+
- `weight`: Number of scenes in which both characters appeared together
40+
41+
## Code Overview
42+
The code is organized into several sections:
43+
44+
1. **Importing Libraries**
45+
```python
46+
import networkx as nx
47+
import matplotlib
48+
import matplotlib.pyplot as plt
49+
%matplotlib inline
50+
```
51+
52+
2. **Reading the Weighted Edgelist**
53+
```python
54+
G = nx.read_weighted_edgelist('data/star-wars-network-edges.csv', delimiter=",")
55+
```
56+
57+
3. **Viewing Edges and Nodes**
58+
```python
59+
G.edges()
60+
G.nodes()
61+
```
62+
63+
4. **Getting Edge Attributes**
64+
```python
65+
nx.get_edge_attributes(G, 'weight')
66+
```
67+
68+
5. **Drawing the Basic Graph**
69+
```python
70+
nx.draw(G)
71+
```
72+
73+
6. **Visualizing with Different Layouts**
74+
- **Fruchterman-Reingold Layout:**
75+
```python
76+
pos = nx.fruchterman_reingold_layout(G)
77+
nx.draw_networkx_labels(G, pos)
78+
nx.draw(G, pos)
79+
```
80+
- **Circular Layout:**
81+
```python
82+
pos = nx.circular_layout(G)
83+
nx.draw_networkx_labels(G, pos)
84+
nx.draw(G, pos)
85+
```
86+
87+
7. **Visualizing Edges with Varying Widths Based on Weights**
88+
```python
89+
esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] < 5]
90+
emid = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] >= 5 and d['weight'] < 10]
91+
elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] >= 10]
92+
93+
nx.draw_networkx_edges(G, pos, edgelist=elarge, width=4, alpha=0.5)
94+
nx.draw_networkx_edges(G, pos, edgelist=emid, width=2, alpha=0.5)
95+
nx.draw_networkx_edges(G, pos, edgelist=esmall, width=1, alpha=0.5)
96+
```
97+
98+
8. **Coloring Nodes Based on Character Alignment**
99+
```python
100+
dark_side = ["DARTH VADER", "MOTTI", "TARKIN"]
101+
light_side = ["R2-D2", "CHEWBACCA", "C-3PO", "LUKE", "CAMIE", "BIGGS",
102+
"LEIA", "BERU", "OWEN", "OBI-WAN", "HAN", "DODONNA",
103+
"GOLD LEADER", "WEDGE", "RED LEADER", "RED TEN"]
104+
other = ["GREEDO", "JABBA"]
105+
106+
nx.draw_networkx_nodes(G, pos, node_color='red', nodelist=dark_side)
107+
nx.draw_networkx_nodes(G, pos, node_color='yellow', nodelist=light_side)
108+
nx.draw_networkx_nodes(G, pos, node_color='gray', nodelist=other)
109+
```
110+
111+
## Visualizations
112+
The visualizations created in this project illustrate the relationships among characters based on their co-occurrences in scenes. Different layouts help in understanding the structure and clustering of characters.
113+
114+
## Network Statistics
115+
The project also computes various network statistics, including:
116+
- **Density**: Measures how many of the possible connections in the network have been made.
117+
- **Degree**: Number of edges connected to each node.
118+
```python
119+
nx.density(G)
120+
nx.degree(G)
121+
```
122+
123+
## Traditional Methods vs. Our Model
124+
With the massive amount of information generated by social networks every day, organizing this data is crucial. Traditional methods often involve hierarchical clustering, which does not allow the formation of overlapping clusters and requires prior knowledge of the number of clusters.
125+
126+
In contrast, our model proposes a novel hierarchical, non-parametric approach that:
127+
- **Discovers social circles automatically**: Our method identifies social circles within an ego-network without requiring predefined parameters.
128+
- **Utilizes both structural and user-attribute information**: By leveraging the principle of homophily, our approach better captures the natural grouping of characters.
129+
- **Overcomes limitations of traditional methods**: Unlike traditional hierarchical clustering, which may fail in overlapping contexts, our method is designed to recognize and represent overlapping clusters effectively.
130+
131+
Results from testing on Facebook datasets of ego-networks indicate that our approach performs comparably to benchmark results, showcasing its effectiveness in social network analysis.
132+
133+
## How to Run the Code
134+
1. Ensure you have Python 3.x and the required libraries installed.
135+
2. Place the `star-wars-network-edges.csv` file in the `data` directory.
136+
3. Run the script in a Jupyter notebook or a Python environment:
137+
```bash
138+
Social Network Analysis.ipynb
139+
```
140+
141+
Feel free to modify the code for further analysis or visualizations!
142+
143+
## License
144+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
145+
```

0 commit comments

Comments
 (0)