Module - 5 - Machine Learning-Gurpinder Brar

docx

School

University Canada West *

*We aren’t endorsed by this school

Course

651

Subject

Business

Date

May 30, 2024

Type

docx

Pages

5

Uploaded by EarlDangerEchidna40

Report
1 Module 5– Python Programming Gurpinder Kaur Brar (2242099) University Canada West BUSI-651-Machine Learning Tools and Techniques-14 Professor- Mohsen Ghodrat Deadline – May 26, 2024
2 Question 1 Figure 1 Screenshot of code to count the lines from a file
3 The code shown above counts the number of lines from a file which is a text file saved as script.txt. Firstly, the function is defined where the path of the file is provided as parameters in function. After this, the file is opened just to read using ‘r’ mode. This also makes sure that the file is closed after it is read. Then the function file.readlines reads all the lines and a list is returned back. After that, the code counts the number of lines with len(lines). The file is shown below which is used in the code. Figure 2 Screenshot of file Script.txt
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
4 Question 2 Figure 3 Screenshot of Code to create a file, read and print the data of the same file. The code above is used to first create a file and add personal data to it and then read the data from the same file and print it. So, firstly the JSON file is created by defining it as
5 “personal_data_of_JSON” where the information such as Name, Age and hobbies are declared to the file. AFerowrds, the path of the file is stored in a variable called file_path. Now, we use the ‘w’ mode to write the data into the file. Then, the mode ‘r’ is used to read the data from the file and loadig the data into a new variable called ‘data’. Now we have to print the data which we have written and read from the file. This is done using the print() function. The output of the function is shown underneath the code in the screenshot. References Google Colaboratory . (n.d.). https://colab.research.google.com/drive/1iayO-AhV4j84A0QJ4- B3s6iNQErdLP0v#scrollTo=zT3VQQZkNnIB Python Tutorial . (n.d.). https://www.w3schools.com/python/python_json.asp OpenAI. (2023). ChatGPT (Mar 14 version) [Large language model]. https://chatgpt.com/c/c35fe30e-d0dc-4955-9543-2a58e24bcc7bda