Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Statistische Bewerkingen | Wiskunde met NumPy
Ultieme NumPy

book
Statistische Bewerkingen

Het uitvoeren van verschillende statistische bewerkingen op arrays is essentieel voor data-analyse en machine learning. NumPy biedt functies en methoden om ze effectief uit te voeren.

Maten van Centrale Neiging

Maten van centrale neiging vertegenwoordigen een centrale of representatieve waarde binnen een kansverdeling. Meestal bereken je deze maten echter voor een bepaalde steekproef.

Hier zijn de twee belangrijkste maten:

  • Gemiddelde: de som van alle waarden gedeeld door het totale aantal waarden;

  • Mediaan: De middelste waarde in een gesorteerde steekproef.

NumPy biedt mean() en median() functies voor het berekenen van respectievelijk het gemiddelde en de mediaan:

import numpy as np
sample = np.array([10, 25, 15, 30, 20, 10, 2])
# Calculating the mean
sample_mean = np.mean(sample)
print(f'Sorted sample: {np.sort(sample)}')
# Calculating the median
sample_median = np.median(sample)
print(f'Mean: {sample_mean}, median: {sample_median}')
12345678
import numpy as np sample = np.array([10, 25, 15, 30, 20, 10, 2]) # Calculating the mean sample_mean = np.mean(sample) print(f'Sorted sample: {np.sort(sample)}') # Calculating the median sample_median = np.median(sample) print(f'Mean: {sample_mean}, median: {sample_median}')
copy

We hebben ook het gesorteerde monster weergegeven, zodat je duidelijk de mediaan kunt zien. Ons monster heeft een oneven aantal elementen (7), dus de mediaan is eenvoudigweg het element op index (n + 1) / 2 in het gesorteerde monster, waarbij n de grootte van het monster is.

Opmerking

Wanneer het monster een even aantal elementen heeft, is de mediaan het gemiddelde van de elementen op index n / 2 en n / 2 - 1 in het gesorteerde monster.

import numpy as np
sample = np.array([1, 2, 8, 10, 15, 20, 25, 30])
sample_median = np.median(sample)
print(f'Median: {sample_median}')
1234
import numpy as np sample = np.array([1, 2, 8, 10, 15, 20, 25, 30]) sample_median = np.median(sample) print(f'Median: {sample_median}')
copy

Ons monster is al gesorteerd en heeft 8 elementen, dus n / 2 - 1 = 3 en sample[3] is 10. n / 2 = 4 en sample[4] is 15. Daarom is onze mediaan (10 + 15) / 2 = 12.5.

Maatregelen van Spreiding

Twee maatregelen van spreiding zijn variantie en standaarddeviatie. Variantie meet hoe verspreid de gegevens zijn. Het is gelijk aan het gemiddelde van de kwadratische verschillen van elke waarde ten opzichte van het gemiddelde.

Standaarddeviatie is de vierkantswortel van de variantie. Het biedt een maat voor hoe verspreid de gegevens zijn in dezelfde eenheden als de gegevens.

NumPy heeft de var() functie om de variantie van de steekproef te berekenen en de std() functie om de standaarddeviatie van de steekproef te berekenen:

import numpy as np
sample = np.array([10, 25, 15, 30, 20, 10, 2])
# Calculating the variance
sample_variance = np.var(sample)
# Calculating the standard deviation
sample_std = np.std(sample)
print(f'Variance: {sample_variance}, standard deviation: {sample_std}')
1234567
import numpy as np sample = np.array([10, 25, 15, 30, 20, 10, 2]) # Calculating the variance sample_variance = np.var(sample) # Calculating the standard deviation sample_std = np.std(sample) print(f'Variance: {sample_variance}, standard deviation: {sample_std}')
copy

Berekeningen in Hogere Dimensionale Arrays

Al deze functies hebben een tweede parameter axis. De standaard waarde is None, wat betekent dat de maatregel wordt berekend over een afgevlakte array (zelfs als de oorspronkelijke array 2D of hoger dimensionaal is).

Je kunt ook de exacte as specificeren waarlangs de maatregel moet worden berekend:

import numpy as np
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
# Calculating the mean in a flattened array
print(np.mean(array_2d))
# Calculating the mean along axis 0
print(np.mean(array_2d, axis=0))
# Calculating the mean along axis 1
print(np.mean(array_2d, axis=1))
12345678
import numpy as np array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Calculating the mean in a flattened array print(np.mean(array_2d)) # Calculating the mean along axis 0 print(np.mean(array_2d, axis=0)) # Calculating the mean along axis 1 print(np.mean(array_2d, axis=1))
copy

De onderstaande afbeelding toont de structuur van de exam_scores array die in de taak wordt gebruikt:

Taak

Swipe to start coding

Je analyseert de exam_scores array, een 2D-array van gesimuleerde testresultaten voor 2 studenten (2 rijen) over 5 verschillende examens (5 kolommen).

  1. Bereken de gemiddelde score voor elke student door het tweede sleutelwoordargument op te geven.

  2. Bereken de mediaan van alle scores.

  3. Bereken de variantie van alle scores.

  4. Bereken de standaarddeviatie van alle scores.

Oplossing

import numpy as np
# Simulated test scores of 2 students for five different exams
exam_scores = np.array([[85, 90, 78, 92, 88], [72, 89, 65, 78, 92]])
# Calculate the mean score for each student
mean_scores = np.mean(exam_scores, axis=1)
print(f'Mean score for each student: {mean_scores}')
# Calculate the median score for all scores
median_score = np.median(exam_scores)
print(f'Median score for all scores: {median_score}')
# Calculate the variance for all scores
scores_variance = np.var(exam_scores)
print(f'Variance for all scores: {scores_variance}')
# Calculate the standard deviation for all scores
scores_std = np.std(exam_scores)
print(f'Standard deviation for all scores: {scores_std}')
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 3
single

single

import numpy as np
# Simulated test scores of 2 students for five different exams
exam_scores = np.array([[85, 90, 78, 92, 88], [72, 89, 65, 78, 92]])
# Calculate the mean score for each student
mean_scores = ___.___(___, ___=___)
print(f'Mean score for each student: {mean_scores}')
# Calculate the median score of all scores
median_score = ___.___(___)
print(f'Median score for all scores: {median_score}')
# Calculate the variance of all scores
scores_variance = ___.___(___)
print(f'Variance for all scores: {scores_variance}')
# Calculate the standard deviation of all scores
scores_std = ___.___(___)
print(f'Standard deviation for all scores: {scores_std}')

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt