magic
August 4, 2024
[1]: import pandas as pd
import [Link] as plt
import numpy as np
from [Link] import StandardScaler
from imblearn.over_sampling import RandomOverSampler
[2]: cols=["fLength","fWidth","fSize","fConc","fConcl","fAsym","fM3long","fM3trans","fAlpha","FDist
df= pd.read_csv(r"C:\New folder\magic+gamma+telescope\[Link]",names=cols)
[Link]()
[2]: fLength fWidth fSize fConc fConcl fAsym fM3long fM3trans \
0 28.7967 16.0021 2.6449 0.3918 0.1982 27.7004 22.0110 -8.2027
1 31.6036 11.7235 2.5185 0.5303 0.3773 26.2722 23.8238 -9.9574
2 162.0520 136.0310 4.0612 0.0374 0.0187 116.7410 -64.8580 -45.2160
3 23.8172 9.5728 2.3385 0.6147 0.3922 27.2107 -6.4633 -7.1513
4 75.1362 30.9205 3.1611 0.3168 0.1832 -5.5277 28.5525 21.8393
fAlpha FDist class
0 40.0920 81.8828 g
1 6.3609 205.2610 g
2 76.9600 256.7880 g
3 10.4490 116.7370 g
4 4.6480 356.4620 g
[3]: df["class"]=(df["class"]=="g").astype(int)
[Link]()
[3]: fLength fWidth fSize fConc fConcl fAsym fM3long fM3trans \
0 28.7967 16.0021 2.6449 0.3918 0.1982 27.7004 22.0110 -8.2027
1 31.6036 11.7235 2.5185 0.5303 0.3773 26.2722 23.8238 -9.9574
2 162.0520 136.0310 4.0612 0.0374 0.0187 116.7410 -64.8580 -45.2160
3 23.8172 9.5728 2.3385 0.6147 0.3922 27.2107 -6.4633 -7.1513
4 75.1362 30.9205 3.1611 0.3168 0.1832 -5.5277 28.5525 21.8393
fAlpha FDist class
0 40.0920 81.8828 1
1 6.3609 205.2610 1
1
2 76.9600 256.7880 1
3 10.4490 116.7370 1
4 4.6480 356.4620 1
[15]: for label in cols[:-1]:
[Link](df[df["class"]==1][label],color="blue",label="alpha",alpha=0.
↪7,density=True)
[Link](df[df["class"]==0][label],color="red",label="alpha",alpha=0.
↪7,density=True)
[Link](label)
[Link]("probability")
[Link](label)
[Link]()
[Link]()
2
3
4
5
6
7
8
9
10
[4]: train,valid,test=[Link]([Link](frac=1),[int(0.6*len(df)),int(0.8*len(df))])
c:\Users\aasi4\AppData\Local\Programs\Python\Python311\Lib\site-
packages\numpy\core\[Link]: FutureWarning: '[Link]' is
deprecated and will be removed in a future version. Please use
'[Link]' instead.
return bound(*args, **kwds)
[5]: def scale_dataset(dataframe,oversample=False):
x=dataframe[[Link][:-1]].values
y=dataframe[[Link][-1]].values
scaler=StandardScaler()
x=scaler.fit_transform(x)
if oversample:
ros=RandomOverSampler()
x,y=ros.fit_resample(x,y)
data= [Link]((x,[Link](y,(-1,1))))
return data,x,y
11
[6]: train,x_train,y_train=scale_dataset(train,oversample=True)
valid,x_valid,y_valid=scale_dataset(valid,oversample=False)
test,x_test,y_test=scale_dataset(test,oversample=False)
[7]: from [Link] import KNeighborsClassifier
from [Link] import classification_report
[8]: knn_model=KNeighborsClassifier(n_neighbors=5)
knn_model.fit(x_train,y_train)
[8]: KNeighborsClassifier()
[9]: y_pred=knn_model.predict(x_test)
print(classification_report(y_test,y_pred))
precision recall f1-score support
0 0.74 0.73 0.73 1370
1 0.85 0.86 0.85 2434
accuracy 0.81 3804
macro avg 0.80 0.79 0.79 3804
weighted avg 0.81 0.81 0.81 3804
[10]: from sklearn.naive_bayes import GaussianNB
nb_model=GaussianNB()
nb_model=nb_model.fit(x_train,y_train)
[11]: y_pred=nb_model.predict(x_test)
print(classification_report(y_test,y_pred))
precision recall f1-score support
0 0.63 0.39 0.48 1370
1 0.72 0.87 0.79 2434
accuracy 0.70 3804
macro avg 0.68 0.63 0.63 3804
weighted avg 0.69 0.70 0.68 3804
[12]: from sklearn.linear_model import LogisticRegression
lg_model=LogisticRegression()
lg_model=lg_model.fit(x_train,y_train)
12
[13]: y_pred=lg_model.predict(x_test)
print(classification_report(y_test,y_pred))
precision recall f1-score support
0 0.68 0.72 0.70 1370
1 0.84 0.81 0.82 2434
accuracy 0.78 3804
macro avg 0.76 0.77 0.76 3804
weighted avg 0.78 0.78 0.78 3804
[14]: from [Link] import SVC
svm_model=SVC()
svm_model=svm_model.fit(x_train,y_train)
y_pred=svm_model.predict(x_test)
print(classification_report(y_test,y_pred))
precision recall f1-score support
0 0.82 0.79 0.80 1370
1 0.88 0.90 0.89 2434
accuracy 0.86 3804
macro avg 0.85 0.84 0.85 3804
weighted avg 0.86 0.86 0.86 3804
13