-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSarahRoy_Lab2.py
More file actions
59 lines (48 loc) · 2.1 KB
/
Copy pathSarahRoy_Lab2.py
File metadata and controls
59 lines (48 loc) · 2.1 KB
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
#Name: Sarah Ann Roy
#Student ID: 0650615
#COIS 4400H - Lab 2
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn.datasets
from sklearn import preprocessing
from sklearn.cluster import KMeans
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.utils import shuffle
#Part 2: Preprocessing and Determining the number of clusters
california = sklearn.datasets.fetch_california_housing() #load california dataset
boston = sklearn.datasets.load_boston() #load boston dataset
df = pd.DataFrame(boston.data, columns = boston.feature_names)
df["MEDV"] = boston.target
X = df.drop("MEDV",1) #Feature Matrix
y = df["MEDV"] #Target Variable
df.head()
numData = boston['data']
scaledData = preprocessing.scale(numData)
SSE=[]# list to keep track of how good the results are
for i in range(1,9):
kmeans = KMeans(n_clusters=i, max_iter=300)
kmeans.fit(scaledData)
SSE.append(kmeans.inertia_)
plt.plot(range(1,9),SSE)
plt.title('COIS 4400 Lab 2 - Sarah Ann Roy (0650615) \n Plot: Number of Clusters vs SSE')
plt.ylabel('SSE') #label y axis
plt.xlabel('Number of Clusters') #label x axis
plt.show()
kmeans = KMeans(n_clusters=3, max_iter=300)
kmeans.fit(scaledData)
predictions = kmeans.predict(scaledData)
plt.scatter(scaledData[:,0],scaledData[:,1], c = predictions)
plt.title('COIS 4400 Lab 2 - Sarah Ann Roy (0650615) \n Scatter Plot')
plt.show()
#Part 3: Decision Tree
X,y = sklearn.datasets.load_wine(return_X_y=True) #I kept getting errors using Boston Housing prices and California Housing prices, this is the only dataset that printed properly other than iris
X= shuffle(X)
X_trainingData, X_testData, y_trainingData, y_testData = train_test_split(X,y,test_size=0.33)
myClassifier=DecisionTreeClassifier()
myClassifier.fit(X_trainingData,y_trainingData)
prediction=myClassifier.predict(X_testData)
print(classification_report(y_testData,prediction))
print(confusion_matrix(y_testData,prediction))