Professional Documents
Culture Documents
Machine Learning Hands-On
Machine Learning Hands-On
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
print(y)
print(X)
"""**Splitting the dataset into the Training set and Test set**"""
y_pred = regressor.predict(X_test)
print(y_pred)
USN: 01FE21MCA023
KLE Technological University Page |2
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
USN: 01FE21MCA023
KLE Technological University Page |3
USN: 01FE21MCA023
KLE Technological University Page |4
USN: 01FE21MCA023
KLE Technological University Page |5
USN: 01FE21MCA023
KLE Technological University Page |6
USN: 01FE21MCA023
KLE Technological University Page |7
print(classifier.predict(sc.transform([[30,87000]])))
USN: 01FE21MCA023
KLE Technological University Page |8
y_pred = classifier.predict(X_test)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)
# Visualising the Training set results
from matplotlib.colors import ListedColormap
X_set, y_set = sc.inverse_transform(X_train), y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step =
0.25),
np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(),
X2.ravel()]).T)).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('SVM (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
# Visualising the Test set results
USN: 01FE21MCA023
KLE Technological University Page |9
USN: 01FE21MCA023
KLE Technological University P a g e | 10
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('SVM (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
USN: 01FE21MCA023
KLE Technological University P a g e | 11
USN: 01FE21MCA023
KLE Technological University P a g e | 12
USN: 01FE21MCA023
KLE Technological University P a g e | 13
Epoch 4/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5197 - accuracy: 0.7565
Epoch 5/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5281 - accuracy: 0.7578
Epoch 6/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5223 - accuracy: 0.7526
Epoch 7/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5092 - accuracy: 0.7513
Epoch 8/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5072 - accuracy: 0.7617
Epoch 9/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5241 - accuracy: 0.7552
Epoch 10/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5311 - accuracy: 0.7292
Epoch 11/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5194 - accuracy: 0.7539
Epoch 12/200
77/77 [==============================] - 0s 1ms/step - loss: 0.5436 - accuracy: 0.7292
Epoch 13/200
...
Epoch 199/200
77/77 [==============================] - 0s 1ms/step - loss: 0.4924 - accuracy: 0.7383
Epoch 200/200
77/77 [==============================] - 0s 1ms/step - loss: 0.4948 - accuracy: 0.7617
Accuracy: 76.56
# make probability predictions with the model
predictions = model.predict(X)
# round predictions
rounded = [round(x[0]) for x in predictions]
# make class predictions with the model
predictions = model.predict_classes(X)
# make class predictions with the model
predictions = model.predict_classes(X)
# summarize the first 5 cases
for i in range(5):
print('%s => %d (expected %d)' % (X[i].tolist(), predictions[i], y[i]))
[6.0, 148.0, 72.0, 35.0, 0.0, 33.6, 0.627, 50.0] => 0 (expected 1)
[1.0, 85.0, 66.0, 29.0, 0.0, 26.6, 0.351, 31.0] => 0 (expected 0)
[8.0, 183.0, 64.0, 0.0, 0.0, 23.3, 0.672, 32.0] => 1 (expected 1)
[1.0, 89.0, 66.0, 23.0, 94.0, 28.1, 0.167, 21.0] => 0 (expected 0)
[0.0, 137.0, 40.0, 35.0, 168.0, 43.1, 2.288, 33.0] => 1 (expected 1)
USN: 01FE21MCA023
KLE Technological University P a g e | 14
USN: 01FE21MCA023
KLE Technological University P a g e | 15
USN: 01FE21MCA023
KLE Technological University P a g e | 16
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
USN: 01FE21MCA023
KLE Technological University P a g e | 17
USN: 01FE21MCA023
KLE Technological University P a g e | 18
USN: 01FE21MCA023