<jupyter_start><jupyter_text>Praca domowa nr 4Jakub Szypuła  Załadowanie danych i pakietów  Apartments<jupyter_code>import pandas as pd
import numpy as np
apartments = pd.read_csv('https://raw.githubusercontent.com/mini-pw/2020L-WUM/master/Prace_domowe/Praca_domowa4/Grupa1/apartments.csv')
apartments.head()<jupyter_output><empty_output><jupyter_text>AoE2 Voobly DataJako drugi zbiór danych postanowiłem wybrać zbiór danych dotyczący meczy 1 na 1 w grze strategicznej Age of Empires II, rozgrywanej przez platformę Voobly. Jako zmienną celu przyjąłem `player_1_wins` oznaczającą zwycięstwo gracza nr 1.Ze względu na ogromny rozmiar danych, postanowiłem je określić do losowych 1000 obserwacji.Źródło danych: https://blog.macuyiko.com/post/2018/predicting-voobly-age-of-empires-2-matches.html<jupyter_code>aoe2 = pd.read_csv('https://github.com/Macuyiko/aoe2predict/raw/master/prepared_data.csv.gz')
aoe2.shape
aoe2 = aoe2.sample(n=1000, random_state=11)
aoe2.shape
aoe2.head()
aoe2braki=aoe2.isnull().mean() 
aoe2braki.sum()<jupyter_output><empty_output><jupyter_text>Na szczęście w zbiorze danych nie ma żadnych braków.<jupyter_code>from sklearn.model_selection import train_test_split
y_ap = apartments[["district"]]
X_ap = apartments.drop(["district"], axis=1)
X_train_ap, X_test_ap, y_train_ap, y_test_ap = train_test_split(X_ap, y_ap, test_size=0.15, random_state=111)
y_train_ap = y_train_ap.values.ravel()
y_test_ap = y_test_ap.values.ravel()
y_aoe2 = aoe2[["player_1_wins"]]
X_aoe2 = aoe2.drop(["player_1_wins"], axis=1)
X_train_aoe2, X_test_aoe2, y_train_aoe2, y_test_aoe2 = train_test_split(X_aoe2, y_aoe2, test_size = 0.15, random_state = 111)
y_train_aoe2 = y_train_aoe2.values.ravel()
y_test_aoe2 = y_test_aoe2.values.ravel()<jupyter_output><empty_output><jupyter_text>SVM bez skalowania<jupyter_code>from sklearn import svm
from sklearn.metrics import accuracy_score
model = svm.SVC()<jupyter_output><empty_output><jupyter_text>Apartments<jupyter_code>model.fit(X_train_ap, y_train_ap)
y_predicted_ap = model.predict(X_test_ap)
accuracy_score(y_test_ap, y_predicted_ap)<jupyter_output><empty_output><jupyter_text>Strojenie<jupyter_code>from sklearn.model_selection import RandomizedSearchCV

C = np.arange(0.1, 15, 0.1)
degree = np.arange(1, 6, 1)
gamma = ["scale", "auto"] + np.arange(0.1, 0.5, 0.1).tolist()
hyperF = dict(C = C, degree = degree, gamma = gamma)

grid = RandomizedSearchCV(model, hyperF, cv = 11, random_state = 11)
grid.fit(X_train_ap, y_train_ap)
grid.best_params_
svm_ap_strj = svm.SVC(gamma = 0.11, degree = 4, C = 14.5)
svm_ap_strj.fit(X_train_ap, y_train_ap)
y_predicted_ap_strj = svm_ap_strj.predict(X_test_ap)
accuracy_score(y_test_ap, y_predicted_ap_strj)<jupyter_output><empty_output><jupyter_text>Automatyczne strojenie znacząco pogorszyło wynik. Być może to kwestia ustalonego random_state.  AoE 2<jupyter_code>model.fit(X_train_aoe2, y_train_aoe2)
y_predicted_aoe2 = model.predict(X_test_aoe2)
accuracy_score(y_test_aoe2, y_predicted_aoe2)<jupyter_output><empty_output><jupyter_text>Strojenie<jupyter_code>grid = RandomizedSearchCV(model, hyperF, cv = 11, random_state = 11)
grid.fit(X_train_aoe2, y_train_aoe2)
grid.best_params_
svm_aoe2_strj = svm.SVC(gamma = 0.22999999999999995, degree = 2, C = 4.9)
svm_aoe2_strj.fit(X_train_aoe2, y_train_aoe2)
y_predicted_aoe2_strj = svm_aoe2_strj.predict(X_test_aoe2)
accuracy_score(y_test_ap, y_predicted_aoe2_strj)<jupyter_output><empty_output><jupyter_text>Stwierdzenie, że losowe strojenie pogorszyło wynik byłoby tutaj niedopowiedzeniem.  Inne jądra Liniowe Apartments<jupyter_code>model = svm.SVC(kernel='linear')
model.fit(X_train_ap, y_train_ap)
y_predicted_ap = model.predict(X_test_ap)
accuracy_score(y_test_ap, y_predicted_ap)<jupyter_output><empty_output><jupyter_text>AoE2<jupyter_code>model.fit(X_train_aoe2, y_train_aoe2)
y_predicted_aoe2 = model.predict(X_test_aoe2)
accuracy_score(y_test_aoe2, y_predicted_aoe2)<jupyter_output><empty_output><jupyter_text>Powyższy kod wykonywał się ponad pół godziny i nie wykonał się, dlatego zrezygnowałem z analizy tego jądra.  Poly Apartments<jupyter_code>model = svm.SVC(kernel='poly')
model.fit(X_train_ap, y_train_ap)
y_predicted_ap = model.predict(X_test_ap)
accuracy_score(y_test_ap, y_predicted_ap)<jupyter_output><empty_output><jupyter_text>AoE2<jupyter_code>model.fit(X_train_aoe2, y_train_aoe2)
y_predicted_aoe2 = model.predict(X_test_aoe2)
accuracy_score(y_test_aoe2, y_predicted_aoe2)<jupyter_output><empty_output><jupyter_text>Sigmoid Apartments<jupyter_code>model = svm.SVC(kernel='sigmoid')
model.fit(X_train_ap, y_train_ap)
y_predicted_ap = model.predict(X_test_ap)
accuracy_score(y_test_ap, y_predicted_ap)<jupyter_output><empty_output><jupyter_text>AoE2<jupyter_code>model.fit(X_train_aoe2, y_train_aoe2)
y_predicted_aoe2 = model.predict(X_test_aoe2)
accuracy_score(y_test_aoe2, y_predicted_aoe2)<jupyter_output><empty_output><jupyter_text>PodsumowanieWykorzystanie innych jąder nie poprawiło zbytnio sytuacji, jeżęli wręcz jej nie pogorszyło.  Skalowanie  Apartments<jupyter_code>from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled_ap = scaler.fit_transform(X_ap)
X_scaled_ap = pd.DataFrame(X_scaled_ap, columns = X_ap.columns)
X_train_ap, X_test_ap, y_train_ap, y_test_ap = train_test_split(X_scaled_ap, y_ap, test_size=0.15, random_state=111)
y_train_ap = y_train_ap.values.ravel()
y_test_ap = y_test_ap.values.ravel()
model = svm.SVC()
model.fit(X_train_ap, y_train_ap)
y_predicted = model.predict(X_test_ap)
accuracy_score(y_test_ap, y_predicted)<jupyter_output><empty_output><jupyter_text>Strojenie<jupyter_code>grid = RandomizedSearchCV(model, hyperF, cv = 11, random_state = 11)
grid.fit(X_train_ap, y_train_ap)
grid.best_params_
model = svm.SVC(gamma = 0.1, C = 1.2)
model.fit(X_train_ap, y_train_ap)
y_predicted = model.predict(X_test_ap)
accuracy_score(y_test_ap, y_predicted)<jupyter_output><empty_output><jupyter_text>AoE 2<jupyter_code>X_scaled_aoe2 = scaler.fit_transform(X_aoe2)
X_train_aoe2, X_test_aoe2, y_train_aoe2, y_test_aoe2 = train_test_split(X_scaled_aoe2, y_aoe2, test_size=0.15, random_state=111)
y_train_aoe2 = y_train_aoe2.values.ravel()
y_test_aoe2 = y_test_aoe2.values.ravel()
model = svm.SVC()
model.fit(X_train_aoe2, y_train_aoe2)
y_predicted = model.predict(X_test_aoe2)
accuracy_score(y_test_aoe2, y_predicted)<jupyter_output><empty_output><jupyter_text>Strojenie<jupyter_code>grid = RandomizedSearchCV(model, hyperF, cv = 11, random_state = 11)
grid.fit(X_train_aoe2, y_train_aoe2)
grid.best_params_
model = svm.SVC(gamma = 0.1, degree = 1, C = 1.2)
model.fit(X_train_aoe2, y_train_aoe2)
y_predicted = model.predict(X_test_aoe2)
accuracy_score(y_test_aoe2, y_predicted)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10529446757882213, 'fraction_numerical': 0.030933967876264127, 'mean_word_length': 9.458786936236391, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '346423', 'license': 'Apache-2.0', 'n_tokens_mistral': 3134, 'n_tokens_neox': 3033, 'n_words': 402, 'path': 'Prace_domowe/Praca_domowa4/Grupa3/SzypulaJakub/SzypulaJakubPD4.ipynb', 'repo_name': 'niladrem/2020L-WUM', 'size': '6724'}