my_list
and my_array
it appears to produce the same result. How would you check the type of data structure for each one?my_list = [11, 12, 33]
print(my_list)
my_array = np.array([11, 12, 33])
print(my_array)
SOLUTION
import numpy as np
my_list = [11, 12, 33]
print(my_list, type(my_list))
my_array = np.array([11, 12, 33])
print(my_array, type(my_array))
# printing also the type, would give us an indication of the data structure
[11, 12, 33] <class 'list'> [11 12 33] <class 'numpy.ndarray'>
SOLUTION
#Trying out with list
X = [11, 12, 12, 14, 15]
Y = [10, 20, 30, 40, 50]
(X + Y) /2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-ee5320697cea> in <module> 3 Y = [10, 20, 30, 40, 50] 4 ----> 5 (X + Y) /2 TypeError: unsupported operand type(s) for /: 'list' and 'int'
#Trying out with array
X = np.array([11, 12, 12, 14, 15])
Y = np.array([10, 20, 30, 40, 50])
(X + Y) /2
array([10.5, 16. , 21. , 27. , 32.5])
It does not work with lists simply because X and Y are Python objects, and one can't make mathematical operations with two objects just like that. On the other hands, Numpy makes arrays to be seen as a mathematical vector, which make it possible to perform operations.
import scipy.stats as stats
Why is the following function not working anymore? Make the appropiate changes to fix the problem.
uniform.rvs(size=100)
SOLUTION
There are two ways to import libraries:
# importing the whole library
import scipy.stats as stats
# adding the alias at the begining
stats.uniform.rvs(size=10)
array([0.86471247, 0.86690817, 0.05705536, 0.46569065, 0.29175586, 0.66459976, 0.79557259, 0.85127024, 0.02890321, 0.03092554])
# importing only the fucntion needed
from scipy.stats import uniform
# no alias needed
uniform.rvs(size=10)
array([0.28894512, 0.47649095, 0.21860038, 0.80341151, 0.32551943, 0.82727626, 0.48046056, 0.08225253, 0.66734134, 0.31230785])
mean = 10
and sd = 5
.They have the same parameters, but do they produce the same results? What are the differences or similarities among them?_
norm.rvs(10, 5, 20)
norm.rvs(loc=10, scale=5, size=20, random_state=2021)
norm.rvs(random_state=2021, scale=5, loc=10, size=20)
SOLUTION
norm.rvs(10, 5, 20)
norm.rvs(loc=10, scale=5, size=20)
norm.rvs(scale=5, loc=10, size=20)
V = np.array([0, 1, 2, 3, 4, 5])
random.sample(V, 2)
V = np.array([0, 1, 2, 3, 4, 5])
random.sample(V, 2)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-12-a9019d9632b4> in <module> 1 V = np.array([0, 1, 2, 3, 4, 5]) ----> 2 random.sample(V, 2) ~/opt/anaconda3/lib/python3.7/random.py in sample(self, population, k) 315 population = tuple(population) 316 if not isinstance(population, _Sequence): --> 317 raise TypeError("Population must be a sequence or set. For dicts, use list(d).") 318 randbelow = self._randbelow 319 n = len(population) TypeError: Population must be a sequence or set. For dicts, use list(d).
SOLUTION
This can't be sampled because of 2 reasons.
import random
V = np.array([0, 1, 2, 3, 4, 5])
V = list(V)
random.sample(V, 2)
[1, 2]
Hint: you can use len()
function
SOLUTION
import random
def percetage_sample(vector, percentage):
"This function takes a vector and a desire percentage sample in decimal notation"
# calculate elements to sample
to_sample = len(vector)*percentage
# convert to closest integer
to_sample = int(to_sample)
# do random sample
sampled = random.sample(list(vector), to_sample)
return sampled
netflix = ["Luis Miguel", "New Amsterdam", "Lupin", "Shtisel", "Taco Chronicles", "The Queen's Gambit",
"Too Hot to Handle", "The Crown", "Rick and Morty", "Anne+", "Selling Sunset", "Vikings"]
# Sampling 20% of the Netflix list
percetage_sample(netflix, 0.20)
['Too Hot to Handle', 'Rick and Morty']