티스토리 뷰
Deep Learning
2. Neural Networks / L7. Keras - Lab : Student Admissions in Keras
chrisysl 2018. 8. 9. 09:49Predicting Student Admissions with Neural Networks in Keras
· Loading the data
1 2 3 4 5 6 7 8 9 | # Importing pandas and numpy import pandas as pd import numpy as np # Reading the csv file into a pandas DataFrame data = pd.read_csv('student_data.csv') # Printing out the first 10 rows of our data data[:10] | cs |
>>
· Plotting the data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # Importing matplotlib import matplotlib.pyplot as plt # Function to help us plot def plot_points(data): X = np.array(data[["gre","gpa"]]) y = np.array(data["admit"]) admitted = X[np.argwhere(y==1)] rejected = X[np.argwhere(y==0)] plt.scatter([s[0][0] for s in rejected], [s[0][1] for s in rejected], s = 25, color = 'red', edgecolor = 'k') plt.scatter([s[0][0] for s in admitted], [s[0][1] for s in admitted], s = 25, color = 'cyan', edgecolor = 'k') plt.xlabel('Test (GRE)') plt.ylabel('Grades (GPA)') # Plotting the points plot_points(data) plt.show() | cs |
>>
· Make 4 plots, each one for each rank
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # Make 4 plots, each one for each rank. # Separating the ranks data_rank1 = data[data["rank"]==1] data_rank2 = data[data["rank"]==2] data_rank3 = data[data["rank"]==3] data_rank4 = data[data["rank"]==4] # Plotting the graphs plot_points(data_rank1) plt.title("Rank 1") plt.show() plot_points(data_rank2) plt.title("Rank 2") plt.show() plot_points(data_rank3) plt.title("Rank 3") plt.show() plot_points(data_rank4) plt.title("Rank 4") plt.show() | cs |
>>
· One-hot encoding the rank
1 2 3 4 5 6 7 | # One-hot encoding the rank # Make dummy variables for rank one_hot_data = pd.concat([data, pd.get_dummies(data['rank'], prefix='rank')], axis=1) # Drop the previous rank column one_hot_data = one_hot_data.drop('rank', axis=1) # Print the first 10 rows of our data one_hot_data[:10] | cs |
>>
· Scaling the data
1
2
3
4
5
6
7
# Scaling the data
# Copying our data
processed_data = one_hot_data[:]
# Scaling the columns
processed_data['gre'] = processed_data['gre']/800
processed_data['gpa'] = processed_data['gpa']/4.0
processed_data[:10]
>>
· Splitting the data into Training and Testing
1 2 3 4 5 6 7 | # Splitting the data into Training and Testing sample = np.random.choice(processed_data.index, size=int(len(processed_data)*0.9), replace=False) train_data, test_data = processed_data.iloc[sample], processed_data.drop(sample) print("Number of training samples is", len(train_data)) print("Number of testing samples is", len(test_data)) print(train_data[:10]) print(test_data[:10]) | cs |
>>
· Splitting the data into features and targets (labels)
1 2 3 4 5 6 7 8 9 10 11 | import keras # Separate data and one-hot encode the output # Note: We're also turning the data into numpy arrays, in order to train the model in Keras features = np.array(train_data.drop('admit', axis=1)) targets = np.array(keras.utils.to_categorical(train_data['admit'], 2)) features_test = np.array(test_data.drop('admit', axis=1)) targets_test = np.array(keras.utils.to_categorical(test_data['admit'], 2)) print(features[:10]) print(targets[:10]) | cs |
>>
· Defining the model architecture
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # Imports import numpy as np from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optimizers import SGD from keras.utils import np_utils # Building the model model = Sequential() model.add(Dense(128, activation='relu', input_shape=(6,))) model.add(Dropout(.2)) model.add(Dense(64, activation='relu')) model.add(Dropout(.1)) model.add(Dense(2, activation='softmax')) # Compiling the model model.compile(loss = 'categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() | cs |
>>
· Training the model
1 2 | # Training the model model.fit(features, targets, epochs=200, batch_size=100, verbose=0) | cs |
· Scoring the model
1 2 3 4 5 | # Evaluating the model on the training and testing set score = model.evaluate(features, targets) print("\n Training Accuracy:", score[1]) score = model.evaluate(features_test, targets_test) print("\n Testing Accuracy:", score[1]) | cs |
>>
· 전체코드
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | # Loading the data # Importing pandas and numpy import pandas as pd import numpy as np # Reading the csv file into a pandas DataFrame data = pd.read_csv('student_data.csv') # Printing out the first 10 rows of our data data[:10] # Plotting the data # Importing matplotlib import matplotlib.pyplot as plt # Function to help us plot def plot_points(data): X = np.array(data[["gre","gpa"]]) y = np.array(data["admit"]) admitted = X[np.argwhere(y==1)] rejected = X[np.argwhere(y==0)] plt.scatter([s[0][0] for s in rejected], [s[0][1] for s in rejected], s = 25, color = 'red', edgecolor = 'k') plt.scatter([s[0][0] for s in admitted], [s[0][1] for s in admitted], s = 25, color = 'cyan', edgecolor = 'k') plt.xlabel('Test (GRE)') plt.ylabel('Grades (GPA)') # Plotting the points plot_points(data) plt.show() # Make 4 plots, each one for each rank. # Separating the ranks data_rank1 = data[data["rank"]==1] data_rank2 = data[data["rank"]==2] data_rank3 = data[data["rank"]==3] data_rank4 = data[data["rank"]==4] # Plotting the graphs plot_points(data_rank1) plt.title("Rank 1") plt.show() plot_points(data_rank2) plt.title("Rank 2") plt.show() plot_points(data_rank3) plt.title("Rank 3") plt.show() plot_points(data_rank4) plt.title("Rank 4") plt.show() # One-hot encoding the rank # Make dummy variables for rank one_hot_data = pd.concat([data, pd.get_dummies(data['rank'], prefix='rank')], axis=1) # Drop the previous rank column one_hot_data = one_hot_data.drop('rank', axis=1) # Print the first 10 rows of our data one_hot_data[:10] # Scaling the data # Copying our data processed_data = one_hot_data[:] # Scaling the columns processed_data['gre'] = processed_data['gre']/800 processed_data['gpa'] = processed_data['gpa']/4.0 processed_data[:10] # Splitting the data into Training and Testing sample = np.random.choice(processed_data.index, size=int(len(processed_data)*0.9), replace=False) train_data, test_data = processed_data.iloc[sample], processed_data.drop(sample) print("Number of training samples is", len(train_data)) print("Number of testing samples is", len(test_data)) print(train_data[:10]) print(test_data[:10]) # Splitting the data into features and targets (labels) import keras # Separate data and one-hot encode the output # Note: We're also turning the data into numpy arrays, in order to train the model in Keras features = np.array(train_data.drop('admit', axis=1)) targets = np.array(keras.utils.to_categorical(train_data['admit'], 2)) features_test = np.array(test_data.drop('admit', axis=1)) targets_test = np.array(keras.utils.to_categorical(test_data['admit'], 2)) print(features[:10]) print(targets[:10]) # Defining the model architecture # Imports import numpy as np from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optimizers import SGD from keras.utils import np_utils # Building the model model = Sequential() model.add(Dense(128, activation='relu', input_shape=(6,))) model.add(Dropout(.2)) model.add(Dense(64, activation='relu')) model.add(Dropout(.1)) model.add(Dense(2, activation='softmax')) # Compiling the model model.compile(loss = 'categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() # Training the model # Training the model model.fit(features, targets, epochs=200, batch_size=100, verbose=0) # Scoring the model # Evaluating the model on the training and testing set score = model.evaluate(features, targets) print("\n Training Accuracy:", score[1]) score = model.evaluate(features_test, targets_test) print("\n Testing Accuracy:", score[1]) | cs |
'Deep Learning' 카테고리의 다른 글
2. Neural Networks / L7. Keras - Lab : IMDB Data in Keras (0) | 2018.08.10 |
---|---|
2. Neural Networks / L7. Keras - Optimizers in Keras (0) | 2018.08.09 |
2. Neural Networks / L7. Keras - Keras (0) | 2018.08.07 |
2. Neural Networks / L6. Sentiment Analysis - Sentiment Classification Projects : Curate a Dataset (0) | 2018.07.26 |
2. Neural Networks / Project : First-Neural-Network (0) | 2018.07.25 |
댓글