Skip to content

Commit aa00aef

Browse files
committed
Update Hands-On-Quantum-Machine-Learning-With-Python-Vol-1
1 parent 11d005f commit aa00aef

File tree

7 files changed

+1955
-3
lines changed

7 files changed

+1955
-3
lines changed

chapter_01.ipynb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"metadata": {},
3535
"outputs": [],
3636
"source": [
37-
"\n",
3837
"sudo apt-get update\n",
3938
"sudo apt-get upgrade\n",
4039
"sudo apt-get install -y build-essential wget python3-dev \\\n",

chapter_02.ipynb

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
}
6565
],
6666
"source": [
67+
"# Listing 2.1: Load the data from the csv‐files\n",
6768
"import pandas as pd\n",
6869
"\n",
6970
"train = pd.read_csv('./data/train.csv')\n",
@@ -86,6 +87,7 @@
8687
}
8788
],
8889
"source": [
90+
"# Listing 2.2: The shapes of the Titanic datasets\n",
8991
"print('train has {} rows and {} columns'.format(*train.shape))\n",
9092
"print('test has {} rows and {} columns'.format(*test.shape))"
9193
]
@@ -106,6 +108,7 @@
106108
}
107109
],
108110
"source": [
111+
"# Listing 2.3: The structure of the train dataset\n",
109112
"train.info()"
110113
]
111114
},
@@ -125,6 +128,7 @@
125128
}
126129
],
127130
"source": [
131+
"# Listing 2.4: The structure of the test dataset\n",
128132
"test.info()"
129133
]
130134
},
@@ -170,6 +174,7 @@
170174
}
171175
],
172176
"source": [
177+
"# Listing 2.5: Look at the data\n",
173178
"train.head()"
174179
]
175180
},
@@ -196,6 +201,7 @@
196201
}
197202
],
198203
"source": [
204+
"# Listing 2.6: Cope with missing values\n",
199205
"# option 1\n",
200206
"# We only have two passengers without it. This is bearable\n",
201207
"train = train.dropna(subset=[\"Embarked\"]) \n",
@@ -234,6 +240,7 @@
234240
}
235241
],
236242
"source": [
243+
"# Listing 2.7: Unique values in columns\n",
237244
"print('There are {} different (unique) PassengerIds in the data'\n",
238245
" .format(train[\"PassengerId\"].nunique()))\n",
239246
"print('There are {} different (unique) names in the data'\n",
@@ -258,6 +265,7 @@
258265
}
259266
],
260267
"source": [
268+
"# Listing 2.8: Remove identifying data\n",
261269
"train = train.drop(\"PassengerId\", axis=1)\n",
262270
"train = train.drop(\"Name\", axis=1)\n",
263271
"train = train.drop(\"Ticket\", axis=1)\n",
@@ -293,6 +301,7 @@
293301
}
294302
],
295303
"source": [
304+
"# Listing 2.9: Transforming textual data into numbers\n",
296305
"from sklearn.preprocessing import LabelEncoder\n",
297306
"le = LabelEncoder()\n",
298307
"\n",
@@ -319,6 +328,7 @@
319328
}
320329
],
321330
"source": [
331+
"# Listing 2.10: The maximum values\n",
322332
"print('The maximum age is {}'.format(train[\"Age\"].max()))\n",
323333
"print('The maximum fare is {}'.format(train[\"Fare\"].max()))"
324334
]
@@ -339,6 +349,7 @@
339349
}
340350
],
341351
"source": [
352+
"# Listing 2.11: Normalization of the data.\n",
342353
"from sklearn.preprocessing import MinMaxScaler\n",
343354
"\n",
344355
"scaler = MinMaxScaler()\n",
@@ -365,6 +376,7 @@
365376
}
366377
],
367378
"source": [
379+
"# Listing 2.12: Separating input from labels and training from testing sets\n",
368380
"from sklearn.model_selection import train_test_split\n",
369381
"\n",
370382
"input_data = train[:, 1:8]\n",
@@ -383,6 +395,7 @@
383395
"metadata": {},
384396
"outputs": [],
385397
"source": [
398+
"# Listing 2.13: Save the data to the filesystem\n",
386399
"import numpy as np\n",
387400
"\n",
388401
"with open('data/train.npy', 'wb') as f:\n",
@@ -408,6 +421,7 @@
408421
"metadata": {},
409422
"outputs": [],
410423
"source": [
424+
"# Listing 2.14: A random classifier\n",
411425
"import random\n",
412426
"random.seed(a=None, version=2)\n",
413427
" \n",
@@ -421,6 +435,7 @@
421435
"metadata": {},
422436
"outputs": [],
423437
"source": [
438+
"# Listing 2.15: The classification runner\n",
424439
"def run(f_classify, x):\n",
425440
" return list(map(f_classify, x))"
426441
]
@@ -433,6 +448,7 @@
433448
},
434449
"outputs": [],
435450
"source": [
451+
"# Listing 2.16: Run the classifier\n",
436452
"result = run(classify, train_input)"
437453
]
438454
},
@@ -461,6 +477,7 @@
461477
}
462478
],
463479
"source": [
480+
"# Listing 2.17: Evaluate the classifier\n",
464481
"def evaluate(predictions, actual):\n",
465482
" correct = list(filter(\n",
466483
" lambda item: item[0] == item[1],\n",
@@ -488,6 +505,7 @@
488505
}
489506
],
490507
"source": [
508+
"# Listing 2.18: Always predict a passenger died\n",
491509
"def predict_death(item):\n",
492510
" return 0\n",
493511
"\n",
@@ -519,6 +537,7 @@
519537
}
520538
],
521539
"source": [
540+
"# Listing 2.19: Confustion matrix of the predict death classifier\n",
522541
"from sklearn.metrics import confusion_matrix\n",
523542
"\n",
524543
"predictions = run(predict_death, train_input)\n",
@@ -541,6 +560,7 @@
541560
}
542561
],
543562
"source": [
563+
"# Listing 2.20: The precision score\n",
544564
"from sklearn.metrics import precision_score\n",
545565
"print('The precision score of the predict_death classifier is {}'\n",
546566
" .format(precision_score(train_labels, predictions)))"
@@ -562,6 +582,7 @@
562582
}
563583
],
564584
"source": [
585+
"# Listing 2.21: The recall score\n",
565586
"from sklearn.metrics import recall_score\n",
566587
"print('The recall score of the predict_death classifier is {}'\n",
567588
" .format(recall_score(train_labels, predictions)))"
@@ -583,6 +604,7 @@
583604
}
584605
],
585606
"source": [
607+
"# Listing 2.22: The specificity and the npv\n",
586608
"def specificity(matrix):\n",
587609
" return matrix[0][0]/(matrix[0][0]+matrix[0][1]) if (matrix[0][0]+matrix[0][1] > 0) else 0\n",
588610
"\n",
@@ -611,6 +633,7 @@
611633
}
612634
],
613635
"source": [
636+
"# Listing 2.23: The scores of the random classifier\n",
614637
"random_predictions = run(classify, train_input)\n",
615638
"random_cm = confusion_matrix(train_labels, random_predictions)\n",
616639
"\n",
@@ -637,6 +660,7 @@
637660
"metadata": {},
638661
"outputs": [],
639662
"source": [
663+
"# Listing 2.24: A hypocrite classifier\n",
640664
"def hypocrite(passenger, weight):\n",
641665
" return round(min(1,max(0,weight*0.5+random.uniform(0, 1))))"
642666
]
@@ -657,6 +681,7 @@
657681
}
658682
],
659683
"source": [
684+
"# Listing 2.25: The scores of the hypocrite classifier\n",
660685
"w_predictions = run(lambda passenger: hypocrite(passenger, -0.5), train_input)\n",
661686
"w_cm = confusion_matrix(train_labels, w_predictions)\n",
662687
"\n",
@@ -678,6 +703,7 @@
678703
},
679704
"outputs": [],
680705
"source": [
706+
"# Listing 2.26: Run the hypocrite classifiers\n",
681707
"import numpy as np\n",
682708
"\n",
683709
"# number of steps to consider between -1 and 1\n",
@@ -736,6 +762,7 @@
736762
}
737763
],
738764
"source": [
765+
"# Listing 2.27: Plot the distribution of predictions\n",
739766
"import matplotlib.pyplot as plt\n",
740767
"import matplotlib\n",
741768
"\n",
@@ -768,6 +795,7 @@
768795
"metadata": {},
769796
"outputs": [],
770797
"source": [
798+
"# Listing 2.28: Metrics of the hypocrite classifier\n",
771799
"l_precision = list(map(lambda step: precision_score(train_labels, l_predictions[step]),steps))\n",
772800
"l_recall = list(map(lambda step: recall_score(train_labels, l_predictions[step]),steps))\n",
773801
"l_specificity = list(map(lambda step: specificity(l_cm[step]),steps))\n",
@@ -808,6 +836,7 @@
808836
}
809837
],
810838
"source": [
839+
"# Listing 2.29: Plot the performance measures\n",
811840
"m_precision, = plt.plot(weights, l_precision, 'pink', label=\"precision\")\n",
812841
"m_recall, = plt.plot(weights, l_recall, 'cyan', label=\"recall\")\n",
813842
"m_specificity, = plt.plot(weights, l_specificity, 'gold', label=\"specificity\")\n",
@@ -851,6 +880,7 @@
851880
}
852881
],
853882
"source": [
883+
"# Listing 2.30: Calculating the mean of the measures\n",
854884
"l_mean = list(map(lambda step: sum(step)*0.25, zip(l_precision, l_recall, l_specificity, l_npv)))\n",
855885
"m_mean, = plt.plot(weights, l_mean, 'pink', label=\"Mean of the measures\")\n",
856886
"\n",
@@ -866,6 +896,7 @@
866896
"metadata": {},
867897
"outputs": [],
868898
"source": [
899+
"# Listing 2.31: A reusable function to unmask the hypocrite classifier\n",
869900
"def classifier_report(name, run, classify, input, labels):\n",
870901
" cr_predictions = run(classify, input)\n",
871902
" cr_cm = confusion_matrix(labels, cr_predictions)\n",
@@ -904,13 +935,13 @@
904935
}
905936
],
906937
"source": [
938+
"# Listing 2.32: The report of the random classifier\n",
907939
"classifier_report(\n",
908940
" \"Random PQC\", \n",
909941
" run,\n",
910942
" classify,\n",
911943
" train_input,\n",
912-
" train_labels)\n",
913-
"#CAPTION The report of the random classifier"
944+
" train_labels)"
914945
]
915946
}
916947
]

chapter_03.ipynb

Lines changed: 611 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)