Python Analytics and Computation Guide
Python Analytics and Computation Guide
To connect to a SQLite database in Python, use the `sqlite3` module. First, connect to the database using `sqlite3.connect('College.db')`. Create a cursor object for executing SQL commands with `connection.cursor()`. Then, use the cursor to execute a `CREATE TABLE` statement like `CREATE TABLE STUDENT (rollNumber INTEGER, studName TEXT, class TEXT)` to create a new table. Commit the changes with `connection.commit()` and close the connection with `connection.close()` .
To apply linear regression on the mpg dataset, first load the dataset using seaborn. Preprocess it by setting the attributes 'weight', 'cylinders', and 'displacement' as independent variables, and 'mpg' as the dependent variable. Standardize these attributes for better performance. Then, create a `LinearRegression` instance from sklearn and fit it using the prepared data. Use `lr.fit(X, y)` where X is your feature matrix and y is the target variable 'mpg'. Evaluate the model using metrics like R² score .
To demonstrate that NumPy arrays are more storage and computationally efficient than lists, you can create two NumPy arrays and two lists, each with 10,000 elements. Measure the size of the NumPy arrays and lists using the `sys.getsizeof()` function, showing that NumPy arrays take less memory. For computational efficiency, compare the time taken to sum two arrays versus summing two lists using the `time` module, showing that arrays are faster due to optimized C library operations .
To replace values with nulls at random indexes in a pandas DataFrame, first generate random indexes using `numpy.random.randint()`, ensuring the chosen indexes are within the DataFrame’s dimensions. Then replace values at these indexes by setting them to `numpy.nan`. This can be accomplished with a nested loop or vectorized operations depending on the size of random indexes produced. Such manipulation is useful for testing data imputation techniques .
Visualize relationships between features of the iris dataset using a pair plot by loading the dataset through `sns.load_dataset('iris')`. Then use `sns.pairplot(iris_df)` to generate plots for every pair of feature combinations. This helps in understanding correlations and distributions within the dataset, allowing for visual analysis of how features relate to each other and differentiate the iris species visually .
To manipulate a 2D NumPy array, you can use various NumPy functions. For example, with `intArray = numpy.array([[34,43,73],[82,22,12],[53,94,66]])`: - Delete the second column using `numpy.delete(intArray, 1, axis=1)`. - Sort the array by the second row using `intArray[intArray[1].argsort()]`. - Sort by the second column using `intArray[intArray[:, 1].argsort()]`. - Find row-wise maximum elements with `intArray.max(axis=1)` and column-wise with `intArray.max(axis=0)`. - Swap the first two columns using `intArray[:, [1, 0]] = intArray[:, [0, 1]]`. - Retrieve even numbers using `intArray[intArray % 2 == 0]` .
To predict penguin species using logistic regression, load the penguins dataset using seaborn and preprocess features: 'bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', and 'body_mass_g'. Use `LogisticRegression` from sklearn, applying a regularization term by setting `C` to a lower value, which penalizes complexity and helps combat overfitting. Fit the model with `model.fit(X, y)` where X contains the features and y contains the species labels. Use cross-validation for further overfitting control .
Normalize data in a pandas DataFrame by applying the min-max scaling to map its range from 0 to 1. Use the formula `(data - data.min()) / (data.max() - data.min())` applied column-wise. This can be done with `df.apply(lambda x: (x - x.min()) / (x.max() - x.min()), axis=0)`. Normalization is important to ensure that the features contribute equally to the distance computations, improving the performance of algorithms that rely on distance calculations .
Using pandas, you can filter students who scored 80% or above by creating a boolean mask with `df['Percentage'] >= 80` applied to the dataframe, and use it to select those rows. To handle missing data, count missing values in each column with `df.isnull().sum()`. To remove rows with excessive missing data, use `df.dropna(thresh=int(df.shape[1] * 0.3))`, which drops any row with more than 70% missing data .
To create a dictionary from two lists in Python, you can use the `zip()` function combined with a dictionary comprehension. For example, with `fruits = ['Apple', 'Mango', 'Peach', 'Banana']` and `prices = [100, 80, 150, 70]`, you can create a dictionary as follows: `{fruit: price for fruit, price in zip(fruits, prices)}`. This pairs each fruit with its corresponding price from the lists, resulting in `{'Apple': 100, 'Mango': 80, 'Peach': 150, 'Banana': 70}` .