专栏名称: 机器学习算法与Python实战
长期跟踪关注统计学、数据挖掘、机器学习算法、深度学习、人工智能技术与行业发展动态,分享Python、机器学习等技术文章。回复机器学习有惊喜资料。
目录
相关文章推荐
深圳特区报  ·  颜宁,获国家级荣誉 ·  2 天前  
深圳特区报  ·  手机这个功能,慎用!警方紧急提醒丨飞阅深圳 ·  2 天前  
深圳发布  ·  “用一片‘创可贴’,颠覆血糖监测行业” ·  3 天前  
51好读  ›  专栏  ›  机器学习算法与Python实战

【Python代码模板】数据预处理、数据分析、假设检验、机器学习

机器学习算法与Python实战  · 公众号  ·  · 2024-09-06 14:39

正文

1 数据介绍

本次分析使用的数据来自"yc_data.csv",该文件包含了 Y Combinator(YC)创业加速器投资的公司详细信息:

  1. 文件包含多个列,如公司ID、公司名称、简短描述、详细描述、YC批次、公司状态、标签、位置、国家等。

  2. 数据涵盖了从YC早期批次(如S05、W06)到最近的批次(如W24、S24)的公司。

  3. 公司状态包括Active(活跃)、Acquired(被收购)和Inactive(不活跃)等。

  4. 数据包含了许多知名公司,如Reddit、Twitch、Scribd等。

  5. 每个公司的信息包括创始人数量、创始人姓名、团队规模、网站、Crunchbase链接和LinkedIn链接等。

  6. 标签列表示公司的业务领域或技术方向,如AI、fintech、SaaS等。

  7. location数据显示了公司的地理分布,主要集中在美国,但也包括其他国家的公司。

  8. 年份信息显示了公司的创立时间,从早期到最近几年都有。

  9. 团队规模从个位数到数千人不等,反映了公司的不同发展阶段。

  10. 最近批次的公司数据显示了当前创业趋势,如人工智能、开源软件、开发者工具等领域的增长。

2 数据预处理

首先,我们使用 pandas 库读取 CSV 文件,并查看数据的基本信息:

import pandas as pd

df = pd.read_csv("yc_data.csv")
print(df.head())

输出结果显示,数据集包含17列,分别为:

  • batch_idx: 批次索引
  • company_id: 公司ID
  • company_name: 公司名称
  • short_description: 简短描述
  • long_description: 详细描述
  • batch: YC批次
  • status: 公司状态
  • tags: 标签
  • location: 位置
  • country: 国家
  • year_founded: 成立年份
  • num_founders: 创始人数量
  • founders_names: 创始人姓名
  • team_size: 团队规模
  • website: 网站
  • cb_url: Crunchbase链接
  • linkedin_url: LinkedIn链接

接下来,我们查看数据的整体情况:

print(df.info())
print(df.isnull().sum())

RangeIndex: 4586 entries, 0 to 4585
Data columns (total 17 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 batch_idx 4586 non-null int64
1 company_id 4586 non-null int64
2 company_name 4586 non-null object
3 short_description 4432 non-null object
4 long_description 4266 non-null object
5 batch 4586 non-null object
6 status 4586 non-null object
7 tags 4586 non-null object
8 location 4324 non-null object
9 country 4331 non-null object
10 year_founded 3563 non-null float64
11 num_founders 4586 non-null int64
12 founders_names 4586 non-null object
13 team_size 4515 non-null float64
14 website 4585 non-null object
15 cb_url 2540 non-null object
16 linkedin_url 2980 non-null object
dtypes: float64(2), int64(3), object(12)
memory usage: 609.2+ KB
None
...
website 1
cb_url 2046
linkedin_url 1606
dtype: int64
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

从输出结果可以看出,数据集共有4586行,部分列存在缺失值,如short_description、long_description、location、country、year_founded等。

3 数据清洗

为了便于后续分析,我们需要对数据进行清洗和预处理。

# 处理缺失值
df['short_description'] = df['short_description'].fillna('No description')
df['year_founded'] = df['year_founded'].fillna(df['year_founded'].median())
df['team_size'] = df['team_size'].fillna(df['team_size'].median())


# 创建一个新列表示公司是否成功(假设Acquired或Active状态为成功)
df['is_successful'] = df['status'].isin(['Acquired''Active'])

# 从batch列中提取年份,处理异常情况
def extract_year(batch):
    try:
        year = batch[-2:]  # 提取字符串的最后两个字符
        return int('20' + year)  # 将年份转换为整数类型
    except:
        return np.nan

df['batch_year'] = df['batch'].apply(extract_year)

# 查看batch_year列的唯一值,以检查是否还有问题
print(df['batch_year'].unique())

4 探索性数据分析

现在我们的数据已经清理完毕,让我们开始探索一些有趣的见解:

4.1 公司状态分布

status_counts = df['status'].value_counts()
plt.figure(figsize=(106))
status_counts.plot(kind='bar')
plt.title('Distribution of Company Statuses')
plt.xlabel('Status')
plt.ylabel('Count')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

这段代码将生成一个柱状图,显示不同公司状态的分布

4.2 每批次公司数量的变化

batch_counts = df['batch'].value_counts().sort_index()
plt.figure(figsize=(126))
batch_counts.plot(kind='line')
plt.title('Number of Companies per Batch')
plt.xlabel('Batch')
plt.ylabel('Number of Companies')
plt.xticks(rotation=90)
plt.tight_layout()
plt.show()

这将生成一个折线图,展示每个批次的公司数量变化。

4.3 最常见的标签

all_tags = [tag for tags in df['tags'for tag in tags]
tag_counts = pd.Series(all_tags).value_counts().head(20)
plt.figure(figsize=(126))
tag_counts.plot(kind='bar')
plt.title('Top 20 Most Common Tags')
plt.xlabel('Tag')
plt.ylabel('Count')
plt.xticks(rotation=90)
plt.tight_layout()
plt.show()

这个图表将展示最常见的20个标签。

4.4 公司成功率随时间的变化

df['success_rate'] = df.groupby('batch_year')['is_successful'].transform('mean')
plt.figure(figsize=(126))
df.groupby('batch_year')['success_rate'].mean().plot(kind='line')
plt.title('Success Rate Over Time')
plt.xlabel('Year')
plt.ylabel('Success Rate')
plt.tight_layout()
plt.show()

从图中可以看出,YC创业公司的成功率总体呈上升趋势,近年来保持在较高水平。

5 假设检验

接下来,我们使用T检验分析不同因素对成功率的影响。首先,我们定义一个函数对给定变量进行T检验:

from scipy import stats

def perform_t_test(variable):
    successful_values = df[df['is_successful']][variable]
    unsuccessful_values = df[~df['is_successful']][variable]
    
    t_stat, p_value = stats.ttest_ind(successful_values, unsuccessful_values)
    
    print(f"Variable: {variable}")
    print(f"T-statistic: {t_stat}")
    print(f"P-value: {p_value}")
    print("---")
Variable: year_founded
T-statistic: 4.2584208077988706
P-value: 2.0999812247726262e-05
---
Variable: num_founders
T-statistic: 3.5994256038457904
P-value: 0.0003222920811796079
---
Variable: team_size
T-statistic: 0.2147248161445528
P-value: 0.8299914248081315
---
Variable: batch_year
T-statistic: 27.695299446266723
P-value: 3.067399233387115e-156
---

从输出结果可以看出:

  • year_founded、num_founders和batch_year对成功率有显著影响(p值小于0.05)
  • team_size对成功率没有显著影响(p值大于0.05)

6 预测模型







请到「今天看啥」查看全文