-
Notifications
You must be signed in to change notification settings - Fork 3
/
Handling-Non-Numeric-Data-day13.py
90 lines (74 loc) · 2.8 KB
/
Handling-Non-Numeric-Data-day13.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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn.cluster import KMeans
from sklearn import preprocessing, model_selection
import pandas as pd
'''
TITANIC DATA DESCRIPTION
Pclass Passenger Class (1 = 1st; 2 = 2nd; 3 = 3rd)
survival Survival (0 = No; 1 = Yes)
name Name
sex Sex
age Age
sibsp Number of Siblings/Spouses Aboard
parch Number of Parents/Children Aboard
ticket Ticket Number
fare Passenger Fare (British pound)
cabin Cabin
embarked Port of Embarkation (C = Cherbourg; Q = Queenstown; S = Southampton)
boat Lifeboat
body Body Identification Number
home.dest Home/Destination
'''
df = pd.read_excel('datasets/titanic.xls') # load excel
#print(df.head()) # test print head
#converting non numeric data to numeric
# lets try for sex first
df.drop(['body', 'name'],1,inplace=True) #dropping body id and name as they are not iportant
df.convert_objects(convert_numeric = True) # convert all coloums to numeric
df.fillna(0,inplace=True)
print(df.head())
#handle non numericdata
def handle_non_numeric_data(df):
coloumns = df.columns.values # all coloums
for column in coloumns:
text_digit_vals = {} # setting to empty dict for now
def convert_to_int(val):
return text_digit_vals[val] # for index of that val
#check if datatype is neither int64 nor float 64
if df[column].dtype != np.int64 and df[column].dtype != np.float64:
column_contents =df[column].values.tolist() # convert to list
unique_elements = set(column_contents) # grabbing unique no reptative value
x = 0 # interating
# populating the dict with unique elements
for unique in unique_elements:
if unique not in text_digit_vals:
text_digit_vals[unique] = x
x+=1
df[column] = list(map(convert_to_int, df[column])) # mapping ro the int
return df
df = handle_non_numeric_data(df)
#print(df.head())
# lets drop ticket column
df.drop(['boat'],1,inplace=True) # tickets number matters to cant drop
#using k means on the data to see the chances of survival of people using the data already provided
X = np.array(df.drop(['survived'],1).astype(float)) # droping survived coloumn
#lets scale X now
X = preprocessing.scale(X)
Y = np.array(df['survived']) # our column
clf = KMeans(n_clusters=2) # kmeans
clf.fit(X)
correct = 0
for i in range(len(X)):
predict_me= np.array(X[i].astype(float))
predict_me = predict_me.reshape(-1, len(predict_me))
prediction = clf.predict(predict_me)
if prediction[0] == Y[i]:
correct +=1
print(correct/len(X))
#currently we are between 49 to 51 which is inconclusive
# after scaling X we jump to 75 to 28
#Clusters are assigned totally arbitararily
#after dropping boat 70 to 30