Home Backend Development Python Tutorial Thanks to learning this Python library, I eliminated a pyramid scheme in one night...

Thanks to learning this Python library, I eliminated a pyramid scheme in one night...

May 28, 2023 am 08:55 AM
python data serial number

"This is a piece of data exported from the computer in their den. You can see if you can find any clues first. I will then go find a few people to ask questions."

Team Wang threw it to me A USB flash drive, he picked up the lunch box and stuffed a few mouthfuls of rice into it, then picked up his hat and walked out of the office quickly.

Thanks to learning this Python library, I eliminated a pyramid scheme in one night...

Tonight, based on the intelligence, we went to a MLM den to carry out an arrest operation and brought back more than a dozen people.

However, no important evidence was found at the scene, and the few people arrested remained silent. Now there is no way to know whether they have other dens, nor do they know who their informants are and where they are. A deadlock was reached for a while.

The document in my hand at this time may become the key to breaking the situation.

Thanks to learning this Python library, I eliminated a pyramid scheme in one night...

I started to observe this list of people: the invite_id field is not repeated and should have a one-to-one relationship with the person's name; while invited_id is repeated a lot, and basically It is the data that appeared in invite_id.

So we can basically infer that this is a list that records the upline and downline relationships of MLM organizations. There are hundreds of pieces of data, which shows that this is a large criminal organization.

In less than an hour, Team Wang returned.

"It's no use, I'm still a tough talker." Captain Wang sat down on the chair and glanced at the time. It was almost twelve o'clock. "How are you looking at that data? Have you found any clues?"

"This is a very large organization, with hundreds of members in total. We may have only caught the tip of the iceberg now. "

"You are right, but because of this, we must hurry up now." Team Wang walked to my work station, "Our current task is to find their upline first and capture the thief. Capture the king first. But none of the people captured tonight can find anything. Although their identities can be determined, the relationship between them cannot be determined for the time being."

"You said they What's the relationship between them?" I suddenly remembered the networkx python library I saw some time ago, and it might come in handy this time. "Leave it to me, give me five minutes."

First use pandas to import the data in the file and filter out the parts we need:

df = pd.read_excel('./doc/1_evidence.xls')
df = df.loc[:, ['id','name','invite_id','invited_id']]
df.columns = ['id','title','to', 'from']
Copy after login

Then call the networkx library to generate the hierarchy Relationship diagram and export it:

G = nx.from_pandas_edgelist(df, 'from', 'to', create_using=nx.DiGraph())

nt = net.Network('960px', '1280px', directed=True)
nt.from_nx(G)
nt.show('1_evidence.html')
Copy after login

In this way, I get the hierarchical relationship diagram corresponding to this document. The relationship between the upper and lower lines is instantly clear:

Thanks to learning this Python library, I eliminated a pyramid scheme in one night...

In an instant Team Wang's face was filled with joy, but he immediately returned to seriousness: "Isn't this a bit flashy? Although it seems intuitive, can you find out who is the top of this organization?"

This certainly does not trouble me. The top level is the root node of the network in the graph. There must be no other points pointing to it, so we only need to traverse all the nodes and find the point with an in-degree of 0.

# 找到根节点
top_node = []
for node, degrees in G.in_degree():
if degrees == 0:
top_node.append(node)
print('Big Boss:', top_node)
Copy after login

"Big Boss: [100000]" This output appears on the screen. "The number 100000 does not have a corresponding person in the table, but there is only one offline person with the number 162385 under 100000. He should be the leader of this organization."

"Yes, this is what you want! I'll go and get it. If other colleagues are looking for information about this person, you continue to study the data and find all the people who are close to this person!"

This must not be a problem. Now that the root node has been found, we can get The number of layers at which all nodes are located.

# 设置所有节点级别
l = nx.shortest_path_length(G, 100000)
nx.set_node_attributes(G, l, 'level')

# 计算每级人员数目
data = {}
for node, level in l.items():
if level in data.keys():
data[level].append(node)
else:
data[level] = [node]
for level, nodes in data.items():
print(level, len(nodes))
Copy after login

This organization has developed to 36 levels. Thinking about it really makes me sweat. Fortunately, my colleagues discovered it in time.

Then color the nodes according to the level for easy observation:

# 添加颜色
for node in G.nodes:
G.nodes[node]['title'] = str(node)
level = G.nodes[node]['level']

if level == 0:
G.nodes[node]['color'] = 'red'
elif level == 1:
G.nodes[node]['color'] = 'orange'
elif level == 2:
G.nodes[node]['color'] = 'yellow'
Copy after login

Thanks to learning this Python library, I eliminated a pyramid scheme in one night...

You can see that the person numbered 162385 has only two offline, and Each of these two downlines has developed dozens of additional downlines, which is quite interesting to think about.

"Found it!" Team Wang opened the door of the office. "The person was found. Among the group of people arrested tonight, two of his subordinates were also inside. They were also They have all admitted it and are now undergoing intensive interrogation." It seems that the actual situation is completely consistent with my speculation, thanks to this list.

"You did a great job tonight!" Captain Wang came over and patted me on the shoulder. "But it's not over yet. According to their confessions, the file contains all the personnel information of their organization. Now you can find out for me the people they have developed the most and we will immediately arrange targeted arrests based on the information. ”

It’s similar to the previous one, but this time I need to traverse the out-degrees of all nodes, then sort them in reverse order, and just take the first few.

# 给下线前十的目标添加颜色
degrees = G.out_degree()
top_nodes = sorted(degrees, key=lambda x: x[1], reverse=True)[:10]
print(top_nodes)

for node in top_nodes:
G.nodes[node[0]]['color'] = 'green'
Copy after login

然后给目标节点加上颜色,方便观察,最终得到了这样的关系图:

Thanks to learning this Python library, I eliminated a pyramid scheme in one night...

“干得不错,只要再把这几个人抓到,就相当于切断了这个组织的大动脉,后面的慢慢收尾就可以了。”王队把文件合上,笑着对我说。“没想到你还有这本事,真是后生可畏啊!”

“今天抓到的那三条大鱼,现在审出什么结果了?”相对于其他,我还是对案情本身更感兴趣。

“别提了,都快笑死我了。这仨人看见证据直接慌了神,开始互相推卸责任,老大说同伙全是另外两个人拉来的他都没参与,另外俩人说骗局全是老大策划的他们就是手下打工的,现在估计还吵着呢...”

The above is the detailed content of Thanks to learning this Python library, I eliminated a pyramid scheme in one night.... For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles