Completion of Python files
This commit is contained in:
31
Code/Day 11_k-NN.py
Normal file
31
Code/Day 11_k-NN.py
Normal file
@ -0,0 +1,31 @@
|
||||
# Importing the libraries
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
# Importing the dataset
|
||||
dataset = pd.read_csv('../datasets/Social_Network_Ads.csv')
|
||||
X = dataset.iloc[:, [2, 3]].values
|
||||
y = dataset.iloc[:, 4].values
|
||||
|
||||
# Splitting the dataset into the Training set and Test set
|
||||
from sklearn.cross_validation import train_test_split
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
|
||||
|
||||
# Feature Scaling
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# Fitting K-NN to the Training set
|
||||
from sklearn.neighbors import KNeighborsClassifier
|
||||
classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
|
||||
classifier.fit(X_train, y_train)
|
||||
|
||||
# Predicting the Test set results
|
||||
y_pred = classifier.predict(X_test)
|
||||
|
||||
# Making the Confusion Matrix
|
||||
from sklearn.metrics import confusion_matrix
|
||||
cm = confusion_matrix(y_test, y_pred)
|
||||
68
Code/Day 25_Decision_Tree.py
Normal file
68
Code/Day 25_Decision_Tree.py
Normal file
@ -0,0 +1,68 @@
|
||||
# Importing the libraries
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
# Importing the dataset
|
||||
dataset = pd.read_csv('../datasets/Social_Network_Ads.csv')
|
||||
X = dataset.iloc[:, [2, 3]].values
|
||||
y = dataset.iloc[:, 4].values
|
||||
|
||||
# Splitting the dataset into the Training set and Test set
|
||||
from sklearn.cross_validation import train_test_split
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
|
||||
|
||||
# Feature Scaling
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# Fitting Decision Tree Classification to the Training set
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
|
||||
classifier.fit(X_train, y_train)
|
||||
|
||||
# Predicting the Test set results
|
||||
y_pred = classifier.predict(X_test)
|
||||
|
||||
# Making the Confusion Matrix
|
||||
from sklearn.metrics import confusion_matrix
|
||||
cm = confusion_matrix(y_test, y_pred)
|
||||
|
||||
# Visualising the Training set results
|
||||
from matplotlib.colors import ListedColormap
|
||||
X_set, y_set = X_train, y_train
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('red', 'green'))(i), label = j)
|
||||
plt.title('Decision Tree Classification (Training set)')
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
# Visualising the Test set results
|
||||
from matplotlib.colors import ListedColormap
|
||||
X_set, y_set = X_test, y_test
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('red', 'green'))(i), label = j)
|
||||
plt.title('Decision Tree Classification (Test set)')
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
28
Code/Day 2_Simple_Linear_Regression.py
Normal file
28
Code/Day 2_Simple_Linear_Regression.py
Normal file
@ -0,0 +1,28 @@
|
||||
# Data Preprocessing
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
dataset = pd.read_csv('../datasets/studentscores.csv')
|
||||
X = dataset.iloc[ : , : 1 ].values
|
||||
Y = dataset.iloc[ : , 1 ].values
|
||||
|
||||
from sklearn.cross_validation import train_test_split
|
||||
X_train, X_test, Y_train, Y_test = train_test_split( X, Y, test_size = 1/4, random_state = 0)
|
||||
|
||||
# Fitting Simple Linear Regression Model to the training set
|
||||
from sklearn.linear_model import LinearRegression
|
||||
regressor = LinearRegression()
|
||||
regressor = regressor.fit(X_train, Y_train)
|
||||
|
||||
# Predecting the Result
|
||||
Y_pred = regressor.predict(X_test)
|
||||
|
||||
# Visualising the Training results
|
||||
plt.scatter(X_train , Y_train, color = 'red')
|
||||
plt.plot(X_train , regressor.predict(X_train), color ='blue')
|
||||
|
||||
# Visualizing the test results
|
||||
plt.scatter(X_test , Y_test, color = 'red')
|
||||
plt.plot(X_test , regressor.predict(X_test), color ='blue')
|
||||
|
||||
68
Code/Day 34_Random_Forests.py
Normal file
68
Code/Day 34_Random_Forests.py
Normal file
@ -0,0 +1,68 @@
|
||||
# Importing the libraries
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
# Importing the dataset
|
||||
dataset = pd.read_csv('../datasets/Social_Network_Ads.csv')
|
||||
X = dataset.iloc[:, [2, 3]].values
|
||||
y = dataset.iloc[:, 4].values
|
||||
|
||||
# Splitting the dataset into the Training set and Test set
|
||||
from sklearn.cross_validation import train_test_split
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
|
||||
|
||||
# Feature Scaling
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# Fitting Random Forest to the Training set
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
|
||||
classifier.fit(X_train, y_train)
|
||||
|
||||
# Predicting the Test set results
|
||||
y_pred = classifier.predict(X_test)
|
||||
|
||||
# Making the Confusion Matrix
|
||||
from sklearn.metrics import confusion_matrix
|
||||
cm = confusion_matrix(y_test, y_pred)
|
||||
|
||||
# Visualising the Training set results
|
||||
from matplotlib.colors import ListedColormap
|
||||
X_set, y_set = X_train, y_train
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('red', 'green'))(i), label = j)
|
||||
plt.title('Random Forest Classification (Training set)')
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
# Visualising the Test set results
|
||||
from matplotlib.colors import ListedColormap
|
||||
X_set, y_set = X_test, y_test
|
||||
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
|
||||
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
|
||||
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
|
||||
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
|
||||
plt.xlim(X1.min(), X1.max())
|
||||
plt.ylim(X2.min(), X2.max())
|
||||
for i, j in enumerate(np.unique(y_set)):
|
||||
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
|
||||
c = ListedColormap(('red', 'green'))(i), label = j)
|
||||
plt.title('Random Forest Classification (Test set)')
|
||||
plt.xlabel('Age')
|
||||
plt.ylabel('Estimated Salary')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
31
Code/Day 3_Multiple_Linear_Regression.py
Normal file
31
Code/Day 3_Multiple_Linear_Regression.py
Normal file
@ -0,0 +1,31 @@
|
||||
# Importing the libraries
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
# Importing the dataset
|
||||
dataset = pd.read_csv('../datasets/50_Startups.csv')
|
||||
X = dataset.iloc[ : , :-1].values
|
||||
Y = dataset.iloc[ : , 4 ].values
|
||||
|
||||
# Encoding Categorical data
|
||||
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
|
||||
labelencoder = LabelEncoder()
|
||||
X[: , 3] = labelencoder.fit_transform(X[ : , 3])
|
||||
onehotencoder = OneHotEncoder(categorical_features = [3])
|
||||
X = onehotencoder.fit_transform(X).toarray()
|
||||
|
||||
# Avoiding Dummy Variable Trap
|
||||
X = X[: , 1:]
|
||||
|
||||
# Splitting the dataset into the Training set and Test set
|
||||
from sklearn.cross_validation import train_test_split
|
||||
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 0)
|
||||
|
||||
# Fitting Multiple Linear Regression to the Training set
|
||||
from sklearn.linear_model import LinearRegression
|
||||
regressor = LinearRegression()
|
||||
regressor.fit(X_train, Y_train)
|
||||
|
||||
# Predicting the Test set results
|
||||
y_pred = regressor.predict(X_test)
|
||||
|
||||
32
Code/Day 6_Logistic_Regression.py
Normal file
32
Code/Day 6_Logistic_Regression.py
Normal file
@ -0,0 +1,32 @@
|
||||
# Importing the Libraries
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
# Importing the dataset
|
||||
dataset = pd.read_csv('../datasets/Social_Network_Ads.csv')
|
||||
X = dataset.iloc[:, [2, 3]].values
|
||||
y = dataset.iloc[:, 4].values
|
||||
|
||||
# Splitting the dataset into the Training set and Test set
|
||||
from sklearn.cross_validation import train_test_split
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
|
||||
|
||||
# Feature Scaling
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
sc = StandardScaler()
|
||||
X_train = sc.fit_transform(X_train)
|
||||
X_test = sc.transform(X_test)
|
||||
|
||||
# Fitting Logistic Regression to the Training set
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
classifier = LogisticRegression()
|
||||
classifier.fit(X_train, y_train)
|
||||
|
||||
# Predicting the Test set results
|
||||
y_pred = classifier.predict(X_test)
|
||||
|
||||
# Making the Confusion Matrix
|
||||
from sklearn.metrics import confusion_matrix
|
||||
cm = confusion_matrix(y_test, y_pred)
|
||||
|
||||
Reference in New Issue
Block a user