{"id":26387,"date":"2022-01-03T09:18:36","date_gmt":"2022-01-03T03:48:36","guid":{"rendered":"https:\/\/python-programs.com\/?p=26387"},"modified":"2022-01-03T09:18:36","modified_gmt":"2022-01-03T03:48:36","slug":"python-predict-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-predict-function-with-examples\/","title":{"rendered":"Python predict() Function With Examples"},"content":{"rendered":"
predict() Function in Python:<\/strong><\/p>\n In the field of data science, we must apply various machine learning models to data sets in order to train the data. We then attempt to predict the values for the untrained data.<\/p>\n This is when the predict() function comes into play.<\/p>\n The Python predict()<\/strong> function predicts the labels of data values based on the training model.<\/p>\n Syntax:<\/strong><\/p>\n The predict() function only accepts one parameter, which is often the data to be tested.<\/p>\n It returns the labels of the data supplied as an argument based on the model’s learned or trained data.<\/p>\n Thus, the predict() method operates on top of the trained model, mapping and predicting the labels for the data to be tested using the learned label.<\/p>\n Implementation:<\/strong><\/p>\n Decision Tree Algorithm:<\/strong><\/p>\n Decision Tree is a Supervised learning technique that may be used to solve classification and regression problems, however, it is most commonly used to solve classification problems. It is a tree-structured classifier in which internal nodes contain dataset characteristics, branches represent decision rules, and each leaf node represents the result. Example:<\/strong><\/p>\n Use the Decision Tree algorithm on the previously split dataset using the predict() function<\/strong> to predict the labels of the testing dataset based on the values predicted by the decision tree model.<\/p>\n Output:<\/strong><\/p>\n <\/p>\n K-Nearest Neighbor(KNN) Algorithm:<\/strong><\/p>\n In this case, we used the Knn algorithm to make predictions from the dataset. On the training data, we used the KNeighborsRegressor() function.<\/p>\n In addition, we used the predict() function to make predictions on the testing dataset.<\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" predict() Function in Python: In the field of data science, we must apply various machine learning models to data sets in order to train the data. We then attempt to predict the values for the untrained data. This is when the predict() function comes into play. The Python predict() function predicts the labels of data …<\/p>\nmodel.predict(data)<\/pre>\n
\n
# Import train_test_split from sklearn.model_selection using the import keyword.\r\nfrom sklearn.model_selection import train_test_split\r\n# Import os module using the import keyword\r\nimport os\r\n# Import pandas module using the import keyword\r\nimport pandas\r\n# Import dataset using read_csv() function by pasing the dataset name as\r\n# an argument to it.\r\n# Store it in a variable.\r\nbike_dataset = pandas.read_csv(\"bikeDataset.csv\")\r\n# Make a copy of the original given dataset and store it in another variable.\r\nbike = bike_dataset.copy()\r\n# Give the columns to be updated list as static input and store it in a variable\r\ncategorical_column_updated = ['season', 'yr', 'mnth', 'weathersit', 'holiday']\r\nbike = pandas.get_dummies(bike, columns=categorical_column_updated)\r\n\r\n# separate the dependent and independent variables into two data frames.\r\n\r\nX = bike.drop(['cnt'], axis=1)\r\nY = bike['cnt']\r\n\r\n# Divide the dataset into 80 percent training and 20 percent testing.\r\nX_train, X_test, Y_train, Y_test = train_test_split(\r\n X, Y, test_size=.20, random_state=0)\r\n<\/pre>\n
predict() Function with Decision Tree Algorithm<\/h4>\n
\nA Decision tree has two nodes: the Decision Node and the Leaf Node. Decision nodes are used to make any decision and have several branches, whereas Leaf nodes are the result of those decisions and have no additional branches.
\nThe decisions or tests are based on the characteristics of the given dataset.
\nIt is a graphical representation of all possible solutions to a problem\/decision given certain parameters.
\nIt is named a decision tree because, like a tree, it begins at the root node and spreads from there.<\/p>\n#On our dataset, we're going to build a Decision Tree Model.\r\nfrom sklearn.tree import DecisionTreeRegressor\r\n#We pass max_depth as argument to decision Tree Regressor\r\nDT_model = DecisionTreeRegressor(max_depth=5).fit(X_train,Y_train)\r\n#Predictions based on data testing\r\nDT_prediction = DT_model.predict(X_test) \r\n#Print the value of prediction\r\nprint(DT_prediction)<\/pre>\n
predict() Function with KNN Algorithm<\/h4>\n
\n
#On our dataset, we're going to build a KNN model.\r\nfrom sklearn.neighbors import KNeighborsRegressor\r\n#We pass n_neighborss as argument to KNeighborsRegressor\r\nKNN_model = KNeighborsRegressor(n_neighbors=3).fit(X_train,Y_train)\r\n#Predictions based on data testing\r\nKNN_predict = KNN_model.predict(X_test)\r\n#Print the value of prediction\r\nprint(KNN_predict)<\/pre>\n