This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathapache_app.py
104 lines (84 loc) · 3.22 KB
/
apache_app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import pandas as pd
import numpy as np
import streamlit as st
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import warnings
warnings.filterwarnings('ignore')
# App title
st.title("CRIMES AGAINST WOMEN IN INDIA")
data_url = ('https://raw.githubusercontent.com/apacheteam/Crime-Against-Women-PP22-C623-APACHE-TEAM/'
'master/crimes_against_women_2001-2014.csv')
#data_url = (r'C:\Users\Tomi\datasets\crimes_against_women_2001-2014.csv')
@st.cache
def load_data():
data = pd.read_csv(data_url)
return data
# Load data
dataset = load_data()
# View dataset checkbox
if st.checkbox('View raw data'):
# Inspect the raw data
st.subheader('Dataset')
st.write(dataset)
st.write(dataset.shape)
# Polynomial regression function
def polynomial_reg(degree, x_train, y_train, x_test):
poly_feat = PolynomialFeatures(degree)
X_poly = poly_feat.fit_transform(x_train)
x_test = poly_feat.fit_transform(x_test)
# create and fit the polynomial regression model
model = LinearRegression()
poly_model = model.fit(X_poly, y_train)
pred = poly_model.predict(x_test)
return pred
# prediction
unique_states = dataset['state_ut'].unique()
def state_reg(df, state, crime, year):
state_df = df[df['state_ut'] == state]
state_df = state_df.groupby('year', as_index=False)['rape', 'kidnapping_and_abduction', 'dowry_deaths',
'assault_on_women', 'insult_to_modesty',
'cruelty_by_husband_or_relatives',
'importation_of_girls'].sum()
state_df.fillna(0, inplace=True)
state_df["t"] = np.arange(1, len(state_df) + 1)
state_df["t_square"] = state_df["t"] * state_df["t"]
state_df['log_crime'] = np.log(state_df[crime])
x_train = state_df[['t', 't_square']]
y_train = state_df[crime]
if year <= 2014:
yeartopred = state_df[['t', 't_square']][state_df['year'] == year]
elif year == 2015:
yeartopred1 = 15
yeartopred2 = 15 ** 2
yeartopred = np.array([[yeartopred1, yeartopred2]])
elif year == 2016:
yeartopred3 = 16
yeartopred4 = 16 ** 2
yeartopred = np.array([[yeartopred3, yeartopred4]])
# Quad = smf.ols(str(crime) + '~ t+ t_square', data = state_df).fit()
pred_Quad = polynomial_reg(2, x_train, y_train, yeartopred)
if pred_Quad <= 0:
return 0
else:
return int(np.round(pred_Quad, 0))
# Prediction selectbox
# select state
state = st.sidebar.selectbox(
'Select a state you\'re interested in: ',
(unique_states)
)
# select crime
crime = st.sidebar.selectbox(
'Crime: ', ('rape', 'kidnapping_and_abduction', 'dowry_deaths', 'assault_on_women',
'insult_to_modesty', 'cruelty_by_husband_or_relatives', 'importation_of_girls')
)
# select year
year = st.sidebar.selectbox(
'Year:', (2014, 2015, 2016)
)
'Predict number of ', crime, 'cases in the year ', str(year), 'at ', state
# on_click of the PREDICT button
if st.button('PREDICT'):
st.write('apache predicts', str(state_reg(dataset, state, crime, year)), crime, 'cases in ', state,
'by the year', str(year))