Posts

Logistic regression after pca

Image
 #from sklearn.datasets import fetch_mldata fr om sklearn.decomposition import PC A from sklearn import metrics import pandas as pd from sklearn.linear_model import LogisticRegression from sklearn.preprocessing import StandardScaler import numpy as np df = pd.read_csv('mnist_test.csv') df_x = df.iloc[:,1:] df_y = df.iloc[:,0] print(df_x.shape,df_y.shape) from sklearn.model_selection import train_test_split x_train,x_test,y_train,y_test = train_test_split(df_x,df_y,test_size=0.2,random_state=0) print(x_train.shape,x_test.shape) print(y_train.shape,y_test.shape) benchmark = [.95,.90,.85,0.80,0.75,0.70,0.60] #standardise the data scl = StandardScaler() scl.fit(x_train) x_train = scl.transform(x_train) x_test = scl.transform(x_test) #lets create pandas dataframe that contains variance_reatined,n_components,accuracy and time cols = ['variance','n_comp','time','accuracy'] df_result = pd.DataFrame(columns=cols) import time def pca_applier(variance,x_tra...

feature selection for classifiers

dataset ---> mobile price dataset kaggle ****************univariate method of feature selection******************* import pandas as pd import numpy as np from sklearn.feature_selection import chi2 from sklearn.feature_selection import SelectKBest  df=pd.read_csv('train.csv') df.head() x_train = df.iloc[:,0:20] y_train = df.iloc[:,-1] best_features = SelectKBest(score_func=chi2,k=10) fit= best_features.fit(x_train,y_train) df_score = pd.DataFrame(fit.scores_) dfcol=pd.DataFrame(x_train.columns)

NAIVE BAYES CLASSIFIER AND LINEAR DISCRIMINANT ANALYSIS COMPARISON

#IMPORT LIBRARIES FIRST import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split import numpy as np from sklearn.naive_bayes import GaussianNB from sklearn.metrics import confusion_matrix from sklearn.metrics import roc_auc_score from sklearn.discriminant_analysis import LinearDiscriminantAnalysis #LOAD CSV FILE AS PANDAS DATAFRAME df = pd.read_csv('mushrooms.csv') # convert categorical data to numerical data le = LabelEncoder() ds = df.apply(le.fit_transform) #df.apply(function) this apply method takes input of function. #prepare x and y data x_data = ds.loc[:,'cap-shape':'habitat'] y_data = ds.loc[:,'class'] #split the data into testing and training x_train,x_test,y_train,y_test = train_test_split(x_data,y_data,test_size=0.2,random_state=0) print(x_train.shape,x_test.shape) print(y_train.shape,y_test.shape) #make naive bayes method alias. gnb = GaussianNB() #predict values using naive bayes...

air canvas

 import numpy as np  import cv2  from collections import deque  # default called trackbar function  def setValues(x):      print("")  # Creating the trackbars needed for  # adjusting the marker colour These  # trackbars will be used for setting  # the upper and lower ranges of the  # HSV required for particular colour  cv2.namedWindow("Color detectors")  cv2.createTrackbar("Upper Hue", "Color detectors",  153, 180, setValues)  cv2.createTrackbar("Upper Saturation", "Color detectors",  255, 255, setValues)  cv2.createTrackbar("Upper Value", "Color detectors",  255, 255, setValues)  cv2.createTrackbar("Lower Hue", "Color detectors",  64, 180, setValues)  cv2.createTrackbar("Lower Saturation", "Color detectors",  72, 255, setValues)  cv2.createTrackbar("Lower Value", "Color detectors",  49, 255, setValues)  # Giving diffe...

adaptive thresholding.

  import numpy as np import cv       img = cv . imread ( 'sudoku.png' , 0 ) _ , th1 = cv . threshold ( img , 127 , 255 , cv . THRESH_BINARY ) th2 = cv . adaptiveThreshold ( img , 255 , cv . ADAPTIVE_THRESH_MEAN_C , cv . THRESH_BINARY , 11 , 2 ) th3 = cv . adaptiveThreshold ( img , 255 , cv . ADAPTIVE_THRESH_GAUSSIAN_C , cv . THRESH_BINARY , 11 , 2 ) cv . imshow ( "Image" , img ) cv . imshow ( "THRESH_BINARY" , th1 ) cv . imshow ( "ADAPTIVE_THRESH_MEAN_C" , th2 ) cv . imshow ( "ADAPTIVE_THRESH_GAUSSIAN_C" , th3 ) cv . waitKey ( 0 ) cv . destroyAllWindows ()

cv2 trackbars-HSV IMAGES

  import cv2 import numpy as np def empty ( a ): pass def stackImages (scale , imgArray): rows = len (imgArray) cols = len (imgArray[ 0 ]) rowsAvailable = isinstance (imgArray[ 0 ] , list ) width = imgArray[ 0 ][ 0 ].shape[ 1 ] height = imgArray[ 0 ][ 0 ].shape[ 0 ] if rowsAvailable: for x in range ( 0 , rows): for y in range ( 0 , cols): if imgArray[x][y].shape[: 2 ] == imgArray[ 0 ][ 0 ].shape [: 2 ]: imgArray[x][y] = cv2.resize(imgArray[x][y] , ( 0 , 0 ) , None, scale , scale) else : imgArray[x][y] = cv2.resize(imgArray[x][y] , (imgArray[ 0 ][ 0 ].shape[ 1 ] , imgArray[ 0 ][ 0 ].shape[ 0 ]) , None, scale , scale) if len (imgArray[x][y].shape) == 2 : imgArray[x][y]= cv2.cvtColor( imgArray[x][y] , cv2.COLOR_GRAY2BGR) imageBlank = np.zeros((height , width , 3 ) , np.uint8) hor = [imageBlank]*rows hor_con = [imageBlank]*rows ...

starting cv2 and face recognition.

Image
import cv2  cap=cv2.VideoCapture(0) face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') while True:     ret,frame=cap.read()     gray_frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)     if ret == False:         continue              faces=face_cascade.detectMultiScale(gray_frame,1.3,5)       for (x,y,w,h) in faces:         cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)     cv2.imshow("VideoFrame",frame)               key_pressed = cv2.waitKey(1) & 0xFF     if key_pressed == ord('q'):        break          cap.release() cv2.destroyAllWindows() ******************************designing opencv logo**************************** import cv2 import numpy as np #angles are measured clockwise img_=np.zeros((591,591,3)) c_c=(300,180) axes_len...