# Scikit-learn Vs Tensorflow – Detailed Comparison

In this Python tutorial, we will learn about scikit-learn vs Tensorflow and also cover different examples related to scikit-learn and TensorFlow in Python. And, we will cover these topics.

• How scikit-learn works
• How TensorFlow works
• Scikit-learn Vs Tensorflow

## How scikit-learn works

In this section, we will learn about the working of scikit-learn by using its scikit-library in python.

• Scikit learn is a user-friendly, open-source, and free library for python.
• The user can use this library freely and that can simplify the task of coding applying in python from this it helps the corder.
• The scikit learn is written in python and built upon Scipy, Numpy, and Matplotlib. It just focuses on modeling the data not to focused on loading or manipulating the data.

Installation of scikit learn

``pip install scikit-learn``

Syntax:

``from sklearn import datasets``

Examples of scikit-learn:

In this example, we will learn about how scikit learn library work and how it focuses on the modeling data.

• plt.figure(figsize=(16, 16)) is used to plot the figure on the screen.
• X, y = make_blobs(n_samples=n_samples, random_state=random_state) issued to make blobs that can store binary data.
• y_pred = KMeans(n_clusters=4, random_state=random_state).fit_predict(X) is used to predic incorrect number of cluster.
• plt.subplot(224) is used for plotting the subplots.
• X_varied, y_varied = make_blobs( n_samples=n_samples, cluster_std=[1.0, 2.5, 0.5], random_state=random_state ) is used make different variance.
• X_filtered = np.vstack((X[y == 0][:500], X[y == 1][:100], X[y == 2][:10])) is used make unevenly sized blocks.
``````import numpy as np
import matplotlib.pyplot as plt

from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs

plt.figure(figsize=(16, 16))

n_samples = 1800
random_state = 200
X, y = make_blobs(n_samples=n_samples, random_state=random_state)

y_pred = KMeans(n_clusters=4, random_state=random_state).fit_predict(X)

plt.subplot(224)
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
plt.title(" Total incorrect number of blobs")

transformations = [[0.60834550, -0.63667344], [-0.40887720, 0.85253230]]
X_aniso = np.dot(X, transformation)
y_pred = KMeans(n_clusters=5, random_state=random_state).fit_predict(X_aniso)

plt.subplot(222)
plt.scatter(X_aniso[:, 0], X_aniso[:, 1], c=y_pred)
plt.title("Total nnumber of Anisotropicly Distributed Blobs")

X_varied, y_varied = make_blobs(
n_samples=n_samples, cluster_std=[1.0, 2.5, 0.5], random_state=random_state
)
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_varied)

plt.subplot(223)
plt.scatter(X_varied[:, 0], X_varied[:, 1], c=y_pred)
plt.title("Total number of Unequal Variance")

X_filtered = np.vstack((X[y == 0][:500], X[y == 1][:100], X[y == 2][:10]))
y_pred = KMeans(n_clusters=3, random_state=random_state).fit_predict(X_filtered)

plt.subplot(224)
plt.scatter(X_filtered[:, 0], X_filtered[:, 1], c=y_pred)
plt.title("Total number of Unevenly Sized Blobs")

plt.show()``````

Output:

After running the above code, we get the following output in which we can see that the different K-Mean clustering is drawn on the screen with the help of the scikit-learn library.

Also, check: Scikit-learn logistic regression

## How Tenserflow works

In this section, we will learn about the working of Tensorflow by using its TensorFlow library in python.

• TensorFlow is a library that was designed by the Google team which make the works easier for the corder.
• It can calculate the mathematical expression easily and simply.
• Tensorflow involves programming support of deep learning and machine learning techniques.
• Tensorflow can train the deep neural network for handwritten digit classification and create various sequence models.
• Tensorflow has a unique feature of enhancing the same memory and data use.

Installation of Tensorflow:

``pip install tensorflow``

Syntax to import Tensorflow:

``import tensorflow as tf``

Example of Tensorflow:

In the following example, we will learn about the accuracy of the model through TensorFlow.

• As we know it combines the algebra of optimization technique makes the calculation easy of any mathematical calculation.
• models = tf.keras.models.Sequential() is used to check the accuracy of model.
• models.compile() is used for compilation of model.
• models.fit(x_train, y_train, epochs=8) is used to fit the model.
• models.evaluate(x_test, y_test) is used to evaluate the whole model.
``````import tensorflow as tf
mnist = tf.keras.datasets.mnist

x_train, x_test = x_train / 260.0, x_test / 260.0

models = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])

loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

models.fit(x_train, y_train, epochs=8)
models.evaluate(x_test, y_test)``````

Output:

After running the above code we get the following output in which we can see that through TensorFlow we can calculate mathematical calculations easier and also get accurate data.