"""
## Cyber Data Analysis

The data is a dummy data. Data generated using website. 

link: https://www.generatedata.com/
"""
# for some basic operations
import numpy as np 
import pandas as pd

# for visualizations
import matplotlib.pyplot as plt
import seaborn as sns

# for interactive visualizations
import plotly.offline as py
from plotly.offline import init_notebook_mode, iplot
import plotly.graph_objs as go


import seaborn as sns
import plotly.express as px
"""
## EDA

* Since the data is generated. There is no error in the data regarding its values. We don't need to clean the data too much.
"""
cyber_csv = pd.read_csv("/kaggle/input/cybercrime/cyber.csv")
#Top 5 Datas
cyber_csv.head()
# Correlations between Data

plt.rcParams['figure.figsize'] = (15, 12)
sns.heatmap(cyber_csv.corr(), cmap='gray', annot=True)
plt.show()
"""
The features are not correlated to each other . Its good. WHY?
The conversation will clear this.
link: https://datascience.stackexchange.com/questions/24452/in-supervised-learning-why-is-it-bad-to-have-correlated-features
"""
"""
# BoxPlot Charts
"""
import plotly.graph_objects as go
# Set notebook mode to work in offline
import plotly.offline as pyo
pyo.init_notebook_mode()

fig = go.Figure()
fig.add_trace(go.Box(y = cyber_csv['Personal'], name='Personal'))
fig.add_trace(go.Box(y = cyber_csv['Social'], name='Social'))
fig.add_trace(go.Box(y = cyber_csv['Technical'], name='Technical'))
fig.add_trace(go.Box(y = cyber_csv['Motivation'], name='Motivation'))

fig.show()
"""
Gained Informations:

1. The median value for each feature is different
2. The max, min values are the same for all parameters
3. More peoples seems to be more technical, motivated and deep personal traits
4. They are less social

# Pair Plot
"""
sns.pairplot(cyber_csv)
"""
* Since the features are in the same range of from 0-10. There is no need to do normalization
"""
"""
# Using PCA to reduce the dimension of data
"""
from sklearn.decomposition import PCA

pca = PCA(n_components=2)

pca.fit(cyber_csv)
values = pca.transform(cyber_csv)

x = values[0:,:1]
y = values[0:,1:2]
"""
# UnSupervised Learning
"""
def plot_scatter(x1,x2):
    plt.scatter(x1, x2)

    plt.xlabel('component 1')
    plt.ylabel('component 2')
    
    plt.show()
plt.rcParams["figure.figsize"] = [10, 5]
plot_scatter(x,y)
X = values
from sklearn.cluster import KMeans

# Fnding the right number of K values

sum_squred_distance = []
K = range(1,15)
for k in K:
    kmeans = KMeans(n_clusters=k, random_state=0).fit(X)
    sum_squred_distance.append(kmeans.inertia_)
plt.plot(K, sum_squred_distance, 'bx-')
plt.xlabel('k')
plt.ylabel('Sum_of_squared_distances')
plt.title('Elbow Method For Optimal k')
plt.show()
"""
* In the plot above the elbow is at k=3 indicating the optimal k for this dataset is 3
"""
kmeans = KMeans(n_clusters=3, random_state=0).fit(X)
"""
# Visualizing KMeans clustering
"""
labels_ = kmeans.labels_
colors = {}

colors[0] = 'b'
colors[1] = 'r'
colors[2] = 'm'

#Build the color vaectore for each data frame point
cvec = [colors[label] for label in labels_]

plt.xlabel('Component 1')
plt.ylabel('Component 2')

#for construction of the legend of the plot
r = plt.scatter(x,y, color='r');
b = plt.scatter(x,y, color='b');
m = plt.scatter(x,y, color='m');

#Plotting DATE in X-axis and no_of_activities in Y-axis
plt.figure(figsize =(10, 5))
plt.scatter(x,y,c = cvec)

#Building the legend
plt.legend((r,b,m),('label 0','label 1', 'label 2'))
plt.xticks(rotation=90)

plt.xlabel('Component 1')
plt.ylabel('Component 2')
    
plt.show()
"""
* Now me have three set of people
* Lets add the second cluster to suspect
"""
X_df = pd.DataFrame()
X_df['component1'] = values[0:,:1].tolist()
X_df['component2'] = values[0:,1:2].tolist()

X_df['Target'] = 0

for v in X_df[kmeans.labels_==1].index:
    X_df['Target'][v] = 1
"""
# Perform Supervised Learning in Labeled Data
"""
X_df.head()
# Making Feature and target

X = values
Y = X_df['Target']

from sklearn.model_selection import train_test_split

#make the x for train and test (also called validation data) 
xtrain,xtest,ytrain,ytest = train_test_split(X,Y,train_size=0.80,random_state=42)
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

clf = LogisticRegression()
clf.fit(xtrain, ytrain)

y_predict = clf.predict(xtest)

accuracy_score(ytest, y_predict)
"""
# More Analysis Coming
"""