MLP Unit-I
MLP Unit-I
W I T H P Y T H O N
UNIT-I
INTRODUCTION TO
MACHINE LEARNING
WITH PYTHON
ARTIFICIAL INTELLIGENCE
MACHINE LEARNING
DEEP LEARNING
ML - Basic Terminology
Machine Learning Relationships
• Machine learning systems uses Relationships between Inputs to
produce Predictions.
y = ax + b y = b + wx
ML - Basic Terminology
Machine Learning Features
• In Machine Learning terminology, the features are the input.
• They are like the x values in a linear graph:
• For each good action, they get a positive reward, and for each bad
action, they get a negative reward. The goal of a Reinforcement
learning agent is to maximize the positive rewards. Since there is no
labeled data, the agent is bound to learn by its experience only.
• --An example of reinforcement learning is teaching a computer
program to play a video game. The program learns by trying different
actions, receiving points for good moves and losing points for
mistakes.
• --RL can help cars navigate complex environments, making self-driving
technology safer and more reliable.
• --Traffic signal control
• RL can be used to control traffic signals in complex urban networks.
ML - Types
4. Semi-supervised Learning
• Sentiment analysis
• Speech synthesis
Applications of Machine Learning
• Speech recognition
• Customer segmentation
• Object recognition
• Fraud detection
• Fraud prevention
In[13]:
print("Target names: {}".format(iris_dataset['target_names']))
• The value of feature_names is a list of strings, giving the description
of each feature:
In[14]:
print("Feature names:
\n{}".format(iris_dataset['feature_names']))
• The data itself is contained in the target and data fields. data contains
the numeric measurements of sepal length, sepal width, petal length,
and petal width in a NumPy array:
In[15]:
print("Type of data: {}".format(type(iris_dataset['data'])))
• The rows in the data array correspond to flowers, while the columns
represent the four measurements that were taken for each flower:
In[16]:
print("Shape of data: {}".format(iris_dataset['data'].shape))
• We see that the array contains measurements for 150 different flowers.
Remember that the individual items are called samples in machine
learning, and their properties are called features. Here are the feature
values for the first five samples:
In[17]:
print("First five columns of
data:\n{}".format(iris_dataset['data'][:5]))
• The target array contains the species of each of the flowers that were
measured, also as a NumPy array:
In[18]:
print("Type of target: {}".format(type(iris_dataset['target'])))
In[19]:
print("Shape of target: {}".format(iris_dataset['target'].shape))
• The species are encoded as integers from 0 to 2:
In[20]:
print("Target:\n{}".format(iris_dataset['target']))
• In[21]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
iris_dataset['data'], iris_dataset['target'], random_state=0)
• The output of the train_test_split function is X_train, X_test, y_train, and
y_test, which are all NumPy arrays. X_train contains 75% of the rows of the
dataset, and X_test contains the remaining 25%:
In[22]:
print("X_train shape: {}".format(X_train.shape))
print("y_train shape: {}".format(y_train.shape))
In[23]:
print("X_test shape: {}".format(X_test.shape))
print("y_test shape: {}".format(y_test.shape))
Look at Your Data
In[24]:
# create dataframe from data in X_train
# label the columns using the strings in iris_dataset.feature_names
iris_dataframe = pd.DataFrame(X_train,
columns=iris_dataset.feature_names)
# create a scatter matrix from the dataframe, color by y_train
grr = pd.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15),
marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)
Building the Model: k-Nearest Neighbors
In[25]:
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=1)
In[26]:
knn.fit(X_train, y_train)
Making Predictions
In[27]:
X_new = np.array([[5, 2.9, 1, 0.2]])
print("X_new.shape: {}".format(X_new.shape))
Out[27]:
X_new.shape: (1, 4)
In[28]:
prediction = knn.predict(X_new)
print("Prediction: {}".format(prediction))
print("Predicted target name: {}".format(
iris_dataset['target_names'][prediction]))
Out[28]:
Prediction: [0]
Predicted target name: ['setosa']
Evaluating the Model
In[29]:
y_pred = knn.predict(X_test)
print("Test set predictions:\n {}".format(y_pred))
Out[29]:
Test set predictions:
[2 1 0 2 0 2 0 1 1 1 2 1 1 1 1 0 1 1 0 0 2 1 0 0 2 0 0 1 1 0 2 1 0 2 2 1 0 2]
In[30]:
print("Test set score: {:.2f}".format(np.mean(y_pred == y_test)))
Out[30]:
Test set score: 0.97