<jupyter_start><jupyter_text>- Classification algorithms- Submitted by Kaushik - email- <EMAIL>- Used Logistic Regression, Decision Tree, Random Forest & Gradient Boosting.- Used Logistics Regression & Decision tree are classification algorithm and Random forest is a parllel ensembler and gradient boosting is a sequential ensembler.- Using Decision tree got the accuracy of 1.0 in both training and validation set using Gradient boosting ensembler alos the accuracy score is 1.0 .- The final output I have submitted using ensembler.- M=1, B=0<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
ds = pd.read_csv("Train_Dataset-01.csv")
ds1 = pd.read_csv("Test_Dataset-01.csv")
ds.head()<jupyter_output><empty_output><jupyter_text>Data preprocessing<jupyter_code>ds.columns
ds.drop(["id"],axis=1,inplace=True)
ds.head()
ds_null=ds.columns[ds.isnull().any()]
ds[ds_null].isnull().sum()
ds["texture_mean"].fillna(method='ffill',inplace=True)
ds["smoothness_mean"].fillna(method='ffill',inplace=True)
ds["compactness_mean"].fillna(method='ffill',inplace=True)
ds["symmetry_mean"].fillna(method='ffill',inplace=True)
ds["fractal_dimension_mean"].fillna(method='ffill',inplace=True)
ds["radius_se"].fillna(method='ffill',inplace=True)
ds["smoothness_se"].fillna(method='ffill',inplace=True)
ds["compactness_se"].fillna(method='ffill',inplace=True)
ds["concavity_se"].fillna(method='ffill',inplace=True)
ds["concave points_se"].fillna(method='ffill',inplace=True)
ds["symmetry_se"].fillna(method='ffill',inplace=True)
ds["fractal_dimension_se"].fillna(method='ffill',inplace=True)
ds["perimeter_worst"].fillna(method='ffill',inplace=True)
ds["area_worst"].fillna(method='ffill',inplace=True)
ds["smoothness_worst"].fillna(method='ffill',inplace=True)
ds["concavity_worst"].fillna(method='ffill',inplace=True)
ds["concave points_worst"].fillna(method='ffill',inplace=True)
ds["symmetry_worst"].fillna(method='ffill',inplace=True)
ds["fractal_dimension_worst"].fillna(method='ffill',inplace=True)
ds.dtypes
ds_categ = list(ds.select_dtypes(exclude = ["number"]).columns)
ds_categ
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for i in ds_categ:
    print(ds[i].unique())
    ds[i] = le.fit_transform(ds[i])
ds.head()<jupyter_output><empty_output><jupyter_text>Feature Engineering Outlier Detection and treatment<jupyter_code>ds['diagnosis'].plot.box()
ds['radius_mean'].plot.box()
ds.loc[ds['radius_mean']>23,'radius_mean']=np.mean(ds['radius_mean'])
ds['radius_mean'].plot.box()
ds['texture_mean'].plot.box()
ds.loc[ds['texture_mean']>28,'texture_mean']=np.mean(ds['texture_mean'])
ds['texture_mean'].plot.box()
ds['area_mean'].plot.box()
ds.loc[ds['area_mean']>900,'area_mean']=np.mean(ds['area_mean'])
ds['area_mean'].plot.box()
ds['smoothness_mean'].plot.box()
ds.loc[ds['smoothness_mean']>0.13,'smoothness_mean']=np.mean(ds['smoothness_mean'])
ds['smoothness_mean'].plot.box()
ds['compactness_mean'].plot.box()
ds.loc[ds['compactness_mean']>0.22,'compactness_mean']=np.mean(ds['compactness_mean'])
ds['compactness_mean'].plot.box()
ds['concavity_mean'].plot.box()
ds.loc[ds['concavity_mean']>0.27,'concavity_mean']=np.mean(ds['concavity_mean'])
ds['concavity_mean'].plot.box()
ds['concave points_mean'].plot.box()
ds.loc[ds['concave points_mean']>0.160,'concave points_mean']=np.mean(ds['concave points_mean'])
ds['concave points_mean'].plot.box()
ds['symmetry_mean'].plot.box()
ds.loc[ds['symmetry_mean']>0.240,'symmetry_mean']=np.mean(ds['symmetry_mean'])
ds.loc[ds['symmetry_mean']<0.12,'symmetry_mean']=np.mean(ds['symmetry_mean'])

