import numpy as np
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import pandas as pd
import librosa
# Veri setini yükle
df = pd.read_csv('C:/Users/mfurk/Desktop/voice.csv')
df.head()
# Veriyi X (özellik matrisi) ve y (hedef değişken) olarak ayır
X = df.drop('label', axis=1)
y = df['label']
# Eğitim ve test setlerini ayır
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Random Forest modelini eğit
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
# Extra Trees modelini eğit
et_model = ExtraTreesClassifier(n_estimators=100, random_state=42)
et_model.fit(X_train, y_train)
# Eğitim sonuçlarını değerlendir
rf_predictions = rf_model.predict(X_test)
et_predictions = et_model.predict(X_test)
print("Random Forest Accuracy:", accuracy_score(y_test, rf_predictions))
print("Extra Trees Accuracy:", accuracy_score(y_test, et_predictions))
from scipy.stats import kurtosis, mode
def extract_features_from_audio(audio_path):
# Ses dosyasını yükle
y, sr = librosa.load(audio_path, sr=None)
# Özellikleri çıkar
meanfreq = librosa.feature.spectral_centroid(y=y)[0].mean()
sd = librosa.feature.spectral_bandwidth(y=y)[0].std()
median = np.median(y)
q25 = np.percentile(y, 25)
q75 = np.percentile(y, 75)
iqr = q75 - q25
kurt = kurtosis(y)
mode_val, _ = mode(y)
centroid = librosa.feature.spectral_centroid(y=y, sr=sr)[0].mean()
# Çıkarılan özellikleri bir dizi olarak döndür
features = [meanfreq, median, sd, q25, q75, iqr, kurt, mode_val, centroid]
return features
# Yeni bir ses dosyasından özellikleri çıkar
new_audio_features = extract_features_from_audio('C:/Users/mfurk/Desktop/denizal.mp3')
# Random Forest modeli ile tahmin yap
rf_prediction = rf_model.predict([new_audio_features])[0]
# Extra Trees modeli ile tahmin yap
et_prediction = et_model.predict([new_audio_features])[0]
print("Random Forest Prediction:", rf_prediction)
print("Extra Trees Prediction:", et_prediction)