Can someone explain to me step by step what this code does?

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Can someone explain to me step by step what this code does?  I need to create a flowchart for it, and am not sure what the steps for it really are.  I dont need an output, just an explanation of how the code works.

The train.csv is a massive file that I have shown part of as a picture.  

 

EmotionRecognition.py:

'''
Data set introduction
The data consists of 48x48 pixel grayscale images of faces
0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral
The faces have been automatically registered so that the face is more or less centered
and occupies about the same amount of space in each image
'''

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

''' ### Read csv data '''
df = pd.read_csv('train.csv')
print("There are total ", len(df), " sample in the loaded dataset.")
print("The size of the dataset is: ", df.shape)
# get a subset of the whole data for now
df = df.sample(frac=0.1, random_state=46)
print("The size of the dataset is: ", df.shape)



''' Extract images and label from the dataframe df '''
width, height = 48, 48
images = df['pixels'].tolist()
faces = []
for sample in images:
face = [int(pixel) for pixel in sample.split(' ')] # Splitting the string by space character as a list
face = np.asarray(face).reshape(width*height) # convert pixels to images and # Resizing the image
faces.append(face.astype('float32') / 255.0) # Normalization
faces = np.asarray(faces)

# Get labels
y = df['emotion'].values


class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
# Visualization a few sample images
plt.figure(figsize=(5, 5))
for i in range(6):
plt.subplot(2, 3, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(np.squeeze(faces[i].reshape(width, height)), cmap='gray')
plt.xlabel(class_names[y[i]])
plt.show()


## Split data into training and test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(faces, y, test_size=0.1, random_state=46)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)


from sklearn.svm import SVC
svclassifier = SVC(kernel='linear')
svclassifier.fit(X_train, y_train)


# Now that our classifier has been trained, let's make predictions on the test data. To make predictions, the predict method of the DecisionTreeClassifier class is used.
y_pred = svclassifier.predict(X_test)

# For classification tasks some commonly used metrics are confusion matrix, precision, recall, and F1 score.
# These are calculated by using sklearn's metrics library contains the classification_report and confusion_matrix methods
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))

 

 

