Answer To: MITS5509 Assignment 3 MITS5509 Intelligent Systems for Analytics Assignment 3 MITS5509 Assignment 3...
Neha answered on Oct 29 2021
70150 - python classifier/code.py
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
import pandas as pd
import random
with open('training.txt', 'r') as f:
data = f.read()
data = data.split('\n')
firm = [int(i) for i in data[0].split()[1:]]
wc = list(map(lambda x: float(x), data[2].split()[1:]))
dc = [float(i) for i in data[4].split()[1:]]
for i in data[6].split():
try:
firm.append(int(i))
except:
wc.append(float(i))
category = [ 1 for i in range(34)] + [0 for i in range(34)]
df_data = [
{
'firm': f,
'wc': w,
'dc': d,
'category': c
} for f, w, d, c in zip(firm, wc, dc, category)
]
df = pd.DataFrame().from_records(df_data)
df.head()
with open('testing.txt', 'r') as f:
data = f.read()
data = data.split('\n')
firm = [int(i) for i in data[0].split()[1:]]
wc = list(map(lambda x: float(x), data[2].split()[1:]))
dc = [float(i) for i in data[4].split()[1:]]
for i in data[6].split():
try:
firm.append(int(i))
except:
wc.append(float(i))
category = [1 for i in range(34)] + [0 for i in range(34)]
df_data = [
{
'firm': f,
'wc': w,
'dc': d,
'category': c
} for f, w, d, c in zip(firm, wc, dc, category)
]
df_test = pd.DataFrame().from_records(df_data)
df_test.head()
df_test.index = [ random.randint(1, 68) for i in range(68)]
df_test = df_test.sort_index()
df_test = pd.concat([df_test[df_test.category == 1].iloc[:20, :], df_test[df_test.category == 0].iloc[:20, :]])
df.index = [ random.randint(1, 68) for i in range(68)]
df = df.sort_index()
df_train = pd.concat([df[df.category == 1].iloc[:20, :], df[df.category == 0].iloc[:20, :]])
print ('Training Data')
print (df_train)
print ('Test Data')
print (df_test)
feature = df_train.iloc[:, 1:]
label = df_train['category']
dtc = DecisionTreeClassifier()
tree = dtc.fit(feature, label)
df_test['Decision_Tree'] = tree.predict(df_test.iloc[:, 1:4])
rfor = RandomForestClassifier()
forest= rfor.fit(feature, label)
df_test['Random_Forest'] = forest.predict(df_test.iloc[:, 1:4])
mlpc = MLPClassifier()
neural= mlpc.fit(feature, label)
df_test['Neural_Networks'] = neural.predict(df_test.iloc[:, 1:4])
df_test.to_csv('Final_Result.csv', index=False)
70150 - python...