Python One-Liners

Logistic Regression in Python Scikit-Learn

Logistic regression is a popular algorithm for classification problems (despite its name indicating that it is a “regression” algorithm). It belongs to one of the most important algorithms in the machine learning space. Linear Regression Background Let’s review linear regression. Given the training data, we compute a line that fits this training data so that …

Logistic Regression in Python Scikit-Learn Read More »

SVM sklearn: Python Support Vector Machines Made Simple

Support Vector Machines (SVM) have gained huge popularity in recent years. The reason is their robust classification performance – even in high-dimensional spaces: SVMs even work if there are more dimensions (features) than data items. This is unusual for classification algorithms because of the curse of dimensionality – with increasing dimensionality, data becomes extremely sparse …

SVM sklearn: Python Support Vector Machines Made Simple Read More »

K-Nearest Neighbors (KNN) with sklearn in Python

The popular K-Nearest Neighbors (KNN) algorithm is used for regression and classification in many applications such as recommender systems, image classification, and financial data forecasting. It is the basis of many advanced machine learning techniques (e.g., in information retrieval). There is no doubt that understanding KNN is an important building block of your proficient computer …

K-Nearest Neighbors (KNN) with sklearn in Python Read More »

Machine Learning Linear Regression Python

Python Linear Regression with sklearn – A Helpful Illustrated Guide

💡 This tutorial will show you the most simple and straightforward way to implement linear regression in Python—by using scikit-learn’s linear regression functionality. I have written this tutorial as part of my book Python One-Liners where I present how expert coders accomplish a lot in a little bit of code. Feel free to bookmark and download …

Python Linear Regression with sklearn – A Helpful Illustrated Guide Read More »

How to Convert a String List to an Integer List in Python

The most Pythonic way to convert a list of strings to a list of ints is to use the list comprehension [int(x) for x in strings]. It iterates over all elements in the list and converts each list element x to an integer value using the int(x) built-in function. This article shows you the simplest …

How to Convert a String List to an Integer List in Python Read More »

How to Convert a String List to a Float List in Python

The most Pythonic way to convert a list of strings to a list of floats is to use the list comprehension floats = [float(x) for x in strings]. It iterates over all elements in the list and converts each list element x to a float value using the float(x) built-in function. This article shows you …

How to Convert a String List to a Float List in Python Read More »

How to Convert an Integer List to a Float List in Python

The most Pythonic way to convert a list of integers ints to a list of floats is to use the list comprehension expression floats = [float(x) for x in ints]. It iterates over all elements in the list ints using list comprehension and converts each list element x to a float value using the float(x) …

How to Convert an Integer List to a Float List in Python Read More »

How to Convert a Float List to a String List in Python

The most Pythonic way to convert a list of floats fs to a list of strings is to use the one-liner fs = [str(x) for x in fs]. It iterates over all elements in the list fs using list comprehension and converts each list element x to a string value using the str(x) constructor. This …

How to Convert a Float List to a String List in Python Read More »

How to Convert an Integer List to a String List in Python

The most Pythonic way to convert a list of integers ints to a list of strings is to use the one-liner strings = [str(x) for x in ints]. It iterates over all elements in the list ints using list comprehension and converts each list element x to a string value using the str(x) constructor. This …

How to Convert an Integer List to a String List in Python Read More »

How to Get an HTML Page from a URL in Python?

This tutorial shows you how to perform simple HTTP get requests to get an HTML page from a given URL in Python! Problem Formulation Given a URL as a string. How to extract the HTML from the given URL and store the result in a Python string variable? Example: Say, you want to accomplish the …

How to Get an HTML Page from a URL in Python? Read More »

How to Replace One or More List Elements at Specific Indices in Python?

Problem Formulation: Replacing One Element Given List lst Element x Index i How to replace the element at index i in the list lst with the new element x? Solution Indexing You use simple indexing using the square bracket notation lst[i] = x to replace the element at index i in list lst with the …

How to Replace One or More List Elements at Specific Indices in Python? Read More »