train - Excel
Michael Koch
File
Home
Insert
Page Layout
Formulas
Data
Review
View
Help
O Tell me what you want to do
요 Share
A1
fe
emotion
A.
B
1 emotion pixels
2
0 70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115 110 98 91 84 84 90 99 110 126 143 153 158 171 169 172 169 165 129 110 113 107 95 79 66 62 56 57 61 52 43 41 65 61 58 57 56 69 75 70 65 56 54 105 146 154 151 151
O 151 150 147 155 148 133 111 140 170 174 182 154 153 164 173 178 185 185 189 187 186 193 194 185 183 186 180 173 166 161 147 133 172 151 114 161 161 146 131 104 95 132 163 123 119 129 140 120 151 149 149 153 13
2 231 212 156 164 174 138 161 173 182 200 106 38 39 74 138 161 164 179 190 201 210 216 220 224 222 218 216 213 217 220 220 218 217 212 174 160 162 160 139 135 137 131 94 56 36 44 27 16 229 175 148 173 154 151 171
4
5
4 24 32 36 30 32 23 19 20 30 41 21 22 32 34 21 19 43 52 13 26 40 59 65 12 20 63 99 98 98 111 75 62 41 73 118 140 192 186 187 188 190 190 187 182 176 173 172 173 25 34 29 35 29 26 20 23 19 31 22 21 20 31 26 17 34 75 371
6
64000000000003 15 23 28 48 50 58 84 115 127137 142 151 156 155 149 153 152 157 160 162 159 145 121 83 58 48 38 21 1775 25 27 24 25 100000000006 18 26 37 50 62 83 115 134 138 144 147 150 162 163 16
2 55 55 55 55 55 54 60 68 54 85 151 163 170 179 181 185 188 188 191 196 189 194 198 197 195 194 190 193 195 184 175 172 161 159 158 159 147 136 137 136 146 120 86 93 114 116 99 74 55 55 55 55 55 52 71 86 79 143 156
4 20 17 19 21 25 38 42 42 46 54 56 62 63 66 82 108 118 130 139 134 132 126 113 97 126 148 157 161 155 154 154 164 189 204 194 168 180 188 214 214 214 216 208 220 205 187 176 162 22 17 17 25 29 32 44 47 46 54 64 67 7
3 77 78 79 79 78 75 60 55 4748 58 73 77 79 57 50 37 44 56 70 80 82 87 91 86 80 73 66 54 57 68 69 68 68 49 46 75 71 69 70 70 72 72 71 72 74 77 76 83 84 82 81 81 69 60 60 46 57 74 71 70 67 36 40 45 54 65 71 78 77 78 80 84 83
7
8
9
10
3 85 84 90 121 101 102 133 153 153 169 177 189 195 199 205 207 209 216 221 225 221 220 218 222 223 217 220 217 211 196 188 173 170 133 117 131 121 88 73 73 50 27 34 32 34 40 46 63 78 76 101 131 116 117 149 158 166
2 255 254 255 254 254 179 122 107 95 124 149 150 169 178 179 179 181 181 184 190 191 191 193 190 190 195 194 192 193 196 193 192 188 182 173 162 152 144 129 116 113 106 184 255 252 254 255 255 255 254 254 255 23
0 30 24 21 23 25 25 49 67 84 103 120 125 130 139 140 139 148 171 178 175 176 174 180 180 178 178 182 185 183 186 186 178 180 172 175 171 155 152 141 136 132 137 131 96 46 3744 37 31 22 21 22 24 28 44 67 86 106 119
6 39 75 78 58 58 45 49 48 103 156 81 45 41 38 49 56 60 49 32 31 28 52 83 81 78 75 62 31 18 19 19 20 17 20 16 15 12 10 11 10 23 36 65 59935793 69 86 90 84 75 51 129 133 63 46 45 41 41 42 38 33 30 29 29 27 29 39 52 65 43
11
12
13
14
6 219 213 206 202 209 217 216 215 219 218 223 230 227 227 233 235 234 236 237 238 234 226 219 212 208 201 190 183 176 161 74 15 24 22 22 22 21 19 19 20 20 23 7 89 255 252 255 255 215 215 206 205 209 214 215 211 21
15
6 148 144 130 129 119 122 129 131 139 153 140 128 139 144 146 143 132 133 134 130 140 142 150 152 150 134 128 149 142 138 156 155 140 136 143 143 139 144 160 170 154 181 185 183 193 193 224 247 149 140 134 1321
3 42 13 41 56 62 67 87 95 62 65 70 80 107 127 149 153 150 165 168 177 187 176 167 152 128 130 149 149 146 130 139 139 143 134 105 78 56 36 50 69 82 64 35 10 11 13 105 44 21 4253 67 61 70 58 86 107 115 132 145 164 1
5 107 107 109 109 109 109 110 101 123 140 144 144 149 153 160 161 161 167 168 169 172 172 173 175 176 171 170 166 165 162 162 157 150 149 145 140 136 132 128 111 98 111 109 107 107 107 107 107 107 107 109 109 10
16
17
18
3 14 14 18 28 27 22 21 30 42 61 77 86 88 95 100 99 101 99 98 99 99 96 101 102 96 95 94 88 78 72 65 55 40 25 20 20 42 64 74 129 133 125 144 151 153 154 154 155 16 14 24 28 27 25 31 43 65 84 97 96 96 101 102 109 104 102 1
19
2 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
20
6 134 124 167 180 197 194 203 210 204 203 209 204 206 211 211 216 219 224 228 230 230 226 222 220 217 217 210 207 213 210 199 191 190 188 177 172 148 142 90 46 81 132 116 81 45 84 162 207 154 107 137 182 201 204
21
4 219 192 179 148 208 254 192 98 121 103 145 185 83 58 114 227 225 220 203 202 168 154 157 164 182 211 164 94 122 155 176 238 240 242 192 87 43 39 60 85 34 57 82 130 113 184 225 150 197 179 63 17 37 107 207 133 10
411111111111122227 12 23 45 38 35 14 43 27 31 24 18 20 29 18 62420111105 13 16 16 16 15 14 11111111111100379 2744 43 42 26 28 60 76 61 42 49 51 38 43 31 10 12 174011113 13 16 16 15 15
2 174 51 37 37 38 41 22 25 22 24 35 51 70 83 98 113 119 127 136 149 149 141 125 107 77 50 30 21 9 38 96 79 72 87 60 23 25 43 29 24 33 51 36 33 26 20 136 255 107 4643 34 48 35 21 20 16 22 40 60 77 96 113 131 149 162 169
22
23
24
0 123 125 124 142 209 226 234 236 231 232 235 223 211 196 184 181 182 186 185 193 208 211 208 201 196 192 191 192 194 201 207 216 225 225 223 220 219 220 172 124 124 122 121 121 122 122 122 122 123 125 123 158
25
089 14 21 26 32 37 46 52 62 72 70 71 73 76 83 98 92 80 90 110 148 158 149 166 172 166 166 164 179 196 197 189 185 194 189 174 173 177 179 186 197 192 191 185 176 171 146 9 14 20 22 24 31 39 47 58 71 71 70 75 76 84 9
26
3 252 250 246 229 182 140 98 72 53 44 67 95 95 89 89 90 90 93 94 89 88 88 83 82 8173 74 69 69 64 58 49 46 49 35 32 27 25 24 21 13 9 11 15 15 15 21 24 254 246 221 176 156 129 87 75 54 64 116 106 94 88 88 93 89 87 88 91 8
27
3 224 227 219 217 215 210 187 177 189 200 206 212 210 208 204 207 206 207 205 203 203 206 206 201 204 199 196 189 187 187 186 185 184 186 183 182 182 173 161 154 176 202 206 203 198 196 191 187 221 222 213 208 2
train
Transcribed Image Text:train - Excel Michael Koch File Home Insert Page Layout Formulas Data Review View Help O Tell me what you want to do 요 Share A1 fe emotion A. B 1 emotion pixels 2 0 70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115 110 98 91 84 84 90 99 110 126 143 153 158 171 169 172 169 165 129 110 113 107 95 79 66 62 56 57 61 52 43 41 65 61 58 57 56 69 75 70 65 56 54 105 146 154 151 151 O 151 150 147 155 148 133 111 140 170 174 182 154 153 164 173 178 185 185 189 187 186 193 194 185 183 186 180 173 166 161 147 133 172 151 114 161 161 146 131 104 95 132 163 123 119 129 140 120 151 149 149 153 13 2 231 212 156 164 174 138 161 173 182 200 106 38 39 74 138 161 164 179 190 201 210 216 220 224 222 218 216 213 217 220 220 218 217 212 174 160 162 160 139 135 137 131 94 56 36 44 27 16 229 175 148 173 154 151 171 4 5 4 24 32 36 30 32 23 19 20 30 41 21 22 32 34 21 19 43 52 13 26 40 59 65 12 20 63 99 98 98 111 75 62 41 73 118 140 192 186 187 188 190 190 187 182 176 173 172 173 25 34 29 35 29 26 20 23 19 31 22 21 20 31 26 17 34 75 371 6 64000000000003 15 23 28 48 50 58 84 115 127137 142 151 156 155 149 153 152 157 160 162 159 145 121 83 58 48 38 21 1775 25 27 24 25 100000000006 18 26 37 50 62 83 115 134 138 144 147 150 162 163 16 2 55 55 55 55 55 54 60 68 54 85 151 163 170 179 181 185 188 188 191 196 189 194 198 197 195 194 190 193 195 184 175 172 161 159 158 159 147 136 137 136 146 120 86 93 114 116 99 74 55 55 55 55 55 52 71 86 79 143 156 4 20 17 19 21 25 38 42 42 46 54 56 62 63 66 82 108 118 130 139 134 132 126 113 97 126 148 157 161 155 154 154 164 189 204 194 168 180 188 214 214 214 216 208 220 205 187 176 162 22 17 17 25 29 32 44 47 46 54 64 67 7 3 77 78 79 79 78 75 60 55 4748 58 73 77 79 57 50 37 44 56 70 80 82 87 91 86 80 73 66 54 57 68 69 68 68 49 46 75 71 69 70 70 72 72 71 72 74 77 76 83 84 82 81 81 69 60 60 46 57 74 71 70 67 36 40 45 54 65 71 78 77 78 80 84 83 7 8 9 10 3 85 84 90 121 101 102 133 153 153 169 177 189 195 199 205 207 209 216 221 225 221 220 218 222 223 217 220 217 211 196 188 173 170 133 117 131 121 88 73 73 50 27 34 32 34 40 46 63 78 76 101 131 116 117 149 158 166 2 255 254 255 254 254 179 122 107 95 124 149 150 169 178 179 179 181 181 184 190 191 191 193 190 190 195 194 192 193 196 193 192 188 182 173 162 152 144 129 116 113 106 184 255 252 254 255 255 255 254 254 255 23 0 30 24 21 23 25 25 49 67 84 103 120 125 130 139 140 139 148 171 178 175 176 174 180 180 178 178 182 185 183 186 186 178 180 172 175 171 155 152 141 136 132 137 131 96 46 3744 37 31 22 21 22 24 28 44 67 86 106 119 6 39 75 78 58 58 45 49 48 103 156 81 45 41 38 49 56 60 49 32 31 28 52 83 81 78 75 62 31 18 19 19 20 17 20 16 15 12 10 11 10 23 36 65 59935793 69 86 90 84 75 51 129 133 63 46 45 41 41 42 38 33 30 29 29 27 29 39 52 65 43 11 12 13 14 6 219 213 206 202 209 217 216 215 219 218 223 230 227 227 233 235 234 236 237 238 234 226 219 212 208 201 190 183 176 161 74 15 24 22 22 22 21 19 19 20 20 23 7 89 255 252 255 255 215 215 206 205 209 214 215 211 21 15 6 148 144 130 129 119 122 129 131 139 153 140 128 139 144 146 143 132 133 134 130 140 142 150 152 150 134 128 149 142 138 156 155 140 136 143 143 139 144 160 170 154 181 185 183 193 193 224 247 149 140 134 1321 3 42 13 41 56 62 67 87 95 62 65 70 80 107 127 149 153 150 165 168 177 187 176 167 152 128 130 149 149 146 130 139 139 143 134 105 78 56 36 50 69 82 64 35 10 11 13 105 44 21 4253 67 61 70 58 86 107 115 132 145 164 1 5 107 107 109 109 109 109 110 101 123 140 144 144 149 153 160 161 161 167 168 169 172 172 173 175 176 171 170 166 165 162 162 157 150 149 145 140 136 132 128 111 98 111 109 107 107 107 107 107 107 107 109 109 10 16 17 18 3 14 14 18 28 27 22 21 30 42 61 77 86 88 95 100 99 101 99 98 99 99 96 101 102 96 95 94 88 78 72 65 55 40 25 20 20 42 64 74 129 133 125 144 151 153 154 154 155 16 14 24 28 27 25 31 43 65 84 97 96 96 101 102 109 104 102 1 19 2 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 20 6 134 124 167 180 197 194 203 210 204 203 209 204 206 211 211 216 219 224 228 230 230 226 222 220 217 217 210 207 213 210 199 191 190 188 177 172 148 142 90 46 81 132 116 81 45 84 162 207 154 107 137 182 201 204 21 4 219 192 179 148 208 254 192 98 121 103 145 185 83 58 114 227 225 220 203 202 168 154 157 164 182 211 164 94 122 155 176 238 240 242 192 87 43 39 60 85 34 57 82 130 113 184 225 150 197 179 63 17 37 107 207 133 10 411111111111122227 12 23 45 38 35 14 43 27 31 24 18 20 29 18 62420111105 13 16 16 16 15 14 11111111111100379 2744 43 42 26 28 60 76 61 42 49 51 38 43 31 10 12 174011113 13 16 16 15 15 2 174 51 37 37 38 41 22 25 22 24 35 51 70 83 98 113 119 127 136 149 149 141 125 107 77 50 30 21 9 38 96 79 72 87 60 23 25 43 29 24 33 51 36 33 26 20 136 255 107 4643 34 48 35 21 20 16 22 40 60 77 96 113 131 149 162 169 22 23 24 0 123 125 124 142 209 226 234 236 231 232 235 223 211 196 184 181 182 186 185 193 208 211 208 201 196 192 191 192 194 201 207 216 225 225 223 220 219 220 172 124 124 122 121 121 122 122 122 122 123 125 123 158 25 089 14 21 26 32 37 46 52 62 72 70 71 73 76 83 98 92 80 90 110 148 158 149 166 172 166 166 164 179 196 197 189 185 194 189 174 173 177 179 186 197 192 191 185 176 171 146 9 14 20 22 24 31 39 47 58 71 71 70 75 76 84 9 26 3 252 250 246 229 182 140 98 72 53 44 67 95 95 89 89 90 90 93 94 89 88 88 83 82 8173 74 69 69 64 58 49 46 49 35 32 27 25 24 21 13 9 11 15 15 15 21 24 254 246 221 176 156 129 87 75 54 64 116 106 94 88 88 93 89 87 88 91 8 27 3 224 227 219 217 215 210 187 177 189 200 206 212 210 208 204 207 206 207 205 203 203 206 206 201 204 199 196 189 187 187 186 185 184 186 183 182 182 173 161 154 176 202 206 203 198 196 191 187 221 222 213 208 2 train
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY