Study
[CS231n] Assignment python 정리
jiwon152
2023. 4. 5. 11:01
1. array끼리 operation
import numpy as np
# create two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# add the two arrays together
result = arr1 + arr2
print(result)
2. 2d array operation
import numpy as np
# create two 2D arrays
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
# add the two arrays together
result = arr1 + arr2
print(result)
3. pow 제곱 연산
pow(base number, exponent)
또는 그냥 base number ** exponent
4. row access in array
row_r1 = a[1, :] # Rank 1 view of the second row of a
row_r2 = a[1:2, :] # Rank 2 view of the second row of a
row_r3 = a[[1], :] # Rank 2 view of the second row of a
row_r1 = a [1, :] creates a one-dimensional (rank 1) view of the second row of a. This is done by indexing the second row with 1 and using the colon : to indicate that we want all columns in that row. The resulting view will be a one-dimensional array with the same number of columns as the original array.
row_r2 = a [1:2, :] creates a two-dimensional (rank 2) view of the second row of a. This is done by using slicing notation to specify a range of rows from the second row to the third row (since the end index is exclusive). The resulting view will be a two-dimensional array with one row and the same number of columns as the original array.
row_r3 = a[[1], :] also creates a two-dimensional (rank 2) view of the second row of a. This is done by using a list of indices [1] to specify the row we want (which is the second row, since indexing starts at 0), and using the colon : to indicate that we want all columns in that row. The resulting view will be a two-dimensional array with one row and the same number of columns as the original array, similar to row_r2.
5. numpy.argsort
import numpy as np
a = np.array([1, 0.13, 4, 1.6])
s = a.argsort()
print(s)
It sorts the arrays in ascending powers and returns the array of the original one's index.
6. counting the number of elements and returning the most frequent one (or smallest value if counts are same)
import numpy as np
# Program to find most frequent
# element in a list
def most_frequent(List):
unique, counts = np.unique(List, return_counts=True)
index = np.argmax(counts)
return unique[index]
List = [2, 1, 2, 2, 1, 3, 1]
print(most_frequent(List))
# 참고 : https://numpy.org/doc/stable/reference/generated/numpy.unique.html
Function np.unique returns sorted array of unique elements in an array, and if the return_counts is True, it also returns a array of counts of those elements appeared. Then with argmax, you get the array of indices. It is a stable sort, so it returns the index of 1 instead of 2 in the unique array.
#argmax 이해를 위한 실행
List = [2, 1, 2, 2, 1, 3, 1]
unique, counts = np.unique(List, return_counts=True)
index = np.argmax(counts)
print(unique)
print(counts)
print(index)
print(unique[index])
7. array square(제곱)
numpy.square
import numpy as np
# Program to find most frequent
# element in a list
List =[[5,6],[7,8]]
print(np.square(List))
8. List Length
import numpy as np
k_choices = [1, 3, 5, 8, 10, 12, 15, 20, 50, 100]
print(len(k_choices))