Skip to content

Instantly share code, notes, and snippets.

View ogyalcin's full-sized avatar

Orhan Yalcin ogyalcin

View GitHub Profile
@ogyalcin
ogyalcin / xy_traintest_split.py
Last active November 9, 2020 20:41
X-Y Split and Train Test Split
# 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)
@ogyalcin
ogyalcin / model_training.py
Created August 2, 2018 12:10
Select and Train Model and Make Predictions
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)
@ogyalcin
ogyalcin / evaluation.py
Created August 2, 2018 12:14
Evaluate the Results
from sklearn.metrics import confusion_matrix, classification_report
print(confusion_matrix(y_test, predictions))
print(classification_report(y_test,predictions))
@ogyalcin
ogyalcin / clean_test.py
Created August 2, 2018 12:21
Clean the Test Dataset
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
@ogyalcin
ogyalcin / predict_and_write.py
Created August 2, 2018 12:25
Make Predictions and Save It to Csv File
preds = model.predict(test)
results = ids.assign(Survived=preds)
results.to_csv('titanic_submission.csv',index=False)
@ogyalcin
ogyalcin / mnisttf.py
Last active October 26, 2019 14:55
Import Tensorflow and MNIST Dataset
import tensorflow as tf
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
@ogyalcin
ogyalcin / imshow.py
Created August 19, 2018 18:55
Visualizing the Images
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')
@ogyalcin
ogyalcin / xtrainshape.py
Created August 19, 2018 19:04
X_Train Shape
x_train.shape
@ogyalcin
ogyalcin / preparing_mnist.py
Created August 19, 2018 19:12
Reshaping and Normalizing the MNIST Images
# 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
@ogyalcin
ogyalcin / cnn_model_with_keras.py
Last active July 3, 2020 07:01
Convolutional Neural Network with Keras
# 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))