Kaggle competition: Schnell-Mal-Klassifizieren
train_df['Age'].value_counts()<feature_engineering>
pd.read_csv?
data = [train_df, test_df]
for dataset in data:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[(dataset['Fare'] > 31)&(dataset['Fare'] <= 99), 'Fare'] = 3
dataset.loc[(dataset['Fare'] > 99)&(dataset['Fare'] <= 250), 'Fare'] = 4
dataset.loc[ dataset['Fare'] > 250, 'Fare'] = 5
dataset['Fare'] = dataset['Fare'].astype(int )<feature_engineering>
fn = '.. /input/male-daan-schnell-mal-klassifizieren/train.csv'
df = pd.read_csv(fn)
 df.head(5 )
data = [train_df, test_df]
for dataset in data:
dataset['Age_Class']= dataset['Age']* dataset['Pclass']<data_type_conversions>
df = pd.read_csv(fn,index_col='Id')
 df.head()
for dataset in data:
dataset['Fare_Per_Person'] = dataset['Fare']/(dataset['relatives']+1)
 dataset['Fare_Per_Person'] = dataset['Fare_Per_Person'].astype(int )<train_model>
data = df.values
data
train_df.head(20 )<prepare_x_and_y>
nTrain=5000
Xtrain = data[:nTrain,:-1]
ytrain = data[:nTrain,-1]
Xtest = data[nTrain:,:-1]
ytest = data[nTrain:,-1]
Xtrain.shape,ytrain.shape,Xtest.shape,ytest.shape
X_train = train_df.drop("Survived", axis=1)
 Y_train = train_df["Survived"]
X_test = test_df.drop("PassengerId", axis=1 ).copy()<compute_train_metric>
ytrain = ytrain.astype('int')
 ytest = ytest.astype('int')
 ytrain, ytrain.dtype
sgd = linear_model.SGDClassifier(max_iter=5, tol=None)
 sgd.fit(X_train, Y_train)
 Y_pred = sgd.predict(X_test)
 sgd.score(X_train, Y_train)
 acc_sgd = round(sgd.score(X_train, Y_train)* 100, 2)
 print(round(acc_sgd,2,), "%" )<compute_train_metric>
k=7
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, Y_train)
 Y_prediction = random_forest.predict(X_test)
 random_forest.score(X_train, Y_train)
 acc_random_forest = round(random_forest.score(X_train, Y_train)* 100, 2)
 print(round(acc_random_forest,2,), "%" )<compute_train_metric>
zufälliger_Index = np.random.randint(low=0,high=len(ytest))
zufälliger_Index
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
 Y_pred = logreg.predict(X_test)
 acc_log = round(logreg.score(X_train, Y_train)* 100, 2)
 print(round(acc_log,2,), "%" )<predict_on_test>
Testzeile = Xtest[zufälliger_Index,:]
Testlabel = ytest[zufälliger_Index]
Testzeile,Testlabel
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, Y_train)
 Y_pred = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, Y_train)* 100, 2)
 print(round(acc_knn,2,), "%" )<compute_train_metric>
distanz = np.sqrt(((Xtrain - Testzeile)**2 ).sum(axis=1))
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_pred = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, Y_train)* 100, 2)
 print(round(acc_gaussian,2,), "%" )<compute_train_metric>
np.sort(distanz)[:k]
perceptron = Perceptron(max_iter=5)
 perceptron.fit(X_train, Y_train)
 Y_pred = perceptron.predict(X_test)
 acc_perceptron = round(perceptron.score(X_train, Y_train)* 100, 2)
 print(round(acc_perceptron,2,), "%" )<compute_train_metric>
np.argsort(distanz)[:k]
linear_svc = LinearSVC()
linear_svc.fit(X_train, Y_train)
 Y_pred = linear_svc.predict(X_test)
 acc_linear_svc = round(linear_svc.score(X_train, Y_train)* 100, 2)
 print(round(acc_linear_svc,2,), "%" )<choose_model_class>
np.argsort([1,7,4,9] )
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
 Y_pred = decision_tree.predict(X_test)
 acc_decision_tree = round(decision_tree.score(X_train, Y_train)* 100, 2)
 print(round(acc_decision_tree,2,), "%" )<compute_train_metric>
distanz[np.argsort(distanz)[:k]]
np.min(distanz )
rf = RandomForestClassifier(n_estimators=100)
 scores = cross_val_score(rf, X_train, Y_train, cv=10, scoring = "accuracy" )<compute_test_metric>
ytrain[np.argsort(distanz)[:k]]
print("Scores:", scores)
 print("Mean:", scores.mean())
 print("Standard Deviation:", scores.std() )<create_dataframe>
[1,0,1,0,1,1,0]
importances = pd.DataFrame({'feature':X_train.columns,'importance':np.round(random_forest.feature_importances_,3)})
 importances = importances.sort_values('importance',ascending=False ).set_index('feature' )<drop_column>
