This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Splitting Features and Label | |
| y = train['Survived'] | |
| X = train.drop(['Survived'],1) | |
| #Using Train Test Split from Sklearn to Split Our Train Dataset into Train and Testing Datasets | |
| from sklearn.model_selection import train_test_split | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.ensemble import GradientBoostingClassifier | |
| model = GradientBoostingClassifier(learning_rate=0.1,max_depth=3) | |
| model.fit(X_train, y_train) | |
| predictions = model.predict(X_test) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.metrics import confusion_matrix, classification_report | |
| print(confusion_matrix(y_test, predictions)) | |
| print(classification_report(y_test,predictions)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test['Age'].fillna(test['Age'].median(),inplace=True) # Age | |
| test['Fare'].fillna(test['Fare'].median(),inplace=True) # Fare | |
| d = {1:'1st',2:'2nd',3:'3rd'} #Pclass | |
| test['Pclass'] = test['Pclass'].map(d) | |
| test['Embarked'].fillna(test['Embarked'].value_counts().index[0], inplace=True) # Embarked | |
| ids = test[['PassengerId']]# Passenger Ids | |
| test.drop(['PassengerId','Name','Ticket','Cabin'],1,inplace=True)# Drop Unnecessary Columns | |
| categorical_vars = test[['Pclass','Sex','Embarked']]# Get Dummies of Categorical Variables | |
| dummies = pd.get_dummies(categorical_vars,drop_first=True) | |
| test = test.drop(['Pclass','Sex','Embarked'],axis=1)#Drop the Original Categorical Variables |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| preds = model.predict(test) | |
| results = ids.assign(Survived=preds) | |
| results.to_csv('titanic_submission.csv',index=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tensorflow as tf | |
| (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as plt | |
| %matplotlib inline # Only use this if using iPython | |
| image_index = 7777 # You may select anything up to 60,000 | |
| print(y_train[image_index]) # The label is 8 | |
| plt.imshow(x_train[image_index], cmap='Greys') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x_train.shape |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Reshaping the array to 4-dims so that it can work with the Keras API | |
| x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) | |
| x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) | |
| input_shape = (28, 28, 1) | |
| # Making sure that the values are float so that we can get decimal points after division | |
| x_train = x_train.astype('float32') | |
| x_test = x_test.astype('float32') | |
| # Normalizing the RGB codes by dividing it to the max RGB value. | |
| x_train /= 255 | |
| x_test /= 255 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Importing the required Keras modules containing model and layers | |
| from tensorflow.keras.models import Sequential | |
| from tensorflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D | |
| # Creating a Sequential Model and adding the layers | |
| model = Sequential() | |
| model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape)) | |
| model.add(MaxPooling2D(pool_size=(2, 2))) | |
| model.add(Flatten()) # Flattening the 2D arrays for fully connected layers | |
| model.add(Dense(128, activation=tf.nn.relu)) | |
| model.add(Dropout(0.2)) |