Definition and Types of Machine Learning
Conepts Machine Learning:
- What is Machine Learning?
- Types of Machine Learning (Supervised, Unsupervised, Reinforcement)
- Regression (Linear, Logistic)
- Decision Trees and Random Forests
- Neural Networks (Perceptron, MLP, CNN, RNN)
What is Machine Learning
Machine learning is a subfield of artificial intelligence that involves building systems that can learn from data and make predictions or decisions based on that data. In other words, instead of explicitly programming a system to perform a task, we give it data and let it learn how to perform the task on its own.
Types of Machine Learning:
Supervised Learning
In supervised learning, the system is given labeled training data and learns to make predictions or decisions based on that data.
Unsupervised Learning
Regression:
Linear Regression:
Machine Learning:
y = b0 + b1x1 + b2x2 + ... + bn*xn
where y is the dependent variable, x1, x2, ..., xn are the independent variables, and b0, b1, b2, ..., bn are the regression coefficients.
Example code for linear regression:
python code
import pandas as pd
from sklearn.linear_model import LinearRegression
# Load data
data = pd.read_csv('data.csv')
# Define dependent variable and independent variables
Y = data['Sales']
X = data[['TV', 'Radio', 'Newspaper']]
# Fit the model
model = LinearRegression().fit(X, Y)
# Print coefficients
print(model.coef_)
print(model.intercept_)
Logistic Regression:
Example code for logistic regression:
Python code
import pandas as pd
from sklearn.linear_model import LogisticRegression
# Load data
data = pd.read_csv('data.csv')
# Define dependent variable and independent variables
Y = data['AdClicked']
X = data[['Age', 'Income', 'Gender']]
# Fit the model
model = LogisticRegression().fit(X, Y)
# Print coefficients
print(model.coef_)
print(model.intercept_)
Decision Trees and Random Forests:
Decision Trees are a type of supervised learning algorithm used for classification and regression analysis. The algorithm divides the data into smaller subsets based on the values of certain features, and recursively splits the subsets to form a tree-like structure. Random Forest is an ensemble learning method that uses multiple decision trees and combines their predictions to improve the accuracy of the model.
Example code for Decision Trees:
python code
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
Load the iris dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target
Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Define the decision tree classifier
clf = DecisionTreeClassifier(random_state=42)
Fit the model on the training data
clf.fit(X_train, y_train)
Predict the classes of the test set
y_pred = clf.predict(X_test)
Calculate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
Example code for random forests:
python code
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
# Load data
data = pd.read_csv('data.csv')
# Define dependent variable and independent variables
Y = data['AdClicked']
X = data[['Age', 'Income', 'Gender']]
# Fit the model
model = RandomForestClassifier().fit(X, Y)
# Print feature importances
print(model.feature_importances_)
Example code for Random Forest:
python code
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
Define the random forest classifier
clf = RandomForestClassifier(n_estimators=100, random_state=42)
Fit the model on the training data
clf.fit(X_train, y_train)
Predict the classes of the test set
y_pred = clf.predict(X_test)
Calculate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
Neural Networks:
Example code for MLP:
Python code
from sklearn.neural_network import MLPClassifier
Define the MLP classifier
clf = MLPClassifier(hidden_layer_sizes=(100,), activation='relu', solver='adam', max_iter=1000, random_state=42)
Fit the model on the training data
clf.fit(X_train, y_train)
Predict the classes of the test set
y_pred = clf.predict(X_test)
Calculate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
Example code for CNN:
python code
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
Define the CNN model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Fit the model on the training data
model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test))
Evaluate the model on the test data
accuracy = model.evaluate(X_test, y_test)[1]
print("Accuracy:", accuracy)
Note: The above
code for CNN assumes that the input data is in the form of images, hence the
use of Conv2D and MaxPooling2D layers.
Example code for MLP:
.python codeimport pandas as pd
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load data
data = pd.read_csv('data.csv')
# Define dependent variable and independent variables
Y = data['Class']
X = data.drop(['Class'], axis=1)
# Split data into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)
# Define and fit the model
model = MLPClassifier(hidden_layer_sizes=(10, 5), activation='relu', solver='adam', max_iter=500)
model.fit(X_train, Y_train)
# Make predictions on the testing set
Y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(Y_test, Y_pred)
print('Accuracy:', accuracy)
Note: The above code for CNN assumes that the input data is in the form of images, hence the use of Conv2D and MaxPooling2D layers.
In this example, we load data from a CSV file and split it into training and testing sets. We define the dependent variable and independent variables, and then create an MLPClassifier object with two hidden layers containing 10 and 5 neurons, respectively. We use the 'relu' activation function and the 'adam' solver, and set the maximum number of iterations to 500. We fit the model on the training data, make predictions on the testing data, and evaluate the model using accuracy as the performance metric.
In depth if you want to know about Machine Learning, then move to blog Machine Learning, topic wise material with Python Code
To Main (Topics of Data Science)
Continue to (Data Preparation and Feature Engineering)
Comments
Post a Comment
Requesting you please share your opinion about my content in this blog for further development in a better way. Thank you. Dr.Srinivas