ds['symmetry_mean'].plot.box()
ds['fractal_dimension_mean'].plot.box()
ds.loc[ds['fractal_dimension_mean']>0.075,'fractal_dimension_mean']=np.mean(ds['fractal_dimension_mean'])
ds['fractal_dimension_mean'].plot.box()
ds['radius_se'].plot.box()
ds.loc[ds['radius_se']>0.76,'radius_se']=np.mean(ds['radius_se'])
ds['radius_se'].plot.box()
ds['texture_se'].plot.box()
ds.loc[ds['texture_se']>2.2,'texture_se']=np.mean(ds['texture_se'])
ds['texture_se'].plot.box()
ds['perimeter_se'].plot.box()
ds.loc[ds['perimeter_se']>4.7,'perimeter_se']=np.mean(ds['perimeter_se'])
ds['perimeter_se'].plot.box()
ds['area_se'].plot.box()
ds.loc[ds['area_se']>80,'area_se']=np.mean(ds['area_se'])
ds['area_se'].plot.box()
ds['smoothness_se'].plot.box()
ds.loc[ds['smoothness_se']>0.009,'smoothness_se']=np.mean(ds['smoothness_se'])
ds.loc[ds['smoothness_se']<0.003,'smoothness_se']=np.mean(ds['smoothness_se'])
ds['smoothness_se'].plot.box()
ds['compactness_se'].plot.box()
ds.loc[ds['compactness_se']>0.054,'compactness_se']=np.mean(ds['compactness_se'])
ds['compactness_se'].plot.box()
ds['concavity_se'].plot.box()
ds.loc[ds['concavity_se']>0.055,'concavity_se']=np.mean(ds['concavity_se'])
ds['concavity_se'].plot.box()
ds['concave points_se'].plot.box()
ds.loc[ds['concave points_se']>0.020,'concave points_se']=np.mean(ds['concave points_se'])
ds['concave points_se'].plot.box()
ds['symmetry_se'].plot.box()
ds.loc[ds['symmetry_se']>0.0307,'symmetry_se']=np.mean(ds['symmetry_se'])
ds['symmetry_se'].plot.box()
ds['fractal_dimension_se'].plot.box()
ds.loc[ds['fractal_dimension_se']>0.0057,'fractal_dimension_se']=np.mean(ds['fractal_dimension_se'])
ds['fractal_dimension_se'].plot.box()
ds.dtypes
ds['radius_worst'].plot.box()
ds.loc[ds['radius_worst']>27,'radius_worst']=np.mean(ds['radius_worst'])
ds['radius_worst'].plot.box()
ds['texture_worst'].plot.box()<jupyter_output><empty_output><jupyter_text>df.loc[ds['radius_mean']>23,'price']=np.mean(ds['price'])<jupyter_code>ds.loc[ds['texture_worst']>40,'texture_worst']=np.mean(ds['texture_worst'])
ds['texture_worst'].plot.box()
ds['perimeter_worst'].plot.box()
ds.loc[ds['perimeter_worst']>190,'perimeter_worst']=np.mean(ds['perimeter_worst'])
ds['perimeter_worst'].plot.box()
ds['area_worst'].plot.box()
ds.loc[ds['area_worst']>1300,'area_worst']=np.mean(ds['area_worst'])
ds['area_worst'].plot.box()
ds['smoothness_worst'].plot.box()
ds.loc[ds['smoothness_worst']>0.19,'smoothness_worst']=np.mean(ds['smoothness_worst'])
ds.loc[ds['smoothness_worst']<0.08,'smoothness_worst']=np.mean(ds['smoothness_worst'])
ds['smoothness_worst'].plot.box()
ds['compactness_worst'].plot.box()
ds.loc[ds['compactness_worst']>0.62,'compactness_worst']=np.mean(ds['compactness_worst'])
ds['compactness_worst'].plot.box()
ds['concavity_worst'].plot.box()
ds.loc[ds['concavity_worst']>0.8,'concavity_worst']=np.mean(ds['concavity_worst'])
ds['concavity_worst'].plot.box()
ds['concave points_worst'].plot.box()
ds['symmetry_worst'].plot.box()
ds.loc[ds['symmetry_worst']>0.3751,'symmetry_worst']=np.mean(ds['symmetry_worst'])
ds.loc[ds['symmetry_worst']<0.20,'symmetry_worst']=np.mean(ds['symmetry_worst'])
ds['symmetry_worst'].plot.box()
ds['fractal_dimension_worst'].plot.box()
ds.loc[ds['fractal_dimension_worst']>0.11,'fractal_dimension_worst']=np.mean(ds['fractal_dimension_worst'])
ds['fractal_dimension_worst'].plot.box()<jupyter_output><empty_output><jupyter_text>ds.loc[ds['radius_mean']>23,'price']=np.mean(ds['price'])ds.loc[ds['sqft_living']>3500,'sqft_living']=np.mean(ds['sqft_living'])ds.loc[ds['sqft_lot']>18000,'sqft_lot']=np.mean(ds['sqft_lot'])ds.loc[ds['condition']<2,'condition']=np.mean(ds['condition'])ds.loc[ds['grade']<6,'grade']=np.mean(ds['grade'])ds.loc[ds['grade']>9,'grade']=np.mean(ds['grade'])<jupyter_code>ds.describe()
ds.head()
X = ds.iloc[:377,0:30]
y = ds.iloc[:377,[0]]<jupyter_output><empty_output><jupyter_text>Scaling the dataset using MIN MAX SCALAR<jupyter_code>from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler2=MinMaxScaler()
X_train = scaler.fit_transform(X)
y = np.reshape(np.array(y), (-1,1))
y_train = scaler2.fit_transform(y)
X.shape
y.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.20, random_state=101, stratify=y)
X_train.shape
y_train.shape
from sklearn.linear_model import LogisticRegression
log1=LogisticRegression()
log1.fit(X_train,y_train)
prediction=log1.predict(X_test)
fig = plt.figure(figsize = (12,8))
plt.plot(prediction[:100], label = 'Optimal')
plt.plot(y_test[:100], label = 'Actual')
plt.legend()
fig=plt.figure(figsize=(12,8))
plt.plot(prediction[:100], label='Optimal',color='b')
plt.show()
fig=plt.figure(figsize=(12,8))
plt.plot(y_test[:100], label='Actual',color='r')
plt.show()
from sklearn.metrics import classification_report,accuracy_score,roc_auc_score
print(classification_report(y_test,prediction))

