-
Notifications
You must be signed in to change notification settings - Fork 0
/
Naive based.py
108 lines (43 loc) · 1.08 KB
/
Naive based.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
#!/usr/bin/env python
# coding: utf-8
# In[36]:
import pandas as pd
df = pd.read_csv("titanic.csv")
df.head()
# In[37]:
df.drop(['PassengerId','Name','SibSp','Parch','Ticket','Cabin','Embarked'],axis='columns',inplace=True)
df.head()
# In[38]:
inputs = df.drop('Survived',axis='columns')
target = df.Survived
# In[39]:
dummies = pd.get_dummies(inputs.Sex)
dummies.head(3)
# In[40]:
inputs = pd.concat([inputs,dummies],axis='columns')
inputs.head(3)
# In[42]:
inputs.drop(['Sex','male'],axis='columns',inplace=True)
inputs.head(3)
# In[43]:
inputs.columns[inputs.isna().any()]
# In[45]:
inputs.Age[:10]
# In[47]:
inputs.Age = inputs.Age.fillna(inputs.Age.mean())
inputs.Age[:10]
# In[48]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(inputs, target, test_size = 0.2)
# In[50]:
len(X_train)
# In[51]:
from sklearn.naive_bayes import GaussianNB
model = GaussianNB()
# In[53]:
model.fit(X_train, y_train)
# In[54]:
model.score(X_test, y_test)
# In[55]:
model.predict(X_test[:10])
# In[ ]: