-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic_regression.py
92 lines (76 loc) · 2.98 KB
/
logistic_regression.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
# data analysis and wrangling
import pandas as pd
import numpy as np
import random as rnd
import csv
# visualization libraries
#import seaborn as sns
import matplotlib.pyplot as pltmatplotlibinline
# sklearn modules
from sklearn.linear_model import LogisticRegression
from sklearn.feature_selection import RFE
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
'''
Dataset taken from kaggle
train.csv - https://www.kaggle.com/c/titanic/data/
test.csv - https://www.kaggle.com/c/titanic/data/
'''
train_df = pd.read_csv("U:\Python_codes\Datasets\\train.csv")
test_df = pd.read_csv("U:\Python_codes\Datasets\\test.csv")
l1 = train_df.columns.values
l2 = test_df.columns.values
print ("The number of null or unvalid values are")
for value in l1:
Number_Nan = train_df[value].isna()
print (value,"- number of Nan are ",Number_Nan.sum()) #Number of null values
train_df.info() #gives info about the training dataset such as max,min values for each dataset.
for value in l2:
Number_Nan = test_df[value].isna()
print (value,"- number of Nan are ",Number_Nan.sum())
test_df.info()
print (len(test_df))
pid = test_df['PassengerId']
test_df = test_df[['Pclass','Sex','Embarked']]
test_df['Sex'].replace( 'female',0,inplace=True)
test_df['Sex'].replace('male',1,inplace=True)
test_df['Embarked'].replace( 'S',1,inplace=True)
test_df['Embarked'].replace('C',2,inplace=True)
test_df['Embarked'].replace('Q',3,inplace=True)
train_df = train_df.drop(columns = ['Ticket','Fare','Cabin'])
mean = train_df['Age'].mean()
train_df['Age'] = train_df['Age'].fillna(mean)
train_df['Embarked'] = train_df['Embarked'].fillna('Q')
l3 = train_df.columns.values
for value in l2:
Number_Nan = train_df[value].isna()
print (Number_Nan.sum())
label = train_df['Survived']
features = train_df.drop(columns = ['PassengerId','Survived','Name'])
features['Sex'].replace( 'female',0,inplace=True)
features['Sex'].replace('male',1,inplace=True)
features['Embarked'].replace( 'S',1,inplace=True)
features['Embarked'].replace('C',2,inplace=True)
features['Embarked'].replace('Q',3,inplace=True)
#To find how the features are ranked
model = LogisticRegression()
rfe = RFE(model, 3)
fit = rfe.fit(features,label)
print("Num Features: %s" % (fit.n_features_))
print("Selected Features: %s" % (fit.support_))
print("Feature Ranking: %s" % (fit.ranking_))
#Final features and predicting the solutions
fin_features = features.drop(columns = ['Age','SibSp','Parch'])
clf = GaussianNB()
clf.fit(fin_features,label)
predicted_labels = clf.predict(test_df)
#print ("FINISHED classifying. accuracy score : ")
#print (accuracy_score(test_labels, predicted_labels))
print (predicted_labels)