Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Pejman Rezaei
Pejman Rezaei

Posted on

     

Getting Started with Python for Machine Learning

Python has become the go-to programming language for Machine Learning (ML) thanks to its simplicity, versatility, and the vast ecosystem of libraries it offers. If you’re new to ML and want to get started with Python, this guide will walk you through the basics, introduce you to essential libraries, and show you how to build a simple ML model.


Why Python for Machine Learning?

Python is widely used in the ML community because:

  • It’s easy to learn and read, even for beginners.

  • It has a rich set of libraries for data manipulation, visualization, and ML.

  • It’s supported by a large and active community.

Whether you’re analyzing data, training models, or deploying ML solutions, Python has the tools to make your life easier.


Essential Python Libraries for Machine Learning

Before diving into ML, let’s take a look at some of the most important Python libraries you’ll need:

NumPy:
NumPy (Numerical Python) is the foundation for numerical computing in Python. It provides support for arrays, matrices, and mathematical functions.

  • Use it for: Basic numerical operations, linear algebra, and array manipulation.

Pandas:
Pandas is a powerful library for data manipulation and analysis. It introduces data structures like DataFrames, which make it easy to work with structured data.

  • Use it for: Loading, cleaning, and exploring datasets.

Scikit-learn:
Scikit-learn is the most popular library for ML in Python. It provides simple and efficient tools for data mining and analysis, including algorithms for classification, regression, clustering, and more.

  • Use it for: Building and evaluating ML models.

Setting Up Your Environment

To get started, you’ll need to install these libraries. If you haven’t already, you can install them using pip:

pipinstallnumpy pandas scikit-learn
Enter fullscreen modeExit fullscreen mode

Once installed, you’re ready to start coding!


A Simple Machine Learning Workflow

Let’s walk through a basic ML workflow using Python. We’ll use the famous Iris dataset, which contains information about different species of iris flowers. Our goal is to build a model that can classify the species based on features like petal length and width.

Step 1: Import Libraries

First, import the necessary libraries:

importnumpyasnpimportpandasaspdfromsklearn.datasetsimportload_irisfromsklearn.model_selectionimporttrain_test_splitfromsklearn.ensembleimportRandomForestClassifierfromsklearn.metricsimportaccuracy_score
Enter fullscreen modeExit fullscreen mode

Step 2: Load the Dataset

Scikit-learn provides built-in datasets, including the Iris dataset. Let’s load it:

# Load the Iris datasetiris=load_iris()# Convert it to a Pandas DataFrame for easier manipulationdata=pd.DataFrame(iris.data,columns=iris.feature_names)data['species']=iris.target
Enter fullscreen modeExit fullscreen mode

Step 3: Explore the Data

Before building a model, it’s important to understand the data:

# Display the first few rowsprint(data.head())# Check for missing valuesprint(data.isnull().sum())# Get basic statisticsprint(data.describe())
Enter fullscreen modeExit fullscreen mode

Step 4: Prepare the Data

Split the data into features (X) and labels (y), and then split it into training and testing sets:

# Features (X) and labels (y)X=data.drop('species',axis=1)y=data['species']# Split the data into training and testing setsX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)
Enter fullscreen modeExit fullscreen mode

Step 5: Train a Model

Let’s use a Random Forest classifier, a popular ML algorithm:

# Initialize the modelmodel=RandomForestClassifier(random_state=42)# Train the modelmodel.fit(X_train,y_train)
Enter fullscreen modeExit fullscreen mode

Step 6: Make Predictions and Evaluate the Model

Use the trained model to make predictions on the test set and evaluate its accuracy:

# Make predictionsy_pred=model.predict(X_test)# Evaluate the modelaccuracy=accuracy_score(y_test,y_pred)print(f"Model Accuracy:{accuracy*100:.2f}%")
Enter fullscreen modeExit fullscreen mode

Congratulations! You’ve just built your first ML model using Python. Here are some next steps to continue your learning journey:

  • Experiment with other datasets from Kaggle or the UCI Machine Learning Repository.

  • Explore different ML algorithms like linear regression, decision trees, or support vector machines.

  • Learn about data preprocessing techniques like scaling, encoding, and feature selection.


Resources to Learn More

If you’re interested in diving deeper, here are some great resources:

  • Scikit-learn Documentation: The official guide to using Scikit-learn.

  • Kaggle Learn: Hands-on tutorials for ML beginners.

  • Python Machine Learning by Sebastian Raschka: A beginner-friendly book on ML with Python.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Tech Enthusiast
  • Work
    MLOps Engineer
  • Joined

More fromPejman Rezaei

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp