-
Notifications
You must be signed in to change notification settings - Fork 0
/
projct.py
98 lines (59 loc) · 2.62 KB
/
projct.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
import numpy as np
import pandas as pd
import matplotlib as plt
#Preprocessing the traing set
dataset = pd.read_csv('train.csv')
dataset = dataset.drop(['PassengerId','Name','Cabin','Ticket'],axis = 1)
#encoding
dataset = pd.get_dummies(dataset, columns = ['Embarked'],drop_first = True)
dataset = pd.get_dummies(dataset, columns = ['Pclass'],drop_first = True)
dataset.columns.values
arr= [ 'Pclass_2', 'Pclass_3', 'Sex', 'SibSp', 'Parch', 'Embarked_Q',
'Embarked_S','Age', 'Fare','Survived']
dataset = dataset[arr]
dataset['Sex'] = dataset['Sex'].map({'male':1,'female':0})
dataset['Age'] =dataset['Age'].fillna(dataset['Age'].mean())
dataset['SibSp'] = dataset['SibSp'].map({0:0,1:1,2:1,3:1,4:1,5:1,8:1})
dataset['Parch'] = dataset['Parch'].map({0:0,1:1,2:1,3:1,4:1,5:1,6:1})
#Scaling
column_to_scale = ['Age','Fare']
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
sc.fit_transform(dataset[column_to_scale])
dataset[column_to_scale]=sc.transform(dataset[column_to_scale])
X_train = dataset.iloc[:,:-1].values
y_train = dataset.iloc[:,9].values
####################PREPROCESSING THE TEST SET#######################################
dataset1 = pd.read_csv('test.csv')
df = dataset1.copy()
df.columns.values
dataset1 = dataset1.drop(['PassengerId','Name','Cabin','Ticket'],axis = 1)
#second method of encoding
dataset1 = pd.get_dummies(dataset1, columns = ['Embarked'],drop_first = True)
dataset1 = pd.get_dummies(dataset1, columns = ['Pclass'],drop_first = True)
dataset1.columns.values
arr= [ 'Pclass_2', 'Pclass_3', 'Sex', 'SibSp', 'Parch', 'Embarked_Q',
'Embarked_S','Age', 'Fare']
dataset1 = dataset1[arr]
dataset1['Sex'] = dataset1['Sex'].map({'male':1,'female':0})
dataset1['Age'] =dataset1['Age'].fillna(dataset1['Age'].mean())
dataset1['Fare'] =dataset1['Fare'].fillna(dataset1['Fare'].mean())
dataset1['Parch'] =dataset1['Parch'].fillna(dataset1['Parch'].median())
dataset1['SibSp'] = dataset1['SibSp'].map({0:0,1:1,2:1,3:1,4:1,5:1,8:1})
dataset1['Parch'] = dataset1['Parch'].map({0:0,1:1,2:1,3:1,4:1,5:1,6:1,9:1})
#dataset1['Parch'].unique()
#Scaling
column_to_scale1 = ['Age','Fare']
sc.fit_transform(dataset1[column_to_scale1])
dataset1[column_to_scale1]=sc.transform(dataset1[column_to_scale1])
X_test = dataset1.iloc[:,0:9].values
#Outling the model
from sklearn.svm import SVC
classifier=SVC(kernel= 'rbf',random_state=0)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
df = df.drop(['Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch',
'Ticket', 'Fare', 'Cabin', 'Embarked'],axis = 1)
df['Survived'] = y_pred
df1 =df.copy()
df.to_csv('Survived.csv',index =False)