Do two arrays have the same shape?

Python Practice › NumPy Programs › Program 90

Do two arrays have the same shape?

EasyNumPy Programs · 90 of 100

Write a NumPy program to check whether two arrays have the same dimensions.

Output
True False

Approach

  1. Compare .shape.

Solution

Python
import numpy as npa, b, c = np.zeros((2, 3)), np.ones((2, 3)), np.ones((3, 2))print(a.shape == b.shape, a.shape == c.shape)
Output
True False

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Create and reshape

Python Practice › NumPy Programs › Program 89

Create and reshape

EasyNumPy Programs · 89 of 100

Write a NumPy program to create an array and reshape it.

Output
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
(2, 6)

Approach

  1. reshape(-1, n) lets NumPy work out one dimension.

Solution

Python
import numpy as npa = np.arange(12)print(a.reshape(3, 4))print(a.reshape(-1, 6).shape)
Output
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
(2, 6)

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Array to a DataFrame with headers

Python Practice › NumPy Programs › Program 88

Array to a DataFrame with headers

EasyNumPy Programs · 88 of 100

Write a NumPy program to convert an array into a pandas DataFrame with column headers.

Output
   maths  science
0     90       85
1     72       64
2     88       91

Approach

  1. pd.DataFrame(array, columns=[...]).

Solution

Python
import numpy as npimport pandas as pd a = np.array([[90, 85], [72, 64], [88, 91]])df = pd.DataFrame(a, columns=["maths", "science"])print(df)
Output
   maths  science
0     90       85
1     72       64
2     88       91

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Merge arrays of the same shape

Python Practice › NumPy Programs › Program 87

Merge arrays of the same shape

EasyNumPy Programs · 87 of 100

Write a NumPy program to merge two arrays of identical shape.

Output
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
[[1 2 5 6]
 [3 4 7 8]]

Approach

  1. np.vstack stacks rows; np.hstack joins side by side.

Solution

Python
import numpy as npa = np.array([[1, 2], [3, 4]])b = np.array([[5, 6], [7, 8]])print(np.vstack((a, b)))print(np.hstack((a, b)))
Output
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
[[1 2 5 6]
 [3 4 7 8]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Row averages ignoring NaN

Python Practice › NumPy Programs › Program 86

Row averages ignoring NaN

EasyNumPy Programs · 86 of 100

Write a NumPy program to calculate the average of each row, ignoring NaNs.

Input
[[6, 5, nan], [nan, 9, 1], [7, 8, 2]]
Output
[5.5  5.   5.67]

Approach

  1. np.nanmean(axis=1).

Solution

Python
import numpy as npa = np.array([[6, 5, np.nan], [np.nan, 9, 1], [7, 8, 2]])print(np.nanmean(a, axis=1).round(2))
Output
[5.5  5.   5.67]
Note

The third row’s average is 17 / 3 = 5.67; the original sample answer (8.5) was not correct.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Sum of all columns

Python Practice › NumPy Programs › Program 85

Sum of all columns

EasyNumPy Programs · 85 of 100

Write a NumPy program to calculate the sum of each column.

Input
[[6, 5, 7], [10, 9, 1], [7, 8, 2]]
Output
[23 22 10]

Approach

  1. sum(axis=0).

Solution

Python
import numpy as npprint(np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]]).sum(axis=0))
Output
[23 22 10]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Copy an array

Python Practice › NumPy Programs › Program 84

Copy an array

EasyNumPy Programs · 84 of 100

Write a NumPy program to copy data from one array to another.

Output
[ 1 77  3] [99  2  3]

Approach

  1. copy() creates independent data; a slice is only a view.

Solution

Python
import numpy as npa = np.array([1, 2, 3])b = a.copy()view = a[:]b[0] = 99view[1] = 77print(a, b)
Output
[ 1 77  3] [99  2  3]

Line by line

Lines 5-7

Changing the copy leaves a alone, but changing the view changes a.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Top-left 2 × 2 block

Python Practice › NumPy Programs › Program 83

Top-left 2 × 2 block

EasyNumPy Programs · 83 of 100

Write a NumPy program to extract the first and second elements of the first and second rows.

Input
[[6, 5, 7], [10, 9, 1], [7, 8, 2]]
Output
[[ 6  5]
 [10  9]]

Approach

  1. a[:2, :2].

Solution

Python
import numpy as npa = np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]])print(a[:2, :2])
Output
[[ 6  5]
 [10  9]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

First two columns

Python Practice › NumPy Programs › Program 82

First two columns

EasyNumPy Programs · 82 of 100

Write a NumPy program to access the first two columns of an array.

Input
[[6, 5, 7], [10, 9, 1], [7, 8, 2]]
Output
[[ 6  5]
 [10  9]
 [ 7  8]]

Approach

  1. a[:, :2].

Solution

Python
import numpy as npa = np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]])print(a[:, :2])
Output
[[ 6  5]
 [10  9]
 [ 7  8]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Save an array to CSV

Python Practice › NumPy Programs › Program 81

Save an array to CSV

EasyNumPy Programs · 81 of 100

Write a NumPy program to save an array to a CSV file.

Output
a,b,c
1,2,3
4,5,6

Approach

  1. np.savetxt with delimiter=",".

Solution

Python
import numpy as npa = np.array([[1, 2, 3], [4, 5, 6]])np.savetxt("data.csv", a, delimiter=",", fmt="%d", header="a,b,c", comments="")print(open("data.csv").read())
Output
a,b,c
1,2,3
4,5,6

Line by line

Line 3

fmt="%d" writes integers; comments="" stops NumPy putting “# ” before the header.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs