Complete the code to calculate the mean of a list of numbers.
numbers = [4, 8, 15, 16, 23, 42] mean = sum(numbers) / [1]
The mean is the sum of all numbers divided by the count of numbers, which is given by len(numbers).
Complete the code to split data into features and labels for training.
data = [[5.1, 3.5, 1.4, 0.2, 'setosa'], [6.2, 3.4, 5.4, 2.3, 'virginica']] features = [row[:[1]] for row in data] labels = [row[-1] for row in data]
The features are the first 4 elements in each row, so we slice up to index 4.
Fix the error in the code to train a simple linear regression model using scikit-learn.
from sklearn.linear_model import LinearRegression X = [[1], [2], [3], [4]] y = [2, 4, 6, 8] model = LinearRegression() model.[1](X, y)
train which is not a method in scikit-learn.predict which is for making predictions, not training.The fit method trains the model on the data.
Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
word as the value instead of its length.We want the length of each word as the value, and only include words longer than 3 characters.
Fill all three blanks to filter a dictionary for items with values greater than 10 and convert keys to uppercase.
data = {'a': 5, 'b': 15, 'c': 25}
filtered = { [1]: [2] for [3], v in data.items() if v > 10 }data instead of loop variable for keys.The keys are converted to uppercase with k.upper(), values are v, and the loop variables are k and v.