Menu

Machine Learning for Data Analysts

Machine Learning for Data Analysts

Machine learning lets you predict outcomes from data. This topic gives analysts a practical start using scikit-learn, without heavy theory.

a. Machine Learning Basics for Analysts

Machine learning teaches a computer to find patterns in data and use them to make predictions. Instead of writing rules by hand, you show the model many examples, and it learns the rules itself.

There are two main types you will use. Supervised learning uses data with known answers, like past house prices, to predict new ones. Unsupervised learning finds hidden groups without known answers, like customer segments.

Supervised problems are split into regression and classification. Regression predicts a number, like price. Classification predicts a category, like yes or no. This section focuses on these two.

b. Preparing Data for scikit-learn

Scikit-learn is the most popular Python library for machine learning. Before using it, your data must be clean, numeric, and split properly. First, separate the inputs, called features, from the target you want to predict.

X = df[["area", "bedrooms", "age"]]
y = df["price"]

Next, split the data into a training set and a test set. The model learns from the training set. You then check it on the test set, which it has never seen.

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)

The random_state makes the split repeatable. Handle missing values and encode categories before training, since most models cannot accept them.

c. Building a Regression Model

Linear regression is a good first model. It finds the best straight-line relationship between the features and a number you want to predict. It is simple and easy to explain.

from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)

The fit() method trains the model, and predict() makes predictions on new data. You can look at model.coef_ to see how much each feature affects the result.

Always start with a simple model. If it performs well enough, there is no need for something more complex. Simple models are easier to understand and explain to others.

d. Building a Classification Model

Logistic regression is a common starting point for classification. Despite its name, it predicts categories, such as whether a customer will leave or stay.

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(max_iter=1000)
clf.fit(X_train, y_train)
predictions = clf.predict(X_test)

You can also use predict_proba() to get the probability of each class. This is helpful when you want to rank customers by risk instead of just labeling them.

Other popular models include decision trees and random forests. They use the same fit() and predict() steps, so you can try different models easily and compare results.

e. Evaluating Model Performance

A model is only useful if it performs well on new data. Always evaluate it on the test set, not the training set. For regression, common measures are mean absolute error and R-squared.

from sklearn.metrics import mean_absolute_error, r2_score
print(mean_absolute_error(y_test, predictions))
print(r2_score(y_test, predictions))

For classification, start with accuracy, then look deeper with a confusion matrix and a classification report. Accuracy alone can mislead when one class is much larger than the other.

from sklearn.metrics import accuracy_score, classification_report
print(accuracy_score(y_test, predictions))
print(classification_report(y_test, predictions))

If the model does well on training data but poorly on test data, it is overfitting. This means it memorized the data instead of learning real patterns. Simpler models and more data often help.


Data Analysis with Python Handbook

WR

Wesline Rinisha



Learn via Video Course

Data Analysis and Processing with Python using Pandas - Data Science Course Logo

Data Analysis and Processing with Python using Pandas - Data Science Course

5 hrs



Get in Touch For Details