2024-programowanie-w-python.../zajecia3/1_odpowiedzi.ipynb
2024-12-07 11:54:47 +01:00

6.2 KiB

import numpy as np
#Zadanie 1.1
# Tworzenie tablicy jednowymiarowej
arr = np.array([10,11,12,13,14,15,16,17,18,19,20])
print(arr)
print(arr.shape)
print(type(arr))
[10 11 12 13 14 15 16 17 18 19 20]
(11,)
<class 'numpy.ndarray'>
# Zadanie 1.2
arr = np.array([[10, 20], [30, 40], [50, 60]])

print(arr)

print(arr.shape)

print(type(arr))
[[10 20]
 [30 40]
 [50 60]]
(3, 2)
<class 'numpy.ndarray'>
# Zadanie 2
# Tworzenie dwuwymiarowej tablicy
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(arr)


print(arr[2, 1])

print(arr[2:])


print(arr[:,2])

print(arr[1:,1:])
[[1 2 3]
 [4 5 6]
 [7 8 9]]
8
[[7 8 9]]
[3 6 9]
[[5 6]
 [8 9]]
# Zadanie 3

# Punkt 1
arr = np.array([10, 20, 30, 40])
print(arr)
print(arr.dtype)

# Punkt 2
arr = arr.astype('float32')
print(arr)
print(arr.dtype)

# Punkt 3
arr = np.array(["Python", "NumPy", "Coding"])
print(arr)
print(arr.dtype)
[10 20 30 40]
int64
[10. 20. 30. 40.]
float32
['Python' 'NumPy' 'Coding']
<U6
### Zadanie 4
import copy


# Punkt 1 - Przypisanie do zmiennej
arr = np.array([1, 2, 3, 4, 5])
x = arr
arr[0] = 50
print(arr)  # Tablica arr po zmianie
print(x)    # Tablica x po zmianie

# Punkt 2 - Kopia tablicy
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 50
print(arr)  # Tablica arr po zmianie
print(x)    # Tablica x po kopii

# Punkt 3 - Głęboka kopia
arr = np.array([1, 2, 3, 4, 5])
x = copy.deepcopy(arr)
arr[0] = 50
print(arr)  # Tablica arr po zmianie

arr2 = np.array([1, 2, 3, 4], ndmin=2)
print(arr2)

# Zmiana wymiaru na jednowymiarowy
arr_squeezed = arr2.squeeze()
print(arr_squeezed)

[50  2  3  4  5]
[50  2  3  4  5]
[50  2  3  4  5]
[1 2 3 4 5]
[50  2  3  4  5]
[[1 2 3 4]]
[1 2 3 4]
## Zadanie 5


arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

arr

arr.reshape(5,2)

arr.reshape(10,1)

arr.reshape(5,-1)

arr.reshape(5,1,2,1)
array([[[[ 1],
         [ 2]]],


       [[[ 3],
         [ 4]]],


       [[[ 5],
         [ 6]]],


       [[[ 7],
         [ 8]]],


       [[[ 9],
         [10]]]])
### Zadanie 6


x = np.array([3, 5, 7])
y = np.array([50, 60, 70])

print(x + y)
print(x + 10)
print(x * 5)
z = np.array([[3, 5, 7], [8, 10, 12], [10,10,10]])
print(z)

print(x.dot(z))
print(x.dot(y))
[53 65 77]
[13 15 17]
[15 25 35]
[[ 3  5  7]
 [ 8 10 12]
 [10 10 10]]
[119 135 151]
940