print("Accuracy = ",accuracy_score(y_test,prediction))

print("Auc = ",roc_auc_score(y_test,prediction))

log1.score(X_train, y_train)
log1.score(X_test, y_test)
ds1.head()
ds1.drop(["id"],axis=1,inplace=True)
ds1.head()
ds1_null=ds1.columns[ds1.isnull().any()]
ds1[ds1_null].isnull().sum()
ds.columns
ds.shape
ds1.shape
X = ds.iloc[:377,0:30]
y = ds.iloc[:377,[0]]

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler2=MinMaxScaler()
X_train = scaler.fit_transform(X)
y = np.reshape(np.array(y), (-1,1))
y_train = scaler2.fit_transform(y)
X_train.shape
y_train.shape
A = ds1.iloc[:192,0:30]
scaler = MinMaxScaler()
X_test = scaler.fit_transform(A)
X_test.shape
from sklearn.linear_model import LogisticRegression
log=LogisticRegression()
log.fit(X_train,y_train)
predictions = log.predict(X_test)
predictions
temp=predictions.reshape(-1,1)
temp=scaler2.inverse_transform(temp)
temp
y=pd.DataFrame(temp)
y
ds2 = pd.read_csv("Test_Dataset-01.csv")
testpplid=ds2.iloc[:,0].values
import xlsxwriter 
  
workbook = xlsxwriter.Workbook('logistics_reg.xlsx') 
worksheet = workbook.add_worksheet() 
  
row = 1
row_id=0
column_id = 0
column=1
worksheet.write(0,0,'id')
worksheet.write(0,1,'diagnosis')
  
for i in range(len(temp)) : 
  
    
    worksheet.write(row, column_id, testpplid[i])
    worksheet.write(row, column, temp[i]) 
    
    row += 1
      
workbook.close()<jupyter_output><empty_output><jupyter_text>Implementing Decision Tree<jupyter_code>ds.head()
X = ds.iloc[:377,0:30]
y = ds.iloc[:377,[0]]
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler2=MinMaxScaler()
X_train = scaler.fit_transform(X)
y = np.reshape(np.array(y), (-1,1))
y_train = scaler2.fit_transform(y)
X.shape
y.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.20, random_state=101, stratify=y)
X_train.shape
y_train.shape
from sklearn.tree import DecisionTreeClassifier
dt_model = DecisionTreeClassifier(random_state=10)
dt_model.fit(X_train, y_train)
prediction1=dt_model.predict(X_test)
fig = plt.figure(figsize = (12,8))
plt.plot(prediction1[:100], label = 'Optimal')
plt.plot(y_test[:100], label = 'Actual')
plt.legend()
dt_model.score(X_train, y_train)
dt_model.score(X_test, y_test)
from sklearn.metrics import classification_report,accuracy_score,roc_auc_score,confusion_matrix
print(classification_report(y_test,prediction1))

print("Accuracy = ",accuracy_score(y_test,prediction1))

