Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Herausforderung: Lösung der Aufgabe mit Korrelation | Kovarianz und Korrelation
Grundlagen der Wahrscheinlichkeitstheorie

book
Herausforderung: Lösung der Aufgabe mit Korrelation

Aufgabe

Swipe to start coding

Eine der wichtigsten Aufgaben im Machine Learning besteht darin, ein lineares Regressionsmodell zu erstellen (weitere Informationen finden Sie im Linear Regression with Python-Kurs).

Da in diesem Modell eine lineare Funktion verwendet wird, können wir die Korrelation zwischen Merkmalen und Zielwert nutzen, um anzuzeigen, wie signifikant ein bestimmtes Merkmal für dieses Modell ist.

Wir werden nun den 'Heart Disease Dataset' verwenden: Er enthält 14 Merkmale, einschließlich des vorhergesagten Attributs, welches auf das Vorhandensein von Herzkrankheiten beim Patienten hinweist. Ihre Aufgabe besteht darin, die Signifikanz der Attribute mithilfe der Korrelation zu berechnen:

  1. Berechnen Sie die Korrelationen zwischen den Merkmalen und dem Zielwert.
  2. Geben Sie diese Korrelationen in aufsteigender Reihenfolge aus.

Lösung

import pandas as pd
import numpy as np

# Load the heart disease dataset into a Pandas DataFrame
heart_df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/Probability_basics_content/heart.csv')

# Extract the target variable and other features
target = heart_df['target']
features = heart_df.drop('target', axis=1)

# Calculate the correlations
correlations = np.corrcoef(features, target, rowvar=False)[:-1, -1]

# Sort the correlations in descending order
sorted_correlations = np.sort(correlations)

# Print the sorted correlations
for correlation in sorted_correlations:
feature_name = features.columns[np.where(correlations == correlation)][0]
print(f'Correlation between {feature_name} and target is {correlation:.2f}')

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 5. Kapitel 3
import pandas as pd
import numpy as np

# Load the heart disease dataset into a Pandas DataFrame
heart_df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/Probability_basics_content/heart.csv')

# Extract the target variable and other features
target = heart_df['target']
features = heart_df.drop('target', axis=1)

# Calculate the correlations
# rowvar=False is specified when attributes are stored in columns
correlations = np.___(___, ___, rowvar=False)[:-1, -1]

# Sort the correlations in descending order
sorted_correlations = np.___(correlations)

# Print the sorted correlations
for correlation in sorted_correlations:
feature_name = features.columns[np.where(correlations == correlation)][0]
print(f'Correlation between {feature_name} and target is {correlation:.2f}')
toggle bottom row
some-alt