np.around(np.mean(np.array([1,0,1,0,1,1,0])) )
train_df = train_df.drop("not_alone", axis=1)
 test_df = test_df.drop("not_alone", axis=1)
 train_df = train_df.drop("Parch", axis=1)
 test_df = test_df.drop("Parch", axis=1 )<train_model>
np.median([1,0,1,0,1,1,0] )
random_forest = RandomForestClassifier(n_estimators=100, oob_score = True)
 random_forest.fit(X_train, Y_train)
 Y_prediction = random_forest.predict(X_test)
 random_forest.score(X_train, Y_train)
 acc_random_forest = round(random_forest.score(X_train, Y_train)* 100, 2)
 print(round(acc_random_forest,2,), "%" )<compute_test_metric>
a= np.array([1,4,8,2])
 sorted_indices = np.argsort(a)
 sorted_indices, a[sorted_indices]
print("oob score:", round(random_forest.oob_score_, 4)*100, "%" )<choose_model_class>
sorted_indices = np.argsort(distanz)
 distanz[sorted_indices]
random_forest = RandomForestClassifier(criterion = "gini",
min_samples_leaf = 1,
min_samples_split = 10,
n_estimators=100,
max_features='auto',
oob_score=True,
random_state=1,
n_jobs=-1)
 random_forest.fit(X_train, Y_train)
 Y_prediction = random_forest.predict(X_test)
 random_forest.score(X_train, Y_train)
 print("oob score:", round(random_forest.oob_score_, 4)*100, "%" )<compute_train_metric>
distanz[sorted_indices[:k]], sorted_indices[:k], ytrain[sorted_indices[:k]]
predictions = cross_val_predict(random_forest, X_train, Y_train, cv=3)
 confusion_matrix(Y_train, predictions )<compute_test_metric>
Auftretende_Trainingslabels = ytrain[sorted_indices[:k]]
print("Precision:", precision_score(Y_train, predictions))
print("Recall:",recall_score(Y_train, predictions))<compute_test_metric>
if np.mean(Auftretende_Trainingslabels)>=0.5:
yhat = 1
else:
yhat = 0
yhat
f1_score(Y_train, predictions )<find_best_params>
print('Häufigstes Label in [1,0,1,0,0]:',np.argmax(np.bincount([1,0,1,0,0])))
 print('Häufigstes Label in [1,0,1,0,1]:',np.argmax(np.bincount([1,0,1,0,1])) )
y_scores = random_forest.predict_proba(X_train)
 y_scores = y_scores[:,1]
precision, recall, threshold = precision_recall_curve(Y_train, y_scores )<find_best_params>
np.bincount([0,3,3,2,1,1] )
false_positive_rate, true_positive_rate, thresholds = roc_curve(Y_train, y_scores )<compute_test_metric>
np.bincount([0,3,3,3,2,2,2,2,2,2,2,1,1,4,4,4,4,5,6] )
r_a_score = roc_auc_score(Y_train, y_scores)
 print("ROC-AUC-Score:", r_a_score )<save_to_csv>
np.argmax(np.bincount([0,3,3,3,2,2,2,2,2,2,2,1,1,4,4,4,4,5,6]))
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_prediction
})
 submission.to_csv('submission.csv', index=False )<import_modules>
def kNN_Vorhersage(Xtrain,Testzeile,k):
distanz =(((Xtrain - Testzeile)**2 ).sum(axis=1)) **0.5
sorted_indices = np.argsort(distanz)
 Auftretende_Trainingslabels = ytrain[sorted_indices[:k]]
return np.argmax(np.bincount(Auftretende_Trainingslabels))
zufälliger_Index = np.random.randint(low=0,high=len(ytest))
Testzeile = Xtest[zufälliger_Index,:]
yhat = kNN_Vorhersage(Xtrain,Testzeile,k)
 print(f'Vorhersage für {Testzeile}:{yhat}' )
make_scorer,classification_report,roc_auc_score,roc_curve,
average_precision_score,precision_recall_curve)
 pd.set_option('display.max_columns', None)
 warnings.filterwarnings("ignore")
 RANDOM_SEED = 101 
<load_from_csv>
clf = DecisionTreeClassifier(max_depth=5)
sub_file = pd.read_csv("/kaggle/input/titanic/gender_submission.csv")
 sub_file.head()<load_from_csv>
df_train = pd.read_csv('.. /input/male-daan-schnell-mal-klassifizieren/train.csv',index_col='Id')
 df_test = pd.read_csv('.. /input/male-daan-schnell-mal-klassifizieren/test.csv',index_col='Id')
 df_train.head(3 )
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 train.head()<load_from_csv>
Xtrain = df_train.values[:,:-1]
ytrain = df_train.values[:,-1]
Xtest = df_test.values
val = pd.read_csv("/kaggle/input/titanic/test.csv")
 val.head()<count_missing_values>
clf.fit(Xtrain,ytrain )
train.isnull().mean()<define_variables>
yhat = clf.predict(Xtest )
<count_values><EOS> 
ser = pd.Series(yhat,name='y' ).astype('int')
 ser.index.name='Id'
ser.to_csv('Submission.csv',header=True)
 !head Submission.csv