allcodesml2
allcodesml2
# Week-1
import pandas as pd
df = pd.read_csv("sample_data/pima_indian.csv")
predicted_class_names = ['diabetes']
xtrain,xtest,ytrain,ytest=train_test_split(X,y,test_size=0.33)
clf = GaussianNB().fit(xtrain,ytrain.ravel())
predicted = clf.predict(xtest)
predictTestData= clf.predict([[6,148,72,35,0,33.6,0.627,50]])
print(metrics.confusion_matrix(ytest,predicted))
print('\n Accuracy of the classifier is',metrics.accuracy_score(ytest,predicted))
week-2
#Week-2
import pandas
import pydotplus
df = pandas.read_csv("/content/sample_data/playtennis.csv")
df['Outlook'] = df['Outlook'].map(d)
df['Temperature'] = df['Temperature'].map(d)
d = {'High': 0, 'Normal': 1}
df['Humidity'] = df['Humidity'].map(d)
d = {'Weak': 0, 'Strong': 1}
df['Wind'] = df['Wind'].map(d)
d = {'No': 0, 'Yes': 1}
df['playtennis'] = df['playtennis'].map(d)
X = df[features]
y = df['playtennis']
dtree = DecisionTreeClassifier(criterion="entropy")
dtree = dtree.fit(X, y)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png('mydecisiontree.png')
img=pltimg.imread('mydecisiontree.png')
imgplot = plt.imshow(img)
plt.show()
Week-3(a)
# Week-3a
import numpy as np
import pandas as pd
X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]
print(X.head())
ypred = classifier.predict(Xtest)
i=0
print ("\n-------------------------------------------------------------------------")
print ("-------------------------------------------------------------------------")
if (label == ypred[i]):
else:
i=i+1
print ("-------------------------------------------------------------------------")
print ("-------------------------------------------------------------------------")
print ("-------------------------------------------------------------------------")
Week-3(b)
#Week-3b
import numpy as nm
import pandas as pd
data_set= pd.read_csv('/content/sample_data/Salary_Data.csv')
x= data_set.iloc[:, :-1].values
y= data_set.iloc[:, 1].values
regressor= LinearRegression()
regressor.fit(x_train, y_train)
y_pred= regressor.predict(x_test)
x_pred= regressor.predict(x_train)
mtp.xlabel("Years of Experience")
mtp.ylabel("Salary(In Rupees)")
mtp.show()
mtp.xlabel("Years of Experience")
mtp.ylabel("Salary(In Rupees)")
mtp.show()
Week-4(a)
#Week-4a
iris = datasets.load_iris()
# Take the first two features. We could avoid this by using a two-dim dataset
X = iris.data[:, :2]
y = iris.target
# we create an instance of SVM and fit out data. We do not scale our
models = (
svm.SVC(kernel="linear", C=C),
svm.LinearSVC(C=C, max_iter=10000),
titles = (
disp = DecisionBoundaryDisplay.from_estimator(
clf,
X,
response_method="predict",
cmap=plt.cm.coolwarm,
alpha=0.8,
ax=ax,
xlabel=iris.feature_names[0],
ylabel=iris.feature_names[1],
ax.set_xticks(())
ax.set_yticks(())
ax.set_title(title)
plt.show()
week-4(b)
#Week-4b
import numpy as np
import pandas as pd
%matplotlib inline
# Checking the dataset
iris.head()
# Creating a pairplot to visualize the similarities and especially difference between the species
x=iris.iloc[:,:-1]
y=iris.iloc[:,4]
model=SVC()
model.fit(x_train, y_train)
pred=model.predict(x_test)
print(confusion_matrix(y_test,pred))
print(classification_report(y_test, pred))
Week-5
#Week-5
import sklearn.metrics as sm
import pandas as pd
import numpy as np
iris = datasets.load_iris()
X = pd.DataFrame(iris.data)
X.columns = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y = pd.DataFrame(iris.target)
y.columns = ['Targets']
model = KMeans(n_clusters=2)
model.fit(X)
plt.figure(figsize=(14,7))
plt.subplot(1, 2, 1)
plt.title('Real Classification')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
plt.subplot(1, 2, 2)
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
scaler = preprocessing.StandardScaler()
scaler.fit(X)
xsa = scaler.transform(X)
#xs.sample(5)
gmm = GaussianMixture(n_components=3)g
gmm.fit(xs)
y_gmm = gmm.predict(xs)
#y_cluster_gmm
plt.subplot(2, 2, 3)
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')