EXAM PREPERATION - Ipynb - Colaboratory-1
EXAM PREPERATION - Ipynb - Colaboratory-1
ipynb - Colaboratory
import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(data= iris["data"], columns = iris["feature_names"])
df
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
df["target"] = iris['target']
X = df.drop("target",axis = 1)
Y = df["target"]
from sklearn.model_selection import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(X,Y, test_size = 0.2)
df.head(10) #first 10 rows
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
df.tail(10) #last 10 rows
https://colab.research.google.com/drive/1smr2_t_MByiPbkNBxCulik3p9TtsJTk-#scrollTo=0f6R1Zt8lAOV 1/8
2/11/23, 6:35 PM EXAM PREPERATION.ipynb - Colaboratory
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
143 6.8
df.iloc[2:10] # 2nd to 10th row 3.2 5.9 2.3
df
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
df.dropna(axis=1) # dropping null columns
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
df_new = (df-df.mean())/df.std()
df_new
https://colab.research.google.com/drive/1smr2_t_MByiPbkNBxCulik3p9TtsJTk-#scrollTo=0f6R1Zt8lAOV 2/8
2/11/23, 6:35 PM EXAM PREPERATION.ipynb - Colaboratory
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
PCA
150 rows × 4 columns
x = df.drop("target", axis=1)
from sklearn.decomposition import PCA
clf = PCA(n_components = 2)
xpca = clf.fit_transform(x)
import matplotlib.pyplot as plt
plt.scatter(xpca[:,0], xpca[:,1])
<matplotlib.collections.PathCollection at 0x7fae3a482970>
Linear Regression
from sklearn.linear_model import LinearRegression
clf = LinearRegression()
clf.fit(xtrain,ytrain)
LinearRegression()
y_pred = clf.predict(xtest)
y_pred
Polynomial Regression
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
clf = LinearRegression()
https://colab.research.google.com/drive/1smr2_t_MByiPbkNBxCulik3p9TtsJTk-#scrollTo=0f6R1Zt8lAOV 3/8
2/11/23, 6:35 PM EXAM PREPERATION.ipynb - Colaboratory
pclf = PolynomialFeatures(degree = 2)
x_poly = pclf.fit_transform(x)
clf.fit(x_poly,Y)
LinearRegression()
y_pred = clf.predict(x_poly)
y_pred
SVM
from sklearn.svm import SVC
clf = SVC(kernel = 'rbf')
clf.fit(xtrain,ytrain)
SVC()
y_pred = clf.predict(xtest)
y_pred
array([0, 2, 2, 2, 2, 1, 1, 2, 1, 0, 2, 0, 0, 2, 0, 0, 2, 1, 1, 0, 0, 2,
0, 1, 1, 1, 2, 2, 1, 1])
from sklearn.metrics import accuracy_score
print(accuracy_score(y_pred,ytest))
from sklearn.metrics import mean_squared_error
print(mean_squared_error(y_pred,ytest))
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_pred,ytest))
from sklearn.metrics import precision_score
print(precision_score(y_pred,ytest, average=None))
1.0
0.0
[[ 9 0 0]
https://colab.research.google.com/drive/1smr2_t_MByiPbkNBxCulik3p9TtsJTk-#scrollTo=0f6R1Zt8lAOV 4/8
2/11/23, 6:35 PM EXAM PREPERATION.ipynb - Colaboratory
[ 0 10 0]
[ 0 0 11]]
[1. 1. 1.]
clf.support_vectors_
MLP
from sklearn.neural_network import MLPClassifier
clf = MLPClassifier(hidden_layer_sizes = (3,5), activation = "tanh", alpha = 0.2, solver = "adam", max_iter = 350)
clf.fit(xtrain,ytrain)
clf.predict(xtest)
clf.coefs_
https://colab.research.google.com/drive/1smr2_t_MByiPbkNBxCulik3p9TtsJTk-#scrollTo=0f6R1Zt8lAOV 5/8
2/11/23, 6:35 PM EXAM PREPERATION.ipynb - Colaboratory
KMeans
from sklearn.cluster import KMeans
clf = KMeans(n_clusters = 3, init='random', n_init=10, max_iter=350, tol=1e-04)
clf.fit(X)
ypred = clf.predict(X)
ypred
array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1], dtype=int32)
ID3
from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier(criterion = "gini", max_depth = 2)
clf.fit(X,Y)
DecisionTreeClassifier(max_depth=2)
y_pred =clf.predict(X)
y_pred
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2,
2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
from sklearn import tree
plt.figure(figsize = (30,10), facecolor = "w")
tree.plot_tree(clf)
https://colab.research.google.com/drive/1smr2_t_MByiPbkNBxCulik3p9TtsJTk-#scrollTo=0f6R1Zt8lAOV 6/8
2/11/23, 6:35 PM EXAM PREPERATION.ipynb - Colaboratory
[Text(0.4, 0.8333333333333334, 'X[2] <= 2.45\ngini = 0.667\nsamples = 150\nvalue = [50, 50, 50]'),
Text(0.2, 0.5, 'gini = 0.0\nsamples = 50\nvalue = [50, 0, 0]'),
Text(0.6, 0.5, 'X[3] <= 1.75\ngini = 0.5\nsamples = 100\nvalue = [0, 50, 50]'),
Text(0.4, 0.16666666666666666, 'gini = 0.168\nsamples = 54\nvalue = [0, 49, 5]'),
Text(0.8, 0.16666666666666666, 'gini = 0.043\nsamples = 46\nvalue = [0, 1, 45]')]
gini = DecisionTreeClassifier(criterion = "gini", max_depth = 2)
gini.fit(X,Y)
print(gini)
DecisionTreeClassifier(max_depth=2)
NAIVE NAYES
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(X,Y)
y_pred = clf.predict(X)
y_pred
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
clf.predict_proba(X)
https://colab.research.google.com/drive/1smr2_t_MByiPbkNBxCulik3p9TtsJTk-#scrollTo=0f6R1Zt8lAOV 8/8