Veri Bilimi İçin Temel Python Kütüphaneleri-1 : Numpy - SiberMega - Basit Yazılım ve Tasarım Eğitimleri

Popüler Yazılar

Post Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

06/04/2023

Veri Bilimi İçin Temel Python Kütüphaneleri-1 : Numpy

 

1) Numpy kütüphanesinin import edilmesi

# Kütüphanemizi np ismi ile import ettik.
import numpy as np

2) Numpy ile Temel İşlemler

- Numpy dizisinin oluşturulması

# python list
python_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# numpy array
numpy_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print("Python listesi :")
print(python_list)
print("Numpy dizisi :")
print(numpy_array)
Output:
Python listesi :
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Numpy dizisi :
[0 1 2 3 4 5 6 7 8 9]

- Dizinin boyutunu bulmak : ndarray.ndim

numpy_array1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(numpy_array1.ndim)
Output:
1
numpy_array2 = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
print(numpy_array2.ndim)
Output:
2

- Dizinin satır ve sütun sayısını bulmak: ndarray.shape

numpy_array1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(numpy_array1.shape)
print(numpy_array1.ndim)
#10 tane elemandan oluşan 1 boyutlu bir dizi(vektör).
Output:
(10,)
1
numpy_array2 = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
print(numpy_array2.ndim)
print(numpy_array2.shape)
# 1 satır ve 10 sütundan oluşan 2 boyutlu bir dizi(matris).
Output:
(1, 10)
2

-Dizinin satır ve sütun sayısını değiştirmek : ndarray.reshape()

numpy_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(numpy_array.reshape(1,10))
Output :
[[0 1 2 3 4 5 6 7 8 9]]
print(numpy_array.reshape(10,1))
Output:
[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]
print(numpy_array.reshape(5,2))
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
print(numpy_array.reshape(2,5))
Output:
[[0 1 2 3 4]
[5 6 7 8 9]]

- np.arange()


np.arange(0,10,3)
Output:
array([0, 3, 6, 9])
np.arange(10)
Output:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,10,1)
Output:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

- Dizinin elemanlarını seçmek

numpy_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(5,2)
print(numpy_array)
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
#Dizinin herhangi bir satırını seçmek
#1.satır
first_row = numpy_array[0]
#1. ve 2. satır
first_and_second_rows = numpy_array[0:2]
print(first_row)
print(first_and_second_rows)
Output:
[0 1]
[[0 1] [2 3]]
#Dizinin herhangi bir kolonunu seçmek
#1. sütun
first_column = numpy_array[:,0]
#1. ve 2. sütun
first_and_second_column = numpy_array[:,0:2]
print(first_column)
print(first_and_second_column)
Output:
[0 2 4 6 8]
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
#Dizinin herhangi bir elemanını seçmek
selecting_item = numpy_array[3,1]
print(selecting_item)
Output:
7

- Diziyi ters çevirmek

numpy_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(5,2)
print(numpy_array)
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
print(numpy_array[::-1])
Output:
[[8 9]
[6 7]
[4 5]
[2 3]
[0 1]]

-0 matrisi oluşturmak : np.zeros()

print(np.zeros((5,4)))
Output:
[[ 0 0 0 0]
[ 0 0 0 0]
[ 0 0 0 0]
[ 0 0 0 0]
[ 0 0 0 0]]
print(np.ones((3,3,3)))Output:
[[[ 1 1 1] [ 1 1 1] [ 1 1 1]]
[[ 1 1 1] [ 1 1 1] [ 1 1 1]]
[[ 1 1 1] [ 1 1 1] [ 1 1 1]]]

- Birim matris oluşturmak : np.eye()

#4X4 lük birim matris
print(np.eye(4))
Output:
[[ 1 0 0 0]
[ 0 1 0 0]
[ 0 0 1 0]
[ 0 0 0 1]]

- Matrisleri birleştirmek

numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(5,2)
#Satır bazlı birleştirme
print(np.concatenate([numpy_array, numpy_array], axis =0))
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]
[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
#Sütun bazlı birleştirme
print(np.concatenate([numpy_array, numpy_array], axis =1))
Output:
[[0 1 0 1]
[2 3 2 3]
[4 5 4 5]
[6 7 6 7]
[8 9 8 9]]

3) Numpy ile Matematiksel İşlemler

-sum(toplam), max ve min değerlerini hesaplamak

numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(5,2)
print(numpy_array)
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
print(numpy_array.max())
Output:9
print(numpy_array.min())
Output:0

print(numpy_array.sum())
Output:45
#Satırların toplamı
print(numpy_array.sum(axis = 1))
Output:
[ 1 5 9 13 17]
#Sütunların toplamı
print(numpy_array.sum(axis = 0))
Output:
[20 25]

- mean, median, varyans ve standart sapma hesaplamak

numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
print(numpy_array.mean())
Output:4.5
print(np.median(numpy_array))
Output:4.5
print(numpy_array.var())
Output:8.25
print(numpy_array.std())
Output:2.8722813232690143

-Matrislerde aritmetik işlemler

numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(3,3)
#dizimizi 3x3 lük, 2 boyutlu bir matrise dönüştürdük.
print(numpy_array)
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
print(numpy_array + numpy_array)
Output:
[[ 2 4 6]
[ 8 10 12]
[14 16 18]]
print(numpy_array - numpy_array)
Output:
[[0 0 0]
[0 0 0]
[0 0 0]]
print(numpy_array * numpy_array)
Output:
[[ 1 4 9]
[16 25 36]
[49 64 81]]
print(numpy_array / numpy_array)
Output:
[[1 1 1]
[1 1 1]
[1 1 1]]
print(numpy_array + 5)
Output:
[[ 6 7 8]
[ 9 10 11]
[12 13 14]]
print(numpy_array * 2)
Output:
[[ 2 4 6]
[ 8 10 12]
[14 16 18]]

- Özel foksiyonlar ile işlemler

numpy_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(3,3)
#dizimizi 3x3 lük, 2 boyutlu bir matrise dönüştürdük.
print(np.sin(numpy_array))
Output:
[[0.84147 0.9093 0.14112]
[-0.7568 -0.95892 -0.27942]
[0.65699 0.98936 0.41212]]
print(np.cos(numpy_array))
Output:
[[0.5403 -0.41615 -0.98999]
[-0.65364 0.28366 0.96017]
[0.7539 -0.1455 -0.91113]] [
print(np.sqrt(numpy_array))
Output:
[[ 1 1.4142 1.7321]
[ 2 2.2361 2.4495]
[2.6458 2.8284 3]]
print(np.exp(numpy_array))
Output:
[[2.7183 7.3891 20.086]
[54.598 148.41 403.43]
[1096.6 2981 8103.1]]
print(np.log(numpy_array))
Output:
[[ 0 0.69315 1.0986]
[1.3863 1.6094 1.7918]
[1.9459 2.0794 2.1972]]

-Matrislerin çarpımı

numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array1 = numpy_array.reshape(5,2)
numpy_array2 = numpy_array.reshape(2,5)
print(np.dot(numpy_array1,numpy_array2))
Output:
[[ 5 6 7 8 9]
[ 15 20 25 30 35]
[ 25 34 43 52 61]
[ 35 48 61 74 87]
[ 45 62 79 96 113]]

-Matrisin transpozu

numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(5,2)
print(numpy_array)
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
print(numpy_array.T)
Output:
[[0 2 4 6 8]
[1 3 5 7 9]]

4)Numpy’da Koşul İfadeleri ile Çalışmak

numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
boolean_array = numpy_array >= 5
print(boolean_array)
Output :
[False False False False False True True True True True]
# 5 ve 5'ten büyük elemanlar
print(numpy_array[boolean_array])
Output:
[5 6 7 8 9]
# 5 ten küçük elemanlar
print(numpy_array<5)
print(numpy_array[numpy_array<5])
Output:
[ True True True True True False False False False False]
[0 1 2 3 4]

Hiç yorum yok:

Yorum Gönder

Post Top Ad

Responsive Ads Here