print("Auc = ",roc_auc_score(y_test,prediction1))
ds1.head()
ds1_null=ds1.columns[ds1.isnull().any()]
ds1[ds1_null].isnull().sum()
X = ds.iloc[:377,0:30]
y = ds.iloc[:377,[0]]

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler2=MinMaxScaler()
X_train = scaler.fit_transform(X)
y = np.reshape(np.array(y), (-1,1))
y_train = scaler2.fit_transform(y)
X_train.shape
y_train.shape
A = ds1.iloc[:192,0:30]
scaler = MinMaxScaler()
X_test = scaler.fit_transform(A)
X_test.shape
from sklearn.tree import DecisionTreeClassifier
dtree = DecisionTreeClassifier()
dtree.fit(X_train,y_train)
predictions3 = dtree.predict(X_test)
predictions3
temp1=predictions3.reshape(-1,1)
temp1=scaler2.inverse_transform(temp1)
temp1
ds2 = pd.read_csv("Test_Dataset-01.csv")
testpplid=ds2.iloc[:,0].values
import xlsxwriter 
  
workbook = xlsxwriter.Workbook('decision_tree.xlsx') 
worksheet = workbook.add_worksheet() 
  
row = 1
row_id=0
column_id = 0
column=1
worksheet.write(0,0,'id')
worksheet.write(0,1,'diagnosis')
  
for i in range(len(temp)) : 
  
    
    worksheet.write(row, column_id, testpplid[i])
    worksheet.write(row, column, temp[i]) 
    
    row += 1
      
workbook.close()<jupyter_output><empty_output><jupyter_text>Implementing Random Forest<jupyter_code>ds.head()
X = ds.iloc[:377,0:30]
y = ds.iloc[:377,[0]]
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler2=MinMaxScaler()
X_train = scaler.fit_transform(X)
y = np.reshape(np.array(y), (-1,1))
y_train = scaler2.fit_transform(y)
X.shape, y.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.20, random_state=101, stratify=y)
X_train.shape, y_train.shape
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(n_estimators=100)
rfc.fit(X_train, y_train)
rfc_pred = rfc.predict(X_test)
print(confusion_matrix(y_test,rfc_pred))
print(classification_report(y_test,rfc_pred))
rfc.score(X_train, y_train)
rfc.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Gradient Boosting<jupyter_code>from sklearn.ensemble import GradientBoostingClassifier
X = ds.iloc[:377,0:30]
y = ds.iloc[:377,[0]]
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler2=MinMaxScaler()
X_train = scaler.fit_transform(X)
y = np.reshape(np.array(y), (-1,1))
y_train = scaler2.fit_transform(y)
X_train.shape, y_train.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.30, random_state=101, stratify=y)
gradient = GradientBoostingClassifier(
    init=dtree,
    max_depth=2,
    n_estimators=3,
    learning_rate=1.0
)
gradient.fit(X_train, y_train)
prediction4=gradient.predict(X_test)
gradient.score(X_train, y_train)
gradient.score(X_test, y_test)
ds1.head()
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler2=MinMaxScaler()
X_train = scaler.fit_transform(X)
y = np.reshape(np.array(y), (-1,1))
y_train = scaler2.fit_transform(y)
X_train.shape, y_train.shape
A = ds1.iloc[:192,0:30]
scaler = MinMaxScaler()
X_test = scaler.fit_transform(A)
X_test.shape
gradient1 = GradientBoostingClassifier(
    init=dtree,
    max_depth=2,
    n_estimators=3,
    learning_rate=1.0
)
gradient1.fit(X_train, y_train)
predictions4 = gradient1.predict(X_test)
prediction4
temp2=predictions4.reshape(-1,1)
temp2=scaler2.inverse_transform(temp2)
temp2
ds2 = pd.read_csv("Test_Dataset-01.csv")
testpplid=ds2.iloc[:,0].values
import xlsxwriter 
  
workbook = xlsxwriter.Workbook('gradient_boosting.xlsx') 
worksheet = workbook.add_worksheet() 
  
row = 1
row_id=0
column_id = 0
column=1
worksheet.write(0,0,'id')
worksheet.write(0,1,'diagnosis')
  
for i in range(len(temp)) : 
  
    
    worksheet.write(row, column_id, testpplid[i])
    worksheet.write(row, column, temp[i]) 
    
    row += 1
      
workbook.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17836179295624333, 'fraction_numerical': 0.026881003201707578, 'mean_word_length': 9.069173942243117, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '416145', 'license': 'MIT', 'n_tokens_mistral': 6245, 'n_tokens_neox': 6002, 'n_words': 712, 'path': 'Assignment_04/Assignment_04.ipynb', 'repo_name': 'kaushik1580/Flipr_Internship_program', 'size': '15011'}