Skip to content

Commit f3d0448

Browse files
authored
Add files via upload
1 parent add094a commit f3d0448

File tree

1 file changed

+324
-0
lines changed

1 file changed

+324
-0
lines changed

Logistic Regression Bank.ipynb

+324
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#import libabry\n",
10+
"\n",
11+
"import pandas as pd\n",
12+
"import numpy as np\n",
13+
"import seaborn as sb\n",
14+
"import matplotlib.pyplot as plt\n",
15+
"from sklearn.linear_model import LogisticRegression\n",
16+
"from sklearn.model_selection import train_test_split\n",
17+
"from sklearn.metrics import confusion_matrix,accuracy_score\n",
18+
"from sklearn.metrics import classification_report"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"#read file\n",
28+
"election_data = pd.read_csv(\"~/Downloads/Data Science/data set/election_data.csv\")"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 3,
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"data": {
38+
"text/html": [
39+
"<div>\n",
40+
"<style scoped>\n",
41+
" .dataframe tbody tr th:only-of-type {\n",
42+
" vertical-align: middle;\n",
43+
" }\n",
44+
"\n",
45+
" .dataframe tbody tr th {\n",
46+
" vertical-align: top;\n",
47+
" }\n",
48+
"\n",
49+
" .dataframe thead th {\n",
50+
" text-align: right;\n",
51+
" }\n",
52+
"</style>\n",
53+
"<table border=\"1\" class=\"dataframe\">\n",
54+
" <thead>\n",
55+
" <tr style=\"text-align: right;\">\n",
56+
" <th></th>\n",
57+
" <th>Election-id</th>\n",
58+
" <th>Result</th>\n",
59+
" <th>Year</th>\n",
60+
" <th>Amount Spent</th>\n",
61+
" <th>Popularity Rank</th>\n",
62+
" </tr>\n",
63+
" </thead>\n",
64+
" <tbody>\n",
65+
" <tr>\n",
66+
" <th>0</th>\n",
67+
" <td>122</td>\n",
68+
" <td>0</td>\n",
69+
" <td>32</td>\n",
70+
" <td>3.81</td>\n",
71+
" <td>3</td>\n",
72+
" </tr>\n",
73+
" <tr>\n",
74+
" <th>1</th>\n",
75+
" <td>315</td>\n",
76+
" <td>1</td>\n",
77+
" <td>48</td>\n",
78+
" <td>6.32</td>\n",
79+
" <td>2</td>\n",
80+
" </tr>\n",
81+
" <tr>\n",
82+
" <th>2</th>\n",
83+
" <td>201</td>\n",
84+
" <td>1</td>\n",
85+
" <td>51</td>\n",
86+
" <td>3.67</td>\n",
87+
" <td>1</td>\n",
88+
" </tr>\n",
89+
" <tr>\n",
90+
" <th>3</th>\n",
91+
" <td>965</td>\n",
92+
" <td>0</td>\n",
93+
" <td>40</td>\n",
94+
" <td>2.93</td>\n",
95+
" <td>4</td>\n",
96+
" </tr>\n",
97+
" <tr>\n",
98+
" <th>4</th>\n",
99+
" <td>410</td>\n",
100+
" <td>1</td>\n",
101+
" <td>52</td>\n",
102+
" <td>3.60</td>\n",
103+
" <td>1</td>\n",
104+
" </tr>\n",
105+
" </tbody>\n",
106+
"</table>\n",
107+
"</div>"
108+
],
109+
"text/plain": [
110+
" Election-id Result Year Amount Spent Popularity Rank\n",
111+
"0 122 0 32 3.81 3\n",
112+
"1 315 1 48 6.32 2\n",
113+
"2 201 1 51 3.67 1\n",
114+
"3 965 0 40 2.93 4\n",
115+
"4 410 1 52 3.60 1"
116+
]
117+
},
118+
"execution_count": 3,
119+
"metadata": {},
120+
"output_type": "execute_result"
121+
}
122+
],
123+
"source": [
124+
"#read to 5 data\n",
125+
"election_data.head()"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 4,
131+
"metadata": {},
132+
"outputs": [
133+
{
134+
"name": "stdout",
135+
"output_type": "stream",
136+
"text": [
137+
"<class 'pandas.core.frame.DataFrame'>\n",
138+
"RangeIndex: 10 entries, 0 to 9\n",
139+
"Data columns (total 5 columns):\n",
140+
" # Column Non-Null Count Dtype \n",
141+
"--- ------ -------------- ----- \n",
142+
" 0 Election-id 10 non-null int64 \n",
143+
" 1 Result 10 non-null int64 \n",
144+
" 2 Year 10 non-null int64 \n",
145+
" 3 Amount Spent 10 non-null float64\n",
146+
" 4 Popularity Rank 10 non-null int64 \n",
147+
"dtypes: float64(1), int64(4)\n",
148+
"memory usage: 528.0 bytes\n"
149+
]
150+
}
151+
],
152+
"source": [
153+
"election_data.info()"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 5,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"data": {
163+
"text/plain": [
164+
"Election-id 0\n",
165+
"Result 0\n",
166+
"Year 0\n",
167+
"Amount Spent 0\n",
168+
"Popularity Rank 0\n",
169+
"dtype: int64"
170+
]
171+
},
172+
"execution_count": 5,
173+
"metadata": {},
174+
"output_type": "execute_result"
175+
}
176+
],
177+
"source": [
178+
"#null value check\n",
179+
"election_data.isna().sum()"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 6,
185+
"metadata": {},
186+
"outputs": [],
187+
"source": [
188+
"#set depend and independent variable\n",
189+
"y = election_data.Result\n",
190+
"election_data.drop(['Result'], axis=1, inplace=True)\n",
191+
"x = election_data"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": 7,
197+
"metadata": {},
198+
"outputs": [
199+
{
200+
"data": {
201+
"text/plain": [
202+
"0 0\n",
203+
"1 1\n",
204+
"2 1\n",
205+
"3 0\n",
206+
"4 1\n",
207+
"5 0\n",
208+
"6 1\n",
209+
"7 1\n",
210+
"8 1\n",
211+
"9 0\n",
212+
"Name: Result, dtype: int64"
213+
]
214+
},
215+
"execution_count": 7,
216+
"metadata": {},
217+
"output_type": "execute_result"
218+
}
219+
],
220+
"source": [
221+
"y"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": 8,
227+
"metadata": {},
228+
"outputs": [],
229+
"source": [
230+
"#split data\n",
231+
"X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1, stratify=y)"
232+
]
233+
},
234+
{
235+
"cell_type": "code",
236+
"execution_count": 9,
237+
"metadata": {},
238+
"outputs": [
239+
{
240+
"data": {
241+
"text/plain": [
242+
"LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
243+
" intercept_scaling=1, l1_ratio=None, max_iter=100,\n",
244+
" multi_class='auto', n_jobs=None, penalty='l2',\n",
245+
" random_state=None, solver='lbfgs', tol=0.0001, verbose=0,\n",
246+
" warm_start=False)"
247+
]
248+
},
249+
"execution_count": 9,
250+
"metadata": {},
251+
"output_type": "execute_result"
252+
}
253+
],
254+
"source": [
255+
"#create Logistic Regression model\n",
256+
"logmodel = LogisticRegression()\n",
257+
"logmodel.fit(X_train, y_train)"
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": 10,
263+
"metadata": {},
264+
"outputs": [],
265+
"source": [
266+
"#predict on test data\n",
267+
"predictions = logmodel.predict(X_test)"
268+
]
269+
},
270+
{
271+
"cell_type": "code",
272+
"execution_count": 11,
273+
"metadata": {},
274+
"outputs": [
275+
{
276+
"name": "stdout",
277+
"output_type": "stream",
278+
"text": [
279+
" precision recall f1-score support\n",
280+
"\n",
281+
" 0 1.00 1.00 1.00 1\n",
282+
" 1 1.00 1.00 1.00 1\n",
283+
"\n",
284+
" accuracy 1.00 2\n",
285+
" macro avg 1.00 1.00 1.00 2\n",
286+
"weighted avg 1.00 1.00 1.00 2\n",
287+
"\n",
288+
"[[1 0]\n",
289+
" [0 1]]\n",
290+
"1.0\n"
291+
]
292+
}
293+
],
294+
"source": [
295+
"#cnfusion matrix, accurarcy\n",
296+
"\n",
297+
"print(classification_report(y_test, predictions))\n",
298+
"print(confusion_matrix(y_test, predictions))\n",
299+
"print(accuracy_score(y_test, predictions))"
300+
]
301+
}
302+
],
303+
"metadata": {
304+
"kernelspec": {
305+
"display_name": "Python 3",
306+
"language": "python",
307+
"name": "python3"
308+
},
309+
"language_info": {
310+
"codemirror_mode": {
311+
"name": "ipython",
312+
"version": 3
313+
},
314+
"file_extension": ".py",
315+
"mimetype": "text/x-python",
316+
"name": "python",
317+
"nbconvert_exporter": "python",
318+
"pygments_lexer": "ipython3",
319+
"version": "3.6.8"
320+
}
321+
},
322+
"nbformat": 4,
323+
"nbformat_minor": 4
324+
}

0 commit comments

Comments
 (0)