Posts

K Means_Clustering

  #!/usr/bin/env python # coding: utf-8 # In[3]: import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.read_csv( r "C: \U sers \91 887 \O neDrive\Desktop \M achine_Learning\week4_Mall_Customers.csv" ) x = df.iloc[:, [ 3 , 4 ]].values # In[4]: from sklearn.cluster import KMeans wcss_list = [] for i in range ( 1 , 11 ):     kmeans = KMeans( n_clusters = i, init = 'k-means++' , random_state = 42 )     kmeans.fit(x)     wcss_list.append(kmeans.inertia_) plt.plot( range ( 1 , 11 ), wcss_list) plt.title( 'The Elobw Method Graph' ) plt.xlabel( 'Number of clusters(k)' ) plt.ylabel( 'wcss_list' ) plt.show() # In[ ]:

AgglomarativeCliutering

  #!/usr/bin/env python # coding: utf-8 # In[11]: import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.read_csv( r "C: \U sers \91 887 \O neDrive\Desktop \M achine_Learning\week4_Mall_Customers.csv" ) x = df.iloc[:,[ 3 , 4 ]].values # In[12]: from sklearn.cluster import AgglomerativeClustering hc = AgglomerativeClustering( n_clusters = 5 , affinity = 'euclidean' , linkage = 'ward' ) y_pred = hc.fit_predict(x) # In[13]: plt.scatter(x[y_pred == 0 , 0 ], x[y_pred == 0 , 1 ], s = 100 , c = 'blue' , label = 'Cluster 1' ) plt.scatter(x[y_pred == 1 , 0 ], x[y_pred == 1 , 1 ], s = 100 , c = 'green' , label = 'Cluster 2' ) plt.scatter(x[y_pred == 2 , 0 ], x[y_pred == 2 , 1 ], s = 100 , c = 'red' , label = 'Cluster 3' ) plt.scatter(x[y_pred == 3 , 0 ], x[y_pred == 3 , 1 ], s = 100 , c = 'cyan' , label = 'Cluster 4' ) plt.scatter(x[y_pred ==

1.Import and Export(How to read csv file using manualvfunction)

#!/usr/bin/env python # coding: utf-8 # In[5]: # Week1: Write a python program to import and export data from/to CSV file ######################################################################### ## (1) How to Read CSV File using Manual Function import pandas as pd def load_csv ( filepath ):     data = []     col = []     checkcol = False     with open (filepath) as f:         for val in f.readlines():             val = val.strip()             val = val.split( ',' )             if checkcol is False :                 col = val                 checkcol = True             else :                 data.append(val)     df = pd.DataFrame( data = data, columns = col)     return df # Replace 'yourfile.csv' with the actual file path myfile = r "C: \U sers \91 887 \O neDrive\Desktop \M achine_Learning\Sales_Records.csv" myData = load_csv(myfile) print (myData.head()) # In[ ]: # In[6]: ## (2) How to Read CSV File in python Using Pandas? df = pd.read_cs

Multinomial_Regression

  #!/usr/bin/env python # coding: utf-8 # In[39]: #import the necessary libraries from sklearn import datasets from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.metrics import accuracy_score # In[40]: #load the digit datasets digits = datasets.load_digits() # In[41]: #defining feature matrix x and response vecoor y X = digits.data y = digits.target print (X) print (y) # In[48]: #split the test train data X_train,X_test,y_train,y_test = train_test_split(X,y, test_size = 0.2 , random_state = 1 ) print (y_test) # In[49]: #standradize features using standardscaler scaler = StandardScaler() scaler.fit(X_train) X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) # In[50]: #create the logistic regression model model = LogisticRegression() # In[51]: #train the model on scaled training data model.fit(X_train_scaled,y_train) # In[52]: #mak

Binomial_Regression

  #!/usr/bin/env python # coding: utf-8 # In[16]: #import the necessary libraries from sklearn.datasets import load_breast_cancer from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.metrics import accuracy_score # In[2]: #load breast cancer dataset X,y = load_breast_cancer( return_X_y = True ) # In[3]: #split the test train data X_train,X_test,y_train,y_test = train_test_split(X,y, test_size = 0.20 , random_state = 42 ) print (y_test) # In[5]: #standardize features using standardscaler scaler = StandardScaler() scaler.fit(X_train) X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) # In[6]: #create the logistic regression model model = LogisticRegression() # In[7]: #train the model on scaled training data model.fit(X_train_scaled,y_train) # In[18]: #make predidction on the sclaed testing data y_pred = model.predict(X_test_scaled) p

Multiple_Linear_Regression

  #!/usr/bin/env python # coding: utf-8 # In[1]: import numpy as np import matplotlib.pyplot as plt import pandas as pd # In[2]: myfile = "D:\Sunny115\week2_50_Startups.csv" dataset = pd.read_csv(myfile) X = dataset.iloc[:,: - 1 ].values y = dataset.iloc[:, - 1 ].values # In[6]: #encoding categorical data from sklearn.compose import ColumnTransformer from sklearn.preprocessing import OneHotEncoder ct = ColumnTransformer( transformers = [( 'encoder' ,OneHotEncoder(),[ 3 ])], remainder = 'passthrough' ) X = np.array(ct.fit_transform(X)) # In[8]: print (X) # In[13]: #splitting the dataset from sklearn.model_selection import train_test_split X_train,X_test,y_train,y_test = train_test_split(X,y, test_size = 0.2 , random_state = 0 ) # In[14]: from sklearn.linear_model import LinearRegression regressor = LinearRegression() regressor.fit(X_train,y_train) # In[15]: y_pred = regressor.predict(X_test) print (y_test) print (y_pred) # In[16]: from sklearn.me

Simple_Linear_Regression

  #!/usr/bin/env python # coding: utf-8 # In[3]: #importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # In[4]: myfile = "D:\Sunny115\week2_Salary_Data.csv" dataset = pd.read_csv(myfile) X = dataset.iloc[:,: - 1 ].values y = dataset.iloc[:, - 1 ].values # In[5]: from sklearn.model_selection import train_test_split X_train,X_test,y_train,y_test = train_test_split(X,y, test_size = 1 / 3 , random_state = 0 ) # In[6]: from sklearn.linear_model import LinearRegression regressor = LinearRegression() regressor.fit(X_train,y_train) # In[7]: print (X_train) # In[9]: y_pred = regressor.predict(X_test) print (y_test) print (y_pred) # In[10]: from sklearn.metrics import r2_score r2_score(y_test,y_pred) # In[13]: #Visulising the training set results plt.scatter(X_train,y_train, color = 'blue' ) plt.plot(X_train,regressor.predict(X_train), color = 'green' ) plt.title( 'Salary vs Expereince (training set)' ) plt.x