1/5
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
import numpy as np
arr = np.arange(0, 10).reshape(2, 5)
print(arr)
A 1-D array is filled with integers from 0-9 and reshaped into a 2-D array with 2 rows and 5 columns. np.arange is used to create a series of number within a certain range and the reshape method changes the shape of the array.
this if for reference
import numpy as np
arr = np.arange(0, 10).reshape(2, 5)
print(arr)
describe these lines of code
arr_2 = arr.reshape(5, 2)
arr_3 = arr + arr_2.transpose()
print(arr_3, arr_3.shape)
arr_2 = arr.reshape(5, 2)
arr_3 = arr + arr_2.T
print(arr_3, arr_3.shape)
In both lines a 2-D array with 2 rows and 5 columns is reshaped into an array with 5 rows and 2 columns with the reshape method. After the new array is created, both lines sum the elements in the original and new array. Both lines change the new array back into a 2×5 with the transpose method since arrays need to be the same shape to be added. The first line uses the full word while the second line uses a capital t.
these lines are just for reference
import numpy as np
arr = np.arange(0, 10).reshape(2, 5)
print(arr)
arr_2 = arr.reshape(5, 2)
arr_3 = arr + arr_2.transpose()
print(arr_3, arr_3.shape)
describe these lines
arr_4 = arr_3.swapaxes(0, 1)
print(arr_4)
print(arr_4[0:1])
# book version
print(arr_4[0, :])
The first line uses the swapaxes function is used on the array to swap its rows and columns and assigned to a new array called arr_4. arr_4[0:1] in the second line and arr_4[0, :] from the third line index the first row from arr_4. [0:1] tells numpy to start indexing at the first indice and stop at but not include the second indice while [0, :] starts at 0 and : tells numpy to include all elements in row. The second returns the result in a 2d format whereas the third line returns a 1d format
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
players = ['Robin', 'Leo', 'Pogba', 'Diego', 'Ronaldo']
teams = ['Man UTD', 'Barca', 'Juve', 'Napo', 'RMadrid']
goals = [[12, 15, 16, 15, 13],
[26, 30, 31, 25, 24],
[10, 12, 8, 6, 13],
[18, 19, 17, 20, 21],
[21, 32, 25, 21, 22]
]
goals = np.array(goals)
fig, ax = plt.subplots(figsize = (12, 8))
sns.heatmap(goals, annot = True, cmap = 'YlGnBu')
ax.set_xticks(np.arange(len(players)), labels = players)
ax.set_yticks(np.arange(len(teams)), rotation = 'horizontal', labels = teams)
plt.title('Goals set by Players', fontsize = 20)
plt.show()
Numpy, matplotlib.pyplot, and seaborn are imported. 3 lists representing players, their teams, and the number of goals they scored are created. The list assigned to the goals variable is a 2-D list unlike the other 2. The goals list is converted into a 2-D array with the np.array function for better compatibility with seaborn.
fig, ax = plt.subplots(figsize = (12, 8)) uses the plt.subplots and figsize functions to create an empty subplot a width of 12 and a height of 8. sns.heatmap(goals = annot = True, cmap = 'YlGnBu') creates a heatmap from the goals array, uses the annot = True to display the numerical values within the cells of the heatmap, and cmap = 'YlGnBu' to set its colors.
ax.set_xticks(np.arange(len(players)), labels = players) creates the ticks and labels for the x-axis by generating a numerical array with np.arrange(len(players)) which returns numbers based on the length of players and represent each player on the list. Labels = players sets the player’s names as labels on the x-axis.
ax.set_yticks(np.arange(len(teams)), rotation = 'horizontal', labels = teams) creates ticks and labels for the y-axis in a similar manner. Rotation = ‘horizontal’ ensures that the teams are displayed horizontally on the heatmap. The plt.title and fontsize functions are used to assign a title of the heatmap with a font of 20.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
players = ['Robin', 'Leo', 'Pogba', 'Diego', 'Ronaldo']
teams = ['Man UTD', 'Barca', 'Juve', 'Napo', 'RMadrid']
goals = [[12, 15, 16, 15, 13],
[26, 30, 31, 25, 24],
[10, 12, 8, 6, 13],
[18, 19, 17, 20, 21],
[21, 32, 25, 21, 22]
]
goals = np.array(goals)
data = [f'{player} from {team}' for team, player in zip(teams, players)]
fig, ax = plt.subplots(figsize = (12, 8))
sns.heatmap(goals, annot = True, cmap = 'YlGnBu')
ax.set_xticks([])
ax.set_yticks(np.arange(len(teams)), rotation = 'horizontal', labels = data)
plt.title('Goals set by Players', fontsize = 20)
plt.show()
Numpy, matplotlib.pyplot, and seaborn are imported. 3 lists representing players, their teams, and the number of goals they scored are created. The list assigned to the goals variable is a 2-D list unlike the other 2. The goals list is converted into a 2-D array with the np.array function for better compatibility with seaborn. data = [f'{player} from {team}' for team, player in zip(teams, players)] merges players and teams lists with a list comprehension. zip(team, player) creates tuples pairing players with their corresponding team while f’{player} from {team}’ adds a from between each player-team pair.
fig, ax = plt.subplots(figsize = (12, 8)) uses the plt.subplots and figsize functions to create an empty subplot a width of 12 and a height of 8. sns.heatmap(goals = annot = True, cmap = 'YlGnBu') creates a heatmap from the goals array, uses the annot = True to display the numerical values within the cells of the heatmap, and cmap = 'YlGnBu' to set its colors.
ax.set_xticks([]) removes the numerical xticks from heatmap. ax.set_yticks(np.arange(len(teams)), rotation = ‘horizontal’, labels = data) creates the ticks and labels for the y-axis by generating a numerical array with np.arange(len(teams)) which returns numbers based on the length of teams and represent each team in the list. Rotation = ‘horizontal’ ensures that the teams are displayed horizontally on the heatmap and labels = data displays the player-team combinations on the y-axis. The plt.title and fontsize functions are used to assign a title of the heatmap with a font of 20.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
players = ['Robin', 'Leo', 'Pogba', 'Diego', 'Ronaldo']
teams = ['Man UTD', 'Barca', 'Juve', 'Napo', 'RMadrid']
goals = [[12, 15, 16, 15, 13],
[26, 30, 31, 25, 24],
[10, 12, 8, 6, 13],
[18, 19, 17, 20, 21],
[21, 32, 25, 21, 22]
]
goals = np.array(goals)
players = pd.Series(players)
teams = pd.Series(teams)
data = players + teams
fig, ax = plt.subplots(figsize = (12, 8))
sns.heatmap(goals, annot = True, cmap = 'YlGnBu')
ax.set_xticks([])
ax.set_yticks(np.arange(len(teams)), rotation = 'horizontal', labels = data)
plt.title('Goals per Player', fontsize = 20)
plt.show()
Numpy, matplotlib.pyplot, seaborn, and pandas are imported. 3 lists representing players, their teams, and the number of goals they scored are created. The list assigned to the goals variable is a 2-D list unlike the other 2. The goals list is converted into a 2-D array with the np.array function for better compatibility with seaborn. The players and teams list are converted into pandas series with the pd.Series function. Both series are merged with the + and stored in a variable called data.
fig, ax = plt.subplots(figsize = (12, 8)) uses the plt.subplots and figsize functions to create an empty subplot a width of 12 and a height of 8. sns.heatmap(goals = annot = True, cmap = 'YlGnBu') creates a heatmap from the goals array, uses the annot = True to display the numerical values within the cells of the heatmap, and cmap = 'YlGnBu' to set its colors.
fig, ax = plt.subplots(figsize = (12, 8)) uses the plt.subplots and figsize functions to create an empty subplot a width of 12 and a height of 8. sns.heatmap(goals = annot = True, cmap = 'YlGnBu') creates a heatmap from the goals array, uses the annot = True to display the numerical values within the cells of the heatmap, and cmap = 'YlGnBu' to set its colors.
ax.set_xticks([]) removes the numerical xticks from heatmap. ax.set_yticks(np.arange(len(teams)), rotation = ‘horizontal’, labels = data) creates the ticks and labels for the y-axis by generating a numerical array with np.arange(len(teams)) which returns numbers based on the length of teams and represent each team in the list. Rotation = ‘horizontal’ ensures that the teams are displayed horizontally on the heatmap and labels = data displays the player-team combinations on the y-axis. The plt.title and fontsize functions are used to assign a title of the heatmap with a font of 20.