speciale list tbv datascience
Hiermee kan je operaties uitvoeren op een hele lijst.
Numpy is a Python package to efficiently do data science. Learn to work with the Numpy array, a faster and more powerful alternative to the list
numpy
- standard for storing numerical data
-essential for other packages scikit-learn (machine learning)
calculatie element wise voorbeeld 1.
bijv converteer inch naar kg dmv van een vermenigvuldiging op een numpy arry# height and weight are available as a regular lists
# Import numpy
import numpy as np
# Calculate the BMI: bmi
np_height_m = np.array(height) * 0.0254
np_weight_kg = np.array(weight) * 0.453592
bmi = np_weight_kg / np_height_m ** 2
print(bmi)
# Create the light array
light=bmi<21
# Print out light
print(light)
# Print out BMIs of all baseball players whose BMI is below 21
print(bmi[light])
voorbeeld 2
# Import numpyimport numpy as np
# Convert positions and heights to numpy arrays: np_positions, np_heights
np_positions=np.array(positions)
np_heights=np.array(heights)
# Heights of the goalkeepers: gk_heights
gk_heights = np_heights[np_positions=='GK']
# Heights of the other players: other_heights
other_heights = np_heights[np_positions!='GK']
# Print out the median height of goalkeepers. Replace 'None'
print("Median height of goalkeepers: " + str(np.median(gk_heights)))
# Print out the median height of other players. Replace 'None'
print("Median height of other players: " + str(np.median(other_heights)))
Numpy matrix
Remember how you calculated the Body Mass Index for all baseball players? Numpy was able to perform all calculations element-wise. For 2D Numpy arrays this isn't any different! You can combine matrices with single numbers, with vectors, and with other matrices.
Execute the code below in the IPython shell and see if you understand:
import numpy as np
np_mat = np.array([[1, 2],
[3, 4],
[5, 6]])
np_mat * 2
np_mat + np.array([10, 10])
np_mat + np_mat
Geen opmerkingen:
Een reactie posten