NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

KNN
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split

# Generate data
X, y = make_blobs(n_samples=500, n_features=2, centers=4, cluster_std=1.5, random_state=4)

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

# Train KNN model with k=5
knn_5 = KNeighborsClassifier(n_neighbors=5)
knn_5.fit(X_train, y_train)

# Evaluate accuracy on test set with k=5
accuracy_5 = knn_5.score(X_test, y_test)
print('Accuracy with k=5:', accuracy_5)

# Plot predicted values with k=5
plt.scatter(X_test[:, 0], X_test[:, 1], c=knn_5.predict(X_test), cmap='rainbow', alpha=0.7)
plt.title('Predicted Values (K = 5)')
plt.show()

# Train KNN model with k=1
knn_1 = KNeighborsClassifier(n_neighbors=1)
knn_1.fit(X_train, y_train)

# Evaluate accuracy on test set with k=1
accuracy_1 = knn_1.score(X_test, y_test)
print('Accuracy with k=1:', accuracy_1)

# Plot predicted values with k=1
plt.scatter(X_test[:, 0], X_test[:, 1], c=knn_1.predict(X_test), cmap='rainbow', alpha=0.7)
plt.title('Predicted Values (K = 1)')
plt.show()


Linear regression
import numpy as np
import matplotlib.pyplot as plt

def estimate_coef(x, y):
# number of observations/points
n = np.size(x)

# mean of x and y vector
m_x = np.mean(x)
m_y = np.mean(y)

# calculating cross-deviation and deviation about x
SS_xy = np.sum(y * x) - n * m_y * m_x
SS_xx = np.sum(x * x) - n * m_x * m_x

# calculating regression coefficients
b_1 = SS_xy / SS_xx
b_0 = m_y - b_1 * m_x

return (b_0, b_1)

def plot_regression_line(x, y, b):
# plotting the actual points as scatter plot
plt.scatter(x, y, color="m", marker="o", s=30)

# predicted response vector
y_pred = b[0] + b[1] * x

# plotting the regression line
plt.plot(x, y_pred, color="g")

# putting labels
plt.xlabel('x')
plt.ylabel('y')

# function to show plot
plt.show()
def main():
# observations / data
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])

# estimating coefficients
b = estimate_coef(x, y)
print("Estimated coefficients:nb_0 = {}
nb_1 = {}".format(b[0], b[1]))

# plotting regression line
plot_regression_line(x, y, b)

if __name__ == "__main__":
main()

AIM: Animal or leaf classification using CNN.

CODE: Leaf Classification

1) Importing libraries

from keras.models import Sequential

from keras.layers import Convolution2D
from keras.layers import MaxPool2D
from keras.layers import BatchNormalization
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers import Dense

from keras.preprocessing.image import ImageDataGenerator

from keras.preprocessing import image

import numpy as np
import pandas as pd

# Visualization
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.model_selection import train_test_split
from tqdm import tqdm

from keras.utils.vis_utils import plot_model

import time

import warnings
warnings.filterwarnings("ignore")
model = Sequential()
2) Build CNN

# 1st hidden layer
model.add(Convolution2D(filters=16, kernel_size=(3,3), activation='relu', input_shape=(128,128,3)))
model.add(BatchNormalization())
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.2))

model.add(Convolution2D(filters=32, kernel_size=(3,3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.3))

model.add(Convolution2D(filters=64, kernel_size=(3,3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.4))

# Till here O/P in matrix form

model.add(Flatten())

model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.3))

model.add(Dense(1, activation='sigmoid'))

3) Mounting drive
from google.colab import drive
drive.mount('/content/drive')

4) Image Preparation

train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2,
horizontal_flip=True)
valid_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory('train', target_size=(128,128), batch_size=32, class_mode='binary')
valid_set = valid_datagen.flow_from_directory('valid', target_size=(128,128),
batch_size=32, class_mode='binary')
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

7) Model Fitting


8) Save and Load the Model

model.save('Two_plants_model.h5')
from tensorflow.keras.models import load_model
model = load_model('model_03062020.h5')

9) Prediction on Test Data
test_images = ImageDataGenerator(rescale=1./255)
test_check = test_images.flow_from_directory(r'C:Neural NetworksPlantstest', target_size=(128,128), batch_size=32, class_mode='binary', shuffle=False)
predictions_proba = model.predict(test_check)
predictions_proba
test_check_classes = test_check.classes
test_check_classes
predictions = predictions_proba.copy()
for i in range(len(predictions_proba)):
if predictions[i][0] > 0.5:
predictions[i][0] = 1
else:
predictions[i][0] = 0
keys = list(test_check.class_indices.keys())
keys
predictions = predictions.flatten()
predictions
test_pred_df = pd.DataFrame({'True Labels': test_check_classes, 'Predicted Labels': predictions})
test_pred_df['Result'] = ''
for i in range(len(test_pred_df)):
if(test_pred_df['True Labels'].iloc[i] == test_pred_df['Predicted Labels'].iloc[i]):
test_pred_df['Result'].iloc[i] = 'Correct'
else:
test_pred_df['Result'].iloc[i] = 'Misclassified'
test_pred_df['Result'].value_counts()
from sklearn.metrics import classification_report
print(classification_report(test_check_classes, predictions, target_names=keys))

10) Generate a classification Report on Val

test_datagen = ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory('valid',
target_size=(128,128),
batch_size=32,
class_mode='binary',
shuffle=False)

test_set_classes = test_set.classes
test_set_classes
keys = list(test_set.class_indices.keys())
keys

predictions_proba = model.predict(test_set)
predictions = predictions_proba.copy()
for i in range(len(predictions_proba)):
if predictions[i][0] > 0.5:
predictions[i][0] = 1
else:
predictions[i][0] = 0
predictions = predictions.flatten()
test_pred_df = pd.DataFrame({'True Labels': test_set_classes, 'Predicted Labels': predictions})
test_pred_df['Result'] = ''
for i in range(len(test_pred_df)):
if(test_pred_df['True Labels'].iloc[i] == test_pred_df['Predicted Labels'].iloc[i]):
test_pred_df['Result'].iloc[i] = 'Correct'
else:
test_pred_df['Result'].iloc[i] = 'Misclassified'


test_pred_df['Result'].value_counts()


from sklearn.metrics import classification_report
print(classification_report(test_set_classes, predictions, target_names=keys))
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.