Skip to content

Commit 3a6b573

Browse files
committed
resolved multiple compile issues. Project now compiles and runs with no
issues. Not 100% sure if it still matches the original intent, but it should now be useable.
1 parent ab57c72 commit 3a6b573

File tree

1 file changed

+38
-50
lines changed
  • Code-Sleep-Python/Classification

1 file changed

+38
-50
lines changed
Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,76 @@
1+
import random
2+
3+
import matplotlib.pyplot as plt
4+
import numpy as np
15
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
310

411

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
518

6-
df2 = data.drop('color', axis=1) # color is redundant
719

20+
data = pd.read_csv('https://s3.amazonaws.com/demo-datasets/wine.csv')
821

9-
import numpy as np
22+
df2 = data.drop('color', axis=1) # color is redundant
1023

24+
numeric_data = df2.values
1125

26+
numeric_data = (numeric_data - np.mean(numeric_data)) / (np.std(numeric_data))
1227

13-
numeric_data = (numeric_data - np.mean(numeric_data))/(np.std(numeric_data))
1428

15-
import sklearn.decomposition
1629
pca = sklearn.decomposition.PCA(n_components=2)
1730
principal_components = pca.fit(numeric_data).transform(numeric_data)
1831

19-
20-
21-
22-
23-
import matplotlib.pyplot as plt
24-
from matplotlib.colors import ListedColormap
25-
from matplotlib.backends.backend_pdf import PdfPages
2632
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]
2935

3036
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")
3544
plt.show()
3645

46+
x = np.array([1, 2, 3])
47+
y = np.array([1, 2, 4])
3748

49+
print(accuracy(x, y))
3850

51+
print(accuracy([], data["high_quality"]))
3952

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)
6254
knn.fit(numeric_data, data['high_quality'])
6355
# Enter your code here!
6456

6557
library_predictions = knn.predict(numeric_data)
6658

67-
print(accuracy(library_predictions,data['high_quality']))
59+
print(accuracy(library_predictions, data['high_quality']))
6860

69-
################
7061
n_rows = data.shape[0]
7162

63+
print()
7264
random.seed(123)
7365
selection = random.sample(range(n_rows), 10)
7466

75-
7667
predictors = np.array(numeric_data)
7768
training_indices = [i for i in range(len(predictors)) if i not in selection]
7869
outcomes = np.array(data["high_quality"])
7970

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)
8275

8376
print(percentage)
84-
85-
86-
87-
88-

0 commit comments

Comments
 (0)