Day: May 22, 2021

Machine Learning for Beginners in Python: How to Encode Days Of The Week

Encode Days Of The Week Preliminaries import pandas as pd Create Date And Time Data dates = pd.Series(pd.date_range(‘2/2/2002′, periods=3, freq=’M’)) dates 0 2002-02-28 1 2002-03-31 2 2002-04-30 dtype: datetime64[ns] Show Days Of The Week dates.dt.weekday_name 0 Thursday 1 Sunday 2 Tuesday dtype: object   Python Example for Beginners Special 95% discount 2000+ Applied Machine Learning …

Machine Learning for Beginners in Python: How to Convert pandas Columns Time Zone

Convert pandas Columns Time Zone Preliminaries import pandas as pd from pytz import all_timezones View Timezones all_timezones[0:10] [‘Africa/Abidjan’, ‘Africa/Accra’, ‘Africa/Addis_Ababa’, ‘Africa/Algiers’, ‘Africa/Asmara’, ‘Africa/Asmera’, ‘Africa/Bamako’, ‘Africa/Bangui’, ‘Africa/Banjul’, ‘Africa/Bissau’] Create pandas Series Of Dates dates = pd.Series(pd.date_range(‘2/2/2002′, periods=10, freq=’M’)) Add Time Zone Of pandas Series dates_with_abidjan_time_zone = dates.dt.tz_localize(‘Africa/Abidjan’) dates_with_abidjan_time_zone 0 2002-02-28 00:00:00+00:00 1 2002-03-31 00:00:00+00:00 2 2002-04-30 …

Machine Learning for Beginners in Python: How to Calculate Difference Between Dates And Times

Calculate Difference Between Dates And Times Preliminaries import pandas as pd Create Date And Time Data df = pd.DataFrame() df[‘Arrived’] = [pd.Timestamp(’01-01-2017′), pd.Timestamp(’01-04-2017′)] df[‘Left’] = [pd.Timestamp(’01-01-2017′), pd.Timestamp(’01-06-2017′)] Calculate Difference (Method 1) df[‘Left’] – df[‘Arrived’] 0 0 days 1 2 days dtype: timedelta64[ns] Calculate Difference (Method 2) pd.Series(delta.days for delta in (df[‘Left’] – df[‘Arrived’])) 0 0 …

Machine Learning for Beginners in Python: How to Break Up Dates And Times Into Multiple Features

Break Up Dates And Times Into Multiple Features Preliminaries import pandas as pd Create Date And Time Data df = pd.DataFrame() df[‘date’] = pd.date_range(‘1/1/2001′, periods=150, freq=’W’) Break Up Dates And Times Into Individual Features df[‘year’] = df[‘date’].dt.year df[‘month’] = df[‘date’].dt.month df[‘day’] = df[‘date’].dt.day df[‘hour’] = df[‘date’].dt.hour df[‘minute’] = df[‘date’].dt.minute df.head(3) date year month day hour …

Machine Learning for Beginners in Python: How to Tokenize Text

Tokenize Text Preliminaries from nltk.tokenize import word_tokenize, sent_tokenize Create Text Data string = “The science of today is the technology of tomorrow. Tomorrow is today.” Tokenize Words word_tokenize(string) [‘The’, ‘science’, ‘of’, ‘today’, ‘is’, ‘the’, ‘technology’, ‘of’, ‘tomorrow’, ‘.’, ‘Tomorrow’, ‘is’, ‘today’, ‘.’] Tokenize Sentences sent_tokenize(string) [‘The science of today is the technology of tomorrow.’, ‘Tomorrow …

Machine Learning for Beginners in Python: How to Find Term Frequency Inverse Document Frequency

Term Frequency Inverse Document Frequency   Preliminaries import numpy as np from sklearn.feature_extraction.text import TfidfVectorizer import pandas as pd Create Text Data text_data = np.array([‘I love Brazil. Brazil!’, ‘Sweden is best’, ‘Germany beats both’]) Create Feature Matrix tfidf = TfidfVectorizer() feature_matrix = tfidf.fit_transform(text_data) feature_matrix.toarray() array([[ 0. , 0. , 0. , 0.89442719, 0. , 0. …

Machine Learning for Beginners in Python: How to Tag Parts Of Speech

Tag Parts Of Speech Preliminaries from nltk import pos_tag from nltk import word_tokenize Create Text Data text_data = “Chris loved outdoor running” Tag Parts Of Speech text_tagged = pos_tag(word_tokenize(text_data)) text_tagged [(‘Chris’, ‘NNP’), (‘loved’, ‘VBD’), (‘outdoor’, ‘RP’), (‘running’, ‘VBG’)] Common Penn Treebank Parts Of Speech Tags The output is a list of tuples with the word …

Machine Learning for Beginners in Python: How to Remove Punctuation

Remove Punctuation Preliminaries import string import numpy as np Create Text Data text_data = [‘Hi!!!! I. Love. This. Song….’, ‘10000% Agree!!!! #LoveIT’, ‘Right?!?!’] Remove Punctuation def remove_punctuation(sentence: str) -> str: return sentence.translate(str.maketrans(”, ”, string.punctuation)) [remove_punctuation(sentence) for sentence in text_data] [‘Hi I Love This Song’, ‘10000 Agree LoveIT’, ‘Right’]     Python Example for Beginners Special …

Machine Learning for Beginners in Python: How to Strip Whitespace

Strip Whitespace Create Text text_data = [‘ Interrobang. By Aishwarya Henriette ‘, ‘Parking And Going. By Karl Gautier’, ‘ Today Is The night. By Jarek Prakash ‘] Remove Whitespace strip_whitespace = [string.strip() for string in text_data]   strip_whitespace [‘Interrobang. By Aishwarya Henriette’, ‘Parking And Going. By Karl Gautier’, ‘Today Is The night. By Jarek Prakash’]   …

Machine Learning for Beginners in Python: How to Parse HTML

Parse HTML Preliminaries from bs4 import BeautifulSoup Create HTML html = “<div class=’full_name’><span style=’font-weight:bold’>Masego</span> Azra</div>” Parse HTML soup = BeautifulSoup(html, “lxml”)   soup.find(“div”, { “class” : “full_name” }).text ‘Masego Azra’     Python Example for Beginners Special 95% discount 2000+ Applied Machine Learning & Data Science Recipes Portfolio Projects for Aspiring Data Scientists: Tabular Text & …