import pandas as pd import numpy as np from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, r2_score import matplotlib.pyplot as plt # Dataset manually create karna (taki aap foran run kar saken) data = { 'sqft': [800, 900, 1000, 1200, 1500, 1800, 2000, 2300], 'price': [115000, 130000, 135000, 165000, 199000, 230000, 250000, 290000] } df = pd DataFrame(data) X = df[['sqft']] y = df['price'] # Split and Train X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) model = LinearRegression() model fit(X_train, y_train) # Plotting plt scatter(df['sqft'], df['price'], color = 'blue') plt plot(df['sqft'], model predict(df[['sqft']]), color = 'red') plt xlabel("Size (sqft)") plt ylabel("Price ($)") plt title("House Price Prediction") plt show() print(f"Slope: {model coef_[0]}, Intercept: {model intercept_}") Slope: 114.89252814739001, Intercept: 25402.251791197457 import pandas as pd import numpy as np from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt # Dataset data = { 'experience_years': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'salary': [35000, 42000, 50000, 55000, 61000, 68000, 72000, 80000, 85000, 93000] } df = pd DataFrame(data) X = df[['experience_years']] y = df['salary'] # Train Model X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0) model = LinearRegression() model fit(X_train, y_train) # Plotting plt scatter(df['experience_years'], df['salary'], color = 'blue') plt plot(df['experience_years'], model predict(df[['experience_years']]), color = 'red') plt xlabel("Experience (Years)") In [1]: In [7]: plt ylabel("Salary") plt title("Salary Prediction") plt show() import pandas as pd from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt # Dataset data = { 'temperature_celsius': [15, 18, 20, 22, 24, 26, 28, 30, 32, 35], 'icecream_sales': [120, 150, 180, 200, 230, 260, 300, 340, 370, 420] } df = pd DataFrame(data) X = df[['temperature_celsius']] y = df['icecream_sales'] # Direct Model Fit model = LinearRegression() model fit(X, y) # Visualization plt scatter(X, y, color = 'green') plt plot(X, model predict(X), color = 'orange') plt xlabel("Temperature (°C)") plt ylabel("Ice Cream Sales") plt title("Practice: Sales vs Temp") plt show() In [6]: In [ ]: