|
| 1 | +import random |
| 2 | + |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import numpy as np |
1 | 5 | import pandas as pd |
2 | | -data = pd.read_csv('https://s3.amazonaws.com/demo-datasets/wine.csv') |
| 6 | +import sklearn.decomposition |
| 7 | +from matplotlib.colors import ListedColormap |
| 8 | +# More accuracy |
| 9 | +from sklearn.neighbors import KNeighborsClassifier |
3 | 10 |
|
4 | 11 |
|
| 12 | +def accuracy(predictions, outcomes): |
| 13 | + # Enter your code here! |
| 14 | + occur = 0 |
| 15 | + v = np.array(predictions) == np.array(outcomes) |
| 16 | + occur = np.sum(v) |
| 17 | + return occur |
5 | 18 |
|
6 | | -df2 = data.drop('color', axis=1) # color is redundant |
7 | 19 |
|
| 20 | +data = pd.read_csv('https://s3.amazonaws.com/demo-datasets/wine.csv') |
8 | 21 |
|
9 | | -import numpy as np |
| 22 | +df2 = data.drop('color', axis=1) # color is redundant |
10 | 23 |
|
| 24 | +numeric_data = df2.values |
11 | 25 |
|
| 26 | +numeric_data = (numeric_data - np.mean(numeric_data)) / (np.std(numeric_data)) |
12 | 27 |
|
13 | | -numeric_data = (numeric_data - np.mean(numeric_data))/(np.std(numeric_data)) |
14 | 28 |
|
15 | | -import sklearn.decomposition |
16 | 29 | pca = sklearn.decomposition.PCA(n_components=2) |
17 | 30 | principal_components = pca.fit(numeric_data).transform(numeric_data) |
18 | 31 |
|
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | -import matplotlib.pyplot as plt |
24 | | -from matplotlib.colors import ListedColormap |
25 | | -from matplotlib.backends.backend_pdf import PdfPages |
26 | 32 | observation_colormap = ListedColormap(['red', 'blue']) |
27 | | -x = principal_components[:,0] |
28 | | -y = principal_components[:,1] |
| 33 | +x = principal_components[:, 0] |
| 34 | +y = principal_components[:, 1] |
29 | 35 |
|
30 | 36 | plt.title("Principal Components of Wine") |
31 | | -plt.scatter(x, y, alpha = 0.2, |
32 | | - c = data['high_quality'], cmap = observation_colormap, edgecolors = 'none') |
33 | | -plt.xlim(-8, 8); plt.ylim(-8, 8) |
34 | | -plt.xlabel("Principal Component 1"); plt.ylabel("Principal Component 2") |
| 37 | +plt.scatter(x, y, alpha=0.2, |
| 38 | + c=data['high_quality'], cmap=observation_colormap, |
| 39 | + edgecolors='none') |
| 40 | +plt.xlim(-8, 8) |
| 41 | +plt.ylim(-8, 8) |
| 42 | +plt.xlabel("Principal Component 1") |
| 43 | +plt.ylabel("Principal Component 2") |
35 | 44 | plt.show() |
36 | 45 |
|
| 46 | +x = np.array([1, 2, 3]) |
| 47 | +y = np.array([1, 2, 4]) |
37 | 48 |
|
| 49 | +print(accuracy(x, y)) |
38 | 50 |
|
| 51 | +print(accuracy([], data["high_quality"])) |
39 | 52 |
|
40 | | - |
41 | | -def accuracy(predictions, outcomes): |
42 | | - # Enter your code here! |
43 | | - occur =0 |
44 | | - for i in range(len(predictions)): |
45 | | - if(predictions[i] == outcomes[i]): |
46 | | - occur += 1 |
47 | | - return occur |
48 | | - |
49 | | - |
50 | | -x = np.array([1,2,3]) |
51 | | -y = np.array([1,2,4]) |
52 | | - |
53 | | -print (accuracy(x,y)) |
54 | | - |
55 | | - |
56 | | -print(accuracy(0,data["high_quality"])) |
57 | | - |
58 | | - |
59 | | -############################## More accuracy |
60 | | -from sklearn.neighbors import KNeighborsClassifier |
61 | | -knn = KNeighborsClassifier(n_neighbors = 5) |
| 53 | +knn = KNeighborsClassifier(n_neighbors=5) |
62 | 54 | knn.fit(numeric_data, data['high_quality']) |
63 | 55 | # Enter your code here! |
64 | 56 |
|
65 | 57 | library_predictions = knn.predict(numeric_data) |
66 | 58 |
|
67 | | -print(accuracy(library_predictions,data['high_quality'])) |
| 59 | +print(accuracy(library_predictions, data['high_quality'])) |
68 | 60 |
|
69 | | -################ |
70 | 61 | n_rows = data.shape[0] |
71 | 62 |
|
| 63 | +print() |
72 | 64 | random.seed(123) |
73 | 65 | selection = random.sample(range(n_rows), 10) |
74 | 66 |
|
75 | | - |
76 | 67 | predictors = np.array(numeric_data) |
77 | 68 | training_indices = [i for i in range(len(predictors)) if i not in selection] |
78 | 69 | outcomes = np.array(data["high_quality"]) |
79 | 70 |
|
80 | | -my_predictions = [knn_predict(p, predictors[training_indices,:], outcomes, k=5) for p in predictors[selection]] |
81 | | -percentage = accuracy(my_predictions,data.high_quality[selection]) |
| 71 | +my_predictions = [ |
| 72 | + knn.predict(predictors[training_indices, :]) for p in |
| 73 | + predictors[selection]] |
| 74 | +percentage = accuracy(my_predictions, outcomes) |
82 | 75 |
|
83 | 76 | print(percentage) |
84 | | - |
85 | | - |
86 | | - |
87 | | - |
88 | | - |
0 commit comments