Wednesday, September 2, 2020

10 ways to iterate through a list in python

10 ways to iterate through a list in python

The list is similar to array in other languages except for python, which provides the extra benefit of being dynamic in size nature. In Python, the list is a type of  Data Structures, which is used to store different and multiple kinds of data at the same time.
10 ways to iterate through a list in python
10 ways to iterate through a list in python

There are many ways to iterate over a list in Python language. Let’s see all different ways in python:

  1.  Using For loop in python
  2.  For loop and range() in python
  3.  Using while loop in python
  4.  Using list comprehension  ways in python
  5.  Using enumerate() in python
  6.  Using Numpy in python
  7. Using iterable without index in python
  8. Using general way via index
  9. Using the enumerate type function
  10. Using negative indexes in python

Sunday, August 30, 2020

how to create two dimensional array in python language

How to create a two-dimensional array in python language:

Welcome everyone, Today we will learn how to create a two-dimensional array in python so let's start:
How do you create a two-dimensional array in Python language?
how to create two dimensional array in python
how to create a two-dimensional array in python


There are different ways to create Numpy arrays in python:

  • Using special library functions
  • Using Numpy functions
  • Conversion from other Python structures like lists
we know that Python provides many ways to create 1-dimensional,2-dimensional lists/arrays.  so let's start to create a 1D array of size N initialized with 1s.
learn Matplotlib for Data Visualization 

Thursday, August 27, 2020

k means clustering algorithm python example

k means clustering algorithm python example

K Means Clustering is unsupervised learning algorithm in python (i.e.it's tries to cluster the different data based on their similarity) and another meaning is that there is no outcome to be predicted data. K Means Clustering algorithm just tries to find patterns in the data.
There are 3 steps for K Means Clustering with Python:
  • # step1: Initialisation – K initial means centroid in python are generated at random
  • # step2: Assignment – K clusters are created by observation with the nearest centroids data
  • # step3: Update – It's becomes the new mean
wan to learn learn Logistic regression then click on it.

 Initialisation – K initial means centroide in python are generated at random

Import Libraries
import seaborn as ns
import matplotlib.pyplot as plt
%matplotlib inline
Create some Data

from sklearn.datasets import make_blobs

Assignment – K clusters are created by observation with the nearest centroides data


data = make_blobs(n_samples=190, n_features=2, 
                           centers=4, cluster_std=1.7,random_state=100)
Visualize Data
plt.scatter(data[0][:,0],data[0][:,1],c=data[1],cmap='rainbow')
<matplotlib.collections.PathCollection at 0x11ab34d30>
k means clustering algorithm python example
k means clustering algorithm python example


from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=4)
kmeans.fit(data[0])
KMeans(copy_x=True, init='k-means++', max_iter=200, n_clusters=4, n_init=9,
    n_jobs=1, precompute_distances='auto', random_state=None, tol=0.001,
    verbose=0)

Update – It's becomes the new mean


kmeans.cluster_centers_
array([[-3.13591321,  6.95389851],
       [-7.46941837, -5.56081545],
       [-1.0123077 ,  3.13407664],
       [ 2.71749226,  6.01388735]])
kmeans.labels_
array([1, 3, 1, 3, 3, 0, 2, 1, 3, 1, 2, 1, 3, 3, 2, 1, 3, 1, 0, 2, 2, 1, 1,
       1, 1, 0, 0, 1, 3, 3, 2, 0, 3, 1, 1, 2, 0, 1, 0, 1, 0, 2, 2, 1, 1, 1,
       1, 1, 0, 1, 1, 2, 3, 1, 0, 2, 1, 1, 2, 3, 0, 3, 0, 2, 3, 1, 0, 3, 1,
       3, 3, 1, 0, 1, 0, 3, 3, 1, 2, 1, 1, 0, 3, 0, 1, 1, 1, 2, 1, 0, 0, 1,
       3, 1, 1, 0, 3, 2, 0, 3, 1, 0, 1, 1, 3, 1, 0, 3, 0, 0, 3, 2, 2, 3, 2,
       2, 2, 2, 3, 2, 1, 2, 1, 2, 1, 3, 2, 1, 0, 2, 2, 2, 1, 0, 0, 2, 3, 2,
       2, 1, 0, 3, 0, 2, 2, 3, 1, 0, 2, 2, 2, 2, 1, 3, 1, 2, 3, 3, 3, 1, 1,
       2, 1, 2, 0, 2, 1, 3, 2, 1, 3, 1, 2, 3, 1, 2, 3, 3, 0, 3, 2, 0, 0, 1,
       1, 0, 3, 0, 0, 1, 3, 3, 3, 2, 0, 1, 3, 3, 0, 3], dtype=int32)

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True,figsize=(11,7))
ax1.set_title('K Means cluster')
ax1.scatter(data[0][:,0],data[0][:,1],c=kmeans.labels_,cmap='rainbow')
ax2.set_title("Original/main")
ax2.scatter(data[0][:,0],data[0][:,1],c=data[1],cmap='rainbow')


<matplotlib.collections.PathCollection at 0x11da01be0>
k means clustering  examples
k means clustering examples
Summary:
In this section we learn the k means clustering algorithm python example in detailed, about this section if you have any problem then please comment me.
Tags:
k means clustering algorithm python example, python, machine learning
If you want to learn sector support machine algorithms then click on it.
BEST OF LUCK!!!

Decision Trees and Random Forests classifier-Types in Python

Decision Trees and Random Forests classifier in Python

Welcome everyone, Today we will see Decision Trees and Random Forests classifier-and Types in Python so let's start:
In this project following steps are used to performed operation:
  • Import algorithm Decision Trees and Random Forests classifier package.
  • Get the data
  • Split data into x/y_training and x/y_test data.
  • Train or fit the data into the different model methods.
  • Prediction and Evaluation the data
  • Decision Trees visualization
  • Random Forests
  • finally generate the Tree (learn different python terminologies)

Import algorithm Decision Trees and Random Forests classifier package

Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

Get the Data
df = pd.read_csv('Decision Trees.csv')

df.head()
Decision Age Number Start
0 present 34 3 9
1 absent 58 4 15
2 absent 28 5 8
3 present 72 3 4
4 absent 81 4 15

Split data into x/y_training and x/y_test data.

Let's start to split up the data into a training and test set.
from sklearn.model_selection import train_test_split
x = df.drop('Decision Trees',axis=1)
y = df['Decision Trees']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20)

Wednesday, August 26, 2020

k nearest neighbor python numpy language

k nearest neighbor python numpy language:

Welcome everyone in python crash course (Machine learning). This is first part of this section,if you want to learn SVM in python then click on it.
 K Nearest Neighbors method also used for data prediction purpose, so in his section we will learn  K Nearest Neighbors predict method.
k nearest neighbor python numpy language
k nearest neighbor python numpy language

How do you use K nearest neighbor in Python language in smart way?

In this project following steps are used to performed operation:
  • Import k-nearest neighbor algorithm package.
  • Then Create feature and target variables using function.
  • Split data into x/y_training and x/y_test data.
  • Generate a k-NN value model using neighbor method.
  • Train or fit the data into the different model methods.
  • finally Predict the future data

What is K nearest neighbor used for?

suppose you have been given a classified data set from a any popular company,they give you the data and the target classes and say predicts a class for a new data point based off of the features.
Let's do it!

Featured Snippet

10 ways to iterate through a list in python

10 ways to iterate through a list in python The list is similar to array in other languages except for python, which provides the extra b...