Skip to content

Commit adb9405

Browse files
authored
logisticRegression
1 parent 64b3c00 commit adb9405

File tree

2 files changed

+358
-0
lines changed

2 files changed

+358
-0
lines changed

logisticRegression/insurance_data.csv

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
age,bought_insurance
2+
22,0
3+
25,0
4+
47,1
5+
52,0
6+
46,1
7+
56,1
8+
55,0
9+
60,1
10+
62,1
11+
61,1
12+
18,0
13+
28,0
14+
27,0
15+
29,0
16+
49,1
17+
55,1
18+
25,1
19+
58,1
20+
19,0
21+
18,0
22+
21,0
23+
26,0
24+
40,1
25+
45,1
26+
50,1
27+
54,1
28+
23,0
Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/html": [
11+
"<div>\n",
12+
"<style scoped>\n",
13+
" .dataframe tbody tr th:only-of-type {\n",
14+
" vertical-align: middle;\n",
15+
" }\n",
16+
"\n",
17+
" .dataframe tbody tr th {\n",
18+
" vertical-align: top;\n",
19+
" }\n",
20+
"\n",
21+
" .dataframe thead th {\n",
22+
" text-align: right;\n",
23+
" }\n",
24+
"</style>\n",
25+
"<table border=\"1\" class=\"dataframe\">\n",
26+
" <thead>\n",
27+
" <tr style=\"text-align: right;\">\n",
28+
" <th></th>\n",
29+
" <th>age</th>\n",
30+
" <th>bought_insurance</th>\n",
31+
" </tr>\n",
32+
" </thead>\n",
33+
" <tbody>\n",
34+
" <tr>\n",
35+
" <th>0</th>\n",
36+
" <td>22</td>\n",
37+
" <td>0</td>\n",
38+
" </tr>\n",
39+
" <tr>\n",
40+
" <th>1</th>\n",
41+
" <td>25</td>\n",
42+
" <td>0</td>\n",
43+
" </tr>\n",
44+
" <tr>\n",
45+
" <th>2</th>\n",
46+
" <td>47</td>\n",
47+
" <td>1</td>\n",
48+
" </tr>\n",
49+
" <tr>\n",
50+
" <th>3</th>\n",
51+
" <td>52</td>\n",
52+
" <td>0</td>\n",
53+
" </tr>\n",
54+
" <tr>\n",
55+
" <th>4</th>\n",
56+
" <td>46</td>\n",
57+
" <td>1</td>\n",
58+
" </tr>\n",
59+
" <tr>\n",
60+
" <th>5</th>\n",
61+
" <td>56</td>\n",
62+
" <td>1</td>\n",
63+
" </tr>\n",
64+
" <tr>\n",
65+
" <th>6</th>\n",
66+
" <td>55</td>\n",
67+
" <td>0</td>\n",
68+
" </tr>\n",
69+
" <tr>\n",
70+
" <th>7</th>\n",
71+
" <td>60</td>\n",
72+
" <td>1</td>\n",
73+
" </tr>\n",
74+
" <tr>\n",
75+
" <th>8</th>\n",
76+
" <td>62</td>\n",
77+
" <td>1</td>\n",
78+
" </tr>\n",
79+
" <tr>\n",
80+
" <th>9</th>\n",
81+
" <td>61</td>\n",
82+
" <td>1</td>\n",
83+
" </tr>\n",
84+
" <tr>\n",
85+
" <th>10</th>\n",
86+
" <td>18</td>\n",
87+
" <td>0</td>\n",
88+
" </tr>\n",
89+
" <tr>\n",
90+
" <th>11</th>\n",
91+
" <td>28</td>\n",
92+
" <td>0</td>\n",
93+
" </tr>\n",
94+
" <tr>\n",
95+
" <th>12</th>\n",
96+
" <td>27</td>\n",
97+
" <td>0</td>\n",
98+
" </tr>\n",
99+
" <tr>\n",
100+
" <th>13</th>\n",
101+
" <td>29</td>\n",
102+
" <td>0</td>\n",
103+
" </tr>\n",
104+
" <tr>\n",
105+
" <th>14</th>\n",
106+
" <td>49</td>\n",
107+
" <td>1</td>\n",
108+
" </tr>\n",
109+
" <tr>\n",
110+
" <th>15</th>\n",
111+
" <td>55</td>\n",
112+
" <td>1</td>\n",
113+
" </tr>\n",
114+
" <tr>\n",
115+
" <th>16</th>\n",
116+
" <td>25</td>\n",
117+
" <td>1</td>\n",
118+
" </tr>\n",
119+
" <tr>\n",
120+
" <th>17</th>\n",
121+
" <td>58</td>\n",
122+
" <td>1</td>\n",
123+
" </tr>\n",
124+
" <tr>\n",
125+
" <th>18</th>\n",
126+
" <td>19</td>\n",
127+
" <td>0</td>\n",
128+
" </tr>\n",
129+
" <tr>\n",
130+
" <th>19</th>\n",
131+
" <td>18</td>\n",
132+
" <td>0</td>\n",
133+
" </tr>\n",
134+
" <tr>\n",
135+
" <th>20</th>\n",
136+
" <td>21</td>\n",
137+
" <td>0</td>\n",
138+
" </tr>\n",
139+
" <tr>\n",
140+
" <th>21</th>\n",
141+
" <td>26</td>\n",
142+
" <td>0</td>\n",
143+
" </tr>\n",
144+
" <tr>\n",
145+
" <th>22</th>\n",
146+
" <td>40</td>\n",
147+
" <td>1</td>\n",
148+
" </tr>\n",
149+
" <tr>\n",
150+
" <th>23</th>\n",
151+
" <td>45</td>\n",
152+
" <td>1</td>\n",
153+
" </tr>\n",
154+
" <tr>\n",
155+
" <th>24</th>\n",
156+
" <td>50</td>\n",
157+
" <td>1</td>\n",
158+
" </tr>\n",
159+
" <tr>\n",
160+
" <th>25</th>\n",
161+
" <td>54</td>\n",
162+
" <td>1</td>\n",
163+
" </tr>\n",
164+
" <tr>\n",
165+
" <th>26</th>\n",
166+
" <td>23</td>\n",
167+
" <td>0</td>\n",
168+
" </tr>\n",
169+
" </tbody>\n",
170+
"</table>\n",
171+
"</div>"
172+
],
173+
"text/plain": [
174+
" age bought_insurance\n",
175+
"0 22 0\n",
176+
"1 25 0\n",
177+
"2 47 1\n",
178+
"3 52 0\n",
179+
"4 46 1\n",
180+
"5 56 1\n",
181+
"6 55 0\n",
182+
"7 60 1\n",
183+
"8 62 1\n",
184+
"9 61 1\n",
185+
"10 18 0\n",
186+
"11 28 0\n",
187+
"12 27 0\n",
188+
"13 29 0\n",
189+
"14 49 1\n",
190+
"15 55 1\n",
191+
"16 25 1\n",
192+
"17 58 1\n",
193+
"18 19 0\n",
194+
"19 18 0\n",
195+
"20 21 0\n",
196+
"21 26 0\n",
197+
"22 40 1\n",
198+
"23 45 1\n",
199+
"24 50 1\n",
200+
"25 54 1\n",
201+
"26 23 0"
202+
]
203+
},
204+
"execution_count": 1,
205+
"metadata": {},
206+
"output_type": "execute_result"
207+
}
208+
],
209+
"source": [
210+
"import pandas as pd\n",
211+
"import matplotlib.pyplot as plt\n",
212+
"%matplotlib inline\n",
213+
"df = pd.read_csv(\"insurance_data.csv\")\n",
214+
"df"
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": 8,
220+
"metadata": {},
221+
"outputs": [],
222+
"source": [
223+
"from sklearn.model_selection import train_test_split"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": 16,
229+
"metadata": {},
230+
"outputs": [],
231+
"source": [
232+
"Xtrain,Xtest,ytrain,ytest = train_test_split(df[['age']],df.bought_insurance,test_size=0.1)"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": 17,
238+
"metadata": {},
239+
"outputs": [],
240+
"source": [
241+
"from sklearn.linear_model import LogisticRegression\n",
242+
"model = LogisticRegression()"
243+
]
244+
},
245+
{
246+
"cell_type": "code",
247+
"execution_count": 18,
248+
"metadata": {},
249+
"outputs": [
250+
{
251+
"data": {
252+
"text/plain": [
253+
"LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
254+
" intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n",
255+
" penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n",
256+
" verbose=0, warm_start=False)"
257+
]
258+
},
259+
"execution_count": 18,
260+
"metadata": {},
261+
"output_type": "execute_result"
262+
}
263+
],
264+
"source": [
265+
"model.fit(Xtrain,ytrain)"
266+
]
267+
},
268+
{
269+
"cell_type": "code",
270+
"execution_count": 24,
271+
"metadata": {},
272+
"outputs": [
273+
{
274+
"data": {
275+
"text/plain": [
276+
"array([1])"
277+
]
278+
},
279+
"execution_count": 24,
280+
"metadata": {},
281+
"output_type": "execute_result"
282+
}
283+
],
284+
"source": [
285+
"model.predict(60) #classify age of 60 will buy insurance or not"
286+
]
287+
},
288+
{
289+
"cell_type": "code",
290+
"execution_count": 21,
291+
"metadata": {},
292+
"outputs": [
293+
{
294+
"data": {
295+
"text/plain": [
296+
"array([[0.63694763, 0.36305237]])"
297+
]
298+
},
299+
"execution_count": 21,
300+
"metadata": {},
301+
"output_type": "execute_result"
302+
}
303+
],
304+
"source": [
305+
"model.predict_proba(Xtest)"
306+
]
307+
}
308+
],
309+
"metadata": {
310+
"kernelspec": {
311+
"display_name": "Python 3",
312+
"language": "python",
313+
"name": "python3"
314+
},
315+
"language_info": {
316+
"codemirror_mode": {
317+
"name": "ipython",
318+
"version": 3
319+
},
320+
"file_extension": ".py",
321+
"mimetype": "text/x-python",
322+
"name": "python",
323+
"nbconvert_exporter": "python",
324+
"pygments_lexer": "ipython3",
325+
"version": "3.6.7"
326+
}
327+
},
328+
"nbformat": 4,
329+
"nbformat_minor": 2
330+
}

0 commit comments

Comments
 (0)