Example 1 isalpha while True: name = input(“Enter the player’s name: “) if name.isalpha() and len(name) > 1: break else: print(“Invalid name. Please enter a valid name with alphabetic characters only.”) print(f”The player’s name is {name}.”) Example 2 integer while True: try: home_runs = int(input(“Enter the number of home runs: “)) if 0
Python Zip
Example 1 Basic Example fb_jersey_numbers = [14, 60, 16] quarterbacks = [‘Y.A. Tittle’, ‘Otto Graham’, ‘George Blanda’] zipped = zip(fb_jersey_numbers, quarterbacks) print(list(zipped)) [(14, ‘Y.A. Tittle’), (60, ‘Otto Graham’), (16, ‘George Blanda’)] Example 2 Length Mismatch If the iterables have different lengths, zip() will stop creating tuples when the shortest iterable is exhausted basseball_jersey_numbers = [44, […]
Python Enumerate
Example 1 Enumerate on List sports = [‘ultra running’, ‘cricket’, ‘baseball’] for index, sport in enumerate(sports): print(index, sport) 0 ultra running1 cricket2 baseball Example 2 Enumerate on List Start 1 for index, sport in enumerate(sports, start=1): print(index, sport) 1 ultra running2 cricket3 baseball Example 3 Enumerate on String word = “DiMaggio” for index, letter in […]
Python List Comprehension
Example 1 Add Integers number_list = [10, 20, 30] number_plus_5 = [x + 5 for x in number_list] print(number_plus_5) [15, 25, 35] What would this look like as a for loop? for_number_plus_5 = [] for x in number_list: for_number_plus_5.append(x + 5) print(number_plus_5) [15, 25, 35] Example 2 Integer Multiplication number_mul_3 = [x * 3 for […]
Python Dictionary Comprehension
Example 1 add 15 to each value concerts = { ‘Trivium’: 100, ‘Nine Inch Nails’: 120, ‘Foo Fighters’: 90, ‘Queens of the Stone Age’: 110 } new_concert = {key: value + 15 for (key, value) in concerts.items()} print(new_concert) {‘Trivium’: 115, ‘Nine Inch Nails’: 135, ‘Foo Fighters’: 105, ‘Queens of the Stone Age’: 125} Example 2 […]
Sklearn Gaussian Mixture Models
import numpy as np from sklearn.mixture import GaussianMixture from sklearn.datasets import make_blobs import matplotlib.pyplot as plt import seaborn as sns import pandas as pd data, true_labels = make_blobs(n_samples=300, centers=3, cluster_std=2.0, random_state=42) plt.scatter(data[:, 0], data[:, 1], s=30) plt.title(“Generated Blob Data”) plt.show() Example 1 – gmm = GaussianMixture(n_components=3, random_state=42) gmm.fit(data) predicted_labels = gmm.predict(data) cluster_centers = gmm.means_ print(cluster_centers) […]
Sklearn Support Vector Machine
import pandas as pd from sklearn.datasets import load_iris import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.svm import SVC iris = load_iris() df = pd.DataFrame(iris.data, columns = iris.feature_names) df[‘target’] = iris.target df.head() iris.target_names df[‘flower_name’] = df.target.apply(lambda x: iris.target_names[x]) df0 = df[df.target==0] df1 = df[df.target==1] df2 = df[df.target==2] plt.xlabel(‘Sepal Length’) plt.ylabel(‘Sepal Width’) plt.scatter(df0[‘sepal length (cm)’], […]
SKLearn Naive Bayes
To start we’re going to create a simple dataframe in python: add in paramatersmooth the likelihood estimates and avoid zero probabilities
SKlearn Multiple Linear Regressions
Generate 500 rows of random data for the dataset drop team and season
Scikit-learn Pipelines
import pandas as pd import numpy as np import joblib from sklearn.model_selection import train_test_split from sklearn.impute import SimpleImputer from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.pipeline import make_pipeline, Pipeline from sklearn.preprocessing import StandardScaler, OneHotEncoder from sklearn.compose import ColumnTransformer d1 = {‘Social_media_followers’:[1000000, np.nan, 2000000, 1310000, 1700000, np.nan, 4100000, 1600000, 2200000, 1000000], ‘Sold_out’:[1,0,0,1,0,0,0,1,0,1]} df